User avatar
CharlyDelta
Posts: 274
Joined: Thu Jul 18, 2013 4:04 am
Location: Near Montreal, Quebec

Rpi 3B+, motors and a PS3 controller

Sun Sep 12, 2021 2:18 am

Hello..

The engines are controlled by the Rpi and a L298N. Everything works well at this level.
Same for the PS3. It is paired, well configured and responds to the command: jstest /dev/input/js0

Code: Select all

Axes:  0:     0  1:     0  2: 16513  3:-24254  4:-32767  5: 32767  6: 32767  7:     0  8:     0  9:     0 10:     0 11:     0 12:     0 13:     0 14    0 18:     0 19:     0 20:     0 21:     0 22:     0 23:     0 24:     0 25:     0 26:     0 27:     0 28:     0 
Buttons:  0:off  1:off  2:off  3
Axes:  0:     0  1:     0  2: 16513  3:-15739  4:-32767  5: 32767  6: 32767  7:     0  8:     0  9:     0 10:     0 11:     0 12:     0 13:     0 14    0 18:     0 19:     0 20:     0 21:     0 22:     0 23:     0 24:     0 25:     0 26:     0 27:     0 28:     0 
Buttons:  0:off  1:off  2:off  3
Axes:  0:     0  1:     0  2: -3097  3:-15739  4:-32767  5: 32767  6: 32767  7:     0  8:     0  9:     0 10:     0 11:     0 12:     0 13:     0 14    0 18:     0 19:     0 20:     0 21:     0 22:     0 23:     0 24:     0 25:     0 26:     0 27:     0 28:     0 
Buttons:  0:off  1:off  2:off  3Axes:  0:     0  1:     0  2: -3097  3:  3354  4:-32767  5: 32767  6: 32767  7:     0  8:     0  9:     0 10:     0 11:     0 12:     0 13:     0 14


I've been searching, but I can't find a way to control the motors with the PS3.

Do you have an idea how to do it?
nidcornifleur@gmail.com

ame
Posts: 3902
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Rpi 3B+, motors and a PS3 controller

Sun Sep 12, 2021 2:38 am

If you can't find an example then I'm afraid you'll have to write your own.

If you know how to read the inputs from the PS3 controller and you know how to control the motors then you'll need a simple program that runs an infinite loop.

All it has to do is read the controller, decide if the motors should be turned on or off, then write to the motor controller.

You can use your favourite language for this. If you don't have a favourite language then your favourite language is Python.
Hmm. What can I put here?

User avatar
HermannSW
Posts: 4829
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Rpi 3B+, motors and a PS3 controller

Sun Sep 12, 2021 10:59 am

I don't have PS3 controller, but cheap SNES gamepads:
Image


You can inspect the code for controlling 4DoF robot arm.
The code is a bash script that uses pigpio pigs commands to control the motors.
It compiles C code at end of the script once in case "/tmp/joystick" is not present.
That C code handles the gamepad events, and could be reused for your project:
https://github.com/Hermann-SW/4DoF_robo ... ls/gamepad

Code: Select all

#!/bin/bash
# contains and uses this version of Jason White's joystick.c at bottom
# https://gist.github.com/jasonwhite/c5b2048c15993d285130/eb3bcd6e1cd88002d21764b9965d075eceaf4b83
#
# requires SNES joystick connected to Raspberry USB, pigpio library installed
#
js=/tmp/joystick
if [ ! -f $js ]; then sed -n "/^\/\*\*$/,\$p" $0 | gcc -x c - -o $js; fi

g=1500
...

While controlling robot arm with gamepad was nice, I do like more this hard coded automatic robot arm demo:
Image


P.S:

Just realized that robot arm did "compute" square function for the powers of 2 die "input 8" ... ;-)
https://www.youtube.com/watch?v=iwZIn6fQfMk
die.compute_square.png
die.compute_square.png
die.compute_square.png (113.58 KiB) Viewed 955 times
https://github.com/Hermann-SW/memrun
https://stamm-wilbrandt.de/2wheel_balancing_robot
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

User avatar
CharlyDelta
Posts: 274
Joined: Thu Jul 18, 2013 4:04 am
Location: Near Montreal, Quebec

Re: Rpi 3B+, motors and a PS3 controller

Sun Sep 12, 2021 12:19 pm

I don't think I can create a script for this. My knowledge is particularly limited in this area. I spent a day to get the PS3 to work on the RPI...

I have an XBOX controller as well... but haven't found anything pertinent.

