Autonomous not functioning as expected using Comp Template

Autonomous mode in competition template is not running any motors. Same code works fine when placed in ‘user control section’ and comp switch is placed in DRIVER. I can display messages on the brain display, i.e. “Auto ON” and then “AUTO OFF” in autonomous void function but no motor commands seem to be executing.

void autonomous(void) {

LeftFront.setVelocity(50,percent);
LeftRear.setVelocity(50,percent);
RightFront.setVelocity(50,percent);
RightRear.setVelocity(50, percent);

// Dumper.setVelocity(25, percent);
// Dumper.spin(forward);
// LeftFront.spinFor(100, turns);

LeftFront.spin(forward);
LeftRear.spin(reverse);

RightFront.spin(forward);
RightRear.spin(reverse);

Brain.Screen.print(“AUTO ON”);

wait(3000,msec);

Brain.Screen.clearScreen();
Brain.Screen.print(“Auto Done”);

//LeftFront.stop();
//LeftRear.stop();
// RightFront.stop();
//RightRear.stop();
}

Try checking the device manager to see if the motor is getting any command.

For general uses put [code] [/code ] tags for your posts
Anyways, if it works in the user control check your callbacks at the bottom of the code that say

    Competition.autonomous( autonomous );

Make sure that that autonomous in the parenthesis is correct, or the code wont use it.

My autonomous is still not functioning. :frowning: As mentioned in my previous post the competition switch does not move motors iwith Autonomous ENABLED although the display shows the autonomous function executing. The same code works when I put it in the usercontrol (Shown below). What I have noticed is that the display prints the message REGARDLESS on the Driver/Autonomous switch even when the disable Switch is in DISABLE but no motor movement. When in ENABLE and DRIVER the motors move for the 2 seconds. Any help would be greatly appreciated.

Jim M.

void usercontrol(void) {

  LeftFront.setVelocity(50,percent);  
  LeftRear.setVelocity(50,percent); 
  RightFront.setVelocity(50,percent);
  RightRear.setVelocity(50, percent); 

  //Dumper.setVelocity(25, percent);
 // Dumper.spin(forward);  
 // LeftFront.spinFor(100, turns);  

  LeftFront.spin(reverse);
  LeftRear.spin(forward);

  RightFront.spin(forward);
  RightRear.spin(reverse);

  Brain.Screen.clearScreen();  

  Brain.Screen.print("userControl_ON");  
  
  wait(2000,msec);  

  Brain.Screen.clearScreen();
  Brain.Screen.print("UserControl_OFF");  

  LeftFront.stop(); 
  LeftRear.stop();
  RightFront.stop();
  RightRear.stop(); 

Can you upload your entire code if possible. I suspect it might have to do something with the int main() part.

1 Like

Here you go. Thank you for looking at it!


#include "vex.h"

using namespace vex;

// 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();

  // 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) {

  LeftFront.setVelocity(50,percent);  
  LeftRear.setVelocity(50,percent); 
  RightFront.setVelocity(50,percent);
  RightRear.setVelocity(50, percent); 

 // Dumper.setVelocity(25, percent);
 // Dumper.spin(forward);  
 // LeftFront.spinFor(100, turns);  

  LeftFront.spin(forward);
  LeftRear.spin(reverse);

  RightFront.spin(forward);
  RightRear.spin(reverse);

  Brain.Screen.print("AUTO ON");  
  
  wait(3000,msec);  

  Brain.Screen.clearScreen();
  Brain.Screen.print("Auto Done");  

  //LeftFront.stop(); 
  //LeftRear.stop();
 // RightFront.stop();
  //RightRear.stop();  
}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              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 drive_direction()
{
  LeftFront.spin(reverse);
  LeftRear.spin(forward);

  RightFront.spin(forward);
  RightRear.spin(reverse);

}

void whenControllerL1Pressed() {
  Dumper.spin(forward);
  waitUntil(!Controller1.ButtonL1.pressing());
  Dumper.stop();
}

void whenControllerL2Pressed() {
  Dumper.spin(reverse);
  waitUntil(!Controller1.ButtonL2.pressing());
  Dumper.stop();
}

