Problem in Autonomous Mode

I’m trying to do the autonomous programming for the world championship, but when I want to run the program, it doesn’t do anything, it literally doesn’t move at all, so a little help doesn’t hurt TT

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// Controller1          controller                    
// Right                motor_group   12, 11          
// Left                 motor_group   14, 15          
// Intaker              motor         2               
// Wings                digital_out   H               
// InsuranceStop        motor         13              
// InsuranceActive      digital_out   G               
// Elevation            motor_group   10, 9           
// ---- END VEXCODE CONFIGURED DEVICES ----

#include "vex.h"

using namespace vex;
void Go(){
  Left.spin(reverse,80,percent);
  Right.spin(forward,80,percent);

  }

void back(){
  Left.spin(forward,80,percent);
  Right.spin(reverse,80,percent);
}  

void TurnRight(){
  Left.spin(reverse,80,percent);
  Right.spin(reverse,80,percent);
}

void TurnLeft(){
  Left.spin(forward,80,percent);
  Right.spin(forward,80,percent);
}

void OneIntaker(){
  Intaker.spin(forward,80,percent);
}

void WingsOpen(){
  Wings.set(true);
}

void WingsClose(){
  Wings.set(false);
}

void StopDriveTain(){
  Right.stop();
  Left.stop();
}

void stop(){
  Right.stop();
  Left.stop();
  Intaker.stop();
  Wings.set(false);
}


// A global instance of competition
competition Competition;

// define your global instances of motors and other devices here

/*---------------------------------------------------------------------------*/
/*                          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 V5 has been powered on and        */
/*  not every time that the robot is disabled.                               */
/*---------------------------------------------------------------------------*/

void pre_auton(void) {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  Intaker.setVelocity(90,percent);
  Right.setVelocity(92,percent);
  Left.setVelocity(92,percent);

  // 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) {
  // ..........................................................................
  // Insert autonomous user code here.
  Brain.Screen.clearScreen();
  Brain.Screen.print("autonomous code");
  Brain.Timer.clear();
  while(true){
   while(Brain.Timer.time(seconds)>0 && Brain.Timer.time(seconds)<0.4){
    TurnLeft(); 
   }
    while(Brain.Timer.time(seconds)>0.5 && Brain.Timer.time(seconds)<1){
   StopDriveTain();
  }
   while(Brain.Timer.time(seconds)>0.8 && Brain.Timer.time(seconds)<1.1){
    Go(); 
   }

   while(Brain.Timer.time(seconds)>0.7 && Brain.Timer.time(seconds)<1.1){
   WingsOpen();
  }
   while(Brain.Timer.time(seconds)>0.8 && Brain.Timer.time(seconds)<1){
   Go();
  }
  
   while(Brain.Timer.time(seconds)>1.2 && Brain.Timer.time(seconds)<1.8){
   WingsClose();
  }

  while(Brain.Timer.time(seconds)>2.5 && Brain.Timer.time(seconds)<2.8){
   StopDriveTain();
  }

   while(Brain.Timer.time(seconds)>2 && Brain.Timer.time(seconds)<2.3){
   back();
  }
   while(Brain.Timer.time(seconds)>2.5 && Brain.Timer.time(seconds)<2.8){
   TurnRight();
  }
  }
  // ..........................................................................
}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              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) {
    
    //Tren motriz
    Right.spin(forward,Controller1.Axis2.position(),percent);
    Left.spin(forward,Controller1.Axis3.position(),percent);


   Right.spin(reverse,Controller1.Axis2.position(),percent);
    Left.spin(reverse,Controller1.Axis3.position(),percent);

    //Intaker
   if(Controller1.ButtonL1.pressing()==1){
    Intaker.spin(forward);
    }
    else if(Controller1.ButtonL2.pressing()==1){
    Intaker.spin(reverse);
    }
    
    else{
     Intaker.stop(hold);
    }

    //Elevation
    if(Controller1.ButtonR1.pressing()==1){
    Elevation.spin(reverse);
    }
    else if(Controller1.ButtonR2.pressing()==1){
    Elevation.spin(forward);
    }
    
    else{
     Elevation.stop(hold);
    }


    //Seguro de la elevacion para que pare la elevcion
    if(Controller1.ButtonA.pressing()==1){
      InsuranceStop.spin(forward);
    }

    else{
      InsuranceStop.stop(hold);
    }

      //Seguro para activar la elevacion
    if (Controller1.ButtonB.pressing()==1) {
            InsuranceActive.set(true);
           }
    //Alas de neumatica
     if (Controller1.ButtonX.pressing()) {
            Wings.set(true);  // Open the solenoid
     }

     if (Controller1.ButtonY.pressing()) { 
            Wings.set(false); // Open the solenoid

    }
  




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

    wait(20, msec); // Sleep the task for a short amount of time to
                    // prevent wasted resources.
  }


}



//
// Main will set up the competition functions and callbacks.

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


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

  // Prevent main from exiting with an infinite loop.
  while (true) {
    wait(100, msec);
  }
}

You are polling the value of Brain.Timer.time, which is an instance of vex::timer created at the start of the program. When connected to field control or using the “timed run” functionality on the V5 controller, autonomous will not immediately run (“timed run” has a 3 second disabled period that runs before autonomous, and field control can disable you indefinetly).

In other words, you are checking the time since program start, rather than the time since autonomous start, which are two very different things. Autonomous may start or stop at any time under field control, and your functions should be built around that fact. I highly recommend using vex::wait or another sleeping function to wait for a certain amount of time rather than while loops with system time.

Additionally since you don’t have any delays at the end of your while loops, you are effectively spinlocking (hogging) the entire CPU, which gives no clock cycles to the vexos task scheduler, which may mess up other things such as device background processing. If you do use while loops, you need to add a vex::wait at the end like this:

while (true) {
    vex::wait(10, vex::msec);
}

This will yield control to other tasks allowing them to run.

3 Likes

Thanks!!! It really works