Vex EDR C++ Autonomous

My team doesn’t have a programmer, so I stepped up, and I figured out through videos how to program the remote stuff, but I can’t figure out how to program an autonomous. Can anyone give me an example program/tutorial?

Look at some of the example programs in Vex Code and try to type your own. Keep practicing and trying new ideas and you will understand how to program auton. Good Luck on programming !

1 Like

Are you talking about the example program “Motion”?

Auton is sort of complex. You can have a complex odometry controller, or you can use wait times. To do wait times just say something like

driveL.spin(fwd);
driveR.spin(fwd);
vex::this_thread::sleep_for(1000);
driveL.stop(brake);
driveR.stop(brake);

You can do that hundreds of times for a long auton routine. Look around on this forum and like @ButterNubs said, look at the example programs on VexCode. Good luck!

Oh by the way, coding auton takes a tooooooooon of patience. Not for the faint of heart. But that 6 point bonus can be rewarding. And it is absolutely thrilling when your auton actually works.

1 Like

And yes basic motion and advanced motion sections in the example programs are good for auton.

this is my first attempt i havent tested it yet tho im about to
#include “robot-config.h”
/*+++++++++++++++++++++++++++++++++++++++++++++| Notes |++++++++++++++++++++++++++++++++++++++++++++++
Drive Forward
This program instructs your robot to move forward at half power for three seconds.
There is a two second pause at the beginning of the program.

Robot Configuration:
[Smart Port] [Name] [Type] [Description] [Reversed]
Motor Port 1 LeftMotor V5 Smart Motor Left side motor false
Motor Port 10 RightMotor V5 Smart Motor Right side motor true

----------------------------------------------------------------------------------------------------*/

int main() {

//Wait 2 seconds or 2000 milliseconds before starting the program.
vex::task::sleep(2000);
//Print to the screen that the program has started.
Brain.Screen.print("User Program has Started.");

//Set the velocity of the left and right motor to 50% power. This command will not make the motor spin.
FrontLeftDr.setVelocity(100, vex::velocityUnits::pct);
FrontRightDr.setVelocity(100, vex::velocityUnits::pct);
BackLeftDr.setVelocity(100, vex::velocityUnits::pct);
BackRightDr.setVelocity(100, vex::velocityUnits::pct);
LeftIntake.setVelocity(100, vex::velocityUnits::pct);
RightIntake.setVelocity(100, vex::velocityUnits::pct);
ChutePush.setVelocity(100, vex::velocityUnits::pct);

//Spin the right and left motor in the forward direction. The motors will spin at 50% power because of the previous commands.
FrontLeftDr.spin(vex::directionType::fwd);
FrontRightDr.spin(vex::directionType::fwd);
BackRightDr.spin(vex::directionType::fwd);
BackLeftDr.spin(vex::directionType::fwd);
  vex::task::sleep(1500);
LeftIntake.spin(vex::directionType::fwd);
RightIntake.spin(vex::directionType::fwd);
  vex::task::sleep(300);
FrontLeftDr.spin(vex::directionType::rev);
BackLeftDr.spin(vex::directionType::rev);
  vex::task::sleep(200);
FrontLeftDr.spin(vex::directionType::rev);
FrontRightDr.spin(vex::directionType::rev);
BackRightDr.spin(vex::directionType::rev);
BackLeftDr.spin(vex::directionType::rev);
  vex::task::sleep(1000);
FrontRightDr.spin(vex::directionType::fwd);
BackRightDr.spin(vex::directionType::fwd);
  vex::task::sleep(200);
FrontLeftDr.spin(vex::directionType::fwd);
FrontRightDr.spin(vex::directionType::fwd);
BackRightDr.spin(vex::directionType::fwd);
BackLeftDr.spin(vex::directionType::fwd);
  vex::task::sleep(1000);
FrontLeftDr.spin(vex::directionType::rev);
BackLeftDr.spin(vex::directionType::rev);
  vex::task::sleep(800);
FrontLeftDr.spin(vex::directionType::fwd);
FrontRightDr.spin(vex::directionType::fwd);
BackRightDr.spin(vex::directionType::fwd);
BackLeftDr.spin(vex::directionType::fwd);
  vex::task::sleep(2000);
ChutePush.spin(vex::directionType::fwd);
  vex::task::sleep(2000);

FrontLeftDr.stop();
FrontRightDr.stop();
BackRightDr.stop();
BackLeftDr.stop();
LeftIntake.stop();
RightIntake.stop();
//Print to the brain's screen that the program has ended.
Brain.Screen.newLine();//Move the cursor to a new line on the screen.
Brain.Screen.print("Autonomous has Ended.");
//Prevent main from exiting with an infinite loop.                        
while(1) {
  vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
}

}

