Users Examples

Forums:

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

Hello everyone,
I am new happy owner of a diablo but I am not programming in python.
I wonder if someone had written a code to control the board with a keaboard (exactly like diabloJoystick.py but with the arrow key).
Thanks.

piborg's picture

I am not aware of a keyboard example for Diablo, but it should be easy to adapt 4ndr345's example for ZeroBorg: Easy script to control ZeroBorg with keyboard.

First swap the import line:

import ZeroBorg

for the Diablo import:

import Diablo

Replace the setup lines:

# Setup the ZeroBorg
ZB = ZeroBorg.ZeroBorg()
#ZB.i2cAddress = 0x40                  # Uncomment and change the value if you have changed the board address
ZB.Init()
if not ZB.foundChip:
    boards = ZeroBorg.ScanForZeroBorg()
    if len(boards) == 0:
        print 'No ZeroBorg found, check you are attached :)'
    else:
        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])
    sys.exit()
#ZB.SetEpoIgnore(True)                 # Uncomment to disable EPO latch, needed if you do not have a switch / jumper
ZB.SetCommsFailsafe(False)
ZB.ResetEpo()

with the Diablo setup lines:

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

Change the power settings lines:

# 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

to the correct values for your battery and motors in volts.

Change the move function:

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)  

to be:

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

Finally remove the line:

ZB.SetLedIr(True)

Thanks a lot for your reply.

Here is the full script for the other who wants:

#!/usr/bin/env python
# coding: Latin-1
 
# Load library functions we want
import time
import os
import Diablo3 as Diablo
import sys
import tty
import termios
 
def readchar():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
    finally:
        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 Diablo
DIABLO = Diablo.Diablo()
#DIABLO.i2cAddress = 0x44                  # Uncomment and change the value if you have changed the board address
DIABLO.Init()
if not DIABLO.foundChip:
    boards = Diablo.ScanForDiablo()
    if len(boards) == 0:
        print 'No Diablo found, check you are attached :)'
    else:
        print 'No Diablo at address %02X, but we did find boards:' % (DIABLO.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 'DIABLO.i2cAddress = 0x%02X' % (boards[0])
    sys.exit()
#DIABLO.SetEpoIgnore(True)                 # Uncomment to disable EPO latch, needed if you do not have a switch / jumper
DIABLO.ResetEpo()
 
# Remote control commands
def MoveForward():
    DIABLO.SetMotor1(1)
    DIABLO.SetMotor2(1)
 
def MoveBackward():
    DIABLO.SetMotor1(-1)
    DIABLO.SetMotor2(-1)
 
def SpinLeft():
    DIABLO.SetMotor1(1)
    DIABLO.SetMotor2(-1)
 
def SpinRight():
    DIABLO.SetMotor1(-1)
    DIABLO.SetMotor2(1)
 
def Stop():
    DIABLO.MotorsOff() 
def Shutdown():
    global running
    running = False
 
 
# The remote decoding loop
global running
running = True
try:
    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 == 'z' or ord(keyp) == 16:
            MoveForward ()
            print 'Forward'
        elif keyp == 's' or ord(keyp) == 17:
            MoveBackward ()
            print 'Reverse'
        elif keyp == 'd' or ord(keyp) == 18:
            SpinRight ()
            print 'Spin Right'
        elif keyp == 'q' or ord(keyp) == 19:
            SpinLeft ()
            print 'Spin Left'
        elif keyp == ' ':
            Stop()
            print 'Stop'
        elif ord(keyp) == 3:
            break
 
except KeyboardInterrupt:
    print
 
 
 
print
Subscribe to Comments for "Users Examples"