USB Robot arm control with Python and Raspberry Pi

I was asked recently if the USB robot arm could be programmed - I knew the answer was yes. The Arm came from the CBiS Education Robot Arm kit ( ) which comes with a Raspberry Pi and SD Card with software to control the Arm. So the question has become - How do I program it in Python from scratch?

Well, the honest answer is I don't I use what others have done before. A good 
starting point is this site, it gives most (pretty much all) of the answer, including where to get the pyusb library need; setting it up (be careful don't use the  zip file but the tar.gz version  - you might need to scroll down the screen to do this) and an example. A second source that was very useful was which contains an explanation of the 'triples' (for example [32,0,0]) used to select which part of the arm moves and what it does (in the example [32,0,0] causes the Robot Elbow to move down).

The code below is largely based on the code in the only real changes are named procedures (such as ElbowUp) in place of the slightly less easy to understand lines such as  MoveArm(Duration,[4,0,0]) #wrist up . The only other changes was a procedure that changes what the light does (0 - light off; 1 - light on; 3 - pulses by a number of times).

import usb.core, usb.util, time
RoboArm = usb.core.find(idVendor=0x1267, idProduct=0x000)

if RoboArm is None:
    raise ValueError("Arm not found")

#Define a procedure to execute each movement
def MoveArm(Duration, ArmCmd):
    #Start the movement
    #Stop the movement after waiting a specified duration

def RotateBaseClockwise(Duration):
    MoveArm(Duration,[0,1,0]) #Rotate base clockwise

def RotateBaseAntiClockwise(Duration):
    MoveArm(Duration,[0,2,0]) #Rotate base clockwise

def ElbowUp(Duration):
    MoveArm(Duration,[16,0,0]) #Elbow up 0x10

def ElbowDown(Duration):
    MoveArm(Duration,[32,0,0]) #Elbow down 0x20

def WristUp(Duration):
    MoveArm(Duration,[4,0,0]) #wrist up 0x04

def WristDown(Duration):
    MoveArm(Duration,[8,0,0]) #wrist down 0x08

def CloseGripper(Duration):
    MoveArm(Duration,[1,0,0]) #close gripper 0x01

def OpenGripper(Duration):
    MoveArm(Duration,[2,0,0]) #open gripper 0x02

def ShoulderUp(Duration):
    MoveArm(Duration,[64,0,0]) #shoulder up 0x40

def ShoulderDown(Duration):
    MoveArm(Duration,[128,0,0]) #shoulder down 0x80

def GripLight(Action, Duration):
    if (Action==1):
        MoveArm(Duration,[0,0,1]) #light on
    if (Action==0):
        MoveArm(Duration,[0,0,0]) #light off
    if (Action==3):
        for x in range(Duration):
            MoveArm(1,[0,0,1]) #light on
            MoveArm(1,[0,0,0]) #light off


See it action in the video below.

All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with.

1 comment: