040 lesson: drive like a tank

../_images/fig-l040-field.png

The two speeds drive the wheels, and the robot drives in the simulator: tank drive!

Before you start.

  • L030 is done, so mytry holds L030SpeedsOpMode.

  • AdvantageScope is installed on the same laptop, and a gamepad is plugged into the laptop.

  • No robot is needed. The robot drives in the simulator only; L050 puts it on blocks.

the steps

L040S010: copy the starter into mytry

Copy. In lessons, click L040TankOpMode once and copy it. Click mytry once and paste. In the Copy Class box, click OK. If Android Studio asks whether to add the file to Git, click Add.

Check.

  • mytry holds L040TankOpMode, and the new file is open.

L040S020: switch your copy on

Change. Delete the line @Disabled, under the comment that names this step.

Check.

  • Under the step’s comment, the next line reads // 'public' so the robot can run this OpMode. 'public' allows access from any code.

L040S030: the left wheels

The starter already reads the sticks and turns them into leftSpeed and rightSpeed, as you did in L030.

Change. The robot has four motors, one on each wheel. hardware holds all four. Under the comment that names this step, type hardware.front. The list offers frontLeft and frontRight. Pick frontLeft and press Tab.

../_images/fig-l040-hardware.png

Type .setP. The list shows what a motor can be told to do. The one we want is setPower(double power).

../_images/fig-l040-setpower.png

Finish the line, and add the same for the back left wheel:

        // When on L040S030, see
        // https://msb-stitt.github.io/ftc31459-26-27-biobuzz/guide/tasks/l040.html#l040s030
        // for what to do here.
        hardware.frontLeft.setPower(leftSpeed);
        hardware.backLeft.setPower(leftSpeed);

A power is a number from -1.0 to +1.0. It is how hard the motor pushes: 1.0 is full forward, -1.0 is full backward, and 0.0 is stop.

Check.

  • Neither line has a red underline.

L040S040: the right wheels

Change. Under the comment that names this step, do the same for the right side:

        // When on L040S040, see
        // https://msb-stitt.github.io/ftc31459-26-27-biobuzz/guide/tasks/l040.html#l040s040
        // for what to do here.
        hardware.frontRight.setPower(rightSpeed);
        hardware.backRight.setPower(rightSpeed);

Simulator. In the Terminal, run:

./gradlew :TeamCode:simRun --args="mytry.L040TankOpMode --pad"

AdvantageScope. Connect to the simulator as in L020, and click 2D Field. Open the sim folder and drag Pose into Poses. The robot appears near the bottom edge of the field. A thin line runs to it from the middle of the field. That is where the simulator put the robot before the OpMode moved it to its start, so you can ignore it.

Check.

  • Push both sticks gently forward, and the robot drives forward.

  • Pull both back, and it comes back.

  • Push one forward and pull the other back, and it spins in place.

  • Push the right stick further than the left, and it curves to the left.

What’s happening. E041 shows what a wheel line does inside the computer.

No gamepad. --args="mytry.L040TankOpMode --left_stick_y=-0.25 --right_stick_y=-0.35" holds both sticks gently forward, the right a little further. The picture at the top was taken that way, 4 s after connecting. The simulator has no walls, so a robot left running drives off the field.

If it didn’t, and the robot creeps with both sticks let go, the sticks do not read exactly 0.0. Nothing in L040 tidies that up; L060 does.

What you just did. Each pass through driveFromTheSticks reads the sticks, turns them into speeds, and hands each speed to the two motors on its side. That is tank drive. In the simulator every motor turns its wheel forward for a positive power. On the robot, a motor mounted the other way around turns its wheel backward, and L050 is where that is found and fixed.

Where next. L050 runs this on the robot, on blocks.