Skip to main content
Version: 3.1.0

Example Autonomous Routines

Reading documentation can be overwhelming, so EZ-Template has example autonomous routines for you to use and modify on your own to learn by doing.  

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 forward for 24 inches with slew enabled, come back -12 inches, and 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 than the slew distance + a few inches

chassis.pid_drive_set(24_in, DRIVE_SPEED, true);
chassis.pid_wait();

chassis.pid_drive_set(-12_in, DRIVE_SPEED);
chassis.pid_wait();

chassis.pid_drive_set(-12_in, DRIVE_SPEED);
chassis.pid_wait();
}

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 the target in degrees
// The second parameter is max speed the robot will drive at

chassis.pid_turn_set(90_deg, TURN_SPEED);
chassis.pid_wait();

chassis.pid_turn_set(45_deg, TURN_SPEED);
chassis.pid_wait();

chassis.pid_turn_set(0_deg, TURN_SPEED);
chassis.pid_wait();
}

Drive and Turn

This autonomous routine will combine driving and turning in a single function.

void drive_and_turn() {
chassis.pid_drive_set(24_in, DRIVE_SPEED, true);
chassis.pid_wait();

chassis.pid_turn_set(45_deg, TURN_SPEED);
chassis.pid_wait();

chassis.pid_turn_set(-45_deg, TURN_SPEED);
chassis.pid_wait();

chassis.pid_turn_set(0_deg, TURN_SPEED);
chassis.pid_wait();

chassis.pid_drive_set(-24_in, DRIVE_SPEED, true);
chassis.pid_wait();
}

Wait Until and Changing Speed

Now we add pid_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 30.  The same thing happens on the return back.

void wait_until_change_speed() {
// pid_wait_until will wait until the robot gets to a desired position

// When the robot gets to 6 inches slowly, the robot will travel the remaining distance at full speed
chassis.pid_drive_set(24_in, 30, true);
chassis.pid_wait_until(6_in);
chassis.pid_speed_max_set(DRIVE_SPEED); // After driving 6 inches at 30 speed, the robot will go the remaining distance at DRIVE_SPEED
chassis.pid_wait();

chassis.pid_turn_set(45_deg, TURN_SPEED);
chassis.pid_wait();

chassis.pid_turn_set(-45_deg, TURN_SPEED);
chassis.pid_wait();

chassis.pid_turn_set(0_deg, TURN_SPEED);
chassis.pid_wait();

// When the robot gets to -6 inches slowly, the robot will travel the remaining distance at full speed
chassis.pid_drive_set(-24_in, 30, true);
chassis.pid_wait_until(-6_in);
chassis.pid_speed_max_set(DRIVE_SPEED); // After driving 6 inches at 30 speed, the robot will go the remaining distance at DRIVE_SPEED
chassis.pid_wait();
}

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 drive in a segmented S curve.  

void swing_example() {
// The first parameter is ez::LEFT_SWING or ez::RIGHT_SWING
// The second parameter is the target in degrees
// The third parameter is the speed of the moving side of the drive
// The fourth parameter is the speed of the still side of the drive, this allows for wider arcs

chassis.pid_swing_set(ez::LEFT_SWING, 45_deg, SWING_SPEED, 45);
chassis.pid_wait();

chassis.pid_swing_set(ez::RIGHT_SWING, 0_deg, SWING_SPEED, 45);
chassis.pid_wait();

chassis.pid_swing_set(ez::RIGHT_SWING, 45_deg, SWING_SPEED, 45);
chassis.pid_wait();

chassis.pid_swing_set(ez::LEFT_SWING, 0_deg, SWING_SPEED, 45);
chassis.pid_wait();
}

Motion Chaining

Motion Chaining is when you exit a movement while the robot is still moving so you carry momentum into the next movement.  

void motion_chaining() {
// Motion chaining is where motions all try to blend together instead of individual movements.
// This works by exiting while the robot is still moving a little bit.
// To use this, replace pid_wait with pid_wait_quick_chain.
chassis.pid_drive_set(24_in, DRIVE_SPEED, true);
chassis.pid_wait();

chassis.pid_turn_set(45_deg, TURN_SPEED);
chassis.pid_wait_quick_chain();

chassis.pid_turn_set(-45_deg, TURN_SPEED);
chassis.pid_wait_quick_chain();

chassis.pid_turn_set(0_deg, TURN_SPEED);
chassis.pid_wait();

// Your final motion should still be a normal pid_wait
chassis.pid_drive_set(-24_in, DRIVE_SPEED, true);
chassis.pid_wait();
}

Combining All Movements

This combines all movements from above.

void combining_movements() {
chassis.pid_drive_set(24_in, DRIVE_SPEED, true);
chassis.pid_wait();

chassis.pid_turn_set(45_deg, TURN_SPEED);
chassis.pid_wait();

chassis.pid_swing_set(ez::RIGHT_SWING, -45_deg, SWING_SPEED, 45);
chassis.pid_wait();

chassis.pid_turn_set(0_deg, TURN_SPEED);
chassis.pid_wait();

chassis.pid_drive_set(-24_in, DRIVE_SPEED, true);
chassis.pid_wait();
}

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's mobile goal a couple of times before giving up.

void tug(int attempts) {
for (int i = 0; i < attempts - 1; i++) {
// Attempt to drive backward
printf("i - %i", i);
chassis.pid_drive_set(-12_in, 127);
chassis.pid_wait();

// If failsafed...
if (chassis.interfered) {
chassis.drive_sensor_reset();
chassis.pid_drive_set(-2_in, 20);
pros::delay(1000);
}
// If the robot successfully drove back, return
else {
return;
}
}
}

// If there is no interference, the robot will drive forward and turn 90 degrees.
// If interfered, the robot will drive forward and then attempt to drive backward.
void interfered_example() {
chassis.pid_drive_set(24_in, DRIVE_SPEED, true);
chassis.pid_wait();

if (chassis.interfered) {
tug(3);
return;
}

chassis.pid_turn_set(90_deg, TURN_SPEED);
chassis.pid_wait();
}