← Back to Course

Making Simple Autonomous Routes

Learn how to make autonomous routes for your robot.

Chapter 4 of 11

36%

Autonomous Routes

An autonomous route is a pre-programmed set of instructions that your robot will follow without human intervention. This is always used during the autonomous period of a VEX competition. You can earn extra points for winning autonomouses, Autonomous Points which are useful for tiebreaks, but most importantly AWPs (Autonomous Win Points) which counts as half a win.

Adding a setDrive Function

Creating your own function to control the motors of the robot to move forward, backward, and turn will save a lot of repetitive coding. Here is an example of how to create a setDrive function:

cpp
void setDrive(int leftPower, int rightPower) {
    driveLB.move(leftPower);
    driveLF.move(leftPower);
    driveRB.move(rightPower);
    driveRF.move(rightPower);
}

This function takes two arguments: leftPower and rightPower, which control the power sent to all of the left and right motors respectively.

Which way should the robot move if setDrive(127, -127) is called?

You'll notice that if you send one side with power and the other side with the opposite power, your robot will turn on the spot.

Adding Delays

To create a sequence of movements, you can use delays between each setDrive command. The pros::delay function allows you to pause the program for a specified number of milliseconds. Here's an example of a simple autonomous route:

cpp
void autonomous() {
    setDrive(127, 127); // Move forward at full power
    pros::delay(2000); // Wait for 2 seconds

    setDrive(0, 0); // Stop
    pros::delay(500); // Wait for 0.5 seconds

    setDrive(-127, -127); // Move backward at full power
    pros::delay(2000); // Wait for 2 seconds

    setDrive(0, 0); // Stop
}

In this example, the robot moves forward for 2 seconds, stops for 0.5 seconds, then moves backward for 2 seconds, and finally stops. The setDrive(0,0) to stop the robot is VERY important or else your motors will continue with the previous input and go at full power forever.

Write code for a 0.75 second delay

Create a new file called autonomous.cpp. Inside, create a function called redRight that moves the robot forward for 0.5 seconds, turns left at around 50% power for 1.5 seconds, moves forward at 70% power for 1.5 seconds, then stops.

Run the code with your robot to see what it does. Make sure you place your robot in the exact same spot everytime because that's what you will be doing at competitions. If you run the same code multiple times, what do you notice?

You will likely see that the robot doesn't go to the same place every single time. As a programmer, that can be strange since you are running the exact same code, but getting different results. That is because there are external factors such as friction that may vary from trial to trial and it can be very frustrating as coders to deal with that.

It is also very difficult to know what distance your robot is travelling if you give it full power for 1 second which makes it slow and tedious to program.

For these reasons, our robots use sensors and other algorithms to help correct for the differences which will be the next 2 chapters.