PyPX4Flow: A cross-platform Python package for reading from the PX4Flow optical-flow sensor
I needed an easy way to access data from the PX4Flow sensor using Python, so I wrote this little package, which you can download from github. It runs on Windows, Linux, and OS X in Python 2 and 3. As with the other Python packages I’ve written, the API is very simple. For example, this program prints the computed X and Y flow velocities from the sensor:
from px4flow import PX4Flow class ShowFlow(PX4Flow): def update(self): print(self.getFlowComp()) if __name__ == '__main__': sensor = ShowFlow('/dev/ttyACM0') while True: sensor.refresh()
As this example shows, PX4Flow is an abstract class that you subclass with a class implementing the update method. To see the other methods available, look at the documentation.
I’ve also included a program that will display the sensor data in a useful way (depicted above) and log the data to a .CSV file for Excel.
You will need:
- A PX4Flow kit. I recommend following the Image Quality and Output instructions to make sure the sensor is working properly.
- The ability to program in Python
- The PySerial package installed on your computer. Like many people, I found it easiest to install from source.
- Administrator (root) privileges on your computer
- The PyPX4Flow repository.
Once you’ve downloaded the repositry, use a terminal (Linux or OS X) or command shell (Windows) to change to the directory where you put it, and issue the command
python setup.py install
On Linux or OS X, you may need to issue this command as root:
sudo python setup.py install
Then you should be able to run the px4flow_display.py program in the display folder.
- Some users have reported a bug in which the display program crashes immediately with a stack trace ending in the following output:
File "/usr/local/lib/python2.7/dist-packages/px4flow/mavlink_parser.py", line 99, in unpack return struct.unpack(fmt, self.msg[lo:hi])[0:n] error: unpack requires a string argument of length 4
So far this bug seem to occur only with versions of Python below 2.7.5, so I’ve added a branch to the repository that should work with such earlier versions. If you are running 2.7.5 or above and still experience this problem, please let me know.
- At startup, the display program occasionally computes and displays bogus large values for the distance traveled. This may have to do with the fact that I am not using the checksum to validate each message.
Copyright and licensing
Copyright and licensing information (Gnu LGPL) can be found in the header of each source file.
This work was supported in part by a Commonwealth Research Commercialization Fund grant from the Center for Innovative Technology (CRCF #MF14F-011-MS) and a Lenfest summer research grant from Washington and Lee University.