One of the basic rules that I try to follow in this library is that the devices shouldn’t use GPIO channels directly, but instead do so through different classes of interfaces. With this my goal is that the same channel isn’t configured more than once and also to abstract the logic of a device from the communications layer with the hardware.
Until now, I only had the InterfaceGPIO class as an interface, but I needed to control devices using pulse width modulation (PWM), such as servomotors for example. For that reason, I developed the InterfacePWM class. Really, the InterfacePWM encapsulate the class PWM from the Rpi.GPIO library (© Ben Croston) to generate a signal at a determined frequency with the capacity to modify the pulse width. InterfacePWM allows us to control up to two PWM channels which we can access using numerical index in some functions.
ifmgr = InterfaceManager() iface = InterfacePWM(ifmgr, outport=(13, 15), frequency=50, name="PWM Interface 1") iface.write(0, 50) # Set the channel 13 with a 50% dutycycle indefinitely iface.write(1, 10, 2000) # Set the channel 15 with a 10% dutycycle for 2' iface.stop() # Sends the stop signal to both PWM channels. ...
The PWM object from the RPi.GPIO library uses threads to function and is reconfigurable; we can stop it and restart with other parameters with no problem. This gives us the flexibility to reuse it in our code and get the most out of it. It just has one small drawback: the amplitude of the pulse width is controlled by software, not hardware, and because of this the pulse width can sometimes vary slightly, provoking small movements in critical elements like servomotors when they should remain stationary in a certain position. It’s a minor issue.
Not all devices will be able to use the InterfacePWM class, basically because they are incompatible due to their internal workings. On the other hand there are devices like servos that depend exclusively on this interface (for now) or which we can use to control the speed of DC motors, for example, or any other application that we want.
I will post an example soon.