1- """Maplin USB Robot arm control"""
2- import os
3- os .environ ['DYLD_LIBRARY_PATH' ] = '/opt/local/lib'
1+ """Maplin USB Robot arm control.
2+ Usage -
3+ >>> import usb_arm
4+ >>> arm = usb_arm.Arm()
5+ >>> arm.move(usb_arm.OpenGrips)
6+ >>> arm.doActions(block_left) # WARNING - ARM SHOULD BE ALL THE WAY RIGHT BEFORE TRYING THIS
7+
8+ Trouble:
9+ "NO back end found" - you need to install a libusb driver on your system.
10+ """
411import usb .core
512from time import sleep
613
@@ -25,18 +32,30 @@ def __or__(self, other):
2532 self .base | other .base ,
2633 self .led | other .led )
2734
28- CloseGrips = BitPattern (1 , 0 , 0 )
29- OpenGrips = BitPattern (2 , 0 , 0 )
30- Stop = BitPattern (0 , 0 , 0 )
31- WristUp = BitPattern (0x4 , 0 , 0 )
32- WristDown = BitPattern (0x8 , 0 , 0 )
33- ElbowUp = BitPattern (0x10 , 0 , 0 )
34- ElbowDown = BitPattern (0x20 , 0 , 0 )
35- ShoulderUp = BitPattern (0x40 , 0 , 0 )
36- ShoulderDown = BitPattern (0x80 , 0 , 0 )
37- BaseClockWise = BitPattern (0 , 1 , 0 )
35+ def __eq__ (self , other ):
36+ return self .arm == other .arm and self .base == other .base and self .led == other .led
37+
38+ def __repr__ (self ):
39+ return "<BitPattern arm:%s base:%s led:%s>" % (self .arm , self .base , self .led )
40+
41+ def __str__ (self ):
42+ return self .__repr__ ()
43+
44+
45+ GripsClose = BitPattern (1 , 0 , 0 )
46+ CloseGrips = GripsClose
47+ GripsOpen = BitPattern (2 , 0 , 0 )
48+ OpenGrips = GripsOpen
49+ Stop = BitPattern (0 , 0 , 0 )
50+ WristUp = BitPattern (0x4 , 0 , 0 )
51+ WristDown = BitPattern (0x8 , 0 , 0 )
52+ ElbowUp = BitPattern (0x10 , 0 , 0 )
53+ ElbowDown = BitPattern (0x20 , 0 , 0 )
54+ ShoulderUp = BitPattern (0x40 , 0 , 0 )
55+ ShoulderDown = BitPattern (0x80 , 0 , 0 )
56+ BaseClockWise = BitPattern (0 , 1 , 0 )
3857BaseCtrClockWise = BitPattern (0 , 2 , 0 )
39- LedOn = BitPattern (0 , 0 , 1 )
58+ LedOn = BitPattern (0 , 0 , 1 )
4059
4160
4261class Arm (object ):
@@ -45,6 +64,8 @@ class Arm(object):
4564
4665 def __init__ (self ):
4766 self .dev = usb .core .find (idVendor = 0x1267 )
67+ if not self .dev :
68+ raise RuntimeError ("USB Arm Not found. Ensure it is plugged in and powered on" )
4869 self .dev .set_configuration ()
4970
5071 def tell (self , msg ):
@@ -57,20 +78,22 @@ def safe_tell(self, fn):
5778 case of an exception"""
5879 try :
5980 fn ()
60- except :
81+ except Exception :
6182 self .tell (Stop )
6283 raise
6384
6485 def move (self , pattern , time = 1 ):
6586 """Perform a pattern move with timing and stop"""
66- self .tell (pattern )
67- sleep (time )
68- self .tell (Stop )
87+ try :
88+ self .tell (pattern )
89+ sleep (time )
90+ finally :
91+ self .tell (Stop )
6992
7093 def doActions (self , actions ):
7194 """Params: List of actions - each is a list/tuple of BitPattern and time
7295 (defaulting to 1 if not set)"""
73- #Validate
96+ # Validate
7497 for action in actions :
7598 if not 1 <= len (action ) <= 2 :
7699 raise ValueError ("Wrong number of parameters in action %s" %
@@ -90,11 +113,14 @@ def doActions(self, actions):
90113 raise
91114
92115
93- block_left = [[ShoulderDown ], [CloseGrips , 0.4 ], [ShoulderUp ],
94- [BaseClockWise , 10.2 ], [ShoulderDown ],
95- [OpenGrips , 0.4 ], [ShoulderUp , 1.2 ]]
96- block_right = [[ShoulderDown ], [CloseGrips , 0.4 ], [ShoulderUp ],
97- [BaseCtrClockWise , 10.2 ], [ShoulderDown ],
98- [OpenGrips , 0.4 ], [ShoulderUp , 1.2 ]]
99- left_and_blink = list (block_left )
100- left_and_blink .extend ([[LedOn , 0.5 ], [Stop , 0.5 ]] * 3 )
116+ def makeGrabAndMove (baseDir ):
117+ return [[CloseGrips , 1.1 ],
118+ [ShoulderUp | ElbowUp | WristDown | baseDir ],
119+ [baseDir , 8.5 ],
120+ [ShoulderDown | ElbowDown | WristUp | baseDir ],
121+ [OpenGrips ]]
122+
123+
124+ blink = [[LedOn , 0.5 ], [Stop , 0.5 ]] * 3
125+ block_left = makeGrabAndMove (BaseClockWise ) + blink
126+ block_right = makeGrabAndMove (BaseCtrClockWise ) + blink
0 commit comments