void usercontrol(void) {

  /*LeftFront.setVelocity(50,percent);  
  LeftRear.setVelocity(50,percent); 
  RightFront.setVelocity(50,percent);
  RightRear.setVelocity(50, percent); 

  //Dumper.setVelocity(25, percent);
 // Dumper.spin(forward);  
 // LeftFront.spinFor(100, turns);  

  LeftFront.spin(reverse);
  LeftRear.spin(forward);

  RightFront.spin(forward);
  RightRear.spin(reverse);

  Brain.Screen.print("Deep State");  
  
  wait(2000,msec);  

  Brain.Screen.clearScreen();
  Brain.Screen.print("Impeachment");  

  LeftFront.stop(); 
  LeftRear.stop();
  RightFront.stop();
  RightRear.stop();  */

    // Deadband stops the motors when Axis values are close to zero.
  int deadband = 5;

  // Set motor's brake mode and velocity
  Dumper.setStopping(hold);
  Wrist.setStopping(hold);
  Claw.setStopping(hold); 
  Wrist.setVelocity(60, percent);
  Dumper.setVelocity(60, percent);
  Claw.setVelocity(60,percent);  

  // Register callbacks to controller button 'pressed' events.
  Controller1.ButtonL1.pressed(whenControllerL1Pressed);
  Controller1.ButtonL2.pressed(whenControllerL2Pressed);


  while (true) {
    // Get the velocity percentage of the left motor. (Axis3)
    int leftMotorSpeed = Controller1.Axis2.position();
    // Get the velocity percentage of the right motor. (Axis2)
    int rightMotorSpeed = Controller1.Axis3.position();

   
    if (Controller1.ButtonR1.pressing()) {
      Wrist.spin(forward);
    } else if (Controller1.ButtonR2.pressing()) {
      Wrist.spin(reverse);
    } else {
      Wrist.stop();
    }

    if (Controller1.ButtonL1.pressing()) {
      Dumper.spin(forward);
    } else if (Controller1.ButtonL2.pressing()) {
      Dumper.spin(reverse);
    } else {
      Dumper.stop();
    }

    if (Controller1.ButtonUp.pressing()) {
      Claw.spin(forward);
    } else if (Controller1.ButtonDown.pressing()) {
      Claw.spin(reverse);
    } else {
      Claw.stop();
    }

    // Set the speed of the left motor. If the value is less than the deadband,
    // set it to zero.
    if (abs(leftMotorSpeed) < deadband) {
      // Set the speed to zero.
      LeftFront.setVelocity(0, percent);
      LeftRear.setVelocity(0, percent);  
    } else {
      // Set the speed to leftMotorSpeed
      LeftFront.setVelocity(leftMotorSpeed, percent);
      LeftRear.setVelocity(leftMotorSpeed, percent);  
    }

    // Set the speed of the right motor. If the value is less than the deadband,
    // set it to zero.
    if (abs(rightMotorSpeed) < deadband) {
      // Set the speed to zero
      RightFront.setVelocity(0, percent);
      RightRear.setVelocity(0, percent); 
    } else {
      // Set the speed to rightMotorSpeed
      RightFront.setVelocity(rightMotorSpeed, percent);
      RightRear.setVelocity(rightMotorSpeed, percent); 
    }

    // Robot Direction
    drive_direction();  
    

    wait(25, msec);
  } 
   
}


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

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

  usercontrol(); 

  
}

The issue is caused by you calling the userControl function in your int main function. You are setting up the callbacks for the auto and then user control already with the first two lines. Then before auto can start you are starting driver control. Try removing the call to userControl that is under the pre_auton function and it should work.

Also just for reference here is the default main function in the competition template.

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);
  }
1 Like

OK so I placed an autonomous call in between the pre_auton() and usercontrol(). Progress… but with the comp switch in disable the auton still runs without motor operation (i.e. the display show autonON). If I run the program with the comp switch in ENABLE and AUTONOMOUS the motors work and the going AUTONOMOUS to DRIVER transition functions as expected. So the questions I have are, first why did I have to place an autonomous call when Competition .autonomous(autonomous) , second why doesn’t the comp Switch in DISABLE prevent the autonomous from executing until switch to ENABLE?

Thanks,
Jim M.

Could you post your updated code? The main function should look like the one posted in my reply above. In regards to your other questions you should not have to place another auto call. You should have just had to setup the main function like the template. As far as the other question goes I do not have my switch with me to test and see why that might be so I can not be of much help for that.

Zurtar, thank you. Not sure why I didn’t see that. My solution prior to your reply sort of got me there. I did as you suggested and all is well :slight_smile:

1 Like