C++ Pro Gyro Coding

I have to say that I’m a noob at C++ and have never used a gyro. Suppose I have a gyro in the middle of the bot and I want to use it to perfect the turns in the autonomous. What would be the steps to approach that? I’m guessing before anything else, I have to clear the gyro every time before a new turn of its previous values. Also, I’m guessing I have to calibrate it too. I’m looking to make a function that I would pass the degree of the turn to and the robot would do it at 50% power to not overshoot. Also, do I need to use positive and negative degrees for different side turns?
This is my view of how it should be layed out:


void turn (double degrees_to_rotate){
    
    double current_deg = Gyro.value(vex::rotationUnits::deg);
    
    while (current_deg < degrees_to_rotate){
        current_deg = Gyro.value(vex::rotationUnits::deg);
        FrontRight.setVelocity(50,vex::percentUnits::pct);
        FrontLeft.setVelocity(50,vex::percentUnits::pct);
        RearRight.setVelocity(50,vex::percentUnits::pct);
        RearLeft.setVelocity(50,vex::percentUnits::pct);
        
        if (degrees_to_rotate > 0){
            FrontRight.spin(vex::directionType::rev);
            FrontLeft.spin(vex::directionType::fwd);
            RearRight.spin(vex::directionType::rev);
            RearLeft.spin(vex::directionType::fwd);
        }else if( degrees_to_rotate < 0){
            FrontRight.spin(vex::directionType::fwd);
            FrontLeft.spin(vex::directionType::rev);
            RearRight.spin(vex::directionType::fwd);
            RearLeft.spin(vex::directionType::rev);
        }else{
            FrontRight.stop();
            FrontLeft.stop();
            RearRight.stop();
            RearLeft.stop();
        }
    }
}

And when i need to call the void:


turn(90);

If there are any errors to be corrected or any way to improve this bit of code, plz comment.

Thank you.

currently I don’t think theres a way to reset gyro to 0 in C++, as for perfect turns I recommend PID control.

@ Joshua_L text me and explain PID if u have time

There’s like 10 million guides on it bro just find one and read.

You have a bunch of problems all based off just what you’re doing with the while() statement. First, since the gyro isn’t reset, you don’t want to actually target your degrees_to_rotate. Second, you’re not paying any attention to the sign of degrees_to_rotate; you did later but not here. Third, you’re never stopping. You’ve only said to stop if you choose not to rotate.

Add something like this at the beginning: double target_deg = Gyro.value(vex::rotationUnits::deg) + degrees_to_rotate;

Put your setVelocity stuff before the while loop.

Put your if-then for direction next, without the else at the end. Inside the if and the else if, as the last line, put a while(Gyro.value(vex::rotationUnits::deg); compared to target_deg){sleep for a short bit}.

After the if-then (which now includes the while loops), turn your motors off.

@callen should I put all of the while loops inside the if/else if statement?

also the last while loop, I didn’t get what it should do? and when you said to sleep for a bit, you meant like 20Msec?

Doesn’t V5 already include PID? And is there a way to start it in the program? Sorry, just new to all of these programming aspects

For some reason when I search the threads for pid, it gives me nothing on it

I would use just two while loops, one inside each of the if possibilities. Yes, just let it sleeps for 20 ms or so.

Yes, V5 has PID. If you set velocities as you are, then you’re already using the V5 PID.

Sorry, Slightly off topic but do you need to still scale the gyros in VCS?

@Gary Patel i honestly don’t know what that is.

@callen how do i calibrate a gyro or lets say - prepare it before auton?