Skip to content

Commit ec03b05

Browse files
authored
Merge pull request #24 from orionrobots/on_pi
Make demos work
2 parents 2b420af + f9878da commit ec03b05

File tree

6 files changed

+160
-61
lines changed

6 files changed

+160
-61
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ __pycache__
44
*.pyc
55
.idea
66
*.dll
7+
*.egg-info
8+
.vscode

demos/key_ctrl.py

Lines changed: 0 additions & 60 deletions
This file was deleted.

demos/pg_key_ctrl.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""Pygame key_ctrl - key based arm controller"""
2+
from functools import partial
3+
import logging
4+
import sys
5+
6+
import pygame
7+
from pygame.locals import *
8+
9+
import owi_maplin_usb_arm as usb_arm
10+
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)
37+
38+
39+
def key_loop():
40+
try:
41+
arm = usb_arm.Arm()
42+
except AttributeError:
43+
print("Please make sure the arm is connected and turned on")
44+
sys.exit(1)
45+
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()
54+
return
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)
63+
64+
65+
def main():
66+
logging.basicConfig()
67+
usb_arm.logger.setLevel(logging.DEBUG)
68+
pygame.init()
69+
pygame.display.set_mode([200, 200])
70+
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")
77+
key_loop()
78+
79+
80+
main()

demos/sh_key_ctrl.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""Console keyboard based arm controller.
2+
Requires readchar
3+
"""
4+
from functools import partial
5+
import logging
6+
import sys
7+
import time
8+
9+
from readchar import readkey, key
10+
11+
import owi_maplin_usb_arm as usb_arm
12+
13+
14+
KEYMAP = {
15+
'z': usb_arm.BaseClockWise,
16+
'x': usb_arm.BaseCtrClockWise,
17+
'r': usb_arm.CloseGrips,
18+
'f': usb_arm.OpenGrips,
19+
'a': usb_arm.ShoulderDown,
20+
'q': usb_arm.ShoulderUp,
21+
's': usb_arm.ElbowDown,
22+
'w': usb_arm.ElbowUp,
23+
'd': usb_arm.WristDown,
24+
'e': usb_arm.WristUp,
25+
'l': usb_arm.LedOn
26+
}
27+
28+
def handle_key(arm, pressed_key):
29+
if pressed_key in KEYMAP:
30+
message = KEYMAP[pressed_key]
31+
print("Key ", pressed_key, "Movement message", message)
32+
33+
arm.move(message, 0.5)
34+
35+
def key_loop():
36+
try:
37+
arm = usb_arm.Arm()
38+
except AttributeError:
39+
print("Please make sure the arm is connected and turned on")
40+
sys.exit(1)
41+
handle = partial(handle_key, arm)
42+
exit_key = key.ESC
43+
44+
while True:
45+
pressed_key = readkey()
46+
if pressed_key == exit_key:
47+
return
48+
else:
49+
handle(pressed_key)
50+
time.sleep(0.1)
51+
52+
def main():
53+
logging.basicConfig()
54+
usb_arm.logger.setLevel(logging.DEBUG)
55+
print("Press z/x to turn the base motor")
56+
print("Press a/q to move the shoulder up/down")
57+
print("Press w/s to move the elbow up/down")
58+
print("Press e/d to move the wrist up/down")
59+
print("Press r/f to close/open the grips")
60+
print("Press l to toggle the LED")
61+
print("Press ESC to exit")
62+
key_loop()
63+
64+
main()

demos/simple.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import owi_maplin_usb_arm as usb_arm
2+
import logging
3+
4+
logging.basicConfig(level=logging.DEBUG)
5+
6+
arm = usb_arm.Arm()
7+
8+
arm.move(usb_arm.LedOn)
9+
print("Wrist up")
10+
arm.move(usb_arm.WristUp)
11+
print("Wrist down")
12+
arm.move(usb_arm.WristDown)
13+

owi_maplin_usb_arm/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def __init__(self):
7373

7474
def tell(self, msg):
7575
"""Send a USB messaqe to the arm"""
76-
bmRequestType = 0x40
76+
bmRequestType = usb.TYPE_VENDOR
7777
bRequest = 6
7878
wValue = 0x100
7979
wIndex = 0

0 commit comments

Comments
 (0)