Is CTRE's SwerveDrivetrain::addVisionMeasurement() thread-safe?

I know that Limelight Lib is great for easily getting the bot pose estimate, and it checks to make sure that a new pose estimate is available every time you call getBotPoseEstimate(). However, it does not provide any way to use the estimate as soon as it arrives.

I was considering using a NetworkTableListener for botpose_orb_wpiblue, and calling SwerveDrivetrain::addVisionMeasurement() in the listener callback. However, I don’t know for sure whether or not this method is thread-safe. I know that in 2024 (and in this year’s LegacySwerveDrivetrain), the method used a write lock to support the odometry thread, but this method is now hidden behind the JNI.

I understand that if it isn’t thread safe, I would have to use an AtomicReference, which wouldn’t provide much benefit over simply using Limelight Lib. However, I’d like to use the listener callback if I can.

If anyone from CTRE sees this, I think it would also be a good idea to mention in the documentation whether or not your native code is thread-safe.

Yes, the API is thread-safe. Almost all the swerve API is thread safe, with the only exceptions being setControl and registerTelemetry (which in Java are thread-safe with respect to the odometry thread but not to threads in your robot program).

Besides the Java calls to JNI, you can also see some of the C++ implementation, including all the public-facing APIs on the SwerveDrivetrain.

3 Likes