Function not working under Autonomous section? (C++)

Hey forum, I have a pretty weird problem. For my autonomous, I have made a function that allows me to input a number of degrees for one motor, and then all of the chassis motors run until that motor reaches that value. For testing, I mapped a command using the function to one of my controller buttons, and it worked great, all 4 motors moved. HOWEVER, when I ran the same command during autonomous, only the motor that I use for the degrees value runs, the other 3 stay stationary. Does anyone know why this happens? My code will be posted below, please tell me how I can fix my function/code in general. Thank you!

#include "robot-config.h"

void sensorControl(int speed, int sensor) //basically input the speed you want and then specify the # of degrees you want to go
    {
        LeftFront.setVelocity(speed,velocityUnits::rpm); 
        LeftBack.setVelocity(speed,velocityUnits::rpm);
        RightFront.setVelocity(speed, velocityUnits::rpm);
        RightBack.setVelocity(speed, velocityUnits::rpm);
        LeftFront.rotateFor(sensor,rotationUnits::deg);

void autonomous( void ) {
 sensorControl(125, 1900);
}

void usercontrol( void ) {
 while(1){
      if(Controller1.ButtonA.pressing()){
          sensorControl(125, 1900);
      }
   }
  }

^these are snippets from my code.

can you post the whole sensorControl function.

My bad, I was copying the code over and I forgot an ending bracket. All that I have written for the function is

void sensorControl(int speed, int sensor) //basically input the speed you want and then specify the # of degrees you want to go
    {
        LeftFront.setVelocity(speed,velocityUnits::rpm); 
        LeftBack.setVelocity(speed,velocityUnits::rpm);
        RightFront.setVelocity(speed, velocityUnits::rpm);
        RightBack.setVelocity(speed, velocityUnits::rpm);
        LeftFront.rotateFor(sensor,rotationUnits::deg); 
}

@csiv23 you only tell the LeftFront motor to rotate, all of the others only have a velocity. You need to tell the rest of the motors to rotate :


LeftBack.rotateFor(sensor,rotationUnits::deg); 
RightFront.rotateFor(sensor,rotationUnits::deg); 
RightBack.rotateFor(sensor,rotationUnits::deg); 

@InsertString is correct about the problem, but not the solution. Read this part of the documentation on Motor.setVelocity:

If you use @InsertString 's solution, you need to add the optional waitForCompletion boolean as false. Otherwise you’ll rotate each motor one at a time. If you want to use your own method properly, change “setVelocity” to “spin” for all but leftFront. You can skip Motor.setVelocity if you use this version of spin:


spin(directionType dir, double velocity, velocityUnits units)

Here is the same documentation for spin:

So would that only run my LeftFront motor or all of them?

Your code only runs LeftFront because you choose velocities but you never tell the motors to use them, except LeftFront. If you switch to spin, as I suggested, you will then be telling those motors to use the speeds. So my version will run all four motors. You could also leave your setVelocity and add three spin calls to tell the motors to use the velocities, but that’s essentially writing three lines of code as six for no real benefit.

Will the other motors continue running after the LeftFront motor meets the specified degrees? This is assuming I switch the commands to spin.

Yes, they will keep running. Stop them all with code at the end of the function.

Would I need to make a new function to go in reverse? I don’t understand how to add a direction value (so the command would be something like this)


sensorControl(fwd, 100, 1250);

(moe forwards at 10 rpm for 1250 degrees).

I have changed the function to this:

  void sensorControl(double speed, int sensor)
    {   
        LeftFront.setVelocity(speed,velocityUnits::rpm); 
        LeftBack.spin(directionType::fwd,speed, velocityUnits::rpm);
        RightFront.spin(directionType::fwd,speed, velocityUnits::rpm);
        RightBack.spin(directionType::fwd,speed, velocityUnits::rpm);
        LeftFront.rotateFor(sensor,rotationUnits::deg);
    }

You could feed the function a negative number to go in reverse using an if statement to check for negative numbers, then set the proper direction.