Using a magnetic encoder to get speed of a motor

We have a magnetic encoder mounted to a test motor and, using the magnetic encoder example code, we get a nice sawtooth signal that gives us a position. However, we cannot just calculate the speed of the motor using (current_position - last_position)/time because we would calculate an erroneous speed when the position loops from 360 degrees to 0 degrees.

Any ideas on how to work around this to calculate the speed of the motor?

Can’t you just WPILib’s encoder VIs? Encoder Get has an output for rate, which is scaled accordingly to Distance Per Count (which you set when you opened the encoder in Begin.vi). That should do it for you.

If you can’t/don’t want to calculate Rate this way, you can do it this way:

average_rate = delta_position / delta_time

So…

average_rate = (current_position - previous_position)/(current_time - previous_time)
```<br><br>![Untitled.png|690x500](upload://g9Cgsa3zZsgItcJmtW9B3frVGUu.png)<br><br><br>![Untitled.png|690x500](upload://g9Cgsa3zZsgItcJmtW9B3frVGUu.png)<br>

I believe WPILib’s encoder VIs only work for the quadrature optical encoders, not the voltage-based magnetic encoders.

Edit: I guess WPILib’s encoder VIs do work with the magnetic encoders. I might see if any of this helps.

For future reference, I would not recommend using a magnetic encoder to get the speed of a motor. While the readings are smooth, there aren’t enough voltage changes in a second to differentiate enough between speeds.