forked from utiasDSL/lsy_drone_racing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
275 lines (233 loc) · 10.1 KB
/
controller.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
"""Write your control strategy.
Then run:
$ python scripts/sim --config config/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) compute_control
3) step_learn (optional)
4) episode_learn (optional)
"""
from __future__ import annotations # Python 3.10 type hints
import numpy as np
from scipy import interpolate
from lsy_drone_racing.command import Command
from lsy_drone_racing.controller import BaseController
from lsy_drone_racing.utils import draw_trajectory
class Controller(BaseController):
"""Template controller class."""
def __init__(
self,
initial_obs: np.ndarray,
initial_info: dict,
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: 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: The a priori information as a dictionary with keys 'symbolic_model',
'nominal_physical_parameters', 'nominal_gates_pos_and_type', etc.
buffer_size: Size of the data buffers used in method `learn()`.
verbose: Turn on and off additional printouts and plots.
"""
super().__init__(initial_obs, initial_info, buffer_size, verbose)
# 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"]
# Reset counters and buffers.
self.reset()
self.episode_reset()
#########################
# REPLACE THIS (START) ##
#########################
# Example: Hard-code waypoints through the gates. Obviously this is a crude way of
# completing the challenge that is highly susceptible to noise and does not generalize at
# all. It is meant solely as an example on how the drones can be controlled
waypoints = []
waypoints.append([self.initial_obs[0], self.initial_obs[2], 0.3])
gates = self.NOMINAL_GATES
z_low = initial_info["gate_dimensions"]["low"]["height"]
z_high = initial_info["gate_dimensions"]["tall"]["height"]
waypoints.append([1, 0, z_low])
waypoints.append([gates[0][0] + 0.2, gates[0][1] + 0.1, z_low])
waypoints.append([gates[0][0] + 0.1, gates[0][1], z_low])
waypoints.append([gates[0][0] - 0.1, gates[0][1], z_low])
waypoints.append(
[
(gates[0][0] + gates[1][0]) / 2 - 0.7,
(gates[0][1] + gates[1][1]) / 2 - 0.3,
(z_low + z_high) / 2,
]
)
waypoints.append(
[
(gates[0][0] + gates[1][0]) / 2 - 0.5,
(gates[0][1] + gates[1][1]) / 2 - 0.6,
(z_low + z_high) / 2,
]
)
waypoints.append([gates[1][0] - 0.3, gates[1][1] - 0.2, z_high])
waypoints.append([gates[1][0] + 0.2, gates[1][1] + 0.2, z_high])
waypoints.append([gates[2][0], gates[2][1] - 0.4, z_low])
waypoints.append([gates[2][0], gates[2][1] + 0.1, z_low])
waypoints.append([gates[2][0], gates[2][1] + 0.1, z_high + 0.2])
waypoints.append([gates[3][0], gates[3][1] + 0.1, z_high])
waypoints.append([gates[3][0], gates[3][1] - 0.1, z_high + 0.1])
waypoints.append(
[
initial_info["x_reference"][0],
initial_info["x_reference"][2],
initial_info["x_reference"][4],
]
)
waypoints = np.array(waypoints)
tck, u = interpolate.splprep([waypoints[:, 0], waypoints[:, 1], waypoints[:, 2]], s=0.1)
self.waypoints = waypoints
duration = 12
t = np.linspace(0, 1, int(duration * self.CTRL_FREQ))
self.ref_x, self.ref_y, self.ref_z = interpolate.splev(t, tck)
assert max(self.ref_z) < 2.5, "Drone must stay below the ceiling"
if self.VERBOSE:
# Draw the trajectory on PyBullet's GUI.
draw_trajectory(initial_info, self.waypoints, self.ref_x, self.ref_y, self.ref_z)
self._take_off = False
self._setpoint_land = False
self._land = False
#########################
# REPLACE THIS (END) ####
#########################
def compute_control(
self,
ep_time: float,
obs: np.ndarray,
reward: float | None = None,
done: bool | None = None,
info: dict | None = None,
) -> tuple[Command, list]:
"""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:
ep_time: Episode's elapsed time, in seconds.
obs: The quadrotor's Vicon data [x, 0, y, 0, z, 0, phi, theta, psi, 0, 0, 0].
reward: The reward signal.
done: Wether the episode has terminated.
info: Current step information as a dictionary with keys 'constraint_violation',
'current_target_gate_pos', etc.
Returns:
The command type and arguments to be sent to the quadrotor. See `Command`.
"""
iteration = int(ep_time * self.CTRL_FREQ)
#########################
# REPLACE THIS (START) ##
#########################
# Handcrafted solution for getting_stated scenario.
if not self._take_off:
command_type = Command.TAKEOFF
args = [0.3, 2] # Height, duration
self._take_off = True # Only send takeoff command once
else:
step = iteration - 2 * self.CTRL_FREQ # Account for 2s delay due to takeoff
if ep_time - 2 > 0 and step < len(self.ref_x):
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.0
target_rpy_rates = np.zeros(3)
command_type = Command.FULLSTATE
args = [target_pos, target_vel, target_acc, target_yaw, target_rpy_rates, ep_time]
# Notify set point stop has to be called every time we transition from low-level
# commands to high-level ones. Prepares for landing
elif step >= len(self.ref_x) and not self._setpoint_land:
command_type = Command.NOTIFYSETPOINTSTOP
args = []
self._setpoint_land = True
elif step >= len(self.ref_x) and not self._land:
command_type = Command.LAND
args = [0.0, 2.0] # Height, duration
self._land = True # Send landing command only once
elif self._land:
command_type = Command.FINISHED
args = []
else:
command_type = Command.NONE
args = []
#########################
# REPLACE THIS (END) ####
#########################
return command_type, args
def step_learn(
self,
action: list,
obs: np.ndarray,
reward: float | None = None,
done: bool | None = None,
info: dict | None = None,
):
"""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: Most recent applied action.
obs: Most recent observation of the quadrotor state.
reward: Most recent reward.
done: Most recent done flag.
info: Most recent information dictionary.
"""
#########################
# REPLACE THIS (START) ##
#########################
# 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)
# Implement some learning algorithm here if needed
#########################
# REPLACE THIS (END) ####
#########################
def episode_learn(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.
"""
#########################
# REPLACE THIS (START) ##
#########################
_ = self.action_buffer
_ = self.obs_buffer
_ = self.reward_buffer
_ = self.done_buffer
_ = self.info_buffer
#########################
# REPLACE THIS (END) ####
#########################