ps3 buttonFastTurn not working

Forums:

I managed to pair my ps3 with my pi. All things are working fine. except my R2 button for controlling the turn speed doesn't work. I checked and I am certain that R2 is controlled by 9. everything is working also L2. But R2 doesn't seem to work? Any suggestions?

piborg's picture

Just to check if you hold down all of the following:

  1. R2 to enable tank steering
  2. Right stick fully to the right
  3. Left stick fully forward

the DiddyBorg should spin on the spot.

The speed of the spin can be controlled by reducing how far forward the left stick is pushed.

When driving normally the rate of turning can be controlled by how far the right stick is pushed.

Unfortunately that doesn't work. The diddyborg turns, but only the outer 3 wheels are spinning. Not the inner three ones!

piborg's picture

That is the behaviour we would expect if the R2 button is not working.

I would suggest trying to check if the controller is working.
Run this program from a terminal:
jstest /dev/input/js0
you should get a lot of lines of text, this is the current status of the controller.

Press and hold R2 and nothing else.

If R2 is working normally you should see 9:on on the last line.
In this case there is probably a problem with the script somewhere.

If R2 has a different number you should still see :on, but the number in front of it is not 9.
In this case change the number in the script to the number you are seeing, then everything should work fine.

If R2 is not working you will not see :on anywhere on the last line.
In this case the problem is with the controller, not with the script.
What you can do is change the script to look for a different button which is working, for example R1.

Press CTRL + C to end the program when you are done.

For the record. I tried to install my ps3 controller the way described on this site. It didn't work. Then i tried paring it via retropie setup for ps3. That worked. I can imagine that this could cause the r2 function not to work?

When i put in your command (jstest /dev/input/js0) I see that r2 states "on" when pressing.

piborg's picture

What number is shown just before the :on?

Number 9.

piborg's picture

In that case it sounds like the script is the problem.

Have you made any changes to the script at all?

I did't make any changes.

piborg's picture

Since you are sure the controller is working the problem must be with the script in some way.

Which script are you trying to use?

I put this script in.

#!/usr/bin/env python
# coding: Latin-1

# Load library functions we want
import time
import os
import sys
import pygame
import PicoBorgRev

# Re-direct our output to standard error, we need to ignore standard out to hide some nasty print statements from pygame
sys.stdout = sys.stderr

# Setup the PicoBorg Reverse
PBR = PicoBorgRev.PicoBorgRev()
#PBR.i2cAddress = 0x44                  # Uncomment and change the value if you have changed the board address
PBR.Init()
if not PBR.foundChip:
    boards = PicoBorgRev.ScanForPicoBorgReverse()
    if len(boards) == 0:
        print 'No PicoBorg Reverse found, check you are attached :)'
    else:
        print 'No PicoBorg Reverse at address %02X, but we did find boards:' % (PBR.i2cAddress)
        for board in boards:
            print '    %02X (%d)' % (board, board)
        print 'If you need to change the I≤C address change the setup line so it is correct, e.g.'
        print 'PBR.i2cAddress = 0x%02X' % (boards[0])
    sys.exit()
#PBR.SetEpoIgnore(True)                 # Uncomment to disable EPO latch, needed if you do not have a switch / jumper
# Ensure the communications failsafe has been enabled!
failsafe = False
for i in range(5):
    PBR.SetCommsFailsafe(True)
    failsafe = PBR.GetCommsFailsafe()
    if failsafe:
        break
if not failsafe:
    print 'Board %02X failed to report in failsafe mode!' % (PBR.i2cAddress)
    sys.exit()
PBR.ResetEpo()

# Settings for the joystick
axisUpDown = 1                          # Joystick axis to read for up / down position
axisUpDownInverted = False              # Set this to True if up and down appear to be swapped
axisLeftRight = 2                       # Joystick axis to read for left / right position
axisLeftRightInverted = False           # Set this to True if left and right appear to be swapped
buttonResetEpo = 3                      # Joystick button number to perform an EPO reset (Start)
buttonSlow = 8                          # Joystick button number for driving slowly whilst held (L2)
slowFactor = 0.5                        # Speed to slow to when the drive slowly button is held, e.g. 0.5 would be half speed
buttonFastTurn = 9                      # Joystick button number for turning fast (R2)
interval = 0.00                         # Time between updates in seconds, smaller responds faster but uses more processor time

# Power settings
voltageIn = 12.0                        # Total battery voltage to the PicoBorg Reverse
voltageOut = 12.0 * 0.95                # Maximum motor voltage, we limit it to 95% to allow the RPi to get uninterrupted power

