This library communicates with PWM servos.
Pulse Width Modulation (PWM) is a protocol for receivers to send commands to servos. Each servo is sent a high pulse with the width of the pulse dictating the commanded position. Traditionally, PWM commands are sent at a 50 Hz interval with pulse widths varying between 1000 us and 2000 us. Some servos and Electronic Speed Controllers (ESCs) support higher frequencies, which typically requires a shorter pulse width range. PWM is the most common protocol for sending servo and ESC commands, but they require a PWM capable pin for each servo being controlled.
CMake is used to build this library, which is exported as a library target called pwm. The header is added as:
#include "pwm/pwm.h"
The library can be also be compiled stand-alone using the CMake idiom of creating a build directory and then, from within that directory issuing:
cmake .. -DMCU=MK66FX1M0
make
This will build the library and example executable called pwm_example. The example executable source file is located at examples/pwm_example.cc. Notice that the cmake command includes a define specifying the microcontroller the code is being compiled for. This is required to correctly configure the code, CPU frequency, and compile/linker options. The available MCUs are:
- MK20DX128
- MK20DX256
- MK64FX512
- MK66FX1M0
- MKL26Z64
- IMXRT1062_T40
- IMXRT1062_T41
These are known to work with the same packages used in Teensy products. Also switching the packages is known to work well, as long as it's only a package change.
The pwm_example target creates an executable for communicating with PWM servos. This target also has a _hex for creating the hex file to upload to the microcontroller.
This library is within the namespace bfs
PwmTx Creates a PwmTx object.
bfs::PwmTx pwm;
void Begin(std::span pins) Initializes the PWM pins. The PWM pin numbers are passed as a span to this method.
std::vector<int> pins({21, 22, 23, 2, 3, 4, 5, 6});
pwm.Begin(pins);
void frequency_hz(float val) Enables changing the update frequency for the PWM commands. By default, this frequency is 50 Hz. Check with your servo or ESC manufacturer for supported frequencies.
pwm.frequency_hz(333);
float frequency_hz() Returns the current update frequency.
std::cout << pwm.frequency_hz() << std::endl;
void Write() Writes the PWM commands. Commands are generated by hardware timers within the microcontroller and will be sent to the servos at the specified update frequency. Write only needs to be called when the servo commands have been updated. There may be up to a one frame delay between issuing the Write command and the updated PWM pulse being sent to the servo.
pwm.Write();
void tx_channels(std::span<uint16_t> val) Sets the channel data to be transmitted.
pwm.tx_channels(pwm_tx_data);
std::vector<uint16_t> tx_channels() Returns the vector of channel data to be transmitted.
std::vector<uint16_t> pwm_tx_data = pwm.tx_channels();