Unit Conversions

The Thrifty Nova position and velocity feedback methods return using encoder units. Closed loop control uses these units as well. However oftentimes programmers want to convert to a more user friendly unit. For this we can use the Conversion class.

Creating a Conversion Object

We can create a converter by providing a human readable unit from the PositionUnit or VelocityUnit types, and the EncoderType of the encoder we are using.

 private Conversion converter1 = new Conversion(
   PositionUnit.RADIANS, EncoderType.INTERNAL);
private Conversion converter2 = new Conversion(
   VelocityUnit.ROTATIONS_PER_MIN, EncoderType.INTERNAL);

Available Units

Position Units

Unit
Description
Common Use Cases

RADIANS

Angular position in radians

Mathematical calculations, arms

DEGREES

Angular position in degrees

Human-readable angles, swerve modules

ROTATIONS

Complete rotations

Wheel rotations, continuous mechanisms

Velocity Units

Unit
Description
Common Use Cases

RADIANS_PER_SEC

Angular velocity in radians/s

Mathematical calculations

DEGREES_PER_SEC

Angular velocity in degrees/s

Angular motion control

ROTATIONS_PER_SEC

Rotations per second

High-speed mechanisms

ROTATIONS_PER_MIN

Rotations per minute

Shooters, intakes

Using the Conversion Object

To convert to encoder units for setting a value we use the toMotor method.

double targetPosition = Math.PI / 2; // radians
positionMotor.setPosition( converter1.toMotor(targetPosition) );

double targetVelocity = 2500; // RPMs
velocityMotor.setVelocity( converter2.toMotor(targetVelocity) );

To convert from encoder units for reading a value we use the fromMotor method.

double position = converter1.fromMotor(positionMotor.getPosition());
SmartDashboard.putNumber("Position (rads)", position);

double velocity = converter2.toMotor(velocityMotor.getVelocity());
SmartDashboard.putNumber("Velocity (rpms)", velocity);

To Motor Units (for Setting Values)

javaCopy// Position control examples
Conversion armConverter = new Conversion(PositionUnit.DEGREES, EncoderType.INTERNAL);
double targetAngle = 90.0;  // We want the arm at 90 degrees
motor.setPosition(armConverter.toMotor(targetAngle));

// Velocity control examples
Conversion shooterConverter = new Conversion(VelocityUnit.ROTATIONS_PER_MIN, EncoderType.INTERNAL);
double targetRPM = 3600;  // We want the shooter at 3600 RPM
motor.setVelocity(shooterConverter.toMotor(targetRPM));

From Motor Units (for Reading Values)

javaCopy// Position reading examples
Conversion armConverter = new Conversion(PositionUnit.DEGREES, EncoderType.INTERNAL);
double currentAngle = armConverter.fromMotor(motor.getPosition());
SmartDashboard.putNumber("Arm Angle", currentAngle);

// Velocity reading examples
Conversion shooterConverter = new Conversion(VelocityUnit.ROTATIONS_PER_MIN, EncoderType.INTERNAL);
double currentRPM = shooterConverter.fromMotor(motor.getVelocity());
SmartDashboard.putNumber("Shooter RPM", currentRPM);

Last updated