Drive Code
*Technically optional, but highly recommended
Last updated
Was this helpful?
*Technically optional, but highly recommended
Last updated
Was this helpful?
You probably want the robot to move around. However, the robot doesn't just do this on its own; you have to program it. In this tutorial, we'll show you how to do that with the double arcade control scheme--the left joystick moves the robot forward and backward, and the right joystick turns the robot.
We'll start off by adding the drive motors to the list of devices. In this example, we'll use a 6-motor drivetrain with the left and right motors plugged in to the following ports. Your port numbers can be different; all that matters is that the motors are named well and match up with the ports on the physical robot.
Now, we can start coding. For the sake of clarity, we'll put all of our drive code in a C++ function. Here's the format for a function in C++:
The first order of business is to get controller inputs. Make sure you have a controller listed in your devices menu:
Now, we can get the inputs from the joysticks and store them in integer variables. The left joystick will control forwards and backwards motion in a straight line, so we'll store that value in a variable called straight
. Next, we'll store the value for the right joystick in a variable called turn
, since the right joystick turns the robot. Put the following code in the driveCode()
function:
Now, we need to calculate the motor power, in percentage points, that we should apply to each side of the drivetrain. We'll use two variables, left
and right
, to keep track of the percentage motor power we will apply to the left and right sides of the drivetrain. The following code calculates motor power, and it should be in the driveCode()
function, right after the previous code.
Now that we have calculated the motor powers for the left and right sides of the drivetrain, we just have to apply that power to them. Since we have 6 motors in this example, we'll apply the motor powers to all six motors.
Let's put all of it together so far! Make sure to put this function before the main loop.
Looks great! but if you add this code to your program, nothing will happen. We've made the function driveCode()
, but we haven't called it yet. Now, we need to add in another function called userControl that runs when the driver controlled period in a match is active.
When the controller is told to run driver control during a match, your code needs to recognize that and call the right function. Thus, we'll initialize a Competition variable at the beginning of the main file, after the using namespace vex;
line.
Now, update your main function to call the userControl function when the driver control period starts:
With that, you should have a fully functioning drivetrain! If the driving seems off, reverse directions of the the motors (try many combinations) until it works properly.