# Martinez Twins vs VentureState

<figure><img src="/files/b2KRBXigbK2V8rKbohg7" alt=""><figcaption></figcaption></figure>

### Rewards

* :trophy: **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](/tutorial/your-first-robot.md). 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. :angry: 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](/tutorial/your-first-robot.md). Plug the left motor into port A and the right motor into port B.

<figure><img src="/files/u94FnGBmqVlGhqEGdK70" alt="" width="563"><figcaption></figcaption></figure>

<figure><img src="/files/cr4uiaxfgkU9CKKwdhC2" alt=""><figcaption><p>1</p></figcaption></figure>

Let's start with the code we made for our [dance video](/adventure/quests/dance-video.md) robot.

```python
# Set up controller
controller = XboxController()

# Set up motor
motor = Motor(Port.A)

while True:

    # Set motor speed
    speed = controller.joystick_left()[1]

    # Rotate motor
    motor.go(speed)
```

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 copying the motor code we already have. Here's where we set up the motor.

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

```python
# Set up motor
motor = Motor(Port.A)
motor = Motor(Port.B)
```

<figure><img src="/files/cr4uiaxfgkU9CKKwdhC2" alt=""><figcaption><p>2</p></figcaption></figure>

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

```python
# Set up motor
motorA = Motor(Port.A)
motorB = Motor(Port.B)
```

<figure><img src="/files/cr4uiaxfgkU9CKKwdhC2" alt=""><figcaption><p>3</p></figcaption></figure>

Much better. We'll need two `speed` variables as well. Copy the line where we set the motor speed. Rename the variables `speedA` and `speedB` .

```python
    # Set motor speed
    speedA = controller.joystick_left()[1]
    speedB = controller.joystick_left()[1]
```

<figure><img src="/files/cr4uiaxfgkU9CKKwdhC2" alt=""><figcaption><p>4</p></figcaption></figure>

We want `speedB` to come from the right joystick. So let's change `joystick_left` to `joystick_right`.

```python
    # Set motor speed
    speedA = controller.joystick_left()[1]
    speedB = controller.joystick_right()[1]
```

<figure><img src="/files/cr4uiaxfgkU9CKKwdhC2" alt=""><figcaption><p>5</p></figcaption></figure>

Lastly, let's copy our motor rotation code and use the new variables.

```python
    # Rotate motor
    motorA.go(speedA)
    motorB.go(speedB)
```

<figure><img src="/files/cr4uiaxfgkU9CKKwdhC2" alt=""><figcaption><p>6</p></figcaption></figure>

The program should look like this.

```python
# Set up controller
controller = XboxController()

# Set up motor
motorA = Motor(Port.A)
motorB = Motor(Port.B)

while True:

    # Set motor speed
    speedA = controller.joystick_left()[1]
    speedB = controller.joystick_right()[1]

    # Rotate motor
    motorA.go(speedA)
    motorB.go(speedB)
```

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.

<figure><img src="/files/cr4uiaxfgkU9CKKwdhC2" alt=""><figcaption><p>7</p></figcaption></figure>

## 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 speed value? Well, we can do the same thing here with math. We multiply `speedA` by `-1`. To multiply, we use the `*` symbol.

```python
    # Rotate motor
    motorA.go(speedA * -1)
    motorB.go(speedB)
```

<figure><img src="/files/cr4uiaxfgkU9CKKwdhC2" alt=""><figcaption><p>8</p></figcaption></figure>

If you haven't used negative numbers before, don't worry about how this works. Just know that you can always multiply by `-1`  to flip a motor's direction.

Here's the final code.

```python
# Set up controller
controller = XboxController()

# Set up motor
motorA = Motor(Port.A)
motorB = Motor(Port.B)

while True:

    # Set motor speed
    speedA = controller.joystick_left()[1]
    speedB = controller.joystick_right()[1]

    # Rotate motor
    motorA.go(speedA * -1)
    motorB.go(speedB)
```

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.

<figure><img src="/files/cr4uiaxfgkU9CKKwdhC2" alt=""><figcaption><p>9</p></figcaption></figure>

## The Race

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

<figure><img src="/files/cr4uiaxfgkU9CKKwdhC2" alt=""><figcaption><p>10</p></figcaption></figure>

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.

<figure><img src="/files/cr4uiaxfgkU9CKKwdhC2" alt=""><figcaption><p>11</p></figcaption></figure>

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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://play.robobattles.com/adventure/quests/martinez-twins-vs-venturestate.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
