Unit Conversions
Contents
Why this matters
A motor controller does not report metres per second. It reports encoder counts. Somewhere in the code, a person must convert one to the other.
If that conversion is wrong by a factor of the gear ratio, the mechanism moves 5.68 times too fast or 5.68 times too slow. The code has no error. The mechanism simply does the wrong thing, and the cause is difficult to find on a field.
Our conversions are in
robot/swerve/conversions.py.
This page makes you write them.
The numbers you need
| Value | Amount | Where it comes from |
|---|---|---|
| Falcon counts for one motor turn | 2048 | The encoder in the motor |
| CANcoder counts for one turn | 4096 | The absolute encoder on the steering |
| Falcon velocity period | 100 ms | The motor controller reports counts for each 100 ms |
| Swerve drive gear ratio | 5.68 | const.SWERVE_DRIVE_GEAR_RATIO |
| Swerve steering gear ratio | 12.1 | const.SWERVE_ANGLE_GEAR_RATIO |
| Swerve wheel circumference | 0.3192 m | A 4 inch wheel |
These are Phoenix 5 units. Phoenix 6 reports rotations and rotations for each second, thus it removes most of this arithmetic. The conversions stay in our repository, and the same errors occur with any sensor that counts. Learn the method, not the constant.
Speed of the motor
A Falcon reports its velocity in encoder counts per 100 milliseconds. There are 2048 counts in one turn of the motor.
The gear ratio is the number of motor turns for one turn of the mechanism.
Our swerve drive uses 5.68.
Write falcon_to_rpm(counts_per_100ms, gear_ratio). It must return the speed
of the mechanism in revolutions per minute.
The code operates in your browser. The page sends nothing to a server and saves your work on this computer. Press Ctrl+Enter to run it.
Speed of the robot
Now convert the same signal into the speed of the robot across the carpet.
falcon_to_rpm from the last exercise is already written for you.
A swerve wheel is 4 inches in diameter, thus its circumference is
0.3192 metres. Our drive gear ratio is 5.68.
Write falcon_to_mps(counts_per_100ms, circumference, gear_ratio). It must
return metres per second.
The code operates in your browser. The page sends nothing to a server and saves your work on this computer. Press Ctrl+Enter to run it.
Write the inverse
Almost every conversion has a partner that goes the other way. The partner is easy to get wrong, because the two functions look almost the same.
A CANcoder reports an absolute angle as 4096 counts for one turn. This function converts counts to degrees, and it is correct:
def cancoder_to_degrees(counts, gear_ratio):
return counts * (360 / (gear_ratio * 4096))
Write degrees_to_cancoder(degrees, gear_ratio). It must be the exact
inverse.
The last check converts a value in one direction and then back again. A conversion pair must always return the value that it started with. This is the cheapest test you can write, and it finds the most common error in a file of conversions.
The code operates in your browser. The page sends nothing to a server and saves your work on this computer. Press Ctrl+Enter to run it.
This is a real error in our repository.
degrees_to_CANcoderinrobot/swerve/conversions.pymultiplies where it must divide. It is the same expression asCANcoder_to_degrees, thus it is not the inverse.
degrees_to_falcon, three lines below it, has the correct form. Compare the two.Nothing calls
degrees_to_CANcodertoday, thus the robot is not affected. But the next person to use it will get a value that is approximately 130 times too small. The round-trip check in the exercise above finds this error in one line.
The method
Use this sequence for any conversion:
- Write the units at each step. Counts, counts for each 100 ms, motor turns for each minute, wheel turns for each minute, metres for each second.
- Change one unit at each step. Do not combine steps.
- For a gear ratio, decide which value is larger. The motor turns more times than the mechanism, thus motor RPM divided by the gear ratio gives mechanism RPM.
- Test one known value. One full turn, or one metre.
- Test a round trip. Convert in one direction, then back again.
Step 5 is the one that people do not do, and it is the one that finds errors.
References
robot/swerve/conversions.py- Swerve Module Optimization — the next tutorial
- CTRE Phoenix 6: units