User avatar
JonnyAlpha
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 534
Joined: Sat Nov 02, 2013 2:06 pm

Reading Wireless Controller Input

Thu Dec 07, 2017 8:52 pm

Hi;

I have recently purchased this Wireless Game Controller from the Pi Hut:

https://thepihut.com/products/raspberry ... controller

I have successfully set it up and have it working with the motor driver board that comes with the CamJamEduKit #3.

What I would like to do now is try and use it to control my larger robot which uses a 2 x 32A Sabertooth Motor Driver Board.
The motor driver board is connected to my Pi via USB and currently I can control the motors attached to the board by sending plain text serial commands. e.g.

Code: Select all

       serial.writeString("M1: 1023\r\n") # motor 1 reverse half speed
        serial.writeString("M2: -1023\r\n") # motor 2 forward half speed

I have read the Pi Hut controller guide on GitHub here:
https://github.com/CamJam-EduKit/EduKit ... uctions.md

And from there have followed the link to Approximate Engineerings 'input' library.

Am correct in thinking that what I need to do is use the following code from Approximate Engineering docs and amend it to send serial commands.

Code: Select all

from approxeng.input.selectbinder import ControllerResource

while True:
    try:
        with ControllerResource() as joystick:
            print('Found a joystick and connected')
            while joystick.connected:
                # Do stuff with your joystick here!
                # ....
                # ....
        # Joystick disconnected...
        print('Connection to joystick lost')
    except IOError:
        # No joystick found, wait for a bit before trying again
        print('Unable to find any joysticks')
        sleep(1.0)
I guess I need to read the input from the joystick and add a

Code: Select all

serial.writeString
to control the motors by using this code to read the joystick:

Code: Select all

# Get a corrected value for the left stick x-axis
        left_x = joystick['lx']

And the insert something like a while statement such as while left_x = ????? serial.writeString(M1: 1023\r\n")

Any help would be appreciated.
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

User avatar
JonnyAlpha
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 534
Joined: Sat Nov 02, 2013 2:06 pm

Re: Reading Wireless Controller Input

Tue Dec 12, 2017 7:52 pm

OK slight deviation but using this guide here:

https://learn.pimoroni.com/tutorial/rob ... s-keyboard

I managed to control some LEDs using buttons on a wireless keyboard, lot of delay though so would not be great for fine motor control :-)

Now to find a guide on generic gamepads / controllers.
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: DougieLawson, mikerr and 5 guests