I am developing a camera tracker to follow objects identified via OpenCV in a dedicated environment on mt Pi3. Currently the two SG90 servos are controlled by software PWM using RPi.GPIO - but the tracking is jittery and unstable.

I am considering implementing hardware PWM to improve stability. I am aware of pigpio, wiringpi and RPIO that offer hardware PWM, but as far as I can tell(?) they currently support only up to Python 3.3 and I am using Python 3.5.

Any suggestions for a library to work with Python 3.5 or 3.6? So far I have stumbled on pwmpy that requires a kernel overlay: https://github.com/scottellis/pwmpy

Results of software PWM vs hardware PWM on servo stability

After sorting out the Python version issues (thx @joan), I can now report back on the performance of the two tracking servo's using software and hardware PWM.

Software PWM implemented via RPi.GPIO

Software PWM implemented via RPi.GPIO

Hardware PWM implemented via pigpio

Hardware PWM implemented via pigpio

Comparing the Standard Deviations clearly shows the improved stability of hardware PWM. Since the image processing is taking a lot of CPU resources, software PWM suffers and is visibly jittery with occasional big jumps. None of that with the hardware PWM and the bit of variation reflected in the numbers is probably due to slight variations in the image object identification and not the PWM server.

  • docs.python.org/3/extending/extending.html Although probably you don't need to do that. I'm actually not a python user, but my understanding is it remains backward compatible with minor versions (.2, .3, .5) and only introduces incompatibilities with the major version changes (2., 3.). I.e., if it works for python 3.3, it should work for python 3.5.
    – goldilocks
    Feb 20 '18 at 14:23
  • Could you put the pigpio code in a C function that gets called by your python code? I think that pigpio works well enough that it is worth a little extra effort to use it.
    – crj11
    Feb 20 '18 at 14:26

For pigpio it may be as simple as building an appropriate version for your Python version, e.g. perhaps try python3.5 setup.py install (in the pigpio build directory).

Alternatively you could use the command line I/F to wiringPi (gpio) or pigpio (pigs).

  • thx for the suggestion. The pigpiod daemon runs fine and I can control the servos directly via pigs in the CLI. So I will try your first suggestion to see if I can get Python to import the library. Btw pigpio is great contribution to the RPi libraries!
    – Nic
    Feb 20 '18 at 16:49
  • Rebuilding pigpio for Python 3.5 using python3.5 setup.py install in the OpenCV environment solved the problem. Now to test stability using pigpio hardware PWM.
    – Nic
    Feb 20 '18 at 18:03
  • @Nic One useful test would be to run python3.5 x_pigpio.py. That will test quite a lot of the functionality.
    – joan
    Feb 20 '18 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.