I have a working servo design that uses an SPI based magnetic absolute encoder that I wrote a generic driver for - the servo only needs to operate of a ± 30 degree span and as such I calibrate the system so that the 'discontinuity" where the encoder would go from 0 to 360 is 180 degrees from the 0 point. This all works perfectly and the servo does what it needs to do. However, I noticed that if someone happens to rotate the servo lets say two turns with the servo in the disabled state when it is re-enabled it will “unwrap” the accumulated angle error - I suspect this might be in the PID part of the code but can’t seem to isolate where this is happening and how to keep it from doing that… Does anyone have any ideas on how to defeat this behavior so that it always reads the absolute encoder value and not some integrated version of it.
Hey @Chris_Duffey , which encoder is it?
The code isn’t integrating, it is simply keeping track of the absolute angle, including full rotations. That is how the Sensor class works. The code for this is in the Sensor base class.
So when your servo is turned through 2 turns, and then the motor is re-enabled in position control mode, it tries to assume the previous position, including unwinding the 2 full rotations.
To change this behaviour, you could modify your sensor class to work without full rotations, but perhaps the following will be simpler: when the motor is re-enabled, check the current absolute angle first, and update the target value to include the current full rotations.
Thank you for the information runger - I’ll dig in there. I’m using an RLS Orbis encoder, the code is fairly trivial for normal operation of reading it, the harder part of that encoder is all the utility functions like calibrating it, setting offsets, etc.