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