uStepper / uStepperS32

7 stars 3 forks source link

Absolute encoder angle #8

Open olliehaffenden opened 1 month ago

olliehaffenden commented 1 month ago

I have what I think might be a common requirement: to be able to read the "absolute" angle from the encoder. This would allow my system to work after power up using an offset value that is stored persistently somewhere and applied by the client code. However the uStepperS32 encoder class TLE5012B effectively does a setHome() operation in the init() function, which means that the client code has no means of knowing the actual angle being read from the encoder.

My code was originally developed for the uStepperS, and I note that the code for the encoder in that library is very similar except that the encoderOffset member variable is not set in the init() function. However, it is not actually initialised at all, which means (I believe) that the behaviour is strictly undefined in C++. I suspect I have benefited from the Arduino compiler being "kind" and initialising it to zero.

I'm happy to submit a pull request, but what is the best approach?

  1. In init(), initialise encoderOffset to zero, rather than the current encoder reading. This should mimic the (accidental) behaviour of the uStepperS code, but would break any applications that assume the angle starts from zero.
  2. Provide a new function GetAngleAbs() which returns the angle without any offset. This allows the existing behaviour to be maintained and allows new applications to choose either behaviour. However, porting applications or writing applications that can be switched easily between S and S32 boards would be more vexatious.
  3. Provide a new function SetAbsolute() which clears the value of encoderOffset. New applications or applications ported from uStepperS can call this at startup to obtain the uStepperS behaviour, without the need to replace potentially many calls to GetAngle()
  4. Provide a new function SetEncoderOffset(float offset). Calling this with zero would have the effect of SetAbsolute() above, but it could also be used to set a user-defined offset value, avoiding the need to duplicate this function in the client code.
zenpai45 commented 3 weeks ago

Hi Ollie,

Thanks for your input. we agree that this issue makes sense to provide a solution for.

From our perspective, we think the best approach would be to just add an argument to the four functions in the encoder class, which provides the angle (getAngle(), getAngleRaw(), getAngleMoved() and getAngleMovedRaw()). this argument could be a bool with a default value of "true" and whenever this argument is set to false, the angle is returned without an offset.

your welcome to make a pull request to the development branch with this functionality.

Best Regards, Thomas