Skip to content

Make demos work #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ __pycache__
*.pyc
.idea
*.dll
*.egg-info
.vscode
60 changes: 0 additions & 60 deletions demos/key_ctrl.py

This file was deleted.

80 changes: 80 additions & 0 deletions demos/pg_key_ctrl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""Pygame key_ctrl - key based arm controller"""
from functools import partial
import logging
import sys

import pygame
from pygame.locals import *

import owi_maplin_usb_arm as usb_arm

def handle_keys_held(arm):
pressed_keys = pygame.key.get_pressed()
pattern = usb_arm.Stop
if pressed_keys[K_z]:
pattern = pattern | usb_arm.BaseClockWise
elif pressed_keys[K_x]:
pattern = pattern | usb_arm.BaseCtrClockWise
if pressed_keys[K_a]:
pattern = pattern | usb_arm.ShoulderDown
elif pressed_keys[K_q]:
pattern = pattern | usb_arm.ShoulderUp
if pressed_keys[K_s]:
pattern = pattern | usb_arm.ElbowDown
elif pressed_keys[K_w]:
pattern = pattern | usb_arm.ElbowUp
if pressed_keys[K_d]:
pattern = pattern | usb_arm.WristDown
elif pressed_keys[K_e]:
pattern = pattern | usb_arm.WristUp
if pressed_keys[K_r]:
pattern = pattern | usb_arm.CloseGrips
elif pressed_keys[K_f]:
pattern = pattern | usb_arm.OpenGrips
if pressed_keys[K_l]:
pattern = pattern | usb_arm.LedOn
arm.tell(pattern)


def key_loop():
try:
arm = usb_arm.Arm()
except AttributeError:
print("Please make sure the arm is connected and turned on")
sys.exit(1)

pygame.clock = pygame.time.Clock()
FPS = 50

try:
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
return
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
return
handle_keys_held(arm)
pygame.clock.tick(FPS)
finally:
arm.tell(usb_arm.Stop)


def main():
logging.basicConfig()
usb_arm.logger.setLevel(logging.DEBUG)
pygame.init()
pygame.display.set_mode([200, 200])
print("Press z/x to turn the base motor")
print("Press a/q to move the shoulder up/down")
print("Press w/s to move the elbow up/down")
print("Press e/d to move the wrist up/down")
print("Press r/f to close/open the grips")
print("Press l to toggle the LED")
print("Press ESC to exit")
key_loop()


main()
64 changes: 64 additions & 0 deletions demos/sh_key_ctrl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""Console keyboard based arm controller.
Requires readchar
"""
from functools import partial
import logging
import sys
import time

from readchar import readkey, key

import owi_maplin_usb_arm as usb_arm


KEYMAP = {
'z': usb_arm.BaseClockWise,
'x': usb_arm.BaseCtrClockWise,
'r': usb_arm.CloseGrips,
'f': usb_arm.OpenGrips,
'a': usb_arm.ShoulderDown,
'q': usb_arm.ShoulderUp,
's': usb_arm.ElbowDown,
'w': usb_arm.ElbowUp,
'd': usb_arm.WristDown,
'e': usb_arm.WristUp,
'l': usb_arm.LedOn
}

def handle_key(arm, pressed_key):
if pressed_key in KEYMAP:
message = KEYMAP[pressed_key]
print("Key ", pressed_key, "Movement message", message)

arm.move(message, 0.5)

def key_loop():
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)
exit_key = key.ESC

while True:
pressed_key = readkey()
if pressed_key == exit_key:
return
else:
handle(pressed_key)
time.sleep(0.1)

def main():
logging.basicConfig()
usb_arm.logger.setLevel(logging.DEBUG)
print("Press z/x to turn the base motor")
print("Press a/q to move the shoulder up/down")
print("Press w/s to move the elbow up/down")
print("Press e/d to move the wrist up/down")
print("Press r/f to close/open the grips")
print("Press l to toggle the LED")
print("Press ESC to exit")
key_loop()

main()
13 changes: 13 additions & 0 deletions demos/simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import owi_maplin_usb_arm as usb_arm
import logging

logging.basicConfig(level=logging.DEBUG)

arm = usb_arm.Arm()

arm.move(usb_arm.LedOn)
print("Wrist up")
arm.move(usb_arm.WristUp)
print("Wrist down")
arm.move(usb_arm.WristDown)

2 changes: 1 addition & 1 deletion owi_maplin_usb_arm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(self):

def tell(self, msg):
"""Send a USB messaqe to the arm"""
bmRequestType = 0x40
bmRequestType = usb.TYPE_VENDOR
bRequest = 6
wValue = 0x100
wIndex = 0
Expand Down
Loading