Helping you program the BrickPi with Python - BrickPython

The main pages invite us all to share enhancements, so here goes… :slight_smile:

Here’s a contribution we’ve put together to help BrickPi python developers.

We wanted to use the NXT motors as servo motors, and the BrickPi_Python software didn’t seem to help much. And we’re not very enthusiastic about working with low-level C-style code, however great it might be. So we’ve put together some software to build on the BrickPi.py module from BrickPi_Pthon, to make it object-oriented and to make it easier for me and my son to program.

The BrickPython package extends BrickPi_python, to introduce two things: objects, and coroutines.

As a taster, here’s code to detect presence via a sensor, open a door, wait and close it again; all while still permitting other things to happen:

def openDoorWhenSensorDetected(self):
    motorA = self.motor('A')
    sensor1 = self.sensor('1')
    motorA.zeroPosition()
    while True:
        while sensor1.value() > 8:
            yield

        for i in motorA.moveTo( -2*90 ):
            yield

        for i in self.doWait( 4000 ):
            yield

        for i in motorA.moveTo( 0 ):
            yield

The full application is a few lines longer – it’s here: https://github.com/charlesweir/BrickPython/blob/develop/ExamplePrograms/DoorControl.py .

You can find the BrickPython package at https://pypi.python.org/pypi/BrickPython . To keep it robust, it has a full set of unit tests. And there’s documentation and a ‘quick start’ guide at https://pythonhosted.org/BrickPython/index.html

There’s plenty more that could be done. But is it useful?

  • Charles

Hey Charles, thanks for this fantastic contribution! This is really awesome!

Let me try to take a look at what you did, and I’ll try to get in touch with you via e-mail so we can figure out the best way to merge this all together. Really awesome work and thanks for contributing to the code base!

Great - I’ll be glad to hear from you. The account has my correct email address [can’t see yours?]…

  • Charles

Hey Charles, just wanted to let you know I’m resending our e-mail from last week.

Charles, love what you’ve done here, definitely the way to go. Hope very much that you are still working on the project.

Tony