Resetting Gyro In VCS C++


#1

As the title suggests how should I reset my Gyro in VCS C++. I’m looking for a command like the .resetEncoder command. Any Ideas?


#2

We are having the same exact issue. Our programmers can’t figure this out either and we have comp tomorrow.


#3

You really don’t need to ever reset a gyro. I know it’s nice from a cleanliness standpoint. But it doesn’t really serve any other purpose. When you would reset the gyro, just read its value and store that value. Then make your targets equal to that value plus whatever you would have used before.


#4

Sample code?


#5

Let’s say you had in your code:

// <------- Something here that would set the gyro to 0.
while(Gyro1.value(roationUnits::deg)) < 90){
   LeftMotor.spin(directionType::rev, 50, velocityUnits::pct);
   RightMotor.spin(directionType::fwd, 50, velocityUnits::pct);
}
LeftMotor.stop();
RightMotor.stop();

Change it to:


double targetGyroValue; // earlier in the code

targetGyroValue = Gyro1.value(roationUnits::deg)) + 90;
while(Gyro1.value(roationUnits::deg)) < targetGyroValue){
   LeftMotor.spin(directionType::rev, 50, velocityUnits::pct);
   RightMotor.spin(directionType::fwd, 50, velocityUnits::pct);
}
LeftMotor.stop();
RightMotor.stop();

You really don’t need to store huge numbers. Your gyro is unlikely to ever get to a really big or really small angle because you’re generally turning and turning back or driving in a sort of box. So you should rarely see values breaking 1000 degrees.


#6

Thank you! My programmers are struggling with the gyro, too.


#7

If they’re familiar with physics (since it’s seen a lot there), you can tell them they’re really looking for ∆ø to reach a value (90 degrees in my example), not ø.

More explicitly, but harder to follow: setting ø = 0 somewhere just means ∆ø = øf - øi = øf - 0 = øf. My example just plugs in what was read for øi instead of the 0 that they want to force upon it.


#8

@Gear Geeks could you post a sample program you are currently using for your gyro sensor? We tried the one above but it seems to not be working right.


#9

@[TVA]Connor could you post a sample code for your gyro sensor? the one above isn’t working.


#10

If you tried what I posted, it won’t. I just showed how to change from using a reset to not using a reset. I didn’t write code that would work generally, just for a 90 degree turn in one direction. You would have to do more to take the direction of the turn into account, and adjust the angle.


#11

would you be able to share a sample code for getting the gyro to turn to a certain degree? i tried the one above and it didn’t work


#12

It turns out that currently the gyro resets at 360 degrees, so you do have to watch out for that. Could that have been the issue? Also, have you made sure you didn’t rescale the gyro while calibrating it? It would be good to show the code you’re using.


#13

I have been getting a constant Gyroscope problem with my V5 robot in both VCS and PROS, but in terms of resetting I do not know. But, there is a way you can make code that could potentially “reset” the gyroscope.


#include "robot-config.h"
 
int AmountToSubtract = 0;
int setZero(){
  AmountToSubtract = Gyro1.value(vex::analogUnits::range8bit);
}

int calculateValue(int subtractgyro){
    int gyroscopePosition = Gyro1.value(vex::analogUnits::range8bit) - subtractgyro;
    return gyroscopePosition;
}

int main() {
   setZero();
   while(1==1){
       int calculatePosition = calculateValue(AmountToSubtract); //Use calculatePosition to get the calibrated Value
   }
   
}

Let me know if it works :slight_smile:
EDIT//: Made some changes I noticed