text
stringlengths 0
715
|
---|
//prevent the main loop from exiting |
while(true) { |
wait(20, msec); |
} |
} |
In this code: |
* We include the necessary VEX header file, "vex.h", to access VEX V5 API functions and classes. |
* We add using namespace vex; to reference vex motors and classes natively. |
* We create the main function, which is run when the program is started. |
* We call the vexcodeInit function--always do this. |
* We use the wait function in a while loop to prevent the code from exiting before the robot can do anything |
Building and Downloading Your Code |
1. 1. |
2. Connect your V5 Brain to your robot and turn it on. |
3. 2. |
4. Connect your computer to the V5 Brain using a USB cable. |
5. 3. |
6. Click on the "Download" button (at the top right) to transfer your code to the V5 Brain. |
Note that the code above doesn't make the robot do anything quite yet; it is just a foundation to build on. Your first order of business should be coding the drivetrain. See this article: |
🚗 |
Drive Code |
Expanding Your Knowledge |
Now that you've successfully programmed your VEX V5 robot to perform a simple task, you can expand your knowledge by exploring the VEX V5 C++ API and trying out different sensors and motor configurations. The VEX V5 C++ API documentation provides detailed information about classes and functions you can use to control your robot. |
VEX V5 C++ API: vex Namespace Reference |
Note: C++ is not an easy language to learn and it is often used by the most highly skilled programmers. Expect to fail many times, it is normal. |
Drive Code |
*Technically optional, but highly recommended |
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. |
For a four-motor drive, just leave out Right3 and Left3 |
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++: |
void driveCode() { |
//drives the robot around based on controller input, double arcade controls |
} |
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: |
//Get controller inputs |
int straight = Controller1.Axis3.value(); //gives a number -100 to 100 |
int turn = Controller1.Axis1.value(); |
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. |
//calculate proper motor powers |
int left = straight + turn * 0.7; //the 0.7 makes the turns less intense |
int right = straight - turn * 0.7; |
Why the 0.7? |
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. |
//Spin the drive motors at the calculated velocity |
Left1.spin(forward, left, percent); |
Left2.spin(forward, left, percent); |
Left3.spin(forward, left, percent); |
Right1.spin(forward, right, percent); |
Right2.spin(forward, right, percent); |
Right3.spin(forward, right, percent); |
Let's put all of it together so far! Make sure to put this function before the main loop. |
void driveCode() { |
//drives the robot around based on controller input, double arcade controls |
//First, get controller inputs |
int straight = Controller1.Axis3.value(); //gives a number -100 to 100 |
int turn = Controller1.Axis1.value(); |
//Calculate proper motor powers |
int left = straight + turn * 0.7; //the 0.7 makes the turns less intense |
int right = straight - turn * 0.7; |
|
//Spin the drive motors at the calculated velocity |
Left1.spin(forward, left, percent); |
Left2.spin(forward, left, percent); |
Left3.spin(forward, left, percent); |
Right1.spin(forward, right, percent); |
Right2.spin(forward, right, percent); |
Right3.spin(forward, right, percent); |
} |
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. |
//driver control code, is activated when the driver control period occurs |
void userControl() { |
while (true) { //run this code indefinitely |
driveCode(); //update the drive motors to be at the right power |
wait(20, msec); //don't hog the CPU |
} |
} |
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. |
competition Competition; |
Now, update your main function to call the userControl function when the driver control period starts: |
int main() { |
//Initializing Robot Configuration. DO NOT REMOVE! |
vexcodeInit(); |
Competition.drivercontrol(userControl); |
//You might have more code below, leave it |
} |
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. |
Coding Motors |
In this tutorial, we'll show you how to code additional mechanisms on the robot, like an intake or catapult. Specifically, you'll learn how to spin a motor forwards and backwards at the press of a button. |
First, add the motor to the devices list. In this example, our motor is named Motor |
Next create a function that spins the motor forwards at full voltage: |
//Spin the motor forward at full voltage |
void spinMotorForward() { |