Lesson: Exercise: Joystick Driving - Arcade Mode
Understand driving under joystick control using one joystick to control both motors (Arcade mode).
Lets look at another style of motor control (driving) called arcade. In arcade mode, a single joystick controls both forward/backward motion but also left and right. This allows the robot to be driven with one finger, typically the thumb.
In arcade mode, the joystick y axis controls the forward and reverse motion of the robot. You will need to set both motors to the power level indicated by the y axis deflection. That controls forward and backward motion. So how do we turn? We use the x axis (side to side) deflection to cause the motors to run at different speeds by adding the x value to one motor power level and subtracting the x value from the other motor power level. Here is the code:
Here we are using the x and y axis deflection values from the right joystick only. We combine the x and y values to get the motor power setting. Again, which side is added to and which is subtracted from is determined by your motor gearing which determines the rotation direction of the motor. In our test case negative y axis values make the motors turn in the direction that yields forward motion. Since the joystick x axis returns negative values for left deflection, for a left turn (for example) we subtract the x value from the left motor (minus a minus) yielding a positive value for the left motor (backward) and add a negative yielding a negative value for the right motor (forward). If no y deflection is present, the left motor turns backward and the right forward and the robot spins left. When you add some y deflection the spin turns into an arc.
Arcade driving is more complex to understand and program and more difficult to operate but may make sense for some robots and some drivers.
This sample and the previous one introduces the idle() method which is part of the super class LinearOpMode. When you program a loop as we have here, it is possible and likely for that loop to run without interruption, monopolizing the phone cpu and causing other parts of the robot controller application, like the part that communicates with the driver station phone for example, to be starved for cpu time and not function. The idle() method interrupts your loop momentarily and allows the other classes in the robot controller app to get some cpu time to perform thier functions before resuming your loop.
Copy and paste this code into a new class called DriveArcade, compile and demonstrate arcade mode driving.