Reading the value of a gyro only when it's outputting data

Hello there,

We have a gyroscope connected to our roborio and I was wondering how we can read the angle only when the robot is being rotated. Currently we are using System.out.println(m_Gyro.getAngle()); to print the values put out by our gyro in the console. How would we have it so the console doesn’t repeat values? Currently it repeats the same value over and over when the robot is not being rotated. How can we code it so that it only shows values when the robot is being rotated?

Thanks in advance!

Maybe just check if the new value is the same as the previous one, and only print if there’s a difference? Something like:

double last_angle;
double current_angle = m_Gyro.getAngle();
if(abs(last_angle - current_angle) > 0.01){
    System.out.println(current_angle);
    last_angle = current_angle;
}

Note that you shouldn’t ever compare floating point numbers directly, but rather check if the difference between them is very small (in this case I’m using 0.01, but you can make it as small as you like). This is because rounding errors can cause “equal” numbers to be numerically not-equal.

SmartDashboard.putNumber(m_Gyro.getAngle());

I find using the SmartDashboard for troubleshooting really simplifies the process a lot, also makes it a lot easier for our students to grasp what is going on without having to try and speed read the console prints.

Thanks for the quick responses! I’ll try both and see what our driver prefers.

Another case for this method of comparison is if you’re gyro is very sensitive you can tune how much change you wish to see before you get a new update. Instead of getting constant small flickering updates even though your robot is sitting still.

If you’re talking about the navX, there’s a isRotating() call in the API. Not sure if that exactly matches what you’re looking for but might be worth trying.