Exposing total encoder impulses/ticks as public method

I have a quick question:
What do you think would be the best way to get the total ticks/impulses from the encoder (hall sensors in my case)?

I think it would be useful:

  • for some external motion planning use cases
  • for robotics to fuse IMU and odometry to obtain a better position estimate
  • external PID

At the moment, I’m thinking of creating a “virtual encoder” and publishing the encoder ticks based on calculations from getVelocity or getAngle. This approach is as bad as it can get because I can not catch all the impulses this way if the motor changed speed or direction between readings.

Another idea is to use some of the private variables from the encoder class, but I haven’t had time to look deeper into. Is the pulse_counter variable in the encoder class storing the total ticks/impulses from the encoder?

Any other suggestions are welcomed.


Yes, the pulse_counter stores the current number of pulses.

But be aware that its semantics are complicated - it is written to by interrupt routines, and it can change at any time. It starts at zero, but it can be “discontinuous” if the index pin is used.

I am wondering why? For any of these use cases, would it not be simpler to implement them based on absolute angles?
The encoder ticks are very hardware specific, and have different meanings depending on the encoder used…
Perhaps I’m not seeing it, but how would having the ticks help you do these things better?

pulse_counter Is indeed private, it wasn’t really intended for public consumption… if you really need it, its easy enough to change in the code.
As for changing it generally in the library, I’d like to hear first what the purpose would be? I any case I’d lean in the direction of making it “protected” rather than private. Then you could sub-class Encoder and write your own methods that expose it the way you need…

I want to reuse some ROS code and I was just looking for the simplest way to couple it with feedback from simpleFoc. Since pulse_counter isn’t reliable, I’ll rewrite the ROS code to accept the angle.

Thank you!

EDIT: getAngle*100 gets me the exact number I needed to avoid modifying the code :slight_smile:

1 Like