You have to note that the spin command never tells the motor when to stop. It just tells it to spin. The motor will not automatically stop after a wait time unless you specifically tell it to stop. If you want the motors to continue running that is fine but you have to be aware of that.

1 Like

I see what you mean, that is what i meant to do

1 Like

I have an Instagram page at 7686b_vex if you want to see our robot

2 Likes

Something important about autonomous is that it will only be turned on during the auto period if its in the competition template.

Another thing is instead of using the classic delay, V5 has built in PID you can use. It’s much more accurate.

2 Likes

What you did is called “hardcoding”. As in, you told each specific motor what to do for a specific amount of time. As you get more advanced, you’ll be able to group motors and have math do this for you. Look into C++ functions/methods in which you can use them to group motors and use parameters to significantly condense your code.

2 Likes

This hard code is a pretty solid first step. Kudos to you for taking up programming.

For more accuracy going forward, try to take a look at the VCS API and figure out how to use the encoders built into the motors. Though the sensors are far from perfect, it will be way more accurate than going based on time loops.
Essentially, you would run the motors until you reach a certain encoder value. Just something to consider as you code your actions.

If you don’t want to do this quite yet, I recommend at least condensing your code by creating a drive method to send power to each side of the drive base so that you can take 4 lines of code and condense it into one by just saying something like drive(200, 200); it will help make things easier to read and sort through for you.

2 Likes

will do, soon I will experiment more with other programs

So i changed it to a competition template, but what do you do with the pre auton and all the voids?
#include “robot-config.h”
/---------------------------------------------------------------------------/
/* /
/
Description: Competition template for VCS VEX V5 /
/
/
/
---------------------------------------------------------------------------*/

//Creates a competition object that allows access to Competition methods.
vex::competition

/---------------------------------------------------------------------------/
/* Pre-Autonomous Functions /
/
/
/
You may want to perform some actions before the competition starts. /
/
Do them in the following function. You must return from this function /
/
or the autonomous and usercontrol tasks will not be started. This /
/
function is only called once after the cortex has been powered on and /
/
not every time that the robot is disabled. /
/
---------------------------------------------------------------------------*/

void pre_auton( void ) {
// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, …

}

/---------------------------------------------------------------------------/
/* /
/
Autonomous Task /
/
/
/
This task is used to control your robot during the autonomous phase of /
/
a VEX Competition. /
/
/
/
You must modify the code to add your own robot specific commands here. /
/
---------------------------------------------------------------------------*/

