text
stringlengths 0
715
|
---|
Motor.spin(forward, 11, volt); |
} |
Now, let's create another function to spin the motor backwards at full voltage: |
//Spin the motor backward at full voltage |
void spinMotorBackward() { |
Motor.spin(reverse, 11, volt); |
} |
Thirdly, we need a function to stop the motor from spinning when the button on the controller is released: |
//Stop the motor |
void stopMotor() { |
Motor.stop(); |
} |
All of this code is great, but none of it will run unless we set up the proper callbacks. We'll use the buttons L1 and L2 in this example, but feel free to change it to any buttons you would like. Put the callback code in the main function, so that the controller buttons activate the functions that spin or stop the motor. |
int main() { |
//Initializing Robot Configuration. DO NOT REMOVE! |
vexcodeInit(); |
//Set up the motor callbacks |
Controller1.ButtonL1.pressed(spinMotorForward); //when button L1 is pressed, spin the motor forward |
Controller1.ButtonL2.pressed(spinMotorBackward); //when button L2 is pressed, spin the motor backward |
β |
Controller1.ButtonL1.released(stopMotor); //when button L1 is released, stop the motor |
Controller1.ButtonL2.released(stopMotor); //when button L2 is released, stop the motor |
β |
//there may be more code below, leave it be |
} |
Now, download the code and test to see if it works! |
Coding Pneumatics |
Pneumatics can be intimidating at first, but the code for them is simple. In this tutorial, we'll show you how to make a toggle button for a pneumatic piston on the robot. |
Start by setting up the pneumatic in VEXCode Pro. It's a 3-wire DigitalOut device: |
β ---> β |
Now, we need to make a variable storing whether or not the pneumatic piston is current active or not. We'll use a boolean variable and set it to false by default. This variable should be defined outside of all functions, below the #include "vex.h"; and using namespace vex; lines of code. |
//global variables |
bool pneumaticActive = false; //true or false, whether or not the pnuematic is active |
Next, we'll make a function that toggles the pneumatic piston on or off. This function flips the pneumaticActive variable first, and then either activates or deactivates the pneumatic based on the value of the pneumaticActive variable. |
//toggle the pneumatic piston on or off |
void togglePneumatic() { |
pneumaticActive = !pneumaticActive; //flip the on/off variable |
if (pneumaticActive) { |
Pneumatic.set(true); //Activate the pneumatic |
} else { |
Pneumatic.set(false); //Deactivate the pneumatic |
} |
} |
Lastly, we have to set up a callback for the togglePneumatic function, so it is run when a button is pressed. Put the following code in the main function, like so: |
int main() { |
//Initializing Robot Configuration. DO NOT REMOVE! |
vexcodeInit(); |
//Set up the pneumatic toggle callback |
Controller1.ButtonX.pressed(togglePneumatic); |
β |
//there may be more code below, leave it be |
} |
That's it! Now you have code that activates and deactivates the pneumatics at the press of a button! |
Advanced |
More coding shenanigans |
Coding Autonomous |
12.5% of the match |
The Autonomous period is only 15 seconds, but it can make a huge difference in matches. In this section, we'll show you how to set up code to run during autonomous. |
Before we can make the autonomous program, we have to set up the code to interface with the field controllers. That is, when the controller is told to run autonomous 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; |
Next, we'll make a function called autonomousProgram(). This function will contain all of the code that your autonomous will run. |
void autonomousProgram(void) { |
//insert autonomous code here |
} |
Now, we need to tell the controller that we want this function to run when autonomous starts. To do that, we need to add the line Competition.autonomous(autonomousProgram); immediately after the vexcodeInit() line. |
int main() { |
// Initializing Robot Configuration. DO NOT REMOVE! |
vexcodeInit(); |
β |
// Set up callback for the autonomous period |
Competition.autonomous(autonomousProgram); |
β |
//you may have more code below, leave it there |
} |
Now, you can simply put whatever code you would like to run during autonomous in the autonomousProgram() function, and it will run. If you want to have multiple options for autonomous, you can create multiple programs. |
Coding PIDs |
Worth it. |
A PID (proportional integral derivative) controller is an advanced coding technique to make the robot's motion consistent, reliable, and efficient. |
Theory |
PIDs are used everywhere in industrial robotics--car factories, robot vacuums, and more. If a robot moves, it likely uses PIDs to move. |
See these robots? They use PIDs |
But why? PIDs do one thing, and they do it really well. They move a robot from point A to point B: |
That's a PID. Yep, it's not that bad. |
But how? This is where the PID algorithm comes in. There are three parts to a PID controller, given by the acronym. Each part applies a specific power to the drive motors based on certain factors: |
* P (Proportional): Update motor power based on the present |
* I (Integral): Update motor power based on the past |
* D (Derivative): Update motor power based on the future |
What each part really does |
The motor power is calculated by the following line of code, which is the heart of the PID algorithm: |
float motorPower = (kP * proportional) + (kI * integral) + (kD * derviative); |
That is, each part of the PID is added together to calculate motor power. kP, kI, and kD are constants that determine the relative weights of each portion of the PID. |
Error |
Error refers to how far the robot is from the target point. If the robot wants to go forward 20 inches, the PID algorithm would calculate the following errors. |
Figure 3 |