At the moment, our motors are set to power on to a full forward at the start of our program. For reference, the image below shows the full scale of movement between forward and reverse:
Let's take this information and think back to when we first programmed a motor to move with our gamepad. During that section our motor was able to rotate at different power levels depending on how far and in which direction our joystick moved. However, do you recall the problem we had with this set up?
While using our previous code our motor only spun when the joystick was moved along the y-axis. Moving to the left or right did not ask the motor to power on, but it would begin to stutter some at the diagonals.
This is where adding some math to our code comes into play. Remember on an arcade drive both motors are being controlled by a single joystick. We need our robot to be able to calculate for both motors how much they should power on and in which direction. Thankfully, once we have it all set up our robot will be able to handle the calculations itself as the program runs!
By the end, we should be able to create situations like the following charts where the motors respond to create different forms of motion:
What happens when we set the power of the rightmotor to 0.3 and leftmotor to 1?
What happens when we set the power of the leftmotor to 0.5 and rightmotor to 1?
What happens when we set the power of the leftmotor to -0.4 and rightmotor to 0.4?
After testing different combinations, let's look at a quick breakdown of how power between the motors effects movement:
For our arcade drive, the goal is for our joystick inputs to calculate to the following motor outputs:
To get the outputs expressed in the table above, the gamepad values must be assigned to each motor in a meaningful way. To do so we are going to set up two equations in our code using the variables we have already established:
How our robot moves is dependent on how much power each motor is receiving. Before continuing, we can explore with our current program how the robot reacts when changing the values in our block.
Power Comparison | Robot Movement |
---|---|
Rather than setting a static numerical value for our motors, the and blocks will help our robot to translate the motion of the joysticks into a power level.
rightMotor power = leftMotor power
Straight Forward or Reverse
rightMotor power > leftMotor power
Left Turn
rightMotor power < leftMotor power
Right Turn
Joystick Direction
( , )
rightmotor
leftmotor
Movement
(0,1)
1
1
Forward
(0,-1)
-1
-1
Reverse
(-1,0)
1
-1
Turn left
(1,0)
-1
1
Turn right