void autonomous( void ) {
// …
// #include “robot-config.h”
/*+++++++++++++++++++++++++++++++++++++++++++++| Notes |++++++++++++++++++++++++++++++++++++++++++++++
Drive Forward

Robot Configuration:
[Smart Port] [Name] [Type] [Description] [Reversed]

----------------------------------------------------------------------------------------------------*/

int main() {

//Wait 2 seconds or 2000 milliseconds before starting the program.
vex::task::sleep(2000);
//Print to the screen that the program has started.
Brain.Screen.print("Match has Started.");
Brain.Screen.print("Autonomous will start soon.");

FrontLeftDr.setVelocity(100, vex::velocityUnits::pct);
FrontRightDr.setVelocity(100, vex::velocityUnits::pct);
BackLeftDr.setVelocity(100, vex::velocityUnits::pct);
BackRightDr.setVelocity(100, vex::velocityUnits::pct);
LeftIntake.setVelocity(100, vex::velocityUnits::pct);
RightIntake.setVelocity(100, vex::velocityUnits::pct);
ChutePush.setVelocity(100, vex::velocityUnits::pct);


FrontLeftDr.spin(vex::directionType::fwd);
FrontRightDr.spin(vex::directionType::fwd);
BackRightDr.spin(vex::directionType::fwd);
BackLeftDr.spin(vex::directionType::fwd);
  vex::task::sleep(1500);
LeftIntake.spin(vex::directionType::fwd);
RightIntake.spin(vex::directionType::fwd);
  vex::task::sleep(300);
FrontLeftDr.spin(vex::directionType::rev);
BackLeftDr.spin(vex::directionType::rev);
  vex::task::sleep(200);
FrontLeftDr.spin(vex::directionType::rev);
FrontRightDr.spin(vex::directionType::rev);
BackRightDr.spin(vex::directionType::rev);
BackLeftDr.spin(vex::directionType::rev);
  vex::task::sleep(1000);
FrontRightDr.spin(vex::directionType::fwd);
BackRightDr.spin(vex::directionType::fwd);
  vex::task::sleep(200);
FrontLeftDr.spin(vex::directionType::fwd);
FrontRightDr.spin(vex::directionType::fwd);
BackRightDr.spin(vex::directionType::fwd);
BackLeftDr.spin(vex::directionType::fwd);
  vex::task::sleep(1000);
FrontLeftDr.spin(vex::directionType::rev);
BackLeftDr.spin(vex::directionType::rev);
  vex::task::sleep(800);
FrontLeftDr.spin(vex::directionType::fwd);
FrontRightDr.spin(vex::directionType::fwd);
BackRightDr.spin(vex::directionType::fwd);
BackLeftDr.spin(vex::directionType::fwd);
  vex::task::sleep(2000);
ChutePush.spin(vex::directionType::fwd);
  vex::task::sleep(2000);

FrontLeftDr.stop();
FrontRightDr.stop();
BackRightDr.stop();
BackLeftDr.stop();
LeftIntake.stop();
RightIntake.stop();
ChutePush.stop();

Controller.Screen.print("Autonomous has Ended.");
Controller.rumble(".-.-");

//Print to the brain's screen that the program has ended.
Brain.Screen.newLine();//Move the cursor to a new line on the screen.
Brain.Screen.print("Autonomous has Ended.");
//Prevent main from exiting with an infinite loop.                        
while(1) {
  vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
}

}
// …

}

/----------------------------------------------------------------------------/
/* /
/
User Control Task /
/
/
/
This task is used to control your robot during the user control phase of /
/
a VEX Competition. /
/
/
/
You must modify the code to add your own robot specific commands here. /
/
----------------------------------------------------------------------------*/

