Adding another motor to the pbrJoystick.py?

Hi all

I'm building an underwater ROV and have two of the three motors working that I need, which are the forward/reverse, and I changed it a bit to get a turn function.

I'm looking through the python file and I can't figure out how to address a second reverse board. I've set up the boards in a daisy chain and they each have their i2c address. So how would I go about adding this third motor? I just want it on and off at 100% + -, so i'd like to add it to a button, like square and triangle, or the D pad.

Any help? Much appreciated.

groome1320

piborg's picture

That sounds like an awesome project, it is great to see people doing fun things with Raspberry Pis.

The first thing you want to do is change the starting part of the script to talk with the two PicoBorg Reverses.

Assuming they have I2C addresses 10 and 11 this would look like:
You can now use them all in a script like so

# Setup the first PicoBorg Reverse
PBR1 = PicoBorgRev.PicoBorgRev()
PBR1.i2cAddress = 10
PBR1.Init()
if not PBR1.foundChip:
    boards = PicoBorgRev.ScanForPicoBorgReverse()
    if len(boards) == 0:
        print 'No PicoBorg Reverse found, check you are attached :)'
    else:
        print 'No PicoBorg Reverse at address %02X, but we did find boards:' % (PBR1.i2cAddress)
        for board in boards:
            print '    %02X (%d)' % (board, board)
        print 'If you need to change the I²C address change the setup line so it is correct, e.g.'
        print 'PBR1.i2cAddress = 0x%02X' % (boards[0])
    sys.exit()
#PBR1.SetEpoIgnore(True)                 # Uncomment to disable EPO latch, needed if you do not have a switch / jumper
PBR1.ResetEpo()

# Setup the second PicoBorg Reverse
PBR2 = PicoBorgRev.PicoBorgRev()
PBR2.i2cAddress = 11
PBR2.Init()
if not PBR2.foundChip:
    boards = PicoBorgRev.ScanForPicoBorgReverse()
    if len(boards) == 0:
        print 'No PicoBorg Reverse found, check you are attached :)'
    else:
        print 'No PicoBorg Reverse at address %02X, but we did find boards:' % (PBR2.i2cAddress)
        for board in boards:
            print '    %02X (%d)' % (board, board)
        print 'If you need to change the I²C address change the setup line so it is correct, e.g.'
        print 'PBR2.i2cAddress = 0x%02X' % (boards[0])
    sys.exit()
#PBR2.SetEpoIgnore(True)                 # Uncomment to disable EPO latch, needed if you do not have a switch / jumper
PBR2.ResetEpo()

The next thing to do is to go through the file and change any lines which use PBR to use PBR1 instead.

At this point the script should function normally, but the second PicoBorg Reverse will not be doing anything.

Next duplicate the MotorsOff and SetLed lines at the end of the script, but for PBR2 like this:

        # Change the LED to reflect the status of the EPO latch
        PBR1.SetLed(PBR1.GetEpo())
        PBR2.SetLed(PBR2.GetEpo())
        # Wait for the interval period
        time.sleep(interval)
    # Disable all drives
    PBR1.MotorsOff()
    PBR2.MotorsOff()
except KeyboardInterrupt:
    # CTRL+C exit, disable all drives
    PBR1.MotorsOff()
    PBR2.MotorsOff()
print

Then duplicate the ResetEpo line for PBR2 a little further above:

                if joystick.get_button(buttonResetEpo):
                    PBR1.ResetEpo()
                    PBR2.ResetEpo()

Assuming we have our third motor attached to M1 +/- we want to add another drive value to the top of the loop:

try:
    print 'Press CTRL+C to quit'
    driveLeft = 0.0
    driveRight = 0.0
	driveThird = 0.0
    running = True
    hadEvent = False
    upDown = 0.0
    leftRight = 0.0

and then apply it after the left / right motor values:

                # Set the motors to the new speeds
                PBR1.SetMotor1(driveLeft)
                PBR1.SetMotor2(driveRight)
                PBR2.SetMotor1(driveThird)

As an example I will define three positions on the D-Pad, for +100%, 0%, and -100%.
We want to do this in the joystick settings section at the top:

buttonFastTurn = 9                      # Joystick button number for turning fast (R2)
buttonThirdMinus = 7                    # Joystick button number to make the third drive run at -100% (D-Pad left)
buttonThirdZero = 6                     # Joystick button number to make the third drive run at 0% (D-Pad down)
buttonThirdPlus = 5                     # Joystick button number to make the third drive run at +100% (D-Pad right)
interval = 0.00                         # Time between updates in seconds, smaller responds faster but uses more processor time

Finally we then add some code to detect these button presses and alter the driveThird value:

                # Check for button presses
                if joystick.get_button(buttonResetEpo):
                    PBR1.ResetEpo()
                    PBR2.ResetEpo()
                if joystick.get_button(buttonSlow):
                    driveLeft *= slowFactor
                    driveRight *= slowFactor
                if joystick.get_button(buttonThirdMinus):
                    driveThird = -1.0
                if joystick.get_button(buttonThirdZero):
                    driveThird = 0.0
                if joystick.get_button(buttonThirdPlus):
                    driveThird = 1.0

Now we should have a script which uses the D-Pad to control the third motor on a second PicoBorg Reverse.

I have attached the original pbrJoystick.py modified like this as a reference.

Attachments: 

Wow, thank you so much for the reply. Looking at this and comparing it to what I was writing, I got most of it but got thrown by the last section. I'll let you know how it goes and I'll post some pics of my build. Btw. The boards are amazing for how big and cheap they are.

Thanks again.

groome1320

Subscribe to Comments for "Adding another motor to the pbrJoystick.py?"