- Notifications
You must be signed in to change notification settings - Fork1k
Servo library
The Servo library allows to control upt to 12 RC (hobby) servo motors on any GPIO pin.
STM32duino Servo API is the same as Arduino Servo API:https://docs.arduino.cc/libraries/servo/
Servo();uint8_tattach(int pin,int value = DEFAULT_PULSE_WIDTH);// attach the given pin to the next free channel, sets pinMode, set angle value, returns channel number or 0 if failureuint8_tattach(int pin,int min,int max,int value = DEFAULT_PULSE_WIDTH);// as above but also sets min and max values for writes.voiddetach();voidwrite(int value);// if value is < 200 its treated as an angle, otherwise as pulse width in microsecondsvoidwriteMicroseconds(int value);// Write pulse width in microsecondsintread();// returns current pulse width as an angle between 0 and 180 degreesintreadMicroseconds();// returns current pulse width in microseconds for this servo (was read_us() in first release)boolattached();// return true if this servo is attached, otherwise false
In order to be able to attach servo on any GPIO pin,
HardwareTimer is used as time base (and not as PWM output generator),
and signal generation will be achieved by software.
Only one single instance of timer is used :TIMER_SERVO.TIMER_SERVOcan be redefined with usual methods: variant.h, build_opt.h
Up to 12 servo motors can be controlled with this timer instance.
Servo signal generation will be done within interrupt routine.
Time base controls when signals should be set or reset.
Each servo will be managed one after the other:
when 1st servo pulse is finished, 2nd servo pulse starts, ...
___ ___Servo1 ___| |__________________________________| |___ ____ ____Servo2 _______| |_________________________________| |__Warning
To avoid CPU load, it is possible to useHardwareTimer library directly to control servo motors.But is this case, all GPIO are not eligible to control servo,only the GPIO that have HardwareTimer output capability.One timer channel is required per servo.Frequency should be 20ms, Pulse: between 1ms and 2ms.