Smart Servo class that implements a simple S-curve (constant acceleration) for servo control
cSmartServo class implements s-curve soft movement control for servos and runs on Arduino IDE and PlatformIO.
Servo control is still done by a Servo (#include <Servo.h>) object.
From initial position (i) to final position (f) motion starts with initial speed defined by VMIN and starts accelerating until
(a) - maximun speed (VMAX) is reached or
(b) - middle point is reached.
If VMAX is reached before the middle point, the distance from the middle point is captured and servo keeps moving with constant speed until it reaches point (c) when it starts deaccelerating down to VMIN and keeps moving until the final point is reached.
If the middle point is reached before VMAX, then it will start deaccelerating immediately.
Servo position limits are defined my the Servo object and are: minimum 0, maximum 180. You can trim those limits and the code will interpolate input values in the full range (0-180) to your trimmed values.
How to use:
init(uint8_t _index, uint8_t _iopin, uint8_t _min, uint8_t _max, uint8_t _initpos)
_index: Servo Index. Used by the servo objeto to define the PWM channel (0 to 15)
_iopin: GPIO used to control the servo. Must be a PWM capable pin
_min / _max: minimum and maximum position values
_initpos: initial position
Scurve(uint8_t _endpos, uint8_t _vmax)
Call this to set the end position
_endpos: end position
_vmax: maximum speed in degrees / SERVO_TASKDELAY ms
If the distance to the end point is less than MIN_DISTANCE, S-curve is disabled and the servo will run in constant speed.
After this, just call loop() as frequent as you can