Martinez Twins vs VentureState

Rewards
🏆 400 XP
[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 robot with two wheel motors, like the Purrmenator. I'll explain why when we get there.
Fastest Robot Takes It
No time to waste. Here's the deal. Have you met the Martinez twins? They're a couple of up-and-coming robot builders who've been running this incredible recycling program across town. Free robot 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 robot controls only. Fastest robot takes it. If the twins win, VentureState backs off. If they lose...
The twins' robot 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 robot — which again, PRACTICALLY PERFECT — has no control program. It's useless.
I need your help creating a program that can take down VentureState's store-bought robot. The race is tonight. Will you help? Great.
Make sure your robot has two wheel motors, like the Purrmenator. Plug the left motor into port A and the right motor into port B.


Let's start with the code we made for our dance video robot.
# Import modules
from pybricks.hubs import TechnicHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.tools import wait, StopWatch
from pybricks.iodevices import XboxController
# Set up controller
controller = XboxController()
# Set up motor
motor = Motor(Port.A)
while True:
# Set motor power
power = controller.joystick_left()[1]
# Rotate motor
motor.dc(power)
This robot uses the joystick to spin on command. But the Martinez Twins don't need a dancing robot. They need a robot they can steer all over a racetrack.
Another One
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.
# Set up motor
motor = Motor(Port.A)
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.
# Set up motor
motor = Motor(Port.A)
motor = Motor(Port.B)

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
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.
# Set up motor
motorA = Motor(Port.A)
motorB = Motor(Port.B)

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
.
# Set motor power
powerA = controller.joystick_left()[1]
powerB = controller.joystick_left()[1]

We want powerB
to come from the right joystick. So let's change joystick_left
to joystick_right
.
# Set motor power
powerA = controller.joystick_left()[1]
powerB = controller.joystick_right()[1]

Lastly, let's copy our motor rotation code and use the new variables.
# Rotate motor
motorA.dc(powerA)
motorB.dc(powerB)

The program should look like this.
# Import modules
from pybricks.hubs import TechnicHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.tools import wait, StopWatch
from pybricks.iodevices import XboxController
# Set up controller
controller = XboxController()
# Set up motor
motorA = Motor(Port.A)
motorB = Motor(Port.B)
while True:
# Set motor power
powerA = controller.joystick_left()[1]
powerB = controller.joystick_right()[1]
# Rotate motor
motorA.dc(powerA)
motorB.dc(powerB)
Load it up. Push both joysticks all the way up. Both wheels will turn, and your robot will drive straight ahead. We did i— oh wait.

Math
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 robot to move forward when we press both joysticks up. So we need to make motor A rotate the opposite direction.
Remember on our sinkhole robot 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.
# Rotate motor
motorA.dc(powerA * -1)
motorB.dc(powerB)

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.
# Import modules
from pybricks.hubs import TechnicHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.tools import wait, StopWatch
from pybricks.iodevices import XboxController
# Set up controller
controller = XboxController()
# Set up motor
motorA = Motor(Port.A)
motorB = Motor(Port.B)
while True:
# Set motor power
powerA = controller.joystick_left()[1]
powerB = controller.joystick_right()[1]
# Rotate motor
motorA.dc(powerA * -1)
motorB.dc(powerB)
You know the drill. Load it up and enjoy your fully steerable robot!
Push both joysticks up to drive forward. Push both joysticks down to drive backward. Push the joysticks in different directions to turn.

The Race
Let's make sure this program works! Design an awesome race course with crazy turns and obstacles to avoid.

Steer your robot through the course as fast as you can. If you have a friend or teammate with another robot, 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 robot won't stand a chance.
Now that you know how to code, you can start experimenting with custom joystick controls for your own robot designs. Later on I'll show you how to use controller buttons.
Last updated