CAN Coder distance per pulse

I am new to writing PIDs and my team is using CAN Coders as our encoders for our robot. The Encoder has the setDistancePerPulse function and I was wondering if there is anything similar for the CAN Coder, if so what would I use?

We need a little more context to answer the question fully, but here is some background.

With Phoenix 6, CANCoders always return their position in rotations. The Encoder class is a generic class that handles lots of different hardware configurations, so you have to configure the distance per pulse. The CANCoder class only supports a single piece of hardware, so there is no need to configure the distance per pulse, it’s built in.

There are lots of different things you could be trying to do here, so to go farther we need a little more context of what you’re trying to solve. What sort of mechanism are you trying control? What motor controller are you using?

1 Like

I am making a drive PID for a swerve drive motor, for now we are using TalonFX motors but we will be switching over to kraken motors. I am using a CAN coder as the encoder for the PID and I am using this documentation to research how to program a PID here

The encoder they use has the setDistancePerPulse() function so I was not sure if there was something similar I would need with the CAN coder.

Another question I have if you would happen to know is about the getRate() function that is shown in the documentation. For now I am using getVelocity() for the CAN coder but since I am switching to phoenix 6 the getVelocity() function does not work anymore, I’ve tried looking through the migration guide but cannot find anything on it, if you know how to fix this it would be a big help.

First off, if you’re using CANCoders, Talon FX motor controllers (Falcon 500 or Kraken), and a Pigeon 2, I highly recommend using the CTRE swerve project generator instead of writing your own swerve code.

If you’re not using that setup, I recommend checking out YAGSL.

If you do choose to write your own, I would consider using closed-loop (PID) control on the motor controller instead of doing it on the roboRIO.

The approach a lot of teams take with your setup (unless they’re buying Phoenix Pro), is to read the position of the CANCoder on startup, and use it’s position to seed the position of the motor. From then on, they use the motor’s encoder and onboard closed-loop to control the steering.

The getVelocity() method is still there in Phoenix 6. It returns a StatusSignal that you can use to read the value. It’s important to note that units are different for Phoenix 6. The CANCoder will return its velocity in rotations per second.