0

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

1 Answers1

0

For the servo part and based on Servokit documentation there is two ways to control servos:

  1. Set desired shaft angle:
    kit.servo[servonum].angle = 180
  1. Indicate rotation direction (1: forward, -1: backward, 0: stop) like:
    kit.continuous_servo[servonum].throttle = 1 
    kit.continuous_servo[servonum].throttle = -1
    kit.continuous_servo[servonum].throttle = 0

I'd rather use angles, even if I'd have to increment/decrement their values (according to time) in the loop. It would give you a hand on speed (or speed curve) and position.

For the Joystick part, the Eric Goebelbecker's "tutorial" worth a reading.

EDIT: Solution with continuous_servo (for future reading)

ABS_HAT0{X,Y} notifies DOWN event with -1 or +1 on the active axis. And UP with 0 value.

axis_servo = {
    'ABS_HAT0X': 5,
    'ABS_HAT0Y': 6,
}

[...]
        axis = ecodes.bytype[absevent.event.type][absevent.event.code]
        if axis == 'ABS_HAT0X':
            servonum = axis_servo[axis]
            kit.continuous_servo[servonum].throttle = absevent.event.value
        if axis == 'ABS_HAT0Y':
            servonum = axis_servo[axis]
            # Reverse Y axis (1 -> up direction)
            kit.continuous_servo[servonum].throttle = -absevent.event.value

Without continuous_servo, one should consider the use of select() with timeout (select([gamepad], [], [], timeout)) as described in readthedoc: python-evdev.

The timeout will allow angle computation.

levif
  • 2,156
  • 16
  • 14
  • Thank you! The trouble I'm having with Eric's tutorial is that he uses the EV_SYN event to kick off the activity. As I understand it, the EV_SYN happens when you release the button. His code then calculates how long the button was held down and then executes the movement. I'm looking for press button-move servo immediately and continue to move it until I let go....but I may be misunderstanding his code? – tsrosenberg May 09 '19 at 23:08