I’m looking into the math needed to do some fancy programming for next year and it looks like object rotation would be encoded in either a matrix or a quaternion. It seems like quaternions are becoming the go to for computer graphics, sensors, and the like but matrices are just so much easier to understand conceptually (if you don’t believe that). Thoughts one way or the other?
The primary advantage to quaternions is that it is much easier to interpolate between two values. This makes smooth rotation much easier. If you’re looking to do “fancy programming” this might be valuable to you.
Some implementations of rotation matrices are significantly less numerically stable than quaternions, but while this is often cited as a reason quaternions are better it is not really inherently true. Similarly, while quaternions inherently avoid gimbal lock, it is possible to implement rotation matrices to avoid the problem as well.
With those things in mind, I would suggest using whatever your sensors are using. No sense doing needless conversions. While quaternions may seem more conceptually complex, at heart, they are essentially just vectors, and some Google-fu will turn up all the formulas necessary to deal with them.
I suppose that’s reasonable. What about library support? It doesn’t seem like there’s much of a standard for either WPILib or Java (my team’s language of choice). Matrices should be easy enough but I’m less sure about quaternions… I know the NavX has them but we’re using the ADX? on the SpartanBoard.
I’ll be less useful for answering this question; I do a decent bit of robotics programming with ROS, but when I mentor FIRST teams it’s mostly build/logistics, so I’m less familiar with what’s available for FRC.
I couldn’t easily find the specs for the gyro on the SpartanBoard, but if it’s really just a gyro I might suggest a different full IMU. I’d be curious if the folks over at WCP would be willing to post more detailed specs.
I’ve had good luck with both Razorand BNO055IMUs for my ROS-based projects. I’ve grown particularly fond of the BNO055 recently because its built in fusion has been pretty reliable for my use cases (both robot-side and controller-side). The Razor is serial and the BNO055 is I2C, so they’re pretty easy to work with.
Pretty sure the ADXRS453 is just single-axis, which makes me wonder what kind of fancy programming you’re doing. It might help to extrapolate on your plans a little.
If you are just controlling robot heading, there are probably simpler ways to represent it than a rotation matrix (like an angle), and quaternions are 3-axis, so I think they wouldn’t be helpful at all.
At the moment, WPILib doesn’t support any 3-axis rotational measurement devices out of the box. If we did, the internal representation of the angles wouldn’t be exposed to the user and we would simply provide three angles. For most devices, we would use accumulators to produce Euler angles. If we did use matrices though, we would use the Eigen matrix library because we’re going to use Eigen for model-based control.
In addition to Quaternions at the IMU level in the NavX-MXP open-source library (pick your language, it’s supported in C++, Java, LabVIEW, Python and C# as well as Arduino) the new SF2 library supports timestamped quaternion histories which can be used for video processing latency correction. The timestamped quaternions and the associated interpolation allows estimation of the orientation quaternion at any instant in time in the history. And SF2 is also open source so you can study the quaternion interpolation methods used as well, if that’s of any interest (see the “Quaternion” and “OrientationHistory” Java classes at the link above).
Thanks for the Linear Algebra video series link. From my perspective, this is a very useful tutorial, quite well done IMHO.
To me, the difference between matrices and quarterions seems similar to the distinction between Cartesian vs polar coordinates in 2D space.
They are useful for different things, so most of the math libraries used in the robotics industry have convenience functions to switch back and forth between quaternions, affine matrices, Euler angles, etc.
This is very important to remember. When you program, use the tool that helps you get the task accomplished correctly and relatively efficiently. Using a tool that in theory might be better suited but which you don’t understand makes it more difficult to write, debug and refine your code. Quaternions are essentially an alternative to the real number system, and they are particularly useful for rotation and vector math. You can do all of that math using matrices and the real numbers, however. So use the tool that will work best for you. If this is an off-season, not time sensitive task, try learning more about the one you understand the least.