C++ hitches or skips

My teams code works perfect without our turn but when we add the turn our code skips and hitches.
Using V5 btw
Can anyone help?

post code if you want help

we posted sample code - this might help

Specifically:


void driveTurnFor(float count)
{
    Leftfront.rotateFor(-1.0*count, rotationUnits::rev,false);
    Leftback.rotateFor(-1.0*count, rotationUnits::rev,false);
    Rightback.rotateFor(count, rotationUnits::rev,false);
    Rightfront.rotateFor(count, rotationUnits::rev);  // block until complete
    task::sleep(50);   
}

void driveTurnDegrees(float degrees)
{
    float rotdegree = 0.82/90;

    driveSetVelocity(40.0);    // turn more slowly to avoid overshoot

    driveTurnFor( degrees * rotdegree );
 
}

void driveTurnLeftDegrees(float degrees)    // counter clockwise - so negative degrees
{
    driveTurnDegrees( 1.0 * degrees);       
}

void driveTurnRightDegrees(float degrees)   // clockwise
{
    driveTurnDegrees(-1.0 * degrees);
}
#include "robot-config.h"
          
int main() {
    RightMotor.setReversed(true);
    LeftMotor.setReversed(false);
    LeftMotor.startRotateFor(3.5,vex::rotationUnits::rev,40,vex::velocityUnits::pct);
    RightMotor.startRotateFor(3.5,vex::rotationUnits::rev,40,vex::velocityUnits::pct);
    while(LeftMotor.isSpinning()||RightMotor.isSpinning());
    vex::task::sleep(1000); 
    RightMotor.startRotateFor(1,vex::rotationUnits::rev,40,vex::velocityUnits::pct);
    LeftMotor.startRotateFor(-1,vex::rotationUnits::rev,40,vex::velocityUnits::pct);
    vex::task::sleep(1000); 
    LeftMotor.startRotateFor(6.5,vex::rotationUnits::rev,40,vex::velocityUnits::pct);
    RightMotor.startRotateFor(6.5,vex::rotationUnits::rev,40,vex::velocityUnits::pct);
    vex::task::sleep(10000);
    RightMotor.startRotateFor(1,vex::rotationUnits::rev,20,vex::velocityUnits::pct);
    LeftMotor.startRotateFor(-1,vex::rotationUnits::rev,20,vex::velocityUnits::pct);
    vex::task::sleep(1000); 
    RightMotor.startRotateFor(2.875,vex::rotationUnits::rev,40,vex::velocityUnits::pct);
    LeftMotor.startRotateFor(2.875,vex::rotationUnits::rev,40,vex::velocityUnits::pct);
    vex::task::sleep(1000); 
}

Oops I did not see you do startRotateFor, this means all your motor commands are trying to operate at once and may or may not complete before you pass to the next command.

Instead of sleeping, use blocking commands like in our example code for motion.

Where would I include the startrotatefor in my code
P.S. Ive only been coding for a week
@lacsap

try this for your motion commands:

LeftMotor.rotateFor(3.5,vex::rotationUnits::rev,40,vex::velocityUnits::pct,false);
RightMotor.rotateFor(3.5,vex::rotationUnits::rev,40,vex::velocityUnits::pct);

and do this for all your subsequent commands. This will cause the two sides of your robot to work together - in the above example to turn 3.5 revolutions.

and this:

LeftMotor.rotateFor(3.5,vex::rotationUnits::rev,40,vex::velocityUnits::pct,false);
RightMotor.rotateFor(-3.5,vex::rotationUnits::rev,40,vex::velocityUnits::pct);

would cause the robot to turn for 3.5 revolutions of the motors

Quick clarification.

RightMotor.startRotateFor() tells the motor to start moving now, and then the program moves to the next line of code. It does not wait for the motor to finish the command. So if your code was:

RightMotor.startRotateFor(3.5,vex::rotationUnits::rev,40,vex::velocityUnits::pct);
RightMotor.stop();

…the motor would start to execute the first command, but then get the stop command so quickly that you might not even see it move.

RightMotor.rotateFor() tells gives the motor the rotate command but does not move to the next line of code until the motor finishes the movement. The rotateFor() function does not return control to the main task until that movement is done. So:

RightMotor.rotateFor(3.5,vex::rotationUnits::rev,40,vex::velocityUnits::pct);
RightMotor.stop();

…would not execute the stop() instruction until the motor had completed its movement of 3.5 rotations. In this case the RightMotor.stop() command is unnecessary because the motor would stop on its own.

So for turns you want to start the first motor with the non-blocking startRotateFor() command, and then use the blocking command rotateFor() for your second motor. That will make sure that nothing else is sent to the motor until it is finished with the turn.

Your code would be:

#include "robot-config.h" 
int main() { 
    RightMotor.setReversed(true);
    LeftMotor.setReversed(false);

    LeftMotor.startRotateFor(3.5,vex::rotationUnits::rev,40,vex::velocityUnits::pct);
    RightMotor.rotateFor(3.5,vex::rotationUnits::rev,40,vex::velocityUnits::pct);

    RightMotor.startRotateFor(1,vex::rotationUnits::rev,40,vex::velocityUnits::pct);
    LeftMotor.rotateFor(1,vex::rotationUnits::rev,40,vex::velocityUnits::pct);

Your intended design pattern of checking to see if the motor is spinning or not with a while loop is valid, but the isSpinning() function is a little misleading. It checks a motor to see if it is in the middle of a rotateFor() command. But since that function is blocking, it’s mostly useful for monitoring motors that are being controlled by threads and tasks that are off doing things on their own from the main line of execution.

You might be able to use the velocity() function to do the same thing with startRotateFor() calls:

while(LeftMotor.velocity() != 0 || RightMotor.velocity() != 0) {
    vex::task::sleep(20);
}

Using the blocking/non-blocking calls is cleaner, though.