# Setup the power limits
if voltageOut > voltageIn:
    maxPower = 1.0
else:
    maxPower = voltageOut / float(voltageIn)

# Setup pygame and wait for the joystick to become available
PBR.MotorsOff()
os.environ["SDL_VIDEODRIVER"] = "dummy" # Removes the need to have a GUI window
pygame.init()
#pygame.display.set_mode((1,1))
print 'Waiting for joystick... (press CTRL+C to abort)'
while True:
    try:
        try:
            pygame.joystick.init()
            # Attempt to setup the joystick
            if pygame.joystick.get_count() < 1:
                # No joystick attached, toggle the LED
                PBR.SetLed(not PBR.GetLed())
                pygame.joystick.quit()
                time.sleep(0.5)
            else:
                # We have a joystick, attempt to initialise it!
                joystick = pygame.joystick.Joystick(0)
                break
        except pygame.error:
            # Failed to connect to the joystick, toggle the LED
            PBR.SetLed(not PBR.GetLed())
            pygame.joystick.quit()
            time.sleep(0.5)
    except KeyboardInterrupt:
        # CTRL+C exit, give up
        print '\nUser aborted'
        PBR.SetLed(True)
        sys.exit()
print 'Joystick found'
joystick.init()
PBR.SetLed(False)

try:
    print 'Press CTRL+C to quit'
    driveLeft = 0.0
    driveRight = 0.0
    running = True
    hadEvent = False
    upDown = 0.0
    leftRight = 0.0
    # Loop indefinitely
    while running:
        # Get the latest events from the system
        hadEvent = False
        events = pygame.event.get()
        # Handle each event individually
        for event in events:
            if event.type == pygame.QUIT:
                # User exit
                running = False
            elif event.type == pygame.JOYBUTTONDOWN:
                # A button on the joystick just got pushed down
                hadEvent = True                    
            elif event.type == pygame.JOYAXISMOTION:
                # A joystick has been moved
                hadEvent = True
            if hadEvent:
                # Read axis positions (-1 to +1)
                if axisUpDownInverted:
                    upDown = -joystick.get_axis(axisUpDown)
                else:
                    upDown = joystick.get_axis(axisUpDown)
                if axisLeftRightInverted:
                    leftRight = -joystick.get_axis(axisLeftRight)
                else:
                    leftRight = joystick.get_axis(axisLeftRight)
                # Apply steering speeds
                if not joystick.get_button(buttonFastTurn):
                    leftRight *= 0.5
                # Determine the drive power levels
                driveLeft = -upDown
                driveRight = -upDown
                if leftRight < -0.05:
                    # Turning left
                    driveLeft *= 1.0 + (2.0 * leftRight)
                elif leftRight > 0.05:
                    # Turning right
                    driveRight *= 1.0 - (2.0 * leftRight)
                # Check for button presses
                if joystick.get_button(buttonResetEpo):
                    PBR.ResetEpo()
                if joystick.get_button(buttonSlow):
                    driveLeft *= slowFactor
                    driveRight *= slowFactor
                # Set the motors to the new speeds
                PBR.SetMotor1(driveRight * maxPower)
                PBR.SetMotor2(-driveLeft * maxPower)
        # Change the LED to reflect the status of the EPO latch
        PBR.SetLed(PBR.GetEpo())
        # Wait for the interval period
        time.sleep(interval)
    # Disable all drives
    PBR.MotorsOff()
except KeyboardInterrupt:
    # CTRL+C exit, disable all drives
    PBR.MotorsOff()
print
piborg's picture

That looks like the diddyRedJoy.py script to me.

I cannot see anything wrong with the code for detecting button 9 (R2), it should be working.
What I would suggest is making a change so we can see if the script thinks R2 is pressed or not.

If you can add the three highlighted lines below into the code:

                # Apply steering speeds
                if not joystick.get_button(buttonFastTurn):
                    leftRight *= 0.5
                    print 'R2 OFF - simple steering'
                else:
                    print 'R2 ON - tank steering'
                # Determine the drive power levels

When you run the script it should write the detected R2 state to the screen.
It will either say:
R2 ON - tank steering when R2 is pressed or
R2 OFF - simple steering when R2 is released.

I imported the lines and this is what it shows

Images: 

When I executed this script. It seems to be working. I don't know what script is running when i reboot the raspberry. But when i run the ps3diddyborgredjoy.sh it is working!
Sorry for the inconvenience.

piborg's picture

No problem, I have made the same mistake myself before, it is easily done.

Subscribe to Comments for &quot;ps3 buttonFastTurn not working&quot;