Using Okapi Units

I am working on a project where I would like to pass a sensor value into okapi angle units. My sensor returns a double. How do I convert a double to a QAngle data type?

Edit: I have attached the line of code giving me trouble. “angle” is my double value from the sensor.
angle = OdomMath::constrainAngle180(angle).convert(degree);

I believe the issue is that OdomMath::constrainAngle180 requires a QAngle, not a double as an argument. Multiplying angle by degree seems to work to me.

double angle = 0;
angle = OdomMath::constrainAngle180(angle * degree).convert(degree);
2 Likes

Yep, to convert a double to a RQuantity you simply do var * unit.
5_m is just syntactic sugar for 5 * meter.
This can actually be done automatically via dimensional analysis, basically if you do something similar to the following formula your unit is automatically created:

// convert ticks (unitless) to distance (length unit)
QLength distanceTravelled = ticks / 360.0 * 4_in * pi; 

In this case, it looks like you are using IMU info, so you will want to annotate the unit that the IMU outputs (probably degree) like

QAngle angle = sensor.get() * degree;
angle = constrainAngle180(angle);
5 Likes