88
99import owi_maplin_usb_arm as usb_arm
1010
11-
12- def handle_key ( arm , delay , key_map , key ):
13- def do_it ():
14- if key in key_map :
15- message = key_map [ key ]
16- print ( "Key " , key , "Movement message" , message )
17-
18- arm . move ( message , delay )
19- arm . safe_tell ( do_it )
20-
21-
22- def make_keymap () :
23- """Bp - an initialised arm bitpattern.
24- returns the keymap"""
25- return {
26- K_z : usb_arm . BaseClockWise ,
27- K_x : usb_arm .BaseCtrClockWise ,
28- K_r : usb_arm . CloseGrips ,
29- K_f : usb_arm .OpenGrips ,
30- K_a : usb_arm . ShoulderDown ,
31- K_q : usb_arm .ShoulderUp ,
32- K_s : usb_arm . ElbowDown ,
33- K_w : usb_arm .ElbowUp ,
34- K_d : usb_arm . WristDown ,
35- K_e : usb_arm .WristUp ,
36- K_l : usb_arm . LedOn }
11+ def handle_keys_held ( arm ):
12+ pressed_keys = pygame . key . get_pressed ()
13+ pattern = usb_arm . Stop
14+ if pressed_keys [ K_z ] :
15+ pattern = pattern | usb_arm . BaseClockWise
16+ elif pressed_keys [ K_x ]:
17+ pattern = pattern | usb_arm . BaseCtrClockWise
18+ if pressed_keys [ K_a ]:
19+ pattern = pattern | usb_arm . ShoulderDown
20+ elif pressed_keys [ K_q ]:
21+ pattern = pattern | usb_arm . ShoulderUp
22+ if pressed_keys [ K_s ] :
23+ pattern = pattern | usb_arm . ElbowDown
24+ elif pressed_keys [ K_w ]:
25+ pattern = pattern | usb_arm . ElbowUp
26+ if pressed_keys [ K_d ]:
27+ pattern = pattern | usb_arm .WristDown
28+ elif pressed_keys [ K_e ]:
29+ pattern = pattern | usb_arm .WristUp
30+ if pressed_keys [ K_r ]:
31+ pattern = pattern | usb_arm .CloseGrips
32+ elif pressed_keys [ K_f ]:
33+ pattern = pattern | usb_arm .OpenGrips
34+ if pressed_keys [ K_l ]:
35+ pattern = pattern | usb_arm .LedOn
36+ arm . tell ( pattern )
3737
3838
3939def key_loop ():
40- km = make_keymap ()
4140 try :
4241 arm = usb_arm .Arm ()
4342 except AttributeError :
4443 print ("Please make sure the arm is connected and turned on" )
4544 sys .exit (1 )
46- handle = partial (handle_key , arm , 0.5 , km )
47- exit_key = K_ESCAPE
4845
49- while True :
50- for event in pygame .event .get ():
51- if event .type == QUIT :
52- pygame .quit ()
53- return
54- if event .type == KEYDOWN :
55- if event .key == exit_key :
46+ pygame .clock = pygame .time .Clock ()
47+ FPS = 50
48+
49+ try :
50+ while True :
51+ for event in pygame .event .get ():
52+ if event .type == QUIT :
53+ pygame .quit ()
5654 return
57- else :
58- handle (event .key )
55+ if event .type == KEYDOWN :
56+ if event .key == K_ESCAPE :
57+ pygame .quit ()
58+ return
59+ handle_keys_held (arm )
60+ pygame .clock .tick (FPS )
61+ finally :
62+ arm .tell (usb_arm .Stop )
5963
6064
6165def main ():
@@ -64,6 +68,12 @@ def main():
6468 pygame .init ()
6569 pygame .display .set_mode ([200 , 200 ])
6670 print ("Press z/x to turn the base motor" )
71+ print ("Press a/q to move the shoulder up/down" )
72+ print ("Press w/s to move the elbow up/down" )
73+ print ("Press e/d to move the wrist up/down" )
74+ print ("Press r/f to close/open the grips" )
75+ print ("Press l to toggle the LED" )
76+ print ("Press ESC to exit" )
6777 key_loop ()
6878
6979
0 commit comments