Units Programming - TAMSFormers5212/TAMSformers-Database GitHub Wiki

The C++ units library is used in WPILib to store and convert between units. This is important to enforce in code that is used in any calculations or interfaces with hardware.

The units will need to be included using the relevant header files. See the docs linked above for the full list.

C++ Units in WPILib are broken up into two categories. Unit Types and Container Types. Container Types are the ones that will be used when you need to specify a unit for a value (Ex. inch_t, meter_t, radian_t, etc).

Unit Types

Units types describe the unit as a whole. They are not meant to measure specific values (Ex. inches, meters, seconds, radians, etc). Instead, they are used to state what units are being used. These are used when constructing classes that will need to use those units such as feedforwards, trapezoidal profiles, and more.

armFeedForward, the only one we've used so far, automatically works in radians

Container Types

Using 'auto' instead of clearly defining each type can make it easier, but be careful to remember what unit every variable is supposed to be.

You can define them in two ways. As a number followed by the unit, or just initializing using curly braces.

units::inch_t sideLength = 10_in;
auto maxSpeed = 3.81_mps; 
auto maxRotation = 2.0_rad_per_s; 
units::radians_per_second_squared_t maxAccel{1};

Using units in this way also makes it easy to convert between units.

units::inch_t wheelCircumfrence = units::inch_t{wheelDiameter.value()*MathConstants::pi};
units::meter_t wheelCircumfrenceMeters = units::meter_t{wheelCircumfrence};

units::volt_t vkaV{0.014};
units::radians_per_second_t akaV{1};
auto kaV = vkaV / akaV;
⚠️ **GitHub.com Fallback** ⚠️