Skip to main content

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 (http://www.cbis.education/Product-Details.aspx?prodid=2 ) 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 http://www.wikihow.com/Use-a-USB-Robotic-Arm-with-a-Raspberry-Pi-(Maplin), 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  http://notbrainsurgery.livejournal.com/38622.html?view=93150#t93150 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 http://www.wikihow.com/Use-a-USB-Robotic-Arm-with-a-Raspberry-Pi-(Maplin) 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")

Duration=1
#Define a procedure to execute each movement
def MoveArm(Duration, ArmCmd):
    #Start the movement
    RoboArm.ctrl_transfer(0x40,6,0x100,0,ArmCmd,3)
    #Stop the movement after waiting a specified duration
    time.sleep(Duration)
    ArmCmd=[0,0,0]
    RoboArm.ctrl_transfer(0x40,6,0x100,0,ArmCmd,3)

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

WristUp(0.5)
OpenGripper(0.5)
RotateBaseClockwise(2)
RotateBaseAntiClockwise(2)
ElbowUp(1)
ElbowDown(1)
WristDown(0.5)
CloseGripper(0.5)
ShoulderUp(3)
ShoulderDown(1)
GripLight(3,3)


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.

Popular posts from this blog

Micro:bit, Servo control with Micropython or blocks

You can control servos (small ones) from a Micro:Bit directly. Following a link from the David Whale (Twitter ) , thank you, took me to a Kitronik blog post, https://www.kitronik.co.uk/blog/using-bbc-microbit-control-servo/, which has the answer.

The code uses Microsoft Blocks taken from the post, runs the servos 180 degrees and back again, when button A is pressed. It does exactly what it should. I am also using the Tower Pro SG90 servo.
Can it be replicated in Micropython? This is a new mini project, there seems to be little out there yet on how do this but the best so far is this video by PHILG2864:



The closest I have is the following, it is essentially there.
from microbit import *
pin0.set_analog_period(20)
while True:
    pin0.write_analog(180)
    sleep(1000)
    pin0.write_analog(1)
    sleep(1000)

Setting the time period to 20ms  pin0.set_analog_period(20)seems by experiment (and used in the video above) to be best value so far. The reason for pin0.write_analog(1)  set to 1 i…

mbots - graphical programming and Arduino

Makeblock (http://mblock.cc/mbot/) funded through Kickstarter the development of a new robot - mBot (https://www.kickstarter.com/projects/1818505613/mbot-49-educational-robot-for-each-kid) with the subtitle "$49 educational robot for each kid". What they came up with is a interesting system that uses their mBlock software, which resembles Scratch but produces code for Arduino, to program a robot with LEDs, light sensors and buzzer integrated on the main board; but also comes with sensors for line-following, ultrasonic sensor and with the version in the kickstarter reward a 16x8 LED matrix.

My impression so far it is really quite intuitive to work with, in the example above the robot:

moves forward;displays 'f' on the LED matrix; turns right;displays 'r' on the LED matrix;repeats until the on-board is pressed to stop the motors. 

What I like most though is seeing the graphical code turned into Arduino code - the potential to see the same thing done into two ways…

4Tronix Bit:Bot Neuron Controlled Edge follower

In thelast post I was playing with 4Tronix'sBit:Bot. In this post I will show the initial experimentation with an artificial neuron controlling the Bit:Bot to follow the edge of a line (it follows the left-hand side of the line).


The neurons (well two separate ones, S1 and S2) are produced using weighted sums - summing the weights x inputs [ right-hand sensor (rs) and left-hand sensor (ls)] plus a bias for each neuron in this case w[0] and w[3].







    net=w[0]+w[1]*rs+w[2]*ls           net2=w[3]+w[4]*rs+w[5]*ls

  If weighted sum >=0 then its output 1 otherwise 0 if net>=0:          s1=1     else:         s1=0
    if net2>=0:         s2=1     else:         s2=0
What actual causes S1 to be either 1 or 0 is all defined by a set of weights w (three for the first neurone, S1,  three for S2).
w=[0,-1,1,-1,1,-1]


Converting the outputs of the two neurones S1 and S2 into actions is shown below.