Servos are great. They generally work really well and contain all the control and drive circuitry for reasonably precise error free position keeping. Servos are interfaced through PPM with the standard generally being deemed as a pulse between 1ms – 2ms and a period of 20ms.
I’ve written code before for the STM32F0 discovery boards to allow servos to be driven by dedicated pins e.g. ones that have timer output compare functionality. While that is great and all if you know what pins you want to use, I’ve not seen any form of library portable enough to be used on ANY pins, so I took the time to write one! This is implemented on the STM32F072 Discovery board as my normal STM32F0 discovery board is in use for my 3rd year group project!
Its not the most efficient implementation and is quite processor intensive as the PPM is software generated but it seems to work fine in my tests, allowing me to test one of my servos without having to consider configuring any pins.
Currently, the library has an internal list to which pins and their corresponding GPIO ports can be added too, allowing the control timer interrupt to toggle pins when necessary according to their defined positions. I’ve also written functions to allow servos to be controlled as an angle (between 0 and 359 degrees – though mapping can be changed!) or as a percentage (-100% = fully left, 100% = fully right) or as an absolute definition from the header file, notably: APS_SERVOLEFT, APS_SERVOMIDDLE and APS_SERVORIGHT.
I’ve tested the library under different optimizations, notable O0 and O3, along with noting their time consumption from the control timer interrupt. I’ve monitored this by having a while(1) loop containing a GPIO pin toggle. The time the toggles aren’t happening is time spent in the interrupt, therefore giving a rough estimate of time spent in the interrupt. Time consumption is larger during the active section of the PPM signal (1ms to 2ms).
Inactive section of PPM waveform with O0
Inactive section of the PPM waveform with O3
Active section of the waveform with O0
Active section of the waveform with O3
Driving more than one servo at once with different pulse widths
As ever, the code for this library can be found on my Github!