-
Notifications
You must be signed in to change notification settings - Fork 120
/
edit_this.py
394 lines (315 loc) · 14.1 KB
/
edit_this.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
"""Write your control strategy.
Then run:
$ python3 getting_started.py --overrides ./getting_started.yaml
Tips:
Search for strings `INSTRUCTIONS:` and `REPLACE THIS (START)` in this file.
Change the code between the 5 blocks starting with
#########################
# REPLACE THIS (START) ##
#########################
and ending with
#########################
# REPLACE THIS (END) ####
#########################
with your own code.
They are in methods:
1) __init__
2) cmdFirmware
3) interStepLearn (optional)
4) interEpisodeLearn (optional)
"""
import numpy as np
from collections import deque
try:
from competition_utils import Command, PIDController, timing_step, timing_ep, plot_trajectory, draw_trajectory
except ImportError:
# PyTest import.
from .competition_utils import Command, PIDController, timing_step, timing_ep, plot_trajectory, draw_trajectory
#########################
# REPLACE THIS (START) ##
#########################
# Optionally, create and import modules you wrote.
# Please refrain from importing large or unstable 3rd party packages.
try:
import example_custom_utils as ecu
except ImportError:
# PyTest import.
from . import example_custom_utils as ecu
#########################
# REPLACE THIS (END) ####
#########################
class Controller():
"""Template controller class.
"""
def __init__(self,
initial_obs,
initial_info,
use_firmware: bool = False,
buffer_size: int = 100,
verbose: bool = False
):
"""Initialization of the controller.
INSTRUCTIONS:
The controller's constructor has access the initial state `initial_obs` and the a priori infromation
contained in dictionary `initial_info`. Use this method to initialize constants, counters, pre-plan
trajectories, etc.
Args:
initial_obs (ndarray): The initial observation of the quadrotor's state
[x, x_dot, y, y_dot, z, z_dot, phi, theta, psi, p, q, r].
initial_info (dict): The a priori information as a dictionary with keys
'symbolic_model', 'nominal_physical_parameters', 'nominal_gates_pos_and_type', etc.
use_firmware (bool, optional): Choice between the on-board controll in `pycffirmware`
or simplified software-only alternative.
buffer_size (int, optional): Size of the data buffers used in method `learn()`.
verbose (bool, optional): Turn on and off additional printouts and plots.
"""
# Save environment and control parameters.
self.CTRL_TIMESTEP = initial_info["ctrl_timestep"]
self.CTRL_FREQ = initial_info["ctrl_freq"]
self.initial_obs = initial_obs
self.VERBOSE = verbose
self.BUFFER_SIZE = buffer_size
# Store a priori scenario information.
self.NOMINAL_GATES = initial_info["nominal_gates_pos_and_type"]
self.NOMINAL_OBSTACLES = initial_info["nominal_obstacles_pos"]
# Check for pycffirmware.
if use_firmware:
self.ctrl = None
else:
# Initialize a simple PID Controller for debugging and test.
# Do NOT use for the IROS 2022 competition.
self.ctrl = PIDController()
# Save additonal environment parameters.
self.KF = initial_info["quadrotor_kf"]
# Reset counters and buffers.
self.reset()
self.interEpisodeReset()
#########################
# REPLACE THIS (START) ##
#########################
# Call a function in module `example_custom_utils`.
ecu.exampleFunction()
# Example: hardcode waypoints through the gates.
if use_firmware:
waypoints = [(self.initial_obs[0], self.initial_obs[2], initial_info["gate_dimensions"]["tall"]["height"])] # Height is hardcoded scenario knowledge.
else:
waypoints = [(self.initial_obs[0], self.initial_obs[2], self.initial_obs[4])]
for idx, g in enumerate(self.NOMINAL_GATES):
height = initial_info["gate_dimensions"]["tall"]["height"] if g[6] == 0 else initial_info["gate_dimensions"]["low"]["height"]
if g[5] > 0.75 or g[5] < 0:
if idx == 2: # Hardcoded scenario knowledge (direction in which to take gate 2).
waypoints.append((g[0]+0.3, g[1]-0.3, height))
waypoints.append((g[0]-0.3, g[1]-0.3, height))
else:
waypoints.append((g[0]-0.3, g[1], height))
waypoints.append((g[0]+0.3, g[1], height))
else:
if idx == 3: # Hardcoded scenario knowledge (correct how to take gate 3).
waypoints.append((g[0]+0.1, g[1]-0.3, height))
waypoints.append((g[0]+0.1, g[1]+0.3, height))
else:
waypoints.append((g[0], g[1]-0.3, height))
waypoints.append((g[0], g[1]+0.3, height))
waypoints.append([initial_info["x_reference"][0], initial_info["x_reference"][2], initial_info["x_reference"][4]])
# Polynomial fit.
self.waypoints = np.array(waypoints)
deg = 6
t = np.arange(self.waypoints.shape[0])
fx = np.poly1d(np.polyfit(t, self.waypoints[:,0], deg))
fy = np.poly1d(np.polyfit(t, self.waypoints[:,1], deg))
fz = np.poly1d(np.polyfit(t, self.waypoints[:,2], deg))
duration = 15
t_scaled = np.linspace(t[0], t[-1], int(duration*self.CTRL_FREQ))
self.ref_x = fx(t_scaled)
self.ref_y = fy(t_scaled)
self.ref_z = fz(t_scaled)
if self.VERBOSE:
# Plot trajectory in each dimension and 3D.
plot_trajectory(t_scaled, self.waypoints, self.ref_x, self.ref_y, self.ref_z)
# Draw the trajectory on PyBullet's GUI.
draw_trajectory(initial_info, self.waypoints, self.ref_x, self.ref_y, self.ref_z)
#########################
# REPLACE THIS (END) ####
#########################
def cmdFirmware(self,
time,
obs,
reward=None,
done=None,
info=None
):
"""Pick command sent to the quadrotor through a Crazyswarm/Crazyradio-like interface.
INSTRUCTIONS:
Re-implement this method to return the target position, velocity, acceleration, attitude, and attitude rates to be sent
from Crazyswarm to the Crazyflie using, e.g., a `cmdFullState` call.
Args:
time (float): Episode's elapsed time, in seconds.
obs (ndarray): The quadrotor's Vicon data [x, 0, y, 0, z, 0, phi, theta, psi, 0, 0, 0].
reward (float, optional): The reward signal.
done (bool, optional): Wether the episode has terminated.
info (dict, optional): Current step information as a dictionary with keys
'constraint_violation', 'current_target_gate_pos', etc.
Returns:
Command: selected type of command (takeOff, cmdFullState, etc., see Enum-like class `Command`).
List: arguments for the type of command (see comments in class `Command`)
"""
if self.ctrl is not None:
raise RuntimeError("[ERROR] Using method 'cmdFirmware' but Controller was created with 'use_firmware' = False.")
iteration = int(time*self.CTRL_FREQ)
#########################
# REPLACE THIS (START) ##
#########################
# Handwritten solution for GitHub's getting_stated scenario.
if iteration == 0:
height = 1
duration = 2
command_type = Command(2) # Take-off.
args = [height, duration]
elif iteration >= 3*self.CTRL_FREQ and iteration < 20*self.CTRL_FREQ:
step = min(iteration-3*self.CTRL_FREQ, len(self.ref_x) -1)
target_pos = np.array([self.ref_x[step], self.ref_y[step], self.ref_z[step]])
target_vel = np.zeros(3)
target_acc = np.zeros(3)
target_yaw = 0.
target_rpy_rates = np.zeros(3)
command_type = Command(1) # cmdFullState.
args = [target_pos, target_vel, target_acc, target_yaw, target_rpy_rates]
elif iteration == 20*self.CTRL_FREQ:
command_type = Command(6) # Notify setpoint stop.
args = []
elif iteration == 20*self.CTRL_FREQ+1:
x = self.ref_x[-1]
y = self.ref_y[-1]
z = 1.5
yaw = 0.
duration = 2.5
command_type = Command(5) # goTo.
args = [[x, y, z], yaw, duration, False]
elif iteration == 23*self.CTRL_FREQ:
x = self.initial_obs[0]
y = self.initial_obs[2]
z = 1.5
yaw = 0.
duration = 6
command_type = Command(5) # goTo.
args = [[x, y, z], yaw, duration, False]
elif iteration == 30*self.CTRL_FREQ:
height = 0.
duration = 3
command_type = Command(3) # Land.
args = [height, duration]
elif iteration == 33*self.CTRL_FREQ-1:
command_type = Command(-1) # Terminate command to be sent once the trajectory is completed.
args = []
else:
command_type = Command(0) # None.
args = []
#########################
# REPLACE THIS (END) ####
#########################
return command_type, args
def cmdSimOnly(self,
time,
obs,
reward=None,
done=None,
info=None
):
"""PID per-propeller thrusts with a simplified, software-only PID quadrotor controller.
INSTRUCTIONS:
You do NOT need to re-implement this method for the IROS 2022 Safe Robot Learning competition.
Only re-implement this method when `use_firmware` == False to return the target position and velocity.
Args:
time (float): Episode's elapsed time, in seconds.
obs (ndarray): The quadrotor's state [x, x_dot, y, y_dot, z, z_dot, phi, theta, psi, p, q, r].
reward (float, optional): The reward signal.
done (bool, optional): Wether the episode has terminated.
info (dict, optional): Current step information as a dictionary with keys
'constraint_violation', 'current_target_gate_pos', etc.
Returns:
List: target position (len == 3).
List: target velocity (len == 3).
"""
if self.ctrl is None:
raise RuntimeError("[ERROR] Attempting to use method 'cmdSimOnly' but Controller was created with 'use_firmware' = True.")
iteration = int(time*self.CTRL_FREQ)
#########################
if iteration < len(self.ref_x):
target_p = np.array([self.ref_x[iteration], self.ref_y[iteration], self.ref_z[iteration]])
else:
target_p = np.array([self.ref_x[-1], self.ref_y[-1], self.ref_z[-1]])
target_v = np.zeros(3)
#########################
return target_p, target_v
@timing_step
def interStepLearn(self,
action,
obs,
reward,
done,
info):
"""Learning and controller updates called between control steps.
INSTRUCTIONS:
Use the historically collected information in the five data buffers of actions, observations,
rewards, done flags, and information dictionaries to learn, adapt, and/or re-plan.
Args:
action (List): Most recent applied action.
obs (List): Most recent observation of the quadrotor state.
reward (float): Most recent reward.
done (bool): Most recent done flag.
info (dict): Most recent information dictionary.
"""
self.interstep_counter += 1
# Store the last step's events.
self.action_buffer.append(action)
self.obs_buffer.append(obs)
self.reward_buffer.append(reward)
self.done_buffer.append(done)
self.info_buffer.append(info)
#########################
# REPLACE THIS (START) ##
#########################
pass
#########################
# REPLACE THIS (END) ####
#########################
@timing_ep
def interEpisodeLearn(self):
"""Learning and controller updates called between episodes.
INSTRUCTIONS:
Use the historically collected information in the five data buffers of actions, observations,
rewards, done flags, and information dictionaries to learn, adapt, and/or re-plan.
"""
self.interepisode_counter += 1
#########################
# REPLACE THIS (START) ##
#########################
_ = self.action_buffer
_ = self.obs_buffer
_ = self.reward_buffer
_ = self.done_buffer
_ = self.info_buffer
#########################
# REPLACE THIS (END) ####
#########################
def reset(self):
"""Initialize/reset data buffers and counters.
Called once in __init__().
"""
# Data buffers.
self.action_buffer = deque([], maxlen=self.BUFFER_SIZE)
self.obs_buffer = deque([], maxlen=self.BUFFER_SIZE)
self.reward_buffer = deque([], maxlen=self.BUFFER_SIZE)
self.done_buffer = deque([], maxlen=self.BUFFER_SIZE)
self.info_buffer = deque([], maxlen=self.BUFFER_SIZE)
# Counters.
self.interstep_counter = 0
self.interepisode_counter = 0
def interEpisodeReset(self):
"""Initialize/reset learning timing variables.
Called between episodes in `getting_started.py`.
"""
# Timing stats variables.
self.interstep_learning_time = 0
self.interstep_learning_occurrences = 0
self.interepisode_learning_time = 0