I'm going to look into Hermann's controller.

I don't think I can create a script for this. My knowledge is particularly limited in this area. I spent a day to get the PS3 to work on the RPI...

I have an XBOX controller as well... but haven't found anything pertinent.
cheap SNES gamepads
I'm going to look into Hermann's controller.
nidcornifleur@gmail.com

ame
Posts: 3902
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Rpi 3B+, motors and a PS3 controller

Sun Sep 12, 2021 1:53 pm

As you wish.
Hmm. What can I put here?

User avatar
CharlyDelta
Posts: 274
Joined: Thu Jul 18, 2013 4:04 am
Location: Near Montreal, Quebec

Re: Rpi 3B+, motors and a PS3 controller

Sun Sep 12, 2021 5:36 pm

I found this script and changed my settings. When I click a button on the PS3, it prints the command.It remains to be seen how I can send the command to a GPIO pin.



Code: Select all

#import evdev
from evdev import InputDevice, categorize, ecodes

#creates object 'gamepad' to store the data
#you can call it whatever you like
gamepad = InputDevice('/dev/input/event0')

#button code variables (change to suit your device)
aBtn = 301
bBtn = 303
xBtn = 302
yBtn = 300

up = 292
down = 294
left = 295
right = 293

start = 291
select = 288

lTrig = 298
rTrig = 299

#prints out device info at start
print(gamepad)

#loop and filter by event code and print the mapped label
for event in gamepad.read_loop():
    if event.type == ecodes.EV_KEY:
        if event.value == 1:
            if event.code == yBtn:
                print("Y")
            elif event.code == bBtn:
                print("B")
            elif event.code == aBtn:
                print("A")
            elif event.code == xBtn:
                print("X")

            elif event.code == up:
                print("up")
            elif event.code == down:
                print("down")
            elif event.code == left:
                print("left")
            elif event.code == right:
                print("right")

            elif event.code == start:
                print("start")
            elif event.code == select:
                print("select")

            elif event.code == lTrig:
                print("left bumper")
            elif event.code == rTrig:
                print("right bumper")
                
Your ideas are welcome.
Last edited by CharlyDelta on Mon Sep 13, 2021 11:50 am, edited 1 time in total.
nidcornifleur@gmail.com

User avatar
HermannSW
Posts: 4829
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Rpi 3B+, motors and a PS3 controller

Sun Sep 12, 2021 7:52 pm

CharlyDelta wrote:
Sun Sep 12, 2021 5:36 pm
When I click a button on the PS3, it prints the command.
good

CharlyDelta wrote:
Sun Sep 12, 2021 2:18 am
The engines are controlled by the Rpi and a L298N.
It remains to be seen how I can send the command to a GPIO pin.
My raspcatbot has a Pi3A+ onboard, that controls the two caterpillar robot motors with L298N as well:
viewtopic.php?f=37&t=267999
Normally one L298N for two motors, here with two L298N for an experiment:
Image


I use pigpio library for that, with C interface.
In case you want to go with Python, pigpio has Python interface as well:
https://abyz.me.uk/rpi/pigpio/python.html

For 2.5m/s inhouse fast brake by motor direction reversal, you can see how the L298N was controlled in C:
https://github.com/Hermann-SW/MIPI_Came ... 012e2932b9
The monochrome global shutter camera analyzes 320x240 frames captured with 204fps, and if it does not see black line anymore, triggers motor hard stop. Basic controlling motors via L298N is not that hard:
viewtopic.php?f=37&t=267999&start=25#p1643242
Image
https://github.com/Hermann-SW/memrun
https://stamm-wilbrandt.de/2wheel_balancing_robot
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

User avatar
CharlyDelta
Posts: 274
Joined: Thu Jul 18, 2013 4:04 am
Location: Near Montreal, Quebec

Re: Rpi 3B+, motors and a PS3 controller

Sun Sep 12, 2021 8:59 pm

Everything works fine with my two L298N. They are well connected and I can control all four motors perfectly.
It's the (probably absurd) idea to use a PS3 to control everything. I've tried several scripts found while browsing but nothing really usable.

Here is the script used to control the motors:

Code: Select all

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
import time

# RoMecanum
GPIO.setwarnings(False)

Motor1A = 15
Motor1B = 13
Motor2A = 18
Motor2B = 16

Motor3A = 32
Motor3B = 36
Motor4A = 38
Motor4B = 40


GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor2A,GPIO.OUT)
GPIO.setup(Motor2B,GPIO.OUT)

