0

I am attempting to trigger an event with a particular value of an analog joystick y axis in a while loop. I have used in an if statement ==, >, <, <>, >= . Because the readout (print y value) lags because of cycling, the value, once triggered, remains until the y value steps up and back down. I really just want to snapshot the y value when it occurs to trigger another event. I would then use another y value to stop the event that was previously triggered. I have two processes going continually; one to handle this backup blinking function and the other to operate motors on robot via gamepad inputs.

suggestions please?

This code works properly if I ask for a user input, but can't get it to work using the joystick value as the trigger(above).

from time import sleep

from inputs import get_gamepad

controller_input = {'ABS_Y': 0}


def left_thumb_joystick_d():

    # Function to Left Thumb Joystick to the UP-DOWN

    print('Left backward speed --> {}'.format(controller_input['ABS_Y']))

    print("Left motor Backward speeding up")


def gamepad_update():

    # Code execution stops at the following line until gamepad event occurs.

    events = get_gamepad()

    return_code = 'No Match'

    for event in events:

        event_test = controller_input.get(event.code, 'No Match')

        if event_test != 'No Match':

            controller_input[event.code] = event.state

            return_code = event.code

        else:

            return_code = 'No Match'

        return return_code


while True:

    control_code = gamepad_update()

    joystick_count = (controller_input['ABS_Y'])

    for i in range(joystick_count):
        joystick = (controller_input['ABS_Y'])
        joy = joystick
        print(joy)

        if 135 < joy < 145:
            n = 1
        #while joy == 145:
            print("repeating loop")
            while n > 0:
                print("hd_lights_on")
                print("tail_lights_on")
                sleep(.5)
                print("hd_lights_off")
                print("tail_lights_off")
                sleep(.5)
                n -= 1
                print(n)
                #n = int(input())
                #n = 0
                joy = 128
                break
        if joy == 128:
            n = 0
        break
10
  • 1
    How is a Pi involved? Place your code in a code block. What are you using to read the joystick value? – CoderMike Nov 12 '20 at 13:39
  • joystick motion is capture by the following at the top of the file – RoboNovice Nov 12 '20 at 14:30
  • from time import sleep from inputs import get_gamepad controller_input = {'ABS_Y': 0} def left_thumb_joystick_d(): # Function to Left Thumb Joystick to the UP-DOWN print('Left backward speed --> {}'.format(controller_input['ABS_Y'])) print("Left motor Backward speeding up") – RoboNovice Nov 12 '20 at 14:30
  • def gamepad_update(): # Code execution stops at the following line until gamepad event occurs. events = get_gamepad() return_code = 'No Match' for event in events: event_test = controller_input.get(event.code, 'No Match') if event_test != 'No Match': controller_input[event.code] = event.state return_code = event.code else: return_code = 'No Match' return return_code – RoboNovice Nov 12 '20 at 14:30
  • Edit your question with this code in code blocks. How is a Pi involved? – CoderMike Nov 12 '20 at 14:37
1

Does the following help?

What do you want to happen based on the Y value?

Tested on my Pi400 with XBox360 wired controller.

Edit: Added a reverse blinking thread

from inputs import get_gamepad
import threading,time

def blinkFunc():
    global reverse
    while True:
        if reverse:
            print("light on")
            time.sleep(0.5)
            print("light off")
        time.sleep(0.05)
        
reverse = False

blinkThread=threading.Thread(target = blinkFunc)
blinkThread.start()

while True:
    events = get_gamepad()
    for event in events:
        if event.code == "ABS_Y":
            #print(event.state)
            if event.state > 4000:
                reverse = True
                print("Backwards")
            elif event.state < -4000:
                print("Forwards")
            else:
                reverse = False
                
6
  • 1
    Mike, that does work when I substituted my gamepad values, ie. 128 neutral, <128 forward, and >128 backwards. However, when I insert the blinking while loop under the print ("Backwards") statement, the result is a follows> Notice the counting up of the print(event.state). Because of the lag in the capture of the value from the joystick the loop continues until the value finally works its way back to 128. see below – RoboNovice Nov 12 '20 at 16:10
  • 0 160 Backwards hd_lights_on tail_lights_on hd_lights_off tail_lights_off 0 173 Backwards hd_lights_on tail_lights_on hd_lights_off tail_lights_off 0 183 Backwards hd_lights_on tail_lights_on hd_lights_off tail_lights_off 0 190 Backwards hd_lights_on tail_lights_on hd_lights_off tail_lights_off 0 201 Backwards hd_lights_on tail_lights_on hd_lights_off tail_lights_off 0 212 Backwards hd_lights_on tail_lights_on hd_lights_off tail_lights_off 0 220 Backwards hd_lights_on tail_lights_on – RoboNovice Nov 12 '20 at 16:10
  • the 0 is n value after going through the loop, the3 digit number is the event.state as it lags in reading the value – RoboNovice Nov 12 '20 at 16:11
  • thank you for your help so far, as you can see, I am just trying to trigger a back blinking sequence when the robot is moving backwards. – RoboNovice Nov 12 '20 at 16:13
  • then stop the sequence when the joystick if neutral or going forward – RoboNovice Nov 12 '20 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.