We’re working in java, and we’re running into an issue with our gyro. Occasionally we get large position spikes from the .getAngle() function. Not sure why. We’re using the gyro in between threads, could it be an issue with the .getAngle() not being thread safe? I noticed that we don’t (I think) get spikes in the main thread that the roborio runs by default, we only get spikes in other threads. It’s been really annoying to debug because it only happens like once a minute or so.
I posted to Programming rather than to Java to see if this was an issue not just specific to java. I also can post code if needed, but for the most part it’s fairly straight forward code. I was wondering if anyone has seen anything similar, or knows about things like this.
Look at your electrical connections. Analog Gyros will give some crazy numbers if they don’t have a good connection.
I might be wrong here, but when you have no radial velocity, the analog gyro should output 2.5V, then increase or decrease depending on which way it’s rotated.
If the connection isn’t good, the controller won’t see 2.5V and will give you widely varying numbers like what you’re seeing.
I’ve replaced the gyro, and it’s still acting the same. Overall, the gyro acts normal, has a bit of drift and measures rotation fairly accurately, but every so often it’ll spike, but still return to where it should be. I suspect it’s software because (iirc) the gyro feeds us rate, and if we were to get a voltage spike, I doubt we’d read the same value as before the spike.
What about the wire going from the RoboRIO to the gyro? Are the connections good on the wire? Is it running next to any power wires for things like motors/batteries?
Java AnalogGyro.getAngle() is indeed NOT thread-safe as currently written. If you are calling it from multiple threads with no locking protection you can get corrupted results. I’ll get a patch submitted that adds “synchronized” to the various AnalogGyro methods, but in the meantime you can add locking yourself around all calls to AnalogGyro and see if that fixes the problem for you.
Thanks! I’ll try that out. I figured that .getAngle() was thread safe because it returned a primitive type and I didn’t think that there was anything too crazy running in the background. Do you know if any other common wpilib stuff that isn’t thread safe? I might start going around and locking everything I can lol
Generally speaking, it should be thread-safe except for setup-type functions. E.g. it’s not safe to call gyro calibrate() in a separate thread from gyro getAngle(). That said, most robot code doesn’t use threads outside of PIDController so there are probably other places where locking may have been missed.