-
Notifications
You must be signed in to change notification settings - Fork 21
/
key_ctrl.py
58 lines (49 loc) · 1.41 KB
/
key_ctrl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""key_ctrl - key based arm controller"""
from functools import partial
import usb_arm
import pygame
from pygame.locals import *
import sys
def handle_key(arm, delay, key_map, key):
def do_it():
if key in key_map:
print "Doing key ", key
arm.move(key_map[key], delay)
arm.safe_tell(do_it)
def make_keymap():
"""Bp - an initialised arm bitpattern.
returns the keymap"""
return {
K_z: usb_arm.BaseClockWise,
K_x: usb_arm.BaseCtrClockWise,
K_r: usb_arm.CloseGrips,
K_f: usb_arm.OpenGrips,
K_a: usb_arm.ShoulderDown,
K_q: usb_arm.ShoulderUp,
K_s: usb_arm.ElbowDown,
K_w: usb_arm.ElbowUp,
K_d: usb_arm.WristDown,
K_e: usb_arm.WristUp}
def key_loop():
km = make_keymap()
try:
arm = usb_arm.Arm()
except AttributeError:
print "Please make sure the arm is connected and turned on"
sys.exit(1)
handle = partial(handle_key, arm, 0.5, km)
exit_key = K_ESCAPE
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
return
if event.type == KEYDOWN:
if event.key == exit_key:
return
else:
handle(event.key)
def main():
pygame.init()
key_loop()
main()