PyMaxBotix: A cross-platform Python package for reading from MaxBotix ultrasonic distance sensors

I needed an easy way to access data from the MaxBotix ultrasonic sensors 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 distance in millimeters obtained from the USB-ProxSonar-EZ sensor:

from maxbotix import USB_ProxSonar

class MySensor(USB_ProxSonar):

    def __init__(self, port):
        USB_ProxSonar.__init__(self, port)

    def handleUpdate(self, distanceMillimeters):
        print('%d mm' % distanceMillimeters)

sensor = MySensor('COM8')

sensor.start()
As this example shows, USB_ProxSonar is an abstract class that you subclass with a class implementing the handleUpdate method. The complete documentation is here.

Instructions

You will need:
  1. The ability to program in Python

  2. The PySerial package installed on your computer. Like many people, I found it easiest to install from source.

  3. Administrator (root) privileges on your computer

  4. The PyMaxBotix repository.

  5. A MaxBotix sensor. As shown in the image above, I have both a USB-ProxSonar-EZ and a MB1240 XL-MaxSonar-EZ4 unit. I love the convenience of USB, but Maxbotix recommends the MB1240 for Unmanned Aerial Vehicle (UAV) applications, which is my focus these days. To use this sensor on a USB port, I bought a 5-volt FTDI adapter cable and reversed its RX polarity by following the instructions here. I soldered a seven-pin header onto the MB1240 for the +5v, GND, and RX (receive) lines, and other signals, and used some 6" Male/Female jumper wires to connect the FTDI adapter to the headers.
Once you've downloaded the repository, 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 usb_prox_sonar_test.py or xl_max_sonar_test.py program, depending on which sensor you have. Be sure to determine the port to which the sensor is connected and modify the test program accordingly.

Copyright and licensing

Copyright and licensing information (Gnu LGPL) can be found in the header of each source file.