forked from piborg/Gamepad
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrockyJoy.py
executable file
·131 lines (113 loc) · 4.32 KB
/
rockyJoy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
# coding: utf-8
# Load the libraries
import sys
import Gamepad
import time
sys.path.insert(0, "/home/pi/rockyborg")
import RockyBorg
# Settings for the gamepad
gamepadType = Gamepad.PS4 # Class for the gamepad (e.g. Gamepad.PS3)
joystickSpeed = 'LEFT-Y' # Joystick axis to read for up / down position
joystickSpeedInverted = True # Set this to True if up and down appear to be swapped
joystickSteering = 'RIGHT-X' # Joystick axis to read for left / right position
joystickSteeringInverted = False # Set this to True if left and right appear to be swapped
buttonSlow = 'L2' # Joystick button for driving slowly whilst held
slowFactor = 0.5 # Speed to slow to when the drive slowly button is held, e.g. 0.5 would be half speed
buttonExit = 'PS' # Joystick button to end the program
interval = 0.05 # Time between motor updates in seconds, smaller responds faster but uses more processor time
# Power settings
voltageIn = 1.2 * 8 # Total battery voltage to the RockyBorg
voltageOut = 6.0 # Maximum motor voltage
# Setup the power limits
if voltageOut > voltageIn:
maxPower = 1.0
else:
maxPower = voltageOut / float(voltageIn)
# Setup the RockyBorg
RB = RockyBorg.RockyBorg()
#RB.i2cAddress = 0x21 # Uncomment and change the value if you have changed the board address
RB.Init()
if not RB.foundChip:
boards = RockyBorg.ScanForRockyBorg()
if len(boards) == 0:
print('No RockyBorg found, check you are attached :)')
else:
print('No RockyBorg at address %02X, but we did find boards:' % (RB.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('RB.i2cAddress = 0x%02X' % (boards[0]))
sys.exit()
# Enable the motors and disable the failsafe
RB.SetCommsFailsafe(False)
RB.MotorsOff()
RB.SetMotorsEnabled(True)
# Setup the state shared with callbacks
global running
running = True
# Create the callback functions
def exitButtonPressed():
global running
print('EXIT')
running = False
# Wait for a connection
RB.MotorsOff()
RB.SetLed(True)
waitingToggle = True
if not Gamepad.available():
print('Please connect your gamepad...')
while not Gamepad.available():
time.sleep(interval * 4)
waitingToggle = not waitingToggle
if waitingToggle:
RB.SetLed(False)
else:
RB.SetLed(True)
print('Gamepad connected')
gamepad = gamepadType()
# Start the background updating
gamepad.startBackgroundUpdates()
RB.SetLed(True)
# Register the callback functions
gamepad.addButtonPressedHandler(buttonExit, exitButtonPressed)
# Keep running while joystick updates are handled by the callbacks
try:
while running and gamepad.isConnected():
# Read the latest speed and steering
if joystickSpeedInverted:
speed = -gamepad.axis(joystickSpeed)
else:
speed = +gamepad.axis(joystickSpeed)
if joystickSteeringInverted:
steering = -gamepad.axis(joystickSteering)
else:
steering = +gamepad.axis(joystickSteering)
# Work out the adjusted speed
if gamepad.isPressed(buttonSlow):
finalSpeed = speed * slowFactor
else:
finalSpeed = speed
# Determine the drive power levels based on steering angle
servoPosition = steering
driveLeft = finalSpeed
driveRight = finalSpeed
if steering < -0.05:
# Turning left
driveLeft *= 1.0 + (0.5 * steering)
elif steering > +0.05:
# Turning right
driveRight *= 1.0 - (0.5 * steering)
# Set the motors to the new speeds and tilt the servo to steer
RB.SetMotor1(-driveLeft * maxPower)
RB.SetMotor2(driveRight * maxPower)
RB.SetServoPosition(servoPosition)
# Sleep for our motor change interval
time.sleep(interval)
finally:
# Ensure the background thread is always terminated when we are done
gamepad.disconnect()
# Turn the motors off
RB.MotorsOff()
# Turn the LED off indicate we have finished
RB.SetLed(False)