LogoLogo
Follow Us
  • Introduction
    • Overview
    • Welcome
      • Create Your Character
      • Visit the Garage
    • Get Parts
  • Tutorial
    • Your First Bot
      • Parts
      • Chassis
      • Decoration
    • Controls
      • Install Hub Firmware
      • Load Program Onto Hub
      • Using Your Controller
    • Next Steps
  • Battle
    • Rules
      • Crafting Rules
      • Battle Rules
    • Build Your Arena
  • Adventure
    • Quests
      • Sinkhole Patrol
      • Dance Video
      • Martinez Twins vs VentureState
      • Big Break
    • Lore
      • Places
        • Stardust Springs
        • Zoe's Garage
        • Bot Battles Stadium
      • Characters
        • Lily Ishii
        • Zoe Foxlin
        • Ethan Hill
Powered by GitBook
On this page
  • Rewards
  • Fastest Bot Takes It
  • Another One
  • Math
  • The Race
Export as PDF
  1. Adventure
  2. Quests

Martinez Twins vs VentureState

PreviousDance VideoNextBig Break

Last updated 3 months ago

Rewards

  • 400 XP

Fastest Bot Takes It

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.

# 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 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.

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

# 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 bot 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 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.

    # 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 bot!

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 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.

😠
🚽
Purrmenator
Purrmenator
dance video
🏆
1
2
3
4
5
6
7
8
9
10
11