Example Autonomous Routines
Assumed Constants​
These are default speeds that we can use throughout our autonomous routines to make it easier to modify them retroactively.
const int DRIVE_SPEED = 110;
const int TURN_SPEED = 90;
const int SWING_SPEED = 90;
Drive​
This autonomous routine will have the robot go forwards for 24 inches with slew enabled, come back -12 inches, then come back another -12 inches to where it started. It will do all of this at the predefined DRIVE_SPEED
.
void drive_example() {
// The first parameter is target inches
// The second parameter is max speed the robot will drive at
// The third parameter is a boolean (true or false) for enabling/disabling a slew at the start of drive motions
// for slew, only enable it when the drive distance is greater then the slew distance + a few inches
chassis.set_drive_pid(24, DRIVE_SPEED, true);
chassis.wait_drive();
chassis.set_drive_pid(-12, DRIVE_SPEED);
chassis.wait_drive();
chassis.set_drive_pid(-12, DRIVE_SPEED);
chassis.wait_drive();
}
Turn​
This autonomous routine will turn 90 degrees, then back 45 degrees, and finally to 0 where it started. It will do all of this at the predefined TURN_SPEED
.
void turn_example() {
// The first parameter is target degrees
// The second parameter is max speed the robot will drive at
chassis.set_turn_pid(90, TURN_SPEED);
chassis.wait_drive();
chassis.set_turn_pid(45, TURN_SPEED);
chassis.wait_drive();
chassis.set_turn_pid(0, TURN_SPEED);
chassis.wait_drive();
}
Drive and Turn​
This autonomous routine will combine driving and turning in a single function.
void drive_and_turn() {
chassis.set_drive_pid(24, DRIVE_SPEED, true);
chassis.wait_drive();
chassis.set_turn_pid(45, TURN_SPEED);
chassis.wait_drive();
chassis.set_turn_pid(-45, TURN_SPEED);
chassis.wait_drive();
chassis.set_turn_pid(0, TURN_SPEED);
chassis.wait_drive();
chassis.set_drive_pid(-24, DRIVE_SPEED, true);
chassis.wait_drive();
}
Wait Until and Changing Speed​
Now we add wait_until()
. This new function will wait until a specified distance has been traveled and then allow the code to continue. The robot will drive at DRIVE_SPEED
until the robot has traveled 6 inches, then will lower the max speed to 40. The same thing happens on the return back.
void wait_until_change_speed() {
// wait_until will wait until the robot gets to a desired position
// When the robot gets to 6 inches, the robot will travel the remaining distance at a max speed of 40
chassis.set_drive_pid(24, DRIVE_SPEED, true);
chassis.wait_until(6);
chassis.set_max_speed(40); // After driving 6 inches at DRIVE_SPEED, the robot will go the remaining distance at 40 speed
chassis.wait_drive();
chassis.set_turn_pid(45, TURN_SPEED);
chassis.wait_drive();
chassis.set_turn_pid(-45, TURN_SPEED);
chassis.wait_drive();
chassis.set_turn_pid(0, TURN_SPEED);
chassis.wait_drive();
// When the robot gets to -6 inches, the robot will travel the remaining distance at a max speed of 40
chassis.set_drive_pid(-24, DRIVE_SPEED, true);
chassis.wait_until(-6);
chassis.set_max_speed(40); // After driving 6 inches at DRIVE_SPEED, the robot will go the remaining distance at 40 speed
chassis.wait_drive();
}
Swing Turns​
Swing turns are turns that only use one side of the drive. Left swings use the left side, and right swings use the right side. This will turn the robot to 45 degrees using the left side, drive 24 inches, then turn to 0 degrees using the right side.
void swing_example() {
// The first parameter is ez::LEFT_SWING or ez::RIGHT_SWING
// The second parameter is target degrees
// The third parameter is speed of the moving side of the drive
chassis.set_swing_pid(ez::LEFT_SWING, 45, SWING_SPEED);
chassis.wait_drive();
chassis.set_drive_pid(24, DRIVE_SPEED, true);
chassis.wait_until(12);
chassis.set_swing_pid(ez::RIGHT_SWING, 0, SWING_SPEED);
chassis.wait_drive();
}
Combining All Movements​
This combines all movements from above.
void combining_movements() {
chassis.set_drive_pid(24, DRIVE_SPEED, true);
chassis.wait_drive();
chassis.set_turn_pid(45, TURN_SPEED);
chassis.wait_drive();
chassis.set_drive_pid(ez::RIGHT_SWING, -45, TURN_SPEED);
chassis.wait_drive();
chassis.set_turn_pid(0, TURN_SPEED);
chassis.wait_drive();
chassis.set_drive_pid(-24, DRIVE_SPEED, true);
chassis.wait_drive();
}
Interference​
Interference checks if the drive exited in an unintentional way. If the robot stops unintentionally or pulls too many amps this will trigger. This allows you to add fail-safes that stop your robot from burning out. The below function will attempt to "tug" an opponent mobile goal a couple times before giving up.
void tug (int attempts) {
for (int i=0; i<attempts-1; i++) {
// Attempt to drive backwards
printf("i - %i", i);
chassis.set_drive_pid(-12, 127);
chassis.wait_drive();
// If failsafed...
if (chassis.interfered) {
chassis.reset_drive_sensor();
chassis.set_drive_pid(-2, 20);
pros::delay(1000);
}
// If robot successfully drove back, return
else {
return;
}
}
}
// If there is no interference, robot will drive forward and turn 90 degrees.
// If interfered, robot will drive forward and then attempt to drive backwards.
void interfered_example() {
chassis.set_drive_pid(24, DRIVE_SPEED, true);
chassis.wait_drive();
if (chassis.interfered) {
tug(3);
return;
}
chassis.set_turn_pid(90, TURN_SPEED);
chassis.wait_drive();
}