Users Examples

Please post any examples you would like to share with other ZeroBorg owners here :)

#!/usr/bin/env python
# coding: Latin-1

# Load library functions we want
import time
import os
import ZeroBorg

# Reading single character by forcing stdin to raw mode
import sys
import tty
import termios

def readchar():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
        ch =
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    if ch == '0x03':
        raise KeyboardInterrupt
    return ch

def readkey(getchar_fn=None):
    getchar = getchar_fn or readchar
    c1 = getchar()
    if ord(c1) != 0x1b:
        return c1
    c2 = getchar()
    if ord(c2) != 0x5b:
        return c1
    c3 = getchar()
    return chr(0x10 + ord(c3) - 65)  # 16=Up, 17=Down, 18=Right, 19=Left arrows

# End of single character reading

# Setup the ZeroBorg
ZB = ZeroBorg.ZeroBorg()
#ZB.i2cAddress = 0x40                  # Uncomment and change the value if you have changed the board address
if not ZB.foundChip:
    boards = ZeroBorg.ScanForZeroBorg()
    if len(boards) == 0:
        print 'No ZeroBorg found, check you are attached :)'
        print 'No ZeroBorg at address %02X, but we did find boards:' % (ZB.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 'ZB.i2cAddress = 0x%02X' % (boards[0])
#ZB.SetEpoIgnore(True)                 # Uncomment to disable EPO latch, needed if you do not have a switch / jumper

# Power settings
voltageIn = 9.0                         # Total battery voltage to the ZeroBorg (change to 9V if using a non-rechargeable battery)
voltageOut = 6.0                        # Maximum motor voltage

# Setup the power limits
if voltageOut > voltageIn:
    maxPower = 1.0
    maxPower = voltageOut / float(voltageIn)

# Remote control commands
def Move(left, right):
    print '%0.2f | %0.2f' % (left, right)
    ZB.SetMotor1(-left * maxPower)
    ZB.SetMotor4(-left * maxPower)
    ZB.SetMotor2(right * maxPower)
    ZB.SetMotor3(right * maxPower)  

def MoveForward():
    Move(+1.0, +1.0)

def MoveBackward():
    Move(-1.0, -1.0)

def SpinLeft():
    Move(-1.0, +1.0)

def SpinRight():
    Move(+1.0, -1.0)

def Stop():
    Move(0.0, 0.0)

def Shutdown():
    global running
    running = False

# The remote decoding loop
global running
running = True
    print 'Press CTRL+C to quit'
    print 'Press arrow keys to control the Raspberry Pi - Spacebar to stop!'
    # Loop indefinitely
    while running:   
        keyp = readkey()
        if keyp == 'w' or ord(keyp) == 16:
            MoveForward ()
            print 'Forward'
        elif keyp == 'z' or ord(keyp) == 17:
            MoveBackward ()
            print 'Reverse'
        elif keyp == 's' or ord(keyp) == 18:
            SpinRight ()
            print 'Spin Right'
        elif keyp == 'a' or ord(keyp) == 19:
            SpinLeft ()
            print 'Spin Left'
        elif keyp == ' ':
            print 'Stop'
        elif ord(keyp) == 3:

except KeyboardInterrupt:


thank you for your code, it's very useful.


I have noticed some strange behaviour with the speed of the motors.
M1 & M2 and M3 & M4 running at different speeds. On the contrary the speeds of M1 & M4 and M2 & M3 are the same...

I have changed the code to compensate this behavior.

def Move(left, right):
    print '%0.2f | %0.2f' % (left, right)
    ZB.SetMotor2(-left * maxPower)
    ZB.SetMotor3(-left * maxPower)
    ZB.SetMotor1(-right * maxPower)
    ZB.SetMotor4(-right * maxPower)

I notice with negative speeds the motors run a little bit faster and with positive speeds they run a little bit slower. Is it a defect of my zeroborg ?

piborg's picture

While it is a bit strange, this is the normal behavior of the ZeroBorg.

Put simply it is to do with how the driver chip works and the control signals used to set the motor speeds by the board.

Subscribe to Comments for "Users Examples"