Using the gyro sensor more than once?

  1. 2 weeks ago

    abennett5139

    Jan 3 Concord, NC 5139

    So I'm using the gyro sensor to turn and to compensate for veering off course when going straight. But whenever I use it a second time in my code it doesn't do anything and skips that section of code entirely. Do I have to do a gyro.isCalibrating before every time I use it. Any help would be much appreciated, thanks :).

  2. jpearman

    Jan 3 Moderator, ROBOTC Tech Support, V5 Beta Moderator Los Angeles 8888
    Edited 2 weeks ago by jpearman

    When your program is executed the gyro will calibrate, that takes about 1 second and the robot should not be moving when that happens. While the gyro is calibrating the gyro.isCalibrating method will return true. Once the gyro is calibrated it should not be necessary to calibrate again.

    The gyro will return 0 degrees after calibration has finished, the gyro will then return values in the range +/- 360 degrees when it turns. It's up to your code to handle any roll over conditions with the current version of vexos, we will change that and also provide a reset (meaning reseting the value to 0, not a full recalibration) in a future vexos/VCS release as it seems so many users are having trouble with that.

    Perhaps post the section of code causing trouble and we can suggest improvements.

  3. abennett5139

    Jan 3 Concord, NC 5139

    Im not able to post the code at the time being but I will first thing in the morning, thanks for the response!

  4. Deleted 2 weeks ago by jpearman
  5. jpearman

    Jan 3 Moderator, ROBOTC Tech Support, V5 Beta Moderator Los Angeles 8888

    @abennett5139 Did you post code ? It looks like you updated this thread 2-3 hours ago but I don't see anything new here.

  6. abennett5139

    Jan 3 Concord, NC 5139

    I thought I did I'll post it in smaller sections and see if that helps it

  7. abennett5139

    Jan 3 Concord, NC 5139

    Here's the gyro calibration

    Gyro.startCalibration(500);
                            
                task::sleep(10);
        
                while( Gyro.isCalibrating() )
                {
                    task::sleep(500);
                }
  8. abennett5139

    Jan 3 Concord, NC 5139

    Here's the function that accounts for the veering of the drive train

    //Drives the bot forward in centimeters using the gryo to compensate for veering
    void DriveFWDforCMGyro (double TravelTarget, int Velocity)
    {
        //Formula to deride the distance needed to go in centimeters
        Velocity *= 2;
        
        double DegreesToRotate = (360*TravelTarget)/Circumference;
    
    
        //Drive the bot forward for the distance specified in centimeters with the gyro compensating for veering
        while(FL_Drive.rotation(rotationUnits::deg) <= DegreesToRotate)
        {
            while (FL_Drive.rotation(rotationUnits::deg) <= DegreesToRotate*.30)
            {
                while (Gyro.value(rotationUnits::deg) < -0.01)
                {
                    FL_Drive.spin(directionType::fwd,Velocity*.5,velocityUnits::rpm);
                    FR_Drive.spin(directionType::fwd,Velocity*0.46,velocityUnits::rpm);
                    BL_Drive.spin(directionType::fwd,Velocity*.5,velocityUnits::rpm);
                    BR_Drive.spin(directionType::fwd,Velocity*0.46,velocityUnits::rpm);
                    task::sleep(125);
                }
            
                while (Gyro.value(rotationUnits::deg) > 0.01)
                {
                    FL_Drive.spin(directionType::fwd,Velocity*0.485,velocityUnits::rpm);
                    FR_Drive.spin(directionType::fwd,Velocity*.5,velocityUnits::rpm);
                    BL_Drive.spin(directionType::fwd,Velocity*0.485,velocityUnits::rpm);
                    BR_Drive.spin(directionType::fwd,Velocity*.5,velocityUnits::rpm);
                    task::sleep(125);
                }
            
                while ( Gyro.value(rotationUnits::deg) > -0.01 &&  Gyro.value(rotationUnits::deg) < 0.01)
                {
                    FL_Drive.spin(directionType::fwd,Velocity*.5,velocityUnits::rpm);
                    FR_Drive.spin(directionType::fwd,Velocity*.5,velocityUnits::rpm);
                    BL_Drive.spin(directionType::fwd,Velocity*.5,velocityUnits::rpm);
                    BR_Drive.spin(directionType::fwd,Velocity*.5,velocityUnits::rpm);
                    task::sleep(125);
                }
            }
            
            while (FL_Drive.rotation(rotationUnits::deg) <= DegreesToRotate*.65)
            {
                while (Gyro.value(rotationUnits::deg) < -0.01)
                {
                    FL_Drive.spin(directionType::fwd,Velocity*.75,velocityUnits::rpm);
                    FR_Drive.spin(directionType::fwd,Velocity*0.71,velocityUnits::rpm);
                    BL_Drive.spin(directionType::fwd,Velocity*.75,velocityUnits::rpm);
                    BR_Drive.spin(directionType::fwd,Velocity*0.71,velocityUnits::rpm);
                    task::sleep(125);
                }
            
                while (Gyro.value(rotationUnits::deg) > 0.01)
                {
                    FL_Drive.spin(directionType::fwd,Velocity*0.735,velocityUnits::rpm);
                    FR_Drive.spin(directionType::fwd,Velocity*.75,velocityUnits::rpm);
                    BL_Drive.spin(directionType::fwd,Velocity*0.735,velocityUnits::rpm);
                    BR_Drive.spin(directionType::fwd,Velocity*.75,velocityUnits::rpm);
                    task::sleep(125);
                }
            
                while ( Gyro.value(rotationUnits::deg) > -0.01 &&  Gyro.value(rotationUnits::deg) < 0.01)
                {
                    FL_Drive.spin(directionType::fwd,Velocity*.75,velocityUnits::rpm);
                    FR_Drive.spin(directionType::fwd,Velocity*.75,velocityUnits::rpm);
                    BL_Drive.spin(directionType::fwd,Velocity*.75,velocityUnits::rpm);
                    BR_Drive.spin(directionType::fwd,Velocity*.75,velocityUnits::rpm);
                    task::sleep(125);
                }
            }
            
            while (FL_Drive.rotation(rotationUnits::deg) <= DegreesToRotate)
            {
                while (Gyro.value(rotationUnits::deg) < -0.01)
                {
                    FL_Drive.spin(directionType::fwd,Velocity,velocityUnits::rpm);
                    FR_Drive.spin(directionType::fwd,Velocity*0.96,velocityUnits::rpm);
                    BL_Drive.spin(directionType::fwd,Velocity,velocityUnits::rpm);
                    BR_Drive.spin(directionType::fwd,Velocity*0.96,velocityUnits::rpm);
                    task::sleep(125);
                }
            
                while (Gyro.value(rotationUnits::deg) > 0.01)
                {
                    FL_Drive.spin(directionType::fwd,Velocity*0.985,velocityUnits::rpm);
                    FR_Drive.spin(directionType::fwd,Velocity,velocityUnits::rpm);
                    BL_Drive.spin(directionType::fwd,Velocity*0.985,velocityUnits::rpm);
                    BR_Drive.spin(directionType::fwd,Velocity,velocityUnits::rpm);
                    task::sleep(125);
                }
            
                while ( Gyro.value(rotationUnits::deg) > -0.01 &&  Gyro.value(rotationUnits::deg) < 0.01)
                {
                    FL_Drive.spin(directionType::fwd,Velocity,velocityUnits::rpm);
                    FR_Drive.spin(directionType::fwd,Velocity,velocityUnits::rpm);
                    BL_Drive.spin(directionType::fwd,Velocity,velocityUnits::rpm);
                    BR_Drive.spin(directionType::fwd,Velocity,velocityUnits::rpm);
                    task::sleep(125);
                }
            }
            
        }
        
    
        //Wait for 100 miliseconds so the bot does not move off course
    
        task::sleep(100);
        
        FL_Drive.stop();  
        FR_Drive.stop();
        BL_Drive.stop();  
        BR_Drive.stop();
    }
  9. abennett5139

    Jan 3 Concord, NC 5139

    Here's the reverse version of that function

    //Drives the bot reverse in centimeters using the gryo to compensate for veering
    void DriveREVforCMGyro (double TravelTarget, int Velocity)
    {
        //Formula to deride the distance needed to go in centimeters
        Velocity *= 2;
        
        double DegreesToRotate = (360*TravelTarget)/Circumference;
    
    
        //Drive the bot forward for the distance specified in centimeters with the gyro compensating for veering
        while(FL_Drive.rotation(rotationUnits::deg) <= DegreesToRotate)
        {
            while (FL_Drive.rotation(rotationUnits::deg) <= DegreesToRotate*.30)
            {
                while (Gyro.value(rotationUnits::deg) < -0.01)
                {
                    FL_Drive.spin(directionType::rev,Velocity*.5,velocityUnits::rpm);
                    FR_Drive.spin(directionType::rev,Velocity*0.46,velocityUnits::rpm);
                    BL_Drive.spin(directionType::rev,Velocity*.5,velocityUnits::rpm);
                    BR_Drive.spin(directionType::rev,Velocity*0.46,velocityUnits::rpm);
                    task::sleep(125);
                }
            
                while (Gyro.value(rotationUnits::deg) > 0.01)
                {
                    FL_Drive.spin(directionType::rev,Velocity*0.485,velocityUnits::rpm);
                    FR_Drive.spin(directionType::rev,Velocity*.5,velocityUnits::rpm);
                    BL_Drive.spin(directionType::rev,Velocity*0.485,velocityUnits::rpm);
                    BR_Drive.spin(directionType::rev,Velocity*.5,velocityUnits::rpm);
                    task::sleep(125);
                }
            
                while ( Gyro.value(rotationUnits::deg) > -0.01 &&  Gyro.value(rotationUnits::deg) < 0.01)
                {
                    FL_Drive.spin(directionType::rev,Velocity*.5,velocityUnits::rpm);
                    FR_Drive.spin(directionType::rev,Velocity*.5,velocityUnits::rpm);
                    BL_Drive.spin(directionType::rev,Velocity*.5,velocityUnits::rpm);
                    BR_Drive.spin(directionType::rev,Velocity*.5,velocityUnits::rpm);
                    task::sleep(125);
                }
            }
            
            while (FL_Drive.rotation(rotationUnits::deg) <= DegreesToRotate*.65)
            {
                while (Gyro.value(rotationUnits::deg) < -0.01)
                {
                    FL_Drive.spin(directionType::rev,Velocity*.75,velocityUnits::rpm);
                    FR_Drive.spin(directionType::rev,Velocity*0.71,velocityUnits::rpm);
                    BL_Drive.spin(directionType::rev,Velocity*.75,velocityUnits::rpm);
                    BR_Drive.spin(directionType::rev,Velocity*0.71,velocityUnits::rpm);
                    task::sleep(125);
                }
            
                while (Gyro.value(rotationUnits::deg) > 0.01)
                {
                    FL_Drive.spin(directionType::rev,Velocity*0.735,velocityUnits::rpm);
                    FR_Drive.spin(directionType::rev,Velocity*.75,velocityUnits::rpm);
                    BL_Drive.spin(directionType::rev,Velocity*0.735,velocityUnits::rpm);
                    BR_Drive.spin(directionType::rev,Velocity*.75,velocityUnits::rpm);
                    task::sleep(125);
                }
            
                while ( Gyro.value(rotationUnits::deg) > -0.01 &&  Gyro.value(rotationUnits::deg) < 0.01)
                {
                    FL_Drive.spin(directionType::rev,Velocity*.75,velocityUnits::rpm);
                    FR_Drive.spin(directionType::rev,Velocity*.75,velocityUnits::rpm);
                    BL_Drive.spin(directionType::rev,Velocity*.75,velocityUnits::rpm);
                    BR_Drive.spin(directionType::rev,Velocity*.75,velocityUnits::rpm);
                    task::sleep(125);
                }
            }
            
            while (FL_Drive.rotation(rotationUnits::deg) <= DegreesToRotate)
            {
                while (Gyro.value(rotationUnits::deg) < -0.01)
                {
                    FL_Drive.spin(directionType::rev,Velocity,velocityUnits::rpm);
                    FR_Drive.spin(directionType::rev,Velocity*0.96,velocityUnits::rpm);
                    BL_Drive.spin(directionType::rev,Velocity,velocityUnits::rpm);
                    BR_Drive.spin(directionType::rev,Velocity*0.96,velocityUnits::rpm);
                    task::sleep(125);
                }
            
                while (Gyro.value(rotationUnits::deg) > 0.01)
                {
                    FL_Drive.spin(directionType::rev,Velocity*0.985,velocityUnits::rpm);
                    FR_Drive.spin(directionType::rev,Velocity,velocityUnits::rpm);
                    BL_Drive.spin(directionType::rev,Velocity*0.985,velocityUnits::rpm);
                    BR_Drive.spin(directionType::rev,Velocity,velocityUnits::rpm);
                    task::sleep(125);
                }
            
                while ( Gyro.value(rotationUnits::deg) > -0.01 &&  Gyro.value(rotationUnits::deg) < 0.01)
                {
                    FL_Drive.spin(directionType::rev,Velocity,velocityUnits::rpm);
                    FR_Drive.spin(directionType::rev,Velocity,velocityUnits::rpm);
                    BL_Drive.spin(directionType::rev,Velocity,velocityUnits::rpm);
                    BR_Drive.spin(directionType::rev,Velocity,velocityUnits::rpm);
                    task::sleep(125);
                }
            }
            
        }
        
    
        //Wait for 100 miliseconds so the bot does not move off course
    
        task::sleep(100);
        
        FL_Drive.stop();  
        FR_Drive.stop();
        BL_Drive.stop();  
        BR_Drive.stop();
    }
  10. abennett5139

    Jan 3 Concord, NC 5139

    Here's the turning functions

    //Turns the bot counter clockwise using the gyro sensor
    void GyroTurnCCW (double TurnTarget, int Velocity)
    {
        //Assignment to convert the velocity from percent to rpm
        Velocity *= 2;
        
        //Turning counter clockwise the gyro value is negative
        //While the gyro value is greater than 40% of the turn target turn the robot 100% of speed
        while (Gyro.value(rotationUnits::deg) >= TurnTarget * .40)
        {
            FL_Drive.spin(directionType::fwd,Velocity,velocityUnits::rpm);
            FR_Drive.spin(directionType::rev,Velocity,velocityUnits::rpm);
            BL_Drive.spin(directionType::fwd,Velocity,velocityUnits::rpm);
            BR_Drive.spin(directionType::rev,Velocity,velocityUnits::rpm);
        }
        
        //While the gyro value is greater than 75% of the turn target turn the robot 75% of speed
        while (Gyro.value(rotationUnits::deg) >= TurnTarget * .75)
        {
            FL_Drive.spin(directionType::fwd,Velocity*.75,velocityUnits::rpm);
            FR_Drive.spin(directionType::rev,Velocity*.75,velocityUnits::rpm);
            BL_Drive.spin(directionType::fwd,Velocity*.75,velocityUnits::rpm);
            BR_Drive.spin(directionType::rev,Velocity*.75,velocityUnits::rpm);
        }
        
        //While the gyro value is greater than the turn target turn the robot at half speed
        while (Gyro.value(rotationUnits::deg) >= TurnTarget)
        {
            FL_Drive.spin(directionType::fwd,Velocity*.50,velocityUnits::rpm);
            FR_Drive.spin(directionType::rev,Velocity*.50,velocityUnits::rpm);
            BL_Drive.spin(directionType::fwd,Velocity*.50,velocityUnits::rpm);
            BR_Drive.spin(directionType::rev,Velocity*.50,velocityUnits::rpm);
        }
        
        FL_Drive.stop();  
        FR_Drive.stop();
        BL_Drive.stop();  
        BR_Drive.stop();
    }
    
    //Turns the bot clockwise using the gyro sensor
    void GyroTurnCW (double TurnTarget, int Velocity)
    {
        //Assignment to convert the velocity from percent to rpm
        Velocity *= 2;
        
        //Turning clockwise the gyro value is positive
        //While the gyro value is less than 40% of the turn target turn the robot 100% of speed
        while (Gyro.value(rotationUnits::deg) <= TurnTarget * .40)
        {
            FL_Drive.spin(directionType::rev,Velocity,velocityUnits::rpm);
            FR_Drive.spin(directionType::fwd,Velocity,velocityUnits::rpm);
            BL_Drive.spin(directionType::rev,Velocity,velocityUnits::rpm);
            BR_Drive.spin(directionType::fwd,Velocity,velocityUnits::rpm);
        }
        
        //While the gyro value is less than 75% of the turn target turn the robot 75% of speed
        while (Gyro.value(rotationUnits::deg) <= TurnTarget * .75)
        {
            FL_Drive.spin(directionType::rev,Velocity*.75,velocityUnits::rpm);
            FR_Drive.spin(directionType::fwd,Velocity*.75,velocityUnits::rpm);
            BL_Drive.spin(directionType::rev,Velocity*.75,velocityUnits::rpm);
            BR_Drive.spin(directionType::fwd,Velocity*.75,velocityUnits::rpm);
        }
        
        //While the gyro value is less than the turn target turn the robot at 50% of speed
        while (Gyro.value(rotationUnits::deg) <= TurnTarget)
        {
            FL_Drive.spin(directionType::rev,Velocity*.50,velocityUnits::rpm);
            FR_Drive.spin(directionType::fwd,Velocity*.50,velocityUnits::rpm);
            BL_Drive.spin(directionType::rev,Velocity*.50,velocityUnits::rpm);
            BR_Drive.spin(directionType::fwd,Velocity*.50,velocityUnits::rpm);
        }
        
        FL_Drive.stop();  
        FR_Drive.stop();
        BL_Drive.stop();  
        BR_Drive.stop();
    }
  11. abennett5139

    Jan 3 Concord, NC 5139

    @jpearman

  12. jpearman

    Jan 3 Moderator, ROBOTC Tech Support, V5 Beta Moderator Los Angeles 8888

    ok, thanks, I'll look later. I think it was a forum glitch that stopped posts displaying.
    btw

    Gyro.startCalibration(500);

    there is no need to pass a number to Gyro.startCalibration, the number is not the calibration time (as such, valid values would be 0, 1 or 2 corresponding to 1, 2 or 4 seconds), it means something completely different (it's a scale factor) so I'm surprised your gyro is returning accurate values.

  13. abennett5139

    Jan 3 Concord, NC 5139

    @jpearman ok, thanks, I'll look later. I think it was a forum glitch that stopped posts displaying.
    btw

    Gyro.startCalibration(500);

    there is no need to pass a number to Gyro.startCalibration, the number is not the calibration time (as such, valid values would be 0, 1 or 2 corresponding to 1, 2 or 4 seconds), it means something completely different (it's a scale factor) so I'm surprised your gyro is returning accurate values.

    Oh ok so I don't need any number in that?

    But would that hinder my gyro from only working once, and just skipping gyro code after that?

 

or Sign Up to reply!