Understanding WPILIB Swervebot Java Example

Our team is working with the WPILIB Swervebot Example code and first trying to understand exactly what it is doing. The example uses SparkMax controllers with encoders plugged into the Roborio and we will be using TalonFX and TalonSRX controllers.

This is the portion of the code we are currently going through: SwerveModule.java

  public SwerveModuleState getState() {
return new SwerveModuleState(m_driveEncoder.getRate(), new Rotation2d(m_turningEncoder.get()));}

In the section above does the m_turningEncoder.get() return a value from -180 to 180? That is what we think, but wasn’t totally sure.

If so, does the Rotation2d function just figure out that we are passing it a degree and not radians?

m_driveMotor.setVoltage(driveOutput + driveFeedforward);

There doesn’t appear to be an equivalent to setVoltage in the TalonFX/SRX. Our thought was to just do percent output using (driveOutput + driveFeedforward)/12 . Would that work as expected?

Java doesn’t have units, so to the Rotation2d constructor it’s just getting a unitless double value. You have to read the documentation for the function to see that the constructor takes radians. If you have degrees, you need to call Rotation2d.fromDegrees instead of the constructor.

Regarding using the Talon classes, the WPI_ variants of the classes (which are also available via the CTRE vendor dep) provide setVoltage, or you can use the methods in the CTRE classes to set control mode to voltage and demand to the voltage setting.


For your first question, the Rotation2d object’s constructor takes in radians. The output of the m_turningEncoder.get() is returning a radian value as they set it up that way in the constructor of the class. You can see that they are setting their measuring pulse to be 2Pi times something, putting it on the radian scale.

1 Like

I think that what was confusing is that in VSCode it indicated that the get() function was returning an integer, which made me think it wasn’t in radians.

What type is turningEncoder? The Encoder get() function will likely be in counts, not degrees or radians, so you’re going to need to do the conversion yourself based on the CPR of the encoder you’re using.


The Swervebot example is using the Encoder class from edu.wpi.first.wpilibj.Encoder

So, does that mean the example program isn’t doing it right?

We looked for a voltage option in the CTRE classes, but didn’t see one.

EDIT: I don’t see any reason we can’t switch to the WPI_ version of the class

Yes, we’ve opened an issue here

1 Like

Thanks for everyone’s input. We know how to move forward now.