I'm building a basic DVa Mech robot (hobby). This is non-bipedal. Wheeled chassis. All code in python.
How do I continuously perform activities while holding down a button, pushing a joystick? And can I even do two (or more) at once: move wheels forward, turn torso, fire guns?
I'm reading joystick input fine. Servos work too. I can't seem to figure out the logic loops of 'while button pushed - do something - and keep scanning for more input'
Tried various things...they didn't work so they are out of the code below.
Running 6 continuous servos (4 for chassis, two for mini-guns) Logitech F710 joystick
from evdev import InputDevice, categorize, ecodes, KeyEvent
from adafruit_servokit import ServoKit
import time
kit = ServoKit(channels = 16)
gamepad = InputDevice('/dev/input/event7')
print (gamepad)
for event in gamepad.read_loop():
if event.type == ecodes.EV_KEY:
keyevent = categorize(event)
if keyevent.keystate == KeyEvent.key_down:
print(keyevent)
....
elif keyevent.keycode == 'BTN_TL':
print ("Guns")
elif event.type == ecodes.EV_ABS:
absevent = categorize(event)
print(absevent.event.code)
if ecodes.bytype[absevent.event.type][absevent.event.code] == 'ABS_HAT0X':
if absevent.event.value == -1:
print('left')
elif absevent.event.value == 1:
print('right')
if ecodes.bytype[absevent.event.type][absevent.event.code] == 'ABS_HAT0Y':
if absevent.event.value == -1:
print('forward')
elif absevent.event.value == 1:
print('back')
Fairly basic...when BTN_TL is pressed, servos 5 and 6 should spin until the button is released
Likewise with HAT0X and 0Y the servos should move forward/back/left/right while pressed.
I've tried while loops and whatnot...but there's a logic/timing sequence in joystick input I'm not putting in the right place