-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps4.py
203 lines (162 loc) · 4.03 KB
/
ps4.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import time
import sys
import comm
import utils.gamepad as pad
UPDATE_FREQ = 100.0
DEADZONE = 0.1
ELEV_MAX = 200.0
PAMI = False
ACTION_ENABLED = True
if len(sys.argv) > 1:
if sys.argv[1] == "p":
PAMI = True
elif sys.argv[1] == "na":
ACTION_ENABLED = False
if PAMI:
ACTION_ENABLED = False
asserv = comm.make_asserv()
asserv.set_blocking(False)
if ACTION_ENABLED:
action = comm.make_action()
action.set_blocking(False)
class LimitedValue:
def __init__(self, max_deriv, max_deriv_minus=None):
self.max_deriv = max_deriv
if max_deriv_minus is None:
self.max_deriv_minus = max_deriv
else:
self.max_deriv_minus = max_deriv_minus
self.reset()
def apply(self, val, dt):
cderiv = (val-self.prev)/dt
if cderiv > self.max_deriv or cderiv < -self.max_deriv_minus:
cderiv = -self.max_deriv_minus if cderiv < 0 else self.max_deriv
val = self.prev+cderiv*dt
self.prev = val
return val
def add(self, val, dt):
return self.apply(self.prev+val, dt)
def val(self):
return self.prev
def reset(self):
self.prev = 0
# Speed setup
if PAMI:
prec_speed_remove = 300 # mm/s
prec_turn_remove = 1.5 # rad/s
turbo_speed_add = 400 # mm/s
move_speed = 400 # mm/s
turn_speed = 2 # rad/s
speed_accel_lim = 1000 # mm^2/s
else:
prec_speed_remove = 300 # mm/s
prec_turn_remove = 1.5 # rad/s
turbo_speed_add = 300 # mm/s
move_speed = 400 # mm/s
elev_speed = 25 # mm per button press
turn_speed = 2 # rad/s
arm_turn_speed = 600 # deg/s
speed_accel_lim = 1000 # mm^2/s
# Gamepad setup
gamepadType = pad.PS4
btnDeploy = "CROSS"
btnFold = "CIRCLE"
btnPump = "TRIANGLE"
btnExit = "PS"
joySpeed = "LEFT-Y"
joyTurn = "RIGHT-X"
joyTurbo = "R2"
joySlow = "L2"
joyElevator = "DPAD-Y"
joyArmTurn = "DPAD-X"
# State variables
dstVel = LimitedValue(speed_accel_lim)
dst = 0
theta = 0
pumpState = False
elevPos = 0
# Dt variables
st = -1/UPDATE_FREQ
end = 0
# Wait for a connection
if not pad.available():
print('Please connect your gamepad...')
while not pad.available():
time.sleep(1.0)
gamepad = gamepadType()
print("Gamepad connected")
asserv.start()
asserv.wait_completed()
time.sleep(1)
print("Asserv started")
if ACTION_ENABLED:
action.start()
action.wait_completed()
print("Action started")
action.elev_home()
action.wait_completed()
print("Elev home")
gamepad.startBackgroundUpdates()
def deadzone(val):
if abs(val) < DEADZONE:
return 0
return val
def trigger(val):
return (val+1.0)/2.0
state = True
try:
while gamepad.isConnected():
dt = end-st
st = time.time()
if gamepad.beenPressed(btnExit):
state = not state
asserv.set_running(state)
dst = 0
theta = 0
dstVel.reset()
print(f"State is {state}")
if ACTION_ENABLED:
if gamepad.beenPressed(btnDeploy):
action.arm_deploy()
if gamepad.beenReleased(btnFold):
action.arm_fold()
if gamepad.beenPressed(btnPump):
pumpState = not pumpState
action.pump_enable(0, pumpState)
if pumpState is False:
action.wait_completed()
action.pump_enable(1, True)
action.wait_completed()
time.sleep(0.1)
action.pump_enable(1, False)
elev = deadzone(gamepad.axis(joyElevator))
speed = -deadzone(gamepad.axis(joySpeed))
turn = -deadzone(gamepad.axis(joyTurn))
turbo = trigger(gamepad.axis(joyTurbo))
slow = trigger(gamepad.axis(joySlow))
armTurn = deadzone(gamepad.axis(joyArmTurn))
move_spd_adj = move_speed + turbo_speed_add*turbo - prec_speed_remove*slow
turn_spd_adj = turn_speed - prec_turn_remove*slow
dst += dstVel.apply(move_spd_adj*speed,dt)*dt
theta += turn_spd_adj*turn*dt
#print(dst, theta)
if ACTION_ENABLED:
if armTurn != 0:
action.arm_turn(arm_turn_speed*armTurn*dt)
if elev != 0:
elevPos = max(min(elevPos-elev*elev_speed,ELEV_MAX),0)
action.elev_move_abs(elevPos)
action.wait_completed()
asserv.debug_set_target(dst, theta)
time.sleep(1.0/UPDATE_FREQ)
end = time.time()
except Exception as e:
print(f"Exception: {e}")
finally:
print("Closing")
gamepad.disconnect()
asserv.stop()
asserv.wait_completed()
if ACTION_ENABLED:
action.stop()
action.wait_completed()