You may not expect it, but there is a little bit of math that needs to be done to get our robot moving smoothly. But before we dive too deeply into that let's start with the basics of movement we'll need.
Remember positive/negative values inputted by the gamepad's y-axis are inverse of the positive/negative values of the motor.
Setting x = gamepad1.right_stick_x;
and y = -gamepad1.right_stick_y;
assigns values from the gamepad joystick to x and y. Depending on the orientation of the joystick, these valuables will receive some value between -1 and 1.
For a quick reference let's take a look at what number each variable would be assigned at their far ends:
Right now we have x and y assigned values based on our joystick's movement, but what does that mean? Why is that useful?
Maybe you have seen in a math class before something like this:
In this case, a is our variable that has been assigned some value. For this example, we can determine that value is 7. But what does that mean in programming?
Variables used in programming follow this same principle. We can define a variable within our code to hold a set value or a value that changes, such as we are doing here. Then whenever that variable is referenced the robot will read it as that assigned value!
So using our example above if I had:
My robot would know my variable of a is equal to 7 and therefore calculate the answer as 17 for me!
Consider for a moment, why should we use a variable when we could just use the number on its own?
We'll be using variables in greater detail in later sections, but even for our drive code you will be able to see the use of variables helps keep our program clean and easier to follow.
By using setting our y variable at the beginning of our code we can inverse it without needing to do so every time we may reference the joystick's y-axis. Within a longer program, having our variables defined at the start would allow us to quickly change a value without having to hunt down or double check that every possible instance in the code has been updated to reflect this change. Instead we are able to change it once and continue testing!
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:
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 assigned to our motors.
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:
Rather than setting a static numerical value for our motors, the variables we've set will help our robot to translate the motion of the joysticks into a power level.
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:
The time has come to program our robot to respond to our gamepad inputs so we can drive it around! To start we will be focusing on controlling our drivetrain.
This section will introduce the use of variables as we create our program. This will allow us to set up calculations in our program that will determine what power the motors should receive based on our gamepad's input. How much power each motor receives changes how the robot will move, so it is important to also review this relationship will establishing our code!
Below is a sneak peek at our final program:
To start, create two variablesand . In OnBot Java to establish a variable with a numerical value we will use the object double
. Our variables are established during our initialization process.
Then within our loop assign as y = -gamepad1.right_stick_y;
and the as the x = gamepad1.right_stick_x;.
Power Comparison | Robot Movement |
---|---|
Think back to the very start of for a moment. We will be programming our robot using the Arcade Style of TeleOp. This means our forward and back movements will be controlled using the y-axis of the joystick while turning will be controlled by the x-axis.
rightMotor power = leftMotor power
Straight Forward or Reverse
rightMotor power > leftMotor power
Left Turn
rightMotor power < leftMotor power
Right Turn
Joystick Direction
0
1
0
-1
-1
0
1
0
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