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);
}
}
}
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);
}
@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:
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.