<< return to Pixycam.com

Python pixy multiprocessing

I’ve got a Raspberry-Pi-3-based robot that I’ve put a pixy on. Everything is running in a python program:

  • webcam server using opencv (serves video from another camera at /dev/video0)
  • websocket server (gets robot commands via a remote javascript websocket connection)
  • PWM server (moves robot motors and various servos, using a Pololu Maestro and its python libraries)

All the above works fine.

Enter the pixycam. I’ve got the pixy python libraries working and integrated into my robot program, but when the pixy python starts its pantilt processing to follow objects, it brings the processor to its knees.

I’m thinking that multiprocessing can fix me up. Everything above is running on a single core of the processor. The raspberry pi 3 has four CPU cores, and three are going unused.

To my question…

How can I get the pixy_init() process to kick off in a separate process? I’ve googled and tried several things, but I just don’t know enough about multithreading to figure it out.

Has anyone here got any examples of the pixy python code working with python’s multiprocessing capabilities?

Thanks very much for any help.

Before anyone spends any time on this, I’m pretty sure I’ve got it figured out.

First off, there was no good reason for the processor to get max’d out. The pantilt python demo program only pushes the cpu to about 3%.

About the multithreading, I got it working by taking the pan_tilt.py demo program and doing the following:

  • modified the main() function to instead be startPixy()
  • created a new main() function as follows:
def main():
  p1=Process(target=startPixy)
  p1.start()

  p2=Process(target=someotherfunction)
  p2.start()

That basically put the main parts of the pixy program into process p1, and someotherfunction() into another process, and the main() code was also in its own process.

If anybody needs specifics about this, let me know. Not mentioned above is the passing of the X:Y positions of the gimbal to other processes, which is the main thing I needed out of the pixy.

My robot code is now running with practically no additional resource utilization from the pixycam python code.

Nice work Nick!

Thanks for sharing!

Edward