-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathenv_cuda.py
More file actions
329 lines (293 loc) · 15.8 KB
/
Copy pathenv_cuda.py
File metadata and controls
329 lines (293 loc) · 15.8 KB
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
import math
import random
import time
import torch
import torch.nn.functional as F
import quadsim_cuda
class GDecay(torch.autograd.Function):
@staticmethod
def forward(ctx, x, alpha):
ctx.alpha = alpha
return x
@staticmethod
def backward(ctx, grad_output):
return grad_output * ctx.alpha, None
g_decay = GDecay.apply
class RunFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, R, dg, z_drag_coef, drag_2, pitch_ctl_delay, act_pred, act, p, v, v_wind, a, grad_decay, ctl_dt, airmode):
act_next, p_next, v_next, a_next = quadsim_cuda.run_forward(
R, dg, z_drag_coef, drag_2, pitch_ctl_delay, act_pred, act, p, v, v_wind, a, ctl_dt, airmode)
ctx.save_for_backward(R, dg, z_drag_coef, drag_2, pitch_ctl_delay, v, v_wind, act_next)
ctx.grad_decay = grad_decay
ctx.ctl_dt = ctl_dt
return act_next, p_next, v_next, a_next
@staticmethod
def backward(ctx, d_act_next, d_p_next, d_v_next, d_a_next):
R, dg, z_drag_coef, drag_2, pitch_ctl_delay, v, v_wind, act_next = ctx.saved_tensors
d_act_pred, d_act, d_p, d_v, d_a = quadsim_cuda.run_backward(
R, dg, z_drag_coef, drag_2, pitch_ctl_delay, v, v_wind, act_next, d_act_next, d_p_next, d_v_next, d_a_next,
ctx.grad_decay, ctx.ctl_dt)
return None, None, None, None, None, d_act_pred, d_act, d_p, d_v, None, d_a, None, None, None
run = RunFunction.apply
class Env:
def __init__(self, batch_size, width, height, grad_decay, device='cpu', fov_x_half_tan=0.53,
single=False, gate=False, ground_voxels=False, scaffold=False, speed_mtp=1,
random_rotation=False, cam_angle=10) -> None:
self.device = device
self.batch_size = batch_size
self.width = width
self.height = height
self.grad_decay = grad_decay
self.ball_w = torch.tensor([8., 18, 6, 0.2], device=device)
self.ball_b = torch.tensor([0., -9, -1, 0.4], device=device)
self.voxel_w = torch.tensor([8., 18, 6, 0.1, 0.1, 0.1], device=device)
self.voxel_b = torch.tensor([0., -9, -1, 0.2, 0.2, 0.2], device=device)
self.ground_voxel_w = torch.tensor([8., 18, 0, 2.9, 2.9, 1.9], device=device)
self.ground_voxel_b = torch.tensor([0., -9, -1, 0.1, 0.1, 0.1], device=device)
self.cyl_w = torch.tensor([8., 18, 0.35], device=device)
self.cyl_b = torch.tensor([0., -9, 0.05], device=device)
self.cyl_h_w = torch.tensor([8., 6, 0.1], device=device)
self.cyl_h_b = torch.tensor([0., 0, 0.05], device=device)
self.gate_w = torch.tensor([2., 2, 1.0, 0.5], device=device)
self.gate_b = torch.tensor([3., -1, 0.0, 0.5], device=device)
self.v_wind_w = torch.tensor([1, 1, 0.2], device=device)
self.g_std = torch.tensor([0., 0, -9.80665], device=device)
self.roof_add = torch.tensor([0., 0., 2.5, 1.5, 1.5, 1.5], device=device)
self.sub_div = torch.linspace(0, 1. / 15, 10, device=device).reshape(-1, 1, 1)
self.p_init = torch.as_tensor([
[-1.5, -3., 1],
[ 9.5, -3., 1],
[-0.5, 1., 1],
[ 8.5, 1., 1],
[ 0.0, 3., 1],
[ 8.0, 3., 1],
[-1.0, -1., 1],
[ 9.0, -1., 1],
], device=device).repeat(batch_size // 8 + 7, 1)[:batch_size]
self.p_end = torch.as_tensor([
[8., 3., 1],
[0., 3., 1],
[8., -1., 1],
[0., -1., 1],
[8., -3., 1],
[0., -3., 1],
[8., 1., 1],
[0., 1., 1],
], device=device).repeat(batch_size // 8 + 7, 1)[:batch_size]
self.flow = torch.empty((batch_size, 0, height, width), device=device)
self.single = single
self.gate = gate
self.ground_voxels = ground_voxels
self.scaffold = scaffold
self.speed_mtp = speed_mtp
self.random_rotation = random_rotation
self.cam_angle = cam_angle
self.fov_x_half_tan = fov_x_half_tan
self.reset()
# self.obj_avoid_grad_mtp = torch.tensor([0.5, 2., 1.], device=device)
def reset(self):
B = self.batch_size
device = self.device
cam_angle = (self.cam_angle + torch.randn(B, device=device)) * math.pi / 180
zeros = torch.zeros_like(cam_angle)
ones = torch.ones_like(cam_angle)
self.R_cam = torch.stack([
torch.cos(cam_angle), zeros, -torch.sin(cam_angle),
zeros, ones, zeros,
torch.sin(cam_angle), zeros, torch.cos(cam_angle),
], -1).reshape(B, 3, 3)
# env
self.balls = torch.rand((B, 30, 4), device=device) * self.ball_w + self.ball_b
self.voxels = torch.rand((B, 30, 6), device=device) * self.voxel_w + self.voxel_b
self.cyl = torch.rand((B, 30, 3), device=device) * self.cyl_w + self.cyl_b
self.cyl_h = torch.rand((B, 2, 3), device=device) * self.cyl_h_w + self.cyl_h_b
self._fov_x_half_tan = (0.95 + 0.1 * random.random()) * self.fov_x_half_tan
self.n_drones_per_group = random.choice([4, 8])
self.drone_radius = random.uniform(0.1, 0.15)
if self.single:
self.n_drones_per_group = 1
rd = torch.rand((B // self.n_drones_per_group, 1), device=device).repeat_interleave(self.n_drones_per_group, 0)
self.max_speed = (0.75 + 2.5 * rd) * self.speed_mtp
scale = (self.max_speed - 0.5).clamp_min(1)
self.thr_est_error = 1 + torch.randn(B, device=device) * 0.01
roof = torch.rand((B,)) < 0.5
self.balls[~roof, :15, :2] = self.cyl[~roof, :15, :2]
self.voxels[~roof, :15, :2] = self.cyl[~roof, 15:, :2]
self.balls[~roof, :15] = self.balls[~roof, :15] + self.roof_add[:4]
self.voxels[~roof, :15] = self.voxels[~roof, :15] + self.roof_add
self.balls[..., 0] = torch.minimum(torch.maximum(self.balls[..., 0], self.balls[..., 3] + 0.3 / scale), 8 - 0.3 / scale - self.balls[..., 3])
self.voxels[..., 0] = torch.minimum(torch.maximum(self.voxels[..., 0], self.voxels[..., 3] + 0.3 / scale), 8 - 0.3 / scale - self.voxels[..., 3])
self.cyl[..., 0] = torch.minimum(torch.maximum(self.cyl[..., 0], self.cyl[..., 2] + 0.3 / scale), 8 - 0.3 / scale - self.cyl[..., 2])
self.cyl_h[..., 0] = torch.minimum(torch.maximum(self.cyl_h[..., 0], self.cyl_h[..., 2] + 0.3 / scale), 8 - 0.3 / scale - self.cyl_h[..., 2])
self.voxels[roof, 0, 2] = self.voxels[roof, 0, 2] * 0.5 + 201
self.voxels[roof, 0, 3:] = 200
if self.ground_voxels:
ground_balls_r = 8 + torch.rand((B, 2), device=device) * 6
ground_balls_r_ground = 2 + torch.rand((B, 2), device=device) * 4
ground_balls_h = ground_balls_r - (ground_balls_r.pow(2) - ground_balls_r_ground.pow(2)).sqrt()
# | ground_balls_h
# ----- ground_balls_r_ground
# | /
# | / ground_balls_r
# |/
self.balls[:, :2, 3] = ground_balls_r
self.balls[:, :2, 2] = ground_balls_h - ground_balls_r - 1
# planner shape in (0.1-2.0) times (0.1-2.0)
ground_voxels = torch.rand((B, 10, 6), device=device) * self.ground_voxel_w + self.ground_voxel_b
ground_voxels[:, :, 2] = ground_voxels[:, :, 5] - 1
self.voxels = torch.cat([self.voxels, ground_voxels], 1)
self.voxels[:, :, 1] *= (self.max_speed + 4) / scale
self.balls[:, :, 1] *= (self.max_speed + 4) / scale
self.cyl[:, :, 1] *= (self.max_speed + 4) / scale
# gates
if self.gate:
gate = torch.rand((B, 4), device=device) * self.gate_w + self.gate_b
p = gate[None, :, :3]
nearest_pt = torch.empty_like(p)
quadsim_cuda.find_nearest_pt(nearest_pt, self.balls, self.cyl, self.cyl_h, self.voxels, p, self.drone_radius, 1)
gate_x, gate_y, gate_z, gate_r = gate.unbind(-1)
gate_x[(nearest_pt - p).norm(2, -1)[0] < 0.5] = -50
ones = torch.ones_like(gate_x)
gate = torch.stack([
torch.stack([gate_x, gate_y + gate_r + 5, gate_z, ones * 0.05, ones * 5, ones * 5], -1),
torch.stack([gate_x, gate_y, gate_z + gate_r + 5, ones * 0.05, ones * 5, ones * 5], -1),
torch.stack([gate_x, gate_y - gate_r - 5, gate_z, ones * 0.05, ones * 5, ones * 5], -1),
torch.stack([gate_x, gate_y, gate_z - gate_r - 5, ones * 0.05, ones * 5, ones * 5], -1),
], 1)
self.voxels = torch.cat([self.voxels, gate], 1)
self.voxels[..., 0] *= scale
self.balls[..., 0] *= scale
self.cyl[..., 0] *= scale
self.cyl_h[..., 0] *= scale
if self.ground_voxels:
self.balls[:, :2, 0] = torch.minimum(torch.maximum(self.balls[:, :2, 0], ground_balls_r_ground + 0.3), scale * 8 - 0.3 - ground_balls_r_ground)
# drone
self.pitch_ctl_delay = 12 + 1.2 * torch.randn((B, 1), device=device)
self.yaw_ctl_delay = 6 + 0.6 * torch.randn((B, 1), device=device)
rd = torch.rand((B // self.n_drones_per_group, 1), device=device).repeat_interleave(self.n_drones_per_group, 0)
scale = torch.cat([
scale,
rd + 0.5,
torch.rand_like(scale) - 0.5], -1)
self.p = self.p_init * scale + torch.randn_like(scale) * 0.1
self.p_target = self.p_end * scale + torch.randn_like(scale) * 0.1
if self.random_rotation:
yaw_bias = torch.rand(B//self.n_drones_per_group, device=device).repeat_interleave(self.n_drones_per_group, 0) * 1.5 - 0.75
c = torch.cos(yaw_bias)
s = torch.sin(yaw_bias)
l = torch.ones_like(yaw_bias)
o = torch.zeros_like(yaw_bias)
R = torch.stack([c,-s, o, s, c, o, o, o, l], -1).reshape(B, 3, 3)
self.p = torch.squeeze(R @ self.p[..., None], -1)
self.p_target = torch.squeeze(R @ self.p_target[..., None], -1)
self.voxels[..., :3] = (R @ self.voxels[..., :3].transpose(1, 2)).transpose(1, 2)
self.balls[..., :3] = (R @ self.balls[..., :3].transpose(1, 2)).transpose(1, 2)
self.cyl[..., :3] = (R @ self.cyl[..., :3].transpose(1, 2)).transpose(1, 2)
# scaffold
if self.scaffold and random.random() < 0.5:
x = torch.arange(1, 6, dtype=torch.float, device=device)
y = torch.arange(-3, 4, dtype=torch.float, device=device)
z = torch.arange(1, 4, dtype=torch.float, device=device)
_x, _y = torch.meshgrid(x, y)
# + torch.rand_like(self.max_speed) * self.max_speed
# + torch.randn_like(self.max_speed)
scaf_v = torch.stack([_x, _y, torch.full_like(_x, 0.02)], -1).flatten(0, 1)
x_bias = torch.rand_like(self.max_speed) * self.max_speed
scale = 1 + torch.rand((B, 1, 1), device=device)
scaf_v = scaf_v * scale + torch.stack([
x_bias,
torch.randn_like(self.max_speed),
torch.rand_like(self.max_speed) * 0.01
], -1)
self.cyl = torch.cat([self.cyl, scaf_v], 1)
_x, _z = torch.meshgrid(x, z)
scaf_h = torch.stack([_x, _z, torch.full_like(_x, 0.02)], -1).flatten(0, 1)
scaf_h = scaf_h * scale + torch.stack([
x_bias,
torch.randn_like(self.max_speed) * 0.1,
torch.rand_like(self.max_speed) * 0.01
], -1)
self.cyl_h = torch.cat([self.cyl_h, scaf_h], 1)
self.v = torch.randn((B, 3), device=device) * 0.2
self.v_wind = torch.randn((B, 3), device=device) * self.v_wind_w
self.act = torch.randn_like(self.v) * 0.1
self.a = self.act
self.dg = torch.randn((B, 3), device=device) * 0.2
R = torch.zeros((B, 3, 3), device=device)
self.R = quadsim_cuda.update_state_vec(R, self.act, torch.randn((B, 3), device=device) * 0.2 + F.normalize(self.p_target - self.p),
torch.zeros_like(self.yaw_ctl_delay), 5)
self.R_old = self.R.clone()
self.p_old = self.p
self.margin = torch.rand((B,), device=device) * 0.2 + 0.1
# drag coef
self.drag_2 = torch.rand((B, 2), device=device) * 0.15 + 0.3
self.drag_2[:, 0] = 0
self.z_drag_coef = torch.ones((B, 1), device=device)
@staticmethod
@torch.no_grad()
def update_state_vec(R, a_thr, v_pred, alpha, yaw_inertia=5):
self_forward_vec = R[..., 0]
g_std = torch.tensor([0, 0, -9.80665], device=R.device)
a_thr = a_thr - g_std
thrust = torch.norm(a_thr, 2, -1, True)
self_up_vec = a_thr / thrust
forward_vec = self_forward_vec * yaw_inertia + v_pred
forward_vec = self_forward_vec * alpha + F.normalize(forward_vec, 2, -1) * (1 - alpha)
forward_vec[:, 2] = (forward_vec[:, 0] * self_up_vec[:, 0] + forward_vec[:, 1] * self_up_vec[:, 1]) / -self_up_vec[2]
self_forward_vec = F.normalize(forward_vec, 2, -1)
self_left_vec = torch.cross(self_up_vec, self_forward_vec)
return torch.stack([
self_forward_vec,
self_left_vec,
self_up_vec,
], -1)
def render(self, ctl_dt):
canvas = torch.empty((self.batch_size, self.height, self.width), device=self.device)
# assert canvas.is_contiguous()
# assert nearest_pt.is_contiguous()
# assert self.balls.is_contiguous()
# assert self.cyl.is_contiguous()
# assert self.voxels.is_contiguous()
# assert Rt.is_contiguous()
quadsim_cuda.render(canvas, self.flow, self.balls, self.cyl, self.cyl_h,
self.voxels, self.R @ self.R_cam, self.R_old, self.p,
self.p_old, self.drone_radius, self.n_drones_per_group,
self._fov_x_half_tan)
return canvas, None
def find_vec_to_nearest_pt(self):
p = self.p + self.v * self.sub_div
nearest_pt = torch.empty_like(p)
quadsim_cuda.find_nearest_pt(nearest_pt, self.balls, self.cyl, self.cyl_h, self.voxels, p, self.drone_radius, self.n_drones_per_group)
return nearest_pt - p
def run(self, act_pred, ctl_dt=1/15, v_pred=None):
self.dg = self.dg * math.sqrt(1 - ctl_dt / 4) + torch.randn_like(self.dg) * 0.2 * math.sqrt(ctl_dt / 4)
self.p_old = self.p
self.act, self.p, self.v, self.a = run(
self.R, self.dg, self.z_drag_coef, self.drag_2, self.pitch_ctl_delay,
act_pred, self.act, self.p, self.v, self.v_wind, self.a,
self.grad_decay, ctl_dt, 0.5)
# update attitude
alpha = torch.exp(-self.yaw_ctl_delay * ctl_dt)
self.R_old = self.R.clone()
self.R = quadsim_cuda.update_state_vec(self.R, self.act, v_pred, alpha, 5)
def _run(self, act_pred, ctl_dt=1/15, v_pred=None):
alpha = torch.exp(-self.pitch_ctl_delay * ctl_dt)
self.act = act_pred * (1 - alpha) + self.act * alpha
self.dg = self.dg * math.sqrt(1 - ctl_dt) + torch.randn_like(self.dg) * 0.2 * math.sqrt(ctl_dt)
z_drag = 0
if self.z_drag_coef is not None:
v_up = torch.sum(self.v * self.R[..., 2], -1, keepdim=True) * self.R[..., 2]
v_prep = self.v - v_up
motor_velocity = (self.act - self.g_std).norm(2, -1, True).sqrt()
z_drag = self.z_drag_coef * v_prep * motor_velocity * 0.07
drag = self.drag_2 * self.v * self.v.norm(2, -1, True)
a_next = self.act + self.dg - z_drag - drag
self.p_old = self.p
self.p = g_decay(self.p, self.grad_decay ** ctl_dt) + self.v * ctl_dt + 0.5 * self.a * ctl_dt**2
self.v = g_decay(self.v, self.grad_decay ** ctl_dt) + (self.a + a_next) / 2 * ctl_dt
self.a = a_next
# update attitude
alpha = torch.exp(-self.yaw_ctl_delay * ctl_dt)
self.R_old = self.R.clone()
self.R = quadsim_cuda.update_state_vec(self.R, self.act, v_pred, alpha, 5)