GPIO.setup(Motor3A,GPIO.OUT)
GPIO.setup(Motor3B,GPIO.OUT)
GPIO.setup(Motor4A,GPIO.OUT)
GPIO.setup(Motor4B,GPIO.OUT)


def backward():
        print("Going backward")
        GPIO.output(Motor1A,GPIO.HIGH)
        GPIO.output(Motor1B,GPIO.LOW)
        GPIO.output(Motor2A,GPIO.HIGH)
        GPIO.output(Motor2B,GPIO.LOW)

        GPIO.output(Motor3A,GPIO.HIGH)
        GPIO.output(Motor3B,GPIO.LOW)
        GPIO.output(Motor4A,GPIO.HIGH)
        GPIO.output(Motor4B,GPIO.LOW)


def forward():
        print("Going forward")
        GPIO.output(Motor1A,GPIO.LOW)
        GPIO.output(Motor1B,GPIO.HIGH)
        GPIO.output(Motor2A,GPIO.LOW)
        GPIO.output(Motor2B,GPIO.HIGH)

        GPIO.output(Motor3A,GPIO.LOW)
        GPIO.output(Motor3B,GPIO.HIGH)
        GPIO.output(Motor4A,GPIO.LOW)
        GPIO.output(Motor4B,GPIO.HIGH)

def turnLeft():
        print("Going Left")
        GPIO.output(Motor1A,GPIO.LOW)
        GPIO.output(Motor1B,GPIO.HIGH)
        GPIO.output(Motor2A,GPIO.HIGH)
        GPIO.output(Motor2B,GPIO.LOW)

        GPIO.output(Motor3A,GPIO.HIGH)
        GPIO.output(Motor3B,GPIO.LOW)
        GPIO.output(Motor4A,GPIO.LOW)
        GPIO.output(Motor4B,GPIO.HIGH)


def turnRight():
        print("Going Right")
        GPIO.output(Motor1A,GPIO.HIGH)
        GPIO.output(Motor1B,GPIO.LOW)
        GPIO.output(Motor2A,GPIO.LOW)
        GPIO.output(Motor2B,GPIO.HIGH)

        GPIO.output(Motor3A,GPIO.LOW)
        GPIO.output(Motor3B,GPIO.HIGH)
        GPIO.output(Motor4A,GPIO.HIGH)
        GPIO.output(Motor4B,GPIO.LOW)


def stop():
        print("Stopping")
        GPIO.output(Motor1A,GPIO.LOW)
        GPIO.output(Motor1B,GPIO.LOW)
        GPIO.output(Motor2A,GPIO.LOW)
        GPIO.output(Motor2B,GPIO.LOW)

        GPIO.output(Motor3A,GPIO.LOW)
        GPIO.output(Motor3B,GPIO.LOW)
        GPIO.output(Motor4A,GPIO.LOW)
        GPIO.output(Motor4B,GPIO.LOW)
And, with th app.py interface I get this:

Image

[moderator changed quote tags to code tags]
nidcornifleur@gmail.com

User avatar
HermannSW
Posts: 4829
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Rpi 3B+, motors and a PS3 controller

Mon Sep 13, 2021 10:01 am

Now only the connection between your motor and event print code is missing. Copy both together, and add "forward()" where event print code does 'print("up") ', add "backward()" where event print code does 'print("down") ', ...
https://github.com/Hermann-SW/memrun
https://stamm-wilbrandt.de/2wheel_balancing_robot
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

User avatar
CharlyDelta
Posts: 274
Joined: Thu Jul 18, 2013 4:04 am
Location: Near Montreal, Quebec

Re: Rpi 3B+, motors and a PS3 controller

Mon Sep 13, 2021 11:48 am

[moderator changed quote tags to code tags]
Thank you, I got confused
HermannSW wrote:
Mon Sep 13, 2021 10:01 am
Copy both together,
This is the unknown part for me but I will work on it
nidcornifleur@gmail.com

User avatar
CharlyDelta
Posts: 274
Joined: Thu Jul 18, 2013 4:04 am
Location: Near Montreal, Quebec

Re: Rpi 3B+, motors and a PS3 controller

Thu Sep 16, 2021 5:25 pm

I didn't find a way to copy both scripts together.

On the other hand, I installed Sixpair and Sixad and it works but I can't find a way to control the engines through the GPIOs.

I also set up Pygame and again, I can't see where to configure for the GPIOs.

Thank you for your suggestions.
nidcornifleur@gmail.com

Return to “Automation, sensing and robotics”