Autonomous program skipping to the end

When I run a timed run or autonomous skills challenge, about 75% of the time the program skips to the last couple lines of code that drop the cubes. The other 25% of the time, the program works perfectly. Does anyone have any ideas why this is happening? We have a competition on Saturday, February 1st and this is pretty urgent.

Here’s the program:

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       C:\Users\th12093                                          */
/*    Created:      Thu Jan 09 2020                                           */
/*    Description:  V5 project                                                */
/*                                                                            */
/*----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// Controller1          controller                    
// LeftMotor            motor         1               
// RightMotor           motor         2               
// RampMotor            motor         3               
// LeftCube             motor         4               
// RightCube            motor         5               
// ---- END VEXCODE CONFIGURED DEVICES ----

#include "vex.h"
using namespace vex;

//Global instance of competition
competition Competition;

//Define other global instances of motors and other devices below

//Start of Pre-Autonomous
void pre_auton(void) {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // All activities that occur before the competition starts
}
//End of Pre-Autonomous

//Start of Autonomous
void autonomous(void) {
  //Moving forward to pick up cubes
  LeftCube.spinFor(10, turns, false);
  RightCube.spinFor(10, turns, false);
  LeftMotor.spinFor(540, degrees, false);
  RightMotor.spinFor(540, degrees);

  //Turning left to go to the alliance scoring zone
  RightMotor.spinFor(610, degrees);

  //Moving to the scoring zone
  LeftMotor.spinFor(520, degrees, false);
  RightMotor.spinFor(520, degrees);

  //Dropping cubes in the scoring zone
  RampMotor.spinFor(270, degrees);
  LeftCube.spin(reverse);
  RightCube.spin(reverse);
}
//End of Autonomous

//Start of User Control
void usercontrol(void) {
  int deadband = 1;
  while (1) {
    //START OF JOYSTICK CONTROL ______________________________________________________________________________________________
    //Setting the velocity of the cube motors
    LeftCube.setVelocity(100, percent);
    RightCube.setVelocity(100, percent);
    RampMotor.setMaxTorque(100, percent);

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

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

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

    //Spinning all motors
    LeftMotor.spin(forward);
    RightMotor.spin(forward);
    //END OF JOYSTICK CONTROL _________________________________________________________________________________________________
    
    //START OF BUTTON CONTROL _________________________________________________________________________________________________
    //R1 spins the motors to pick up the cubes
    if (Controller1.ButtonR1.pressing()) {
      RightCube.spin(forward);
      LeftCube.spin(forward);}
      
      //R2 Spins the motors to drop up the cubes
      else if (Controller1.ButtonR2.pressing()) {
      RightCube.spin(reverse);
      LeftCube.spin(reverse);
      }

      else {
      RightCube.stop();
      LeftCube.stop();
      }
    //END OF BUTTON CONTROL __________________________________________________________________________________________________
    wait(1, msec);
  }
}
//End of User Control

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

  // Runining pre-auton
  pre_auton();

  // Prevents main from exiting with an infinite loop
  while (true) {
    wait(100, msec);
  }
}
1 Like

Try resetting your motors in preauton and right before your auton

2 Likes

How would I do that? Sorry, I’m new to the ‘.V5code’ language. I’m the lead programmer for the team, and this is the first year I programmed the robot. I like programming, just haven’t done it with this ‘.V5code’

Motor.resetRotation();
Where Motor is the name of your motor

2 Likes

All of the motors or just the drive motors?

You should do all the motors just in case. Might as well reset them all to 0

2 Likes

Okay! Thank you! I’ll test it when we meet today, but I’ve already fixed it in the code. Our team coach mentioned that the code wouldn’t work perfectly until you restarted the robot, and I’ve tested that, so I don’t think that’s the issue.

When I do that, it just resets the value of the motor correct? It doesn’t move the motor at all?

That would be correct

Resetting the robot is what @thorstenl312’s code does for you. It’s the same as a reset. You could also just put the reset code in the front of the autonomous code to set everything to zero before the routine runs. I’d most likely do that so I could do multiple runs without restarting the robot.

But in any case @thorstenl312 has your solution.

2 Likes

I put @thorstenl312’s code in and it worked more consistently, about to a 50/50 ratio, but the program would still skip to the same, last three lines of code. I created a new competition template and put the autonomous code in, along with @thorstenl312’s pre-auton code (suggested by our team coaches which are both programmers). That program worked 100% of the time. IDK why this is, but today I’m going to build off of the new program with the autonomous code in it that works. I’ll see if that fixes it.

Does anyone know why the same code would work in one program, but not in the other? I haven’t had this happen when I code in HTML, Python 2 or C++ before.

2 Likes

What happens when you add a delay between each segment of code? if the code waits at the delays, then the .spinFor method isnt waiting until it is finished before continuing.

1 Like

It will keep spinning the cube motors and stay stationary for a second, then move onto the next piece of code.

The code was working correctly for about ten runs of autonomous, and now the program is back to skipping down to the last three lines of code.