void usercontrol( void ) {
// User control code here, inside the loop
while (1){
// This is the main execution loop for the user control program.
// Each time through the loop your program should update motor + servo
// values based on feedback from the joysticks.

// ........................................................................
// int main() {
Brain.Screen.setOrigin(1,1);    
//Display that the program has started to the screen.
Brain.Screen.print(""); 


//Use these variables to set the speed of the arm and claw.
int armSpeedPCT = 100;
int slowClawSpeedPCT = 100;
int chuteSpeedPCT = 100;
Brain.Screen.print("(Subbing to PewDiePie);");
    Brain.Screen.newLine();
    Brain.Screen.newLine();
    Brain.Screen.newLine();
    Brain.Screen.print("(Complete!);");
    Brain.Screen.newLine();
    Brain.Screen.newLine();
    Brain.Screen.newLine();
    Brain.Screen.newLine();


//Create an infinite loop so that the program can pull remote control values every iteration.
//This loop causes the program to run forever.
while(1) {
    
    

    //Drive Control 
    //Set the left and right motor to spin forward using the controller Axis values as the velocity value.
    FrontLeftDr.spin(vex::directionType::fwd, Controller1.Axis3.value(), vex::velocityUnits::pct);
    BackLeftDr.spin(vex::directionType::fwd, Controller1.Axis3.value(), vex::velocityUnits::pct);
    FrontRightDr.spin(vex::directionType::fwd, Controller1.Axis2.value(), vex::velocityUnits::pct);
    BackRightDr.spin(vex::directionType::fwd, Controller1.Axis2.value(), vex::velocityUnits::pct);
    
    //ArmMotor
    if(Controller1.ButtonL1.pressing()) { //If button L1 is pressed...
        //...Spin the arm motor forward.
        ArmMotor.spin(vex::directionType::fwd, armSpeedPCT, vex::velocityUnits::pct);
    }
    else if(Controller1.ButtonL2.pressing()) { //If the L2 button is pressed...
        //...Spin the arm motor backward.
        ArmMotor.spin(vex::directionType::rev, armSpeedPCT, vex::velocityUnits::pct);
    }
    else { //If the the L1 or L2 button is not pressed...
        //...Stop the arm motor.
        ArmMotor.stop(vex::brakeType::brake);
    }
     //LeftIntake Control
    if(Controller1.ButtonR1.pressing()) { //If the R1 button is pressed...
        //...Spin the LeftIntake forward.
        LeftIntake.spin(vex::directionType::fwd, slowClawSpeedPCT, vex::velocityUnits::pct);
    }
    else if(Controller1.ButtonR2.pressing()) { //If the R2 button is pressed...
        //...Spin the LeftIntake backward.
        LeftIntake.spin(vex::directionType::rev, slowClawSpeedPCT, vex::velocityUnits::pct);
    }
    else { //If the R1 or R2 button are not pressed...        
        //...Stop the LeftIntake.
        LeftIntake.stop(vex::brakeType::brake);        
    }
     //Slow RightIntake Control
    if(Controller1.ButtonR1.pressing()) { //If the R1 button is pressed...
        //...Spin the RightIntake forward.
        RightIntake.spin(vex::directionType::fwd, slowClawSpeedPCT, vex::velocityUnits::pct);
    }
    else if(Controller1.ButtonR2.pressing()) { //If the R2 button is pressed...
        //...Spin the RightIntake backward.
        RightIntake.spin(vex::directionType::rev, slowClawSpeedPCT, vex::velocityUnits::pct);
    }
    else { //If the R1 or R2 button are not pressed...        
        //...Stop the RightArmSlurper.
        RightIntake.stop(vex::brakeType::brake);                
    }
    //ChutePush Control
    if(Controller1.ButtonUp.pressing()) { //If the Up button is pressed...
        //...Spin the ChuteMotor forward.
        ChutePush.spin(vex::directionType::fwd, chuteSpeedPCT, vex::velocityUnits::pct);
    }
    else if(Controller1.ButtonDown.pressing()) { //If the Down button is pressed...
        //...Spin the ChuteMotor backward.
        ChutePush.spin(vex::directionType::rev, chuteSpeedPCT, vex::velocityUnits::pct);
    }
    else { //If the Up or Down button are not pressed...        
        //...Stop the ChutePush.
        ChutePush.stop(vex::brakeType::brake);        
    }
    
	vex::task::sleep(20); //Sleep the task for a short amount of time to prevent wasted resources.
}

}
// …

vex::task::sleep(20); //Sleep the task for a short amount of time to prevent wasted resources. 

}
}

//
// Main will set up the competition functions and callbacks.
//
int main() {

//Run the pre-autonomous function. 
pre_auton();

//Set up callbacks for autonomous and driver control periods.
Competition.autonomous( autonomous );
Competition.drivercontrol( usercontrol );

//Prevent main from exiting with an infinite loop.                        
while(1) {
  vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
}    

}

i put in the default pre auton but the program is still incorrect because of the voids

Those are called functions. void signifies the type of data the function returns (nothing, in the case of a void). The int in front of main indicates that it returns an integer number.

And speaking of functions, you tried to put a function inside another function, which is not allowed in C++ (not the way you did it anyway, and the way to actually do so is not useful here and is quite an advanced topic). You stuck your old main function inside the autonomous function, which is a no-no. You only wanted to move the contents of main into autonomous, not the whole declaration.

2 Likes

Ok, but how can I fix it?

My last comment was actually telling you how to fix it. Which bit of my recommendation did you need elaboration on?

Additionally, now that I look a little closer you made the same mistake inside usercontrol, just with a smaller version of your old main.

I dont seem to understand one of the errors in the competition project. this doesnt seem to work and I dont know how to fix it

while (1){
// This is the main execution loop for the user control program.
// Each time through the loop your program should update motor + servo
// values based on feedback from the joysticks.

// ........................................................................
// Insert user code here. This is where you use the joystick values to 
// update your motors, etc.
// ........................................................................

vex::task::sleep(20); //Sleep the task for a short amount of time to prevent wasted resources. 

}
}

it says expected unqualified-id