Martinez Twins vs VentureState
Last updated
Last updated
400 XP
No time to waste. Here's the deal. Have you met the Martinez twins? They're a couple of up-and-coming bot builders who've been running this incredible recycling program across town. Free bot repair clinics, big collection of salvaged parts, the works.
Here's the problem. VentureState showed up last week. A big land corporation. They're trying to shut down the twins' program so they can take over the building. The twins challenged them to settle it with a race. Custom bot controls only. Fastest bot takes it. If the twins win, VentureState backs off. If they lose...
I need your help creating a program that can take down VentureState's store-bought bot. The race is tonight. Will you help? Great.
This bot uses the joystick to spin on command. But the Martinez Twins don't need a dancing bot. They need a bot they can steer all over a racetrack.
We started with the motor on the left. We control it with the left joystick.
Next we'll do the motor on the right. We should control this one with the right joystick.
Let's start by duplicating the motor code we already have. Here's where we set up the motor.
The motor on the left is plugged into the hub's port A. So when we set up the new motor in our code, we'll want to use Port.B
instead of Port.A
. This is called a parameter. Let's copy this line and change the port parameter.
Hmm, I already see a problem here. We're using the motor
variable twice.
The first line stores motor A in motor
. But then the second line replaces it with motor B. Now motor A is lost. We have no way to control it.
We'll need to use two different variables by giving them different names. What should we call them? A variable can have basically any name, as long as it:
Only uses letters, numbers, and underscores _
Doesn't start with a number
Much better. We'll need two power
variables as well. Copy the line where we set the motor power. Rename the variables powerA
and powerB
.
We want powerB
to come from the right joystick. So let's change joystick_left
to joystick_right
.
Lastly, let's copy our motor rotation code and use the new variables.
The program should look like this.
Load it up. Push both joysticks all the way up. Both wheels will turn, and your bot will drive straight ahead. We did i— oh wait.
Problem! What's going on here?
The motors are running in different directions! Nooooo!
This is because the motors are facing opposite directions from each other. We want our bot to move forward when we press both joysticks up. So we need to make motor A rotate the opposite direction.
Remember on our sinkhole bot how we switched motor direction by adding -
to the power value? Well, we can do the same thing here with math. We multiply powerA
by -1
. To multiply, we use the *
symbol.
If you haven't used negative numbers before, don't worry about how this works. Just know that you can multiply by -1
any time to flip a motor's direction.
Here's the final code.
You know the drill. Load it up and enjoy your fully steerable bot!
Push both joysticks up to drive forward. Push both joysticks down to drive backward. Push the joysticks in different directions to turn.
Let's make sure this program works! Design an awesome race course with crazy turns and obstacles to avoid.
Steer your bot through the course as fast as you can. If you have a friend or teammate with another bot, challenge them to race.
Amazing. I couldn't have done it without you.
Wait, what time is it!? I have to get this program over to the Martinez Twins. VentureState's bot won't stand a chance.
Now that you know how to code, you can start experimenting with custom joystick controls for your own bot designs. Later on I'll show you how to use controller buttons.
[Zoe Foxlin] I need your help. Like, right now. Meet me at the abandoned test track east of town. We've got a situation. Bring a bot with two wheel motors, like the . I'll explain why when we get there.
The twins' bot is practically perfect. One of the best designs I've ever seen. But their coder quit yesterday. VentureState offered them a job they couldn't refuse. So this bot — which again, PRACTICALLY PERFECT — has no control program. It's useless.
Make sure your bot has two wheel motors, like the . Plug the left motor into port A and the right motor into port B.
Let's start with the code we made for our bot.
We COULD name the second variable toilet
. But the code will be easier to understand if the variable names actually makes sense. So flush that idea. We'll call them motorA
and motorB
instead.