You have everything you need in those links, but perhaps you just need a bit of help understanding what they mean.
So firstly what is a Pulse Width Modulated signal?
It is a signal of a fixed frequency with a variable mark-space ratio. i.e. as the size of the pulse (the mark) increases in width, the gap between pulses (the space) reduces in width to compensate, so the the length of the mark plus the space is a constant. This constant value is the cycle time.
In the following figure, the cycle time is 20. It starts off with a pulse width of 5 that changes to 9
Code: Select all
+----+ +----+ +--------+ +--------+
| 5 | 15 | 5 | 15 | 9 | 11 | 9 |
----+ +--------------+ +--------------+ +----------+ +-----
| 20 | 20 | 20 |
The servo HAT generates this waveform for you. You just need to initialise the cycle time (e.g. 20) and set the width of the pulse whenever you want to change it.
It is the size of the pulse that determines the position of the servo, or the speed of the Electronic Speed Controller.
So how would the servo HAT generate this waveform?
One way is with a clock generator, a 12-bit binary counter and 2x 12-bit comparators.
The clock generator creates a square wave at a set frequency. Each tick of this clock will increment the binary counter. The counter will count from 0 to 4095 (4096 steps) and then rollover to 0 again. Each comparator is set to different 12-bit values and monitor the binary counter output. When the counter equals the first comparator (on), the output of the HAT goes high. When the counter equals the second comparator value (off) the HAT output goes low.
So setting the clock generator rate determines the tick time. The tick time multiplied by 4096 is the cycle period, since it will take 4096 ticks of the clock for the counter to wrap around. The frequency is 1/(cycle period).
The pulse width is (off-on)*tick time.
The tick time is a feature of the design of the servo HAT as it has a 12 bit counter. Other designs may have more or less ticks per cycle, which affects the resolution of the time that you can set. The more ticks per cycle, the more accurate you can set the pulse time.
The library is designed so that you need to set the cycle frequency(Hz) and the on and off tick times, probably because these make nice integer value ranges.
Most servos will operate at 50Hz, so the cycle period is 20ms. So hopefully you can see now why the tick time is 20ms/4096 = 4.88us.
Your servo spec sheet says that the middle position of the servo is 1500us. At 50Hz, that is 1500us/4.88us = 307 ticks.
So to set the middle position you set ON=0, OFF=307 when you call the library function to set the pwm width. The HAT will repeat that waveform until you set it to something else.
If you set a different pwm frequency you will need to adjust the on/off tick numbers accordingly to set the same pulse width.
The above is all theoretical from my point of view as I don't have a servo HAT to try this out, but hopefully it will help in your understanding.