warp-lang 1.0.2__py3-none-manylinux2014_x86_64.whl → 1.2.0__py3-none-manylinux2014_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of warp-lang might be problematic. Click here for more details.
- warp/__init__.py +108 -97
- warp/__init__.pyi +1 -1
- warp/bin/warp-clang.so +0 -0
- warp/bin/warp.so +0 -0
- warp/build.py +88 -113
- warp/build_dll.py +383 -375
- warp/builtins.py +3693 -3354
- warp/codegen.py +2925 -2792
- warp/config.py +40 -36
- warp/constants.py +49 -45
- warp/context.py +5409 -5102
- warp/dlpack.py +442 -442
- warp/examples/__init__.py +16 -16
- warp/examples/assets/bear.usd +0 -0
- warp/examples/assets/bunny.usd +0 -0
- warp/examples/assets/cartpole.urdf +110 -110
- warp/examples/assets/crazyflie.usd +0 -0
- warp/examples/assets/cube.usd +0 -0
- warp/examples/assets/nv_ant.xml +92 -92
- warp/examples/assets/nv_humanoid.xml +183 -183
- warp/examples/assets/quadruped.urdf +267 -267
- warp/examples/assets/rocks.nvdb +0 -0
- warp/examples/assets/rocks.usd +0 -0
- warp/examples/assets/sphere.usd +0 -0
- warp/examples/benchmarks/benchmark_api.py +381 -383
- warp/examples/benchmarks/benchmark_cloth.py +278 -277
- warp/examples/benchmarks/benchmark_cloth_cupy.py +88 -88
- warp/examples/benchmarks/benchmark_cloth_jax.py +97 -100
- warp/examples/benchmarks/benchmark_cloth_numba.py +146 -142
- warp/examples/benchmarks/benchmark_cloth_numpy.py +77 -77
- warp/examples/benchmarks/benchmark_cloth_pytorch.py +86 -86
- warp/examples/benchmarks/benchmark_cloth_taichi.py +112 -112
- warp/examples/benchmarks/benchmark_cloth_warp.py +145 -146
- warp/examples/benchmarks/benchmark_launches.py +293 -295
- warp/examples/browse.py +29 -29
- warp/examples/core/example_dem.py +232 -219
- warp/examples/core/example_fluid.py +291 -267
- warp/examples/core/example_graph_capture.py +142 -126
- warp/examples/core/example_marching_cubes.py +186 -174
- warp/examples/core/example_mesh.py +172 -155
- warp/examples/core/example_mesh_intersect.py +203 -193
- warp/examples/core/example_nvdb.py +174 -170
- warp/examples/core/example_raycast.py +103 -90
- warp/examples/core/example_raymarch.py +197 -178
- warp/examples/core/example_render_opengl.py +183 -141
- warp/examples/core/example_sph.py +403 -387
- warp/examples/core/example_torch.py +219 -181
- warp/examples/core/example_wave.py +261 -248
- warp/examples/fem/bsr_utils.py +378 -380
- warp/examples/fem/example_apic_fluid.py +432 -389
- warp/examples/fem/example_burgers.py +262 -0
- warp/examples/fem/example_convection_diffusion.py +180 -168
- warp/examples/fem/example_convection_diffusion_dg.py +217 -209
- warp/examples/fem/example_deformed_geometry.py +175 -159
- warp/examples/fem/example_diffusion.py +199 -173
- warp/examples/fem/example_diffusion_3d.py +178 -152
- warp/examples/fem/example_diffusion_mgpu.py +219 -214
- warp/examples/fem/example_mixed_elasticity.py +242 -222
- warp/examples/fem/example_navier_stokes.py +257 -243
- warp/examples/fem/example_stokes.py +218 -192
- warp/examples/fem/example_stokes_transfer.py +263 -249
- warp/examples/fem/mesh_utils.py +133 -109
- warp/examples/fem/plot_utils.py +292 -287
- warp/examples/optim/example_bounce.py +258 -246
- warp/examples/optim/example_cloth_throw.py +220 -209
- warp/examples/optim/example_diffray.py +564 -536
- warp/examples/optim/example_drone.py +862 -835
- warp/examples/optim/example_inverse_kinematics.py +174 -168
- warp/examples/optim/example_inverse_kinematics_torch.py +183 -169
- warp/examples/optim/example_spring_cage.py +237 -231
- warp/examples/optim/example_trajectory.py +221 -199
- warp/examples/optim/example_walker.py +304 -293
- warp/examples/sim/example_cartpole.py +137 -129
- warp/examples/sim/example_cloth.py +194 -186
- warp/examples/sim/example_granular.py +122 -111
- warp/examples/sim/example_granular_collision_sdf.py +195 -186
- warp/examples/sim/example_jacobian_ik.py +234 -214
- warp/examples/sim/example_particle_chain.py +116 -105
- warp/examples/sim/example_quadruped.py +191 -180
- warp/examples/sim/example_rigid_chain.py +195 -187
- warp/examples/sim/example_rigid_contact.py +187 -177
- warp/examples/sim/example_rigid_force.py +125 -125
- warp/examples/sim/example_rigid_gyroscopic.py +107 -95
- warp/examples/sim/example_rigid_soft_contact.py +132 -122
- warp/examples/sim/example_soft_body.py +188 -177
- warp/fabric.py +337 -335
- warp/fem/__init__.py +61 -27
- warp/fem/cache.py +403 -388
- warp/fem/dirichlet.py +178 -179
- warp/fem/domain.py +262 -263
- warp/fem/field/__init__.py +100 -101
- warp/fem/field/field.py +148 -149
- warp/fem/field/nodal_field.py +298 -299
- warp/fem/field/restriction.py +22 -21
- warp/fem/field/test.py +180 -181
- warp/fem/field/trial.py +183 -183
- warp/fem/geometry/__init__.py +16 -19
- warp/fem/geometry/closest_point.py +69 -70
- warp/fem/geometry/deformed_geometry.py +270 -271
- warp/fem/geometry/element.py +748 -744
- warp/fem/geometry/geometry.py +184 -186
- warp/fem/geometry/grid_2d.py +380 -373
- warp/fem/geometry/grid_3d.py +437 -435
- warp/fem/geometry/hexmesh.py +953 -953
- warp/fem/geometry/nanogrid.py +455 -0
- warp/fem/geometry/partition.py +374 -376
- warp/fem/geometry/quadmesh_2d.py +532 -532
- warp/fem/geometry/tetmesh.py +840 -840
- warp/fem/geometry/trimesh_2d.py +577 -577
- warp/fem/integrate.py +1684 -1615
- warp/fem/operator.py +190 -191
- warp/fem/polynomial.py +214 -213
- warp/fem/quadrature/__init__.py +2 -2
- warp/fem/quadrature/pic_quadrature.py +243 -245
- warp/fem/quadrature/quadrature.py +295 -294
- warp/fem/space/__init__.py +179 -292
- warp/fem/space/basis_space.py +522 -489
- warp/fem/space/collocated_function_space.py +100 -105
- warp/fem/space/dof_mapper.py +236 -236
- warp/fem/space/function_space.py +148 -145
- warp/fem/space/grid_2d_function_space.py +148 -267
- warp/fem/space/grid_3d_function_space.py +167 -306
- warp/fem/space/hexmesh_function_space.py +253 -352
- warp/fem/space/nanogrid_function_space.py +202 -0
- warp/fem/space/partition.py +350 -350
- warp/fem/space/quadmesh_2d_function_space.py +261 -369
- warp/fem/space/restriction.py +161 -160
- warp/fem/space/shape/__init__.py +90 -15
- warp/fem/space/shape/cube_shape_function.py +728 -738
- warp/fem/space/shape/shape_function.py +102 -103
- warp/fem/space/shape/square_shape_function.py +611 -611
- warp/fem/space/shape/tet_shape_function.py +565 -567
- warp/fem/space/shape/triangle_shape_function.py +429 -429
- warp/fem/space/tetmesh_function_space.py +224 -292
- warp/fem/space/topology.py +297 -295
- warp/fem/space/trimesh_2d_function_space.py +153 -221
- warp/fem/types.py +77 -77
- warp/fem/utils.py +495 -495
- warp/jax.py +166 -141
- warp/jax_experimental.py +341 -339
- warp/native/array.h +1081 -1025
- warp/native/builtin.h +1603 -1560
- warp/native/bvh.cpp +402 -398
- warp/native/bvh.cu +533 -525
- warp/native/bvh.h +430 -429
- warp/native/clang/clang.cpp +496 -464
- warp/native/crt.cpp +42 -32
- warp/native/crt.h +352 -335
- warp/native/cuda_crt.h +1049 -1049
- warp/native/cuda_util.cpp +549 -540
- warp/native/cuda_util.h +288 -203
- warp/native/cutlass_gemm.cpp +34 -34
- warp/native/cutlass_gemm.cu +372 -372
- warp/native/error.cpp +66 -66
- warp/native/error.h +27 -27
- warp/native/exports.h +187 -0
- warp/native/fabric.h +228 -228
- warp/native/hashgrid.cpp +301 -278
- warp/native/hashgrid.cu +78 -77
- warp/native/hashgrid.h +227 -227
- warp/native/initializer_array.h +32 -32
- warp/native/intersect.h +1204 -1204
- warp/native/intersect_adj.h +365 -365
- warp/native/intersect_tri.h +322 -322
- warp/native/marching.cpp +2 -2
- warp/native/marching.cu +497 -497
- warp/native/marching.h +2 -2
- warp/native/mat.h +1545 -1498
- warp/native/matnn.h +333 -333
- warp/native/mesh.cpp +203 -203
- warp/native/mesh.cu +292 -293
- warp/native/mesh.h +1887 -1887
- warp/native/nanovdb/GridHandle.h +366 -0
- warp/native/nanovdb/HostBuffer.h +590 -0
- warp/native/nanovdb/NanoVDB.h +6624 -4782
- warp/native/nanovdb/PNanoVDB.h +3390 -2553
- warp/native/noise.h +850 -850
- warp/native/quat.h +1112 -1085
- warp/native/rand.h +303 -299
- warp/native/range.h +108 -108
- warp/native/reduce.cpp +156 -156
- warp/native/reduce.cu +348 -348
- warp/native/runlength_encode.cpp +61 -61
- warp/native/runlength_encode.cu +46 -46
- warp/native/scan.cpp +30 -30
- warp/native/scan.cu +36 -36
- warp/native/scan.h +7 -7
- warp/native/solid_angle.h +442 -442
- warp/native/sort.cpp +94 -94
- warp/native/sort.cu +97 -97
- warp/native/sort.h +14 -14
- warp/native/sparse.cpp +337 -337
- warp/native/sparse.cu +544 -544
- warp/native/spatial.h +630 -630
- warp/native/svd.h +562 -562
- warp/native/temp_buffer.h +30 -30
- warp/native/vec.h +1177 -1133
- warp/native/volume.cpp +529 -297
- warp/native/volume.cu +58 -32
- warp/native/volume.h +960 -538
- warp/native/volume_builder.cu +446 -425
- warp/native/volume_builder.h +34 -19
- warp/native/volume_impl.h +61 -0
- warp/native/warp.cpp +1057 -1052
- warp/native/warp.cu +2949 -2828
- warp/native/warp.h +321 -305
- warp/optim/__init__.py +9 -9
- warp/optim/adam.py +120 -120
- warp/optim/linear.py +1104 -939
- warp/optim/sgd.py +104 -92
- warp/render/__init__.py +10 -10
- warp/render/render_opengl.py +3356 -3204
- warp/render/render_usd.py +768 -749
- warp/render/utils.py +152 -150
- warp/sim/__init__.py +52 -59
- warp/sim/articulation.py +685 -685
- warp/sim/collide.py +1594 -1590
- warp/sim/import_mjcf.py +489 -481
- warp/sim/import_snu.py +220 -221
- warp/sim/import_urdf.py +536 -516
- warp/sim/import_usd.py +887 -881
- warp/sim/inertia.py +316 -317
- warp/sim/integrator.py +234 -233
- warp/sim/integrator_euler.py +1956 -1956
- warp/sim/integrator_featherstone.py +1917 -1991
- warp/sim/integrator_xpbd.py +3288 -3312
- warp/sim/model.py +4473 -4314
- warp/sim/particles.py +113 -112
- warp/sim/render.py +417 -403
- warp/sim/utils.py +413 -410
- warp/sparse.py +1289 -1227
- warp/stubs.py +2192 -2469
- warp/tape.py +1162 -225
- warp/tests/__init__.py +1 -1
- warp/tests/__main__.py +4 -4
- warp/tests/assets/test_index_grid.nvdb +0 -0
- warp/tests/assets/torus.usda +105 -105
- warp/tests/aux_test_class_kernel.py +26 -26
- warp/tests/aux_test_compile_consts_dummy.py +10 -10
- warp/tests/aux_test_conditional_unequal_types_kernels.py +21 -21
- warp/tests/aux_test_dependent.py +20 -22
- warp/tests/aux_test_grad_customs.py +21 -23
- warp/tests/aux_test_reference.py +9 -11
- warp/tests/aux_test_reference_reference.py +8 -10
- warp/tests/aux_test_square.py +15 -17
- warp/tests/aux_test_unresolved_func.py +14 -14
- warp/tests/aux_test_unresolved_symbol.py +14 -14
- warp/tests/disabled_kinematics.py +237 -239
- warp/tests/run_coverage_serial.py +31 -31
- warp/tests/test_adam.py +155 -157
- warp/tests/test_arithmetic.py +1088 -1124
- warp/tests/test_array.py +2415 -2326
- warp/tests/test_array_reduce.py +148 -150
- warp/tests/test_async.py +666 -656
- warp/tests/test_atomic.py +139 -141
- warp/tests/test_bool.py +212 -149
- warp/tests/test_builtins_resolution.py +1290 -1292
- warp/tests/test_bvh.py +162 -171
- warp/tests/test_closest_point_edge_edge.py +227 -228
- warp/tests/test_codegen.py +562 -553
- warp/tests/test_compile_consts.py +217 -101
- warp/tests/test_conditional.py +244 -246
- warp/tests/test_copy.py +230 -215
- warp/tests/test_ctypes.py +630 -632
- warp/tests/test_dense.py +65 -67
- warp/tests/test_devices.py +89 -98
- warp/tests/test_dlpack.py +528 -529
- warp/tests/test_examples.py +403 -378
- warp/tests/test_fabricarray.py +952 -955
- warp/tests/test_fast_math.py +60 -54
- warp/tests/test_fem.py +1298 -1278
- warp/tests/test_fp16.py +128 -130
- warp/tests/test_func.py +336 -337
- warp/tests/test_generics.py +596 -571
- warp/tests/test_grad.py +885 -640
- warp/tests/test_grad_customs.py +331 -336
- warp/tests/test_hash_grid.py +208 -164
- warp/tests/test_import.py +37 -39
- warp/tests/test_indexedarray.py +1132 -1134
- warp/tests/test_intersect.py +65 -67
- warp/tests/test_jax.py +305 -307
- warp/tests/test_large.py +169 -164
- warp/tests/test_launch.py +352 -354
- warp/tests/test_lerp.py +217 -261
- warp/tests/test_linear_solvers.py +189 -171
- warp/tests/test_lvalue.py +419 -493
- warp/tests/test_marching_cubes.py +63 -65
- warp/tests/test_mat.py +1799 -1827
- warp/tests/test_mat_lite.py +113 -115
- warp/tests/test_mat_scalar_ops.py +2905 -2889
- warp/tests/test_math.py +124 -193
- warp/tests/test_matmul.py +498 -499
- warp/tests/test_matmul_lite.py +408 -410
- warp/tests/test_mempool.py +186 -190
- warp/tests/test_mesh.py +281 -324
- warp/tests/test_mesh_query_aabb.py +226 -241
- warp/tests/test_mesh_query_point.py +690 -702
- warp/tests/test_mesh_query_ray.py +290 -303
- warp/tests/test_mlp.py +274 -276
- warp/tests/test_model.py +108 -110
- warp/tests/test_module_hashing.py +111 -0
- warp/tests/test_modules_lite.py +36 -39
- warp/tests/test_multigpu.py +161 -163
- warp/tests/test_noise.py +244 -248
- warp/tests/test_operators.py +248 -250
- warp/tests/test_options.py +121 -125
- warp/tests/test_peer.py +131 -137
- warp/tests/test_pinned.py +76 -78
- warp/tests/test_print.py +52 -54
- warp/tests/test_quat.py +2084 -2086
- warp/tests/test_rand.py +324 -288
- warp/tests/test_reload.py +207 -217
- warp/tests/test_rounding.py +177 -179
- warp/tests/test_runlength_encode.py +188 -190
- warp/tests/test_sim_grad.py +241 -0
- warp/tests/test_sim_kinematics.py +89 -97
- warp/tests/test_smoothstep.py +166 -168
- warp/tests/test_snippet.py +303 -266
- warp/tests/test_sparse.py +466 -460
- warp/tests/test_spatial.py +2146 -2148
- warp/tests/test_special_values.py +362 -0
- warp/tests/test_streams.py +484 -473
- warp/tests/test_struct.py +708 -675
- warp/tests/test_tape.py +171 -148
- warp/tests/test_torch.py +741 -743
- warp/tests/test_transient_module.py +85 -87
- warp/tests/test_types.py +554 -659
- warp/tests/test_utils.py +488 -499
- warp/tests/test_vec.py +1262 -1268
- warp/tests/test_vec_lite.py +71 -73
- warp/tests/test_vec_scalar_ops.py +2097 -2099
- warp/tests/test_verify_fp.py +92 -94
- warp/tests/test_volume.py +961 -736
- warp/tests/test_volume_write.py +338 -265
- warp/tests/unittest_serial.py +38 -37
- warp/tests/unittest_suites.py +367 -359
- warp/tests/unittest_utils.py +434 -578
- warp/tests/unused_test_misc.py +69 -71
- warp/tests/walkthrough_debug.py +85 -85
- warp/thirdparty/appdirs.py +598 -598
- warp/thirdparty/dlpack.py +143 -143
- warp/thirdparty/unittest_parallel.py +563 -561
- warp/torch.py +321 -295
- warp/types.py +4941 -4450
- warp/utils.py +1008 -821
- {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/LICENSE.md +126 -126
- {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/METADATA +365 -400
- warp_lang-1.2.0.dist-info/RECORD +359 -0
- warp/examples/assets/cube.usda +0 -42
- warp/examples/assets/sphere.usda +0 -56
- warp/examples/assets/torus.usda +0 -105
- warp/examples/fem/example_convection_diffusion_dg0.py +0 -194
- warp/native/nanovdb/PNanoVDBWrite.h +0 -295
- warp_lang-1.0.2.dist-info/RECORD +0 -352
- {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/WHEEL +0 -0
- {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/top_level.txt +0 -0
|
@@ -1,835 +1,862 @@
|
|
|
1
|
-
# Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved.
|
|
2
|
-
# NVIDIA CORPORATION and its licensors retain all intellectual property
|
|
3
|
-
# and proprietary rights in and to this software, related documentation
|
|
4
|
-
# and any modifications thereto. Any use, reproduction, disclosure or
|
|
5
|
-
# distribution of this software and related documentation without an express
|
|
6
|
-
# license agreement from NVIDIA CORPORATION is strictly prohibited.
|
|
7
|
-
|
|
8
|
-
###########################################################################
|
|
9
|
-
# Example Drone
|
|
10
|
-
#
|
|
11
|
-
# A drone and its 4 propellers is simulated with the goal of reaching
|
|
12
|
-
# different targets via model-predictive control (MPC) that continuously
|
|
13
|
-
# optimizes the control trajectory.
|
|
14
|
-
#
|
|
15
|
-
###########################################################################
|
|
16
|
-
|
|
17
|
-
import os
|
|
18
|
-
from typing import Optional, Tuple
|
|
19
|
-
|
|
20
|
-
import numpy as np
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
import warp
|
|
24
|
-
import warp.
|
|
25
|
-
import warp.
|
|
26
|
-
import warp.sim
|
|
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
|
-
target
|
|
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
|
-
geo_type
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
d =
|
|
190
|
-
elif geo_type == wp.sim.
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
d
|
|
196
|
-
elif geo_type == wp.sim.
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
d =
|
|
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
|
-
torque_dim
|
|
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
|
-
prop =
|
|
298
|
-
prop.
|
|
299
|
-
prop.
|
|
300
|
-
prop.
|
|
301
|
-
prop.
|
|
302
|
-
|
|
303
|
-
prop
|
|
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
|
-
builder.add_shape_box(
|
|
348
|
-
body,
|
|
349
|
-
hx=
|
|
350
|
-
hy=crossbar_height,
|
|
351
|
-
hz=
|
|
352
|
-
density=carbon_fiber_density,
|
|
353
|
-
collision_group=i,
|
|
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
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
#
|
|
429
|
-
|
|
430
|
-
self.
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
self.
|
|
440
|
-
|
|
441
|
-
@property
|
|
442
|
-
def
|
|
443
|
-
return self.
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
#
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
#
|
|
467
|
-
self.
|
|
468
|
-
|
|
469
|
-
#
|
|
470
|
-
self.
|
|
471
|
-
|
|
472
|
-
#
|
|
473
|
-
self.
|
|
474
|
-
|
|
475
|
-
#
|
|
476
|
-
self.
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
#
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
#
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
#
|
|
492
|
-
self.
|
|
493
|
-
|
|
494
|
-
# Number of
|
|
495
|
-
self.
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
self.
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
self.
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
self.
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
)
|
|
533
|
-
|
|
534
|
-
self.
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
drone_xform
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
)
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
self.
|
|
585
|
-
self.
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
drone.
|
|
601
|
-
self.
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
self.
|
|
642
|
-
self.
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
self.
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
self.
|
|
673
|
-
self.rollouts.
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
self.
|
|
735
|
-
self.
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
)
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
1
|
+
# Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved.
|
|
2
|
+
# NVIDIA CORPORATION and its licensors retain all intellectual property
|
|
3
|
+
# and proprietary rights in and to this software, related documentation
|
|
4
|
+
# and any modifications thereto. Any use, reproduction, disclosure or
|
|
5
|
+
# distribution of this software and related documentation without an express
|
|
6
|
+
# license agreement from NVIDIA CORPORATION is strictly prohibited.
|
|
7
|
+
|
|
8
|
+
###########################################################################
|
|
9
|
+
# Example Drone
|
|
10
|
+
#
|
|
11
|
+
# A drone and its 4 propellers is simulated with the goal of reaching
|
|
12
|
+
# different targets via model-predictive control (MPC) that continuously
|
|
13
|
+
# optimizes the control trajectory.
|
|
14
|
+
#
|
|
15
|
+
###########################################################################
|
|
16
|
+
|
|
17
|
+
import os
|
|
18
|
+
from typing import Optional, Tuple
|
|
19
|
+
|
|
20
|
+
import numpy as np
|
|
21
|
+
|
|
22
|
+
import warp as wp
|
|
23
|
+
import warp.examples
|
|
24
|
+
import warp.optim
|
|
25
|
+
import warp.sim
|
|
26
|
+
import warp.sim.render
|
|
27
|
+
from warp.sim.collide import box_sdf, capsule_sdf, cone_sdf, cylinder_sdf, mesh_sdf, plane_sdf, sphere_sdf
|
|
28
|
+
|
|
29
|
+
DEFAULT_DRONE_PATH = os.path.join(warp.examples.get_asset_directory(), "crazyflie.usd") # Path to input drone asset
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@wp.struct
|
|
33
|
+
class Propeller:
|
|
34
|
+
body: int
|
|
35
|
+
pos: wp.vec3
|
|
36
|
+
dir: wp.vec3
|
|
37
|
+
thrust: float
|
|
38
|
+
power: float
|
|
39
|
+
diameter: float
|
|
40
|
+
height: float
|
|
41
|
+
max_rpm: float
|
|
42
|
+
max_thrust: float
|
|
43
|
+
max_torque: float
|
|
44
|
+
turning_direction: float
|
|
45
|
+
max_speed_square: float
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@wp.kernel
|
|
49
|
+
def increment_seed(
|
|
50
|
+
seed: wp.array(dtype=int),
|
|
51
|
+
):
|
|
52
|
+
seed[0] += 1
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@wp.kernel
|
|
56
|
+
def sample_gaussian(
|
|
57
|
+
mean_trajectory: wp.array(dtype=float, ndim=3),
|
|
58
|
+
noise_scale: float,
|
|
59
|
+
num_control_points: int,
|
|
60
|
+
control_dim: int,
|
|
61
|
+
control_limits: wp.array(dtype=float, ndim=2),
|
|
62
|
+
seed: wp.array(dtype=int),
|
|
63
|
+
rollout_trajectories: wp.array(dtype=float, ndim=3),
|
|
64
|
+
):
|
|
65
|
+
env_id, point_id, control_id = wp.tid()
|
|
66
|
+
unique_id = (env_id * num_control_points + point_id) * control_dim + control_id
|
|
67
|
+
r = wp.rand_init(seed[0], unique_id)
|
|
68
|
+
mean = mean_trajectory[0, point_id, control_id]
|
|
69
|
+
lo, hi = control_limits[control_id, 0], control_limits[control_id, 1]
|
|
70
|
+
sample = mean + noise_scale * wp.randn(r)
|
|
71
|
+
for _i in range(10):
|
|
72
|
+
if sample < lo or sample > hi:
|
|
73
|
+
sample = mean + noise_scale * wp.randn(r)
|
|
74
|
+
else:
|
|
75
|
+
break
|
|
76
|
+
rollout_trajectories[env_id, point_id, control_id] = wp.clamp(sample, lo, hi)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@wp.kernel
|
|
80
|
+
def replicate_states(
|
|
81
|
+
body_q_in: wp.array(dtype=wp.transform),
|
|
82
|
+
body_qd_in: wp.array(dtype=wp.spatial_vector),
|
|
83
|
+
bodies_per_env: int,
|
|
84
|
+
body_q_out: wp.array(dtype=wp.transform),
|
|
85
|
+
body_qd_out: wp.array(dtype=wp.spatial_vector),
|
|
86
|
+
):
|
|
87
|
+
tid = wp.tid()
|
|
88
|
+
env_offset = tid * bodies_per_env
|
|
89
|
+
for i in range(bodies_per_env):
|
|
90
|
+
body_q_out[env_offset + i] = body_q_in[i]
|
|
91
|
+
body_qd_out[env_offset + i] = body_qd_in[i]
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@wp.kernel
|
|
95
|
+
def drone_cost(
|
|
96
|
+
body_q: wp.array(dtype=wp.transform),
|
|
97
|
+
body_qd: wp.array(dtype=wp.spatial_vector),
|
|
98
|
+
targets: wp.array(dtype=wp.vec3),
|
|
99
|
+
prop_control: wp.array(dtype=float),
|
|
100
|
+
step: int,
|
|
101
|
+
horizon_length: int,
|
|
102
|
+
weighting: float,
|
|
103
|
+
cost: wp.array(dtype=wp.float32),
|
|
104
|
+
):
|
|
105
|
+
env_id = wp.tid()
|
|
106
|
+
tf = body_q[env_id]
|
|
107
|
+
target = targets[0]
|
|
108
|
+
|
|
109
|
+
pos_drone = wp.transform_get_translation(tf)
|
|
110
|
+
pos_cost = wp.length_sq(pos_drone - target)
|
|
111
|
+
altitude_cost = wp.max(pos_drone[1] - 0.75, 0.0) + wp.max(0.25 - pos_drone[1], 0.0)
|
|
112
|
+
upvector = wp.vec3(0.0, 1.0, 0.0)
|
|
113
|
+
drone_up = wp.transform_vector(tf, upvector)
|
|
114
|
+
upright_cost = 1.0 - wp.dot(drone_up, upvector)
|
|
115
|
+
|
|
116
|
+
vel_drone = body_qd[env_id]
|
|
117
|
+
|
|
118
|
+
# Encourage zero velocity.
|
|
119
|
+
vel_cost = wp.length_sq(vel_drone)
|
|
120
|
+
|
|
121
|
+
control = wp.vec4(
|
|
122
|
+
prop_control[env_id * 4 + 0],
|
|
123
|
+
prop_control[env_id * 4 + 1],
|
|
124
|
+
prop_control[env_id * 4 + 2],
|
|
125
|
+
prop_control[env_id * 4 + 3],
|
|
126
|
+
)
|
|
127
|
+
control_cost = wp.dot(control, control)
|
|
128
|
+
|
|
129
|
+
discount = 0.8 ** wp.float(horizon_length - step - 1) / wp.float(horizon_length) ** 2.0
|
|
130
|
+
|
|
131
|
+
pos_weight = 1000.0
|
|
132
|
+
altitude_weight = 100.0
|
|
133
|
+
control_weight = 0.05
|
|
134
|
+
vel_weight = 0.1
|
|
135
|
+
upright_weight = 10.0
|
|
136
|
+
total_weight = pos_weight + altitude_weight + control_weight + vel_weight + upright_weight
|
|
137
|
+
|
|
138
|
+
wp.atomic_add(
|
|
139
|
+
cost,
|
|
140
|
+
env_id,
|
|
141
|
+
(
|
|
142
|
+
pos_cost * pos_weight
|
|
143
|
+
+ altitude_cost * altitude_weight
|
|
144
|
+
+ control_cost * control_weight
|
|
145
|
+
+ vel_cost * vel_weight
|
|
146
|
+
+ upright_cost * upright_weight
|
|
147
|
+
)
|
|
148
|
+
* (weighting / total_weight)
|
|
149
|
+
* discount,
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
@wp.kernel
|
|
154
|
+
def collision_cost(
|
|
155
|
+
body_q: wp.array(dtype=wp.transform),
|
|
156
|
+
obstacle_ids: wp.array(dtype=int, ndim=2),
|
|
157
|
+
shape_X_bs: wp.array(dtype=wp.transform),
|
|
158
|
+
geo: wp.sim.ModelShapeGeometry,
|
|
159
|
+
margin: float,
|
|
160
|
+
weighting: float,
|
|
161
|
+
cost: wp.array(dtype=wp.float32),
|
|
162
|
+
):
|
|
163
|
+
env_id, obs_id = wp.tid()
|
|
164
|
+
shape_index = obstacle_ids[env_id, obs_id]
|
|
165
|
+
|
|
166
|
+
px = wp.transform_get_translation(body_q[env_id])
|
|
167
|
+
|
|
168
|
+
X_bs = shape_X_bs[shape_index]
|
|
169
|
+
|
|
170
|
+
# transform particle position to shape local space
|
|
171
|
+
x_local = wp.transform_point(wp.transform_inverse(X_bs), px)
|
|
172
|
+
|
|
173
|
+
# geo description
|
|
174
|
+
geo_type = geo.type[shape_index]
|
|
175
|
+
geo_scale = geo.scale[shape_index]
|
|
176
|
+
|
|
177
|
+
# evaluate shape sdf
|
|
178
|
+
d = 1e6
|
|
179
|
+
|
|
180
|
+
if geo_type == wp.sim.GEO_SPHERE:
|
|
181
|
+
d = sphere_sdf(wp.vec3(), geo_scale[0], x_local)
|
|
182
|
+
elif geo_type == wp.sim.GEO_BOX:
|
|
183
|
+
d = box_sdf(geo_scale, x_local)
|
|
184
|
+
elif geo_type == wp.sim.GEO_CAPSULE:
|
|
185
|
+
d = capsule_sdf(geo_scale[0], geo_scale[1], x_local)
|
|
186
|
+
elif geo_type == wp.sim.GEO_CYLINDER:
|
|
187
|
+
d = cylinder_sdf(geo_scale[0], geo_scale[1], x_local)
|
|
188
|
+
elif geo_type == wp.sim.GEO_CONE:
|
|
189
|
+
d = cone_sdf(geo_scale[0], geo_scale[1], x_local)
|
|
190
|
+
elif geo_type == wp.sim.GEO_MESH:
|
|
191
|
+
mesh = geo.source[shape_index]
|
|
192
|
+
min_scale = wp.min(geo_scale)
|
|
193
|
+
max_dist = margin / min_scale
|
|
194
|
+
d = mesh_sdf(mesh, wp.cw_div(x_local, geo_scale), max_dist)
|
|
195
|
+
d *= min_scale # TODO fix this, mesh scaling needs to be handled properly
|
|
196
|
+
elif geo_type == wp.sim.GEO_SDF:
|
|
197
|
+
volume = geo.source[shape_index]
|
|
198
|
+
xpred_local = wp.volume_world_to_index(volume, wp.cw_div(x_local, geo_scale))
|
|
199
|
+
nn = wp.vec3(0.0, 0.0, 0.0)
|
|
200
|
+
d = wp.volume_sample_grad_f(volume, xpred_local, wp.Volume.LINEAR, nn)
|
|
201
|
+
elif geo_type == wp.sim.GEO_PLANE:
|
|
202
|
+
d = plane_sdf(geo_scale[0], geo_scale[1], x_local)
|
|
203
|
+
|
|
204
|
+
d = wp.max(d, 0.0)
|
|
205
|
+
if d < margin:
|
|
206
|
+
c = margin - d
|
|
207
|
+
wp.atomic_add(cost, env_id, weighting * c)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
@wp.kernel
|
|
211
|
+
def enforce_control_limits(
|
|
212
|
+
control_limits: wp.array(dtype=float, ndim=2),
|
|
213
|
+
control_points: wp.array(dtype=float, ndim=3),
|
|
214
|
+
):
|
|
215
|
+
env_id, t_id, control_id = wp.tid()
|
|
216
|
+
lo, hi = control_limits[control_id, 0], control_limits[control_id, 1]
|
|
217
|
+
control_points[env_id, t_id, control_id] = wp.clamp(control_points[env_id, t_id, control_id], lo, hi)
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
@wp.kernel
|
|
221
|
+
def pick_best_trajectory(
|
|
222
|
+
rollout_trajectories: wp.array(dtype=float, ndim=3),
|
|
223
|
+
lowest_cost_id: int,
|
|
224
|
+
best_traj: wp.array(dtype=float, ndim=3),
|
|
225
|
+
):
|
|
226
|
+
t_id, control_id = wp.tid()
|
|
227
|
+
best_traj[0, t_id, control_id] = rollout_trajectories[lowest_cost_id, t_id, control_id]
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
@wp.kernel
|
|
231
|
+
def interpolate_control_linear(
|
|
232
|
+
control_points: wp.array(dtype=float, ndim=3),
|
|
233
|
+
control_dofs: wp.array(dtype=int),
|
|
234
|
+
control_gains: wp.array(dtype=float),
|
|
235
|
+
t: float,
|
|
236
|
+
torque_dim: int,
|
|
237
|
+
torques: wp.array(dtype=float),
|
|
238
|
+
):
|
|
239
|
+
env_id, control_id = wp.tid()
|
|
240
|
+
t_id = int(t)
|
|
241
|
+
frac = t - wp.floor(t)
|
|
242
|
+
control_left = control_points[env_id, t_id, control_id]
|
|
243
|
+
control_right = control_points[env_id, t_id + 1, control_id]
|
|
244
|
+
torque_id = env_id * torque_dim + control_dofs[control_id]
|
|
245
|
+
action = control_left * (1.0 - frac) + control_right * frac
|
|
246
|
+
torques[torque_id] = action * control_gains[control_id]
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
@wp.kernel
|
|
250
|
+
def compute_prop_wrenches(
|
|
251
|
+
props: wp.array(dtype=Propeller),
|
|
252
|
+
controls: wp.array(dtype=float),
|
|
253
|
+
body_q: wp.array(dtype=wp.transform),
|
|
254
|
+
body_com: wp.array(dtype=wp.vec3),
|
|
255
|
+
body_f: wp.array(dtype=wp.spatial_vector),
|
|
256
|
+
):
|
|
257
|
+
tid = wp.tid()
|
|
258
|
+
prop = props[tid]
|
|
259
|
+
control = controls[tid]
|
|
260
|
+
tf = body_q[prop.body]
|
|
261
|
+
dir = wp.transform_vector(tf, prop.dir)
|
|
262
|
+
force = dir * prop.max_thrust * control
|
|
263
|
+
torque = dir * prop.max_torque * control * prop.turning_direction
|
|
264
|
+
moment_arm = wp.transform_point(tf, prop.pos) - wp.transform_point(tf, body_com[prop.body])
|
|
265
|
+
torque += wp.cross(moment_arm, force)
|
|
266
|
+
# Apply angular damping.
|
|
267
|
+
torque *= 0.8
|
|
268
|
+
wp.atomic_add(body_f, prop.body, wp.spatial_vector(torque, force))
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def define_propeller(
|
|
272
|
+
drone: int,
|
|
273
|
+
pos: wp.vec3,
|
|
274
|
+
fps: float,
|
|
275
|
+
thrust: float = 0.109919,
|
|
276
|
+
power: float = 0.040164,
|
|
277
|
+
diameter: float = 0.2286,
|
|
278
|
+
height: float = 0.01,
|
|
279
|
+
max_rpm: float = 6396.667,
|
|
280
|
+
turning_direction: float = 1.0,
|
|
281
|
+
):
|
|
282
|
+
# Air density at sea level.
|
|
283
|
+
air_density = 1.225 # kg / m^3
|
|
284
|
+
|
|
285
|
+
rps = max_rpm / fps
|
|
286
|
+
max_speed = rps * wp.TAU # radians / sec
|
|
287
|
+
rps_square = rps**2
|
|
288
|
+
|
|
289
|
+
prop = Propeller()
|
|
290
|
+
prop.body = drone
|
|
291
|
+
prop.pos = pos
|
|
292
|
+
prop.dir = wp.vec3(0.0, 1.0, 0.0)
|
|
293
|
+
prop.thrust = thrust
|
|
294
|
+
prop.power = power
|
|
295
|
+
prop.diameter = diameter
|
|
296
|
+
prop.height = height
|
|
297
|
+
prop.max_rpm = max_rpm
|
|
298
|
+
prop.max_thrust = thrust * air_density * rps_square * diameter**4
|
|
299
|
+
prop.max_torque = power * air_density * rps_square * diameter**5 / wp.TAU
|
|
300
|
+
prop.turning_direction = turning_direction
|
|
301
|
+
prop.max_speed_square = max_speed**2
|
|
302
|
+
|
|
303
|
+
return prop
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
class Drone:
|
|
307
|
+
def __init__(
|
|
308
|
+
self,
|
|
309
|
+
name: str,
|
|
310
|
+
fps: float,
|
|
311
|
+
trajectory_shape: Tuple[int, int],
|
|
312
|
+
variation_count: int = 1,
|
|
313
|
+
size: float = 1.0,
|
|
314
|
+
requires_grad: bool = False,
|
|
315
|
+
state_count: Optional[int] = None,
|
|
316
|
+
) -> None:
|
|
317
|
+
self.variation_count = variation_count
|
|
318
|
+
self.requires_grad = requires_grad
|
|
319
|
+
|
|
320
|
+
# Current tick of the simulation, including substeps.
|
|
321
|
+
self.sim_tick = 0
|
|
322
|
+
|
|
323
|
+
# Initialize the helper to build a physics scene.
|
|
324
|
+
builder = wp.sim.ModelBuilder()
|
|
325
|
+
builder.rigid_contact_margin = 0.05
|
|
326
|
+
|
|
327
|
+
# Initialize the rigid bodies, propellers, and colliders.
|
|
328
|
+
props = []
|
|
329
|
+
colliders = []
|
|
330
|
+
crossbar_length = size
|
|
331
|
+
crossbar_height = size * 0.05
|
|
332
|
+
crossbar_width = size * 0.05
|
|
333
|
+
carbon_fiber_density = 1750.0 # kg / m^3
|
|
334
|
+
for i in range(variation_count):
|
|
335
|
+
# Register the drone as a rigid body in the simulation model.
|
|
336
|
+
body = builder.add_body(name=f"{name}_{i}")
|
|
337
|
+
|
|
338
|
+
# Define the shapes making up the drone's rigid body.
|
|
339
|
+
builder.add_shape_box(
|
|
340
|
+
body,
|
|
341
|
+
hx=crossbar_length,
|
|
342
|
+
hy=crossbar_height,
|
|
343
|
+
hz=crossbar_width,
|
|
344
|
+
density=carbon_fiber_density,
|
|
345
|
+
collision_group=i,
|
|
346
|
+
)
|
|
347
|
+
builder.add_shape_box(
|
|
348
|
+
body,
|
|
349
|
+
hx=crossbar_width,
|
|
350
|
+
hy=crossbar_height,
|
|
351
|
+
hz=crossbar_length,
|
|
352
|
+
density=carbon_fiber_density,
|
|
353
|
+
collision_group=i,
|
|
354
|
+
)
|
|
355
|
+
|
|
356
|
+
# Initialize the propellers.
|
|
357
|
+
props.extend(
|
|
358
|
+
(
|
|
359
|
+
define_propeller(
|
|
360
|
+
body,
|
|
361
|
+
wp.vec3(crossbar_length, 0.0, 0.0),
|
|
362
|
+
fps,
|
|
363
|
+
turning_direction=-1.0,
|
|
364
|
+
),
|
|
365
|
+
define_propeller(
|
|
366
|
+
body,
|
|
367
|
+
wp.vec3(-crossbar_length, 0.0, 0.0),
|
|
368
|
+
fps,
|
|
369
|
+
turning_direction=1.0,
|
|
370
|
+
),
|
|
371
|
+
define_propeller(
|
|
372
|
+
body,
|
|
373
|
+
wp.vec3(0.0, 0.0, crossbar_length),
|
|
374
|
+
fps,
|
|
375
|
+
turning_direction=1.0,
|
|
376
|
+
),
|
|
377
|
+
define_propeller(
|
|
378
|
+
body,
|
|
379
|
+
wp.vec3(0.0, 0.0, -crossbar_length),
|
|
380
|
+
fps,
|
|
381
|
+
turning_direction=-1.0,
|
|
382
|
+
),
|
|
383
|
+
),
|
|
384
|
+
)
|
|
385
|
+
|
|
386
|
+
# Initialize the colliders.
|
|
387
|
+
colliders.append(
|
|
388
|
+
(
|
|
389
|
+
builder.add_shape_capsule(
|
|
390
|
+
-1,
|
|
391
|
+
pos=(0.5, 2.0, 0.5),
|
|
392
|
+
radius=0.15,
|
|
393
|
+
half_height=2.0,
|
|
394
|
+
collision_group=i,
|
|
395
|
+
),
|
|
396
|
+
),
|
|
397
|
+
)
|
|
398
|
+
self.props = wp.array(props, dtype=Propeller)
|
|
399
|
+
self.colliders = wp.array(colliders, dtype=int)
|
|
400
|
+
|
|
401
|
+
# Build the model and set-up its properties.
|
|
402
|
+
self.model = builder.finalize(requires_grad=requires_grad)
|
|
403
|
+
self.model.ground = False
|
|
404
|
+
|
|
405
|
+
# Initialize the required simulation states.
|
|
406
|
+
if requires_grad:
|
|
407
|
+
self.states = tuple(self.model.state() for _ in range(state_count + 1))
|
|
408
|
+
self.controls = tuple(self.model.control() for _ in range(state_count))
|
|
409
|
+
else:
|
|
410
|
+
# When only running a forward simulation, we don't need to store
|
|
411
|
+
# the history of the states at each step, instead we use double
|
|
412
|
+
# buffering to represent the previous and next states.
|
|
413
|
+
self.states = [self.model.state(), self.model.state()]
|
|
414
|
+
self.controls = (self.model.control(),)
|
|
415
|
+
|
|
416
|
+
# create array for the propeller controls
|
|
417
|
+
for control in self.controls:
|
|
418
|
+
control.prop_controls = wp.zeros(len(self.props), dtype=float, requires_grad=requires_grad)
|
|
419
|
+
|
|
420
|
+
# Define the trajectories as arrays of control points.
|
|
421
|
+
# The point data has an additional item to support linear interpolation.
|
|
422
|
+
self.trajectories = wp.zeros(
|
|
423
|
+
(variation_count, trajectory_shape[0], trajectory_shape[1]),
|
|
424
|
+
dtype=float,
|
|
425
|
+
requires_grad=requires_grad,
|
|
426
|
+
)
|
|
427
|
+
|
|
428
|
+
# Store some miscellaneous info.
|
|
429
|
+
self.body_count = len(builder.body_q)
|
|
430
|
+
self.collider_count = self.colliders.shape[1]
|
|
431
|
+
self.collision_radius = crossbar_length
|
|
432
|
+
|
|
433
|
+
@property
|
|
434
|
+
def state(self) -> wp.sim.State:
|
|
435
|
+
return self.states[self.sim_tick if self.requires_grad else 0]
|
|
436
|
+
|
|
437
|
+
@property
|
|
438
|
+
def next_state(self) -> wp.sim.State:
|
|
439
|
+
return self.states[self.sim_tick + 1 if self.requires_grad else 1]
|
|
440
|
+
|
|
441
|
+
@property
|
|
442
|
+
def control(self) -> wp.sim.Control:
|
|
443
|
+
return self.controls[min(len(self.controls) - 1, self.sim_tick) if self.requires_grad else 0]
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
class Example:
|
|
447
|
+
def __init__(
|
|
448
|
+
self,
|
|
449
|
+
stage_path="example_drone.usd",
|
|
450
|
+
verbose=False,
|
|
451
|
+
render_rollouts=False,
|
|
452
|
+
drone_path=DEFAULT_DRONE_PATH,
|
|
453
|
+
num_frames=360,
|
|
454
|
+
num_rollouts=16,
|
|
455
|
+
headless=True,
|
|
456
|
+
) -> None:
|
|
457
|
+
# Number of frames per second.
|
|
458
|
+
self.fps = 60
|
|
459
|
+
|
|
460
|
+
# Duration of the simulation in number of frames.
|
|
461
|
+
self.num_frames = num_frames
|
|
462
|
+
|
|
463
|
+
# Number of simulation substeps to take per step.
|
|
464
|
+
self.sim_substep_count = 1
|
|
465
|
+
|
|
466
|
+
# Delta time between each simulation substep.
|
|
467
|
+
self.frame_dt = 1.0 / self.fps
|
|
468
|
+
|
|
469
|
+
# Delta time between each simulation substep.
|
|
470
|
+
self.sim_dt = self.frame_dt / self.sim_substep_count
|
|
471
|
+
|
|
472
|
+
# Frame number used for simulation and rendering.
|
|
473
|
+
self.frame = 0
|
|
474
|
+
|
|
475
|
+
# Targets positions that the drone will try to reach in turn.
|
|
476
|
+
self.targets = (
|
|
477
|
+
wp.vec3(0.0, 0.5, 1.0),
|
|
478
|
+
wp.vec3(1.0, 0.5, 0.0),
|
|
479
|
+
)
|
|
480
|
+
|
|
481
|
+
# Define the index of the active target.
|
|
482
|
+
# We start with -1 since it'll be incremented on the first frame.
|
|
483
|
+
self.target_idx = -1
|
|
484
|
+
# use a Warp array to store the current target so that we can assign
|
|
485
|
+
# a new target to it while retaining the original CUDA graph.
|
|
486
|
+
self.current_target = wp.array([self.targets[self.target_idx + 1]], dtype=wp.vec3)
|
|
487
|
+
|
|
488
|
+
# Number of steps to run at each frame for the optimisation pass.
|
|
489
|
+
self.optim_step_count = 20
|
|
490
|
+
|
|
491
|
+
# Time steps between control points.
|
|
492
|
+
self.control_point_step = 10
|
|
493
|
+
|
|
494
|
+
# Number of control horizon points to interpolate between.
|
|
495
|
+
self.control_point_count = 3
|
|
496
|
+
|
|
497
|
+
self.control_point_data_count = self.control_point_count + 1
|
|
498
|
+
self.control_dofs = wp.array((0, 1, 2, 3), dtype=int)
|
|
499
|
+
self.control_dim = len(self.control_dofs)
|
|
500
|
+
self.control_gains = wp.array((0.8,) * self.control_dim, dtype=float)
|
|
501
|
+
self.control_limits = wp.array(((0.1, 1.0),) * self.control_dim, dtype=float)
|
|
502
|
+
|
|
503
|
+
drone_size = 0.2
|
|
504
|
+
|
|
505
|
+
# Declare the reference drone.
|
|
506
|
+
self.drone = Drone(
|
|
507
|
+
"drone",
|
|
508
|
+
self.fps,
|
|
509
|
+
(self.control_point_data_count, self.control_dim),
|
|
510
|
+
size=drone_size,
|
|
511
|
+
)
|
|
512
|
+
|
|
513
|
+
# Declare the drone's rollouts.
|
|
514
|
+
# These allow to run parallel simulations in order to find the best
|
|
515
|
+
# trajectory at each control point.
|
|
516
|
+
self.rollout_count = num_rollouts
|
|
517
|
+
self.rollout_step_count = self.control_point_step * self.control_point_count
|
|
518
|
+
self.rollouts = Drone(
|
|
519
|
+
"rollout",
|
|
520
|
+
self.fps,
|
|
521
|
+
(self.control_point_data_count, self.control_dim),
|
|
522
|
+
variation_count=self.rollout_count,
|
|
523
|
+
size=drone_size,
|
|
524
|
+
requires_grad=True,
|
|
525
|
+
state_count=self.rollout_step_count * self.sim_substep_count,
|
|
526
|
+
)
|
|
527
|
+
|
|
528
|
+
self.seed = wp.zeros(1, dtype=int)
|
|
529
|
+
self.rollout_costs = wp.zeros(self.rollout_count, dtype=float, requires_grad=True)
|
|
530
|
+
|
|
531
|
+
# Use the Euler integrator for stepping through the simulation.
|
|
532
|
+
self.integrator = wp.sim.SemiImplicitIntegrator()
|
|
533
|
+
|
|
534
|
+
self.optimizer = wp.optim.SGD(
|
|
535
|
+
[self.rollouts.trajectories.flatten()],
|
|
536
|
+
lr=1e-2,
|
|
537
|
+
nesterov=False,
|
|
538
|
+
momentum=0.0,
|
|
539
|
+
)
|
|
540
|
+
|
|
541
|
+
self.tape = None
|
|
542
|
+
|
|
543
|
+
if stage_path:
|
|
544
|
+
if not headless:
|
|
545
|
+
self.renderer = wp.sim.render.SimRendererOpenGL(self.drone.model, stage_path, fps=self.fps)
|
|
546
|
+
else:
|
|
547
|
+
# Helper to render the physics scene as a USD file.
|
|
548
|
+
self.renderer = wp.sim.render.SimRenderer(self.drone.model, stage_path, fps=self.fps)
|
|
549
|
+
|
|
550
|
+
if isinstance(self.renderer, warp.sim.render.SimRendererUsd):
|
|
551
|
+
from pxr import UsdGeom
|
|
552
|
+
|
|
553
|
+
# Remove the default drone geometries.
|
|
554
|
+
drone_root_prim = self.renderer.stage.GetPrimAtPath("/root/body_0_drone_0")
|
|
555
|
+
for prim in drone_root_prim.GetChildren():
|
|
556
|
+
self.renderer.stage.RemovePrim(prim.GetPath())
|
|
557
|
+
|
|
558
|
+
# Add a reference to the drone geometry.
|
|
559
|
+
drone_prim = self.renderer.stage.OverridePrim(f"{drone_root_prim.GetPath()}/crazyflie")
|
|
560
|
+
drone_prim.GetReferences().AddReference(drone_path)
|
|
561
|
+
drone_xform = UsdGeom.Xform(drone_prim)
|
|
562
|
+
drone_xform.AddTranslateOp().Set((0.0, -0.05, 0.0))
|
|
563
|
+
drone_xform.AddRotateYOp().Set(45.0)
|
|
564
|
+
drone_xform.AddScaleOp().Set((drone_size * 20.0,) * 3)
|
|
565
|
+
|
|
566
|
+
# Get the propellers to spin
|
|
567
|
+
for turning_direction in ("cw", "ccw"):
|
|
568
|
+
spin = 100.0 * 360.0 * self.num_frames / self.fps
|
|
569
|
+
spin = spin if turning_direction == "ccw" else -spin
|
|
570
|
+
for side in ("back", "front"):
|
|
571
|
+
prop_prim = self.renderer.stage.OverridePrim(
|
|
572
|
+
f"{drone_prim.GetPath()}/propeller_{turning_direction}_{side}"
|
|
573
|
+
)
|
|
574
|
+
prop_xform = UsdGeom.Xform(prop_prim)
|
|
575
|
+
rot = prop_xform.AddRotateYOp()
|
|
576
|
+
rot.Set(0.0, 0.0)
|
|
577
|
+
rot.Set(spin, self.num_frames)
|
|
578
|
+
else:
|
|
579
|
+
self.renderer = None
|
|
580
|
+
|
|
581
|
+
self.use_cuda_graph = wp.get_device().is_cuda
|
|
582
|
+
self.optim_graph = None
|
|
583
|
+
|
|
584
|
+
self.render_rollouts = render_rollouts
|
|
585
|
+
self.verbose = verbose
|
|
586
|
+
|
|
587
|
+
def update_drone(self, drone: Drone) -> None:
|
|
588
|
+
drone.state.clear_forces()
|
|
589
|
+
|
|
590
|
+
wp.launch(
|
|
591
|
+
interpolate_control_linear,
|
|
592
|
+
dim=(
|
|
593
|
+
drone.variation_count,
|
|
594
|
+
self.control_dim,
|
|
595
|
+
),
|
|
596
|
+
inputs=(
|
|
597
|
+
drone.trajectories,
|
|
598
|
+
self.control_dofs,
|
|
599
|
+
self.control_gains,
|
|
600
|
+
drone.sim_tick / (self.sim_substep_count * self.control_point_step),
|
|
601
|
+
self.control_dim,
|
|
602
|
+
),
|
|
603
|
+
outputs=(drone.control.prop_controls,),
|
|
604
|
+
)
|
|
605
|
+
|
|
606
|
+
wp.launch(
|
|
607
|
+
compute_prop_wrenches,
|
|
608
|
+
dim=len(drone.props),
|
|
609
|
+
inputs=(
|
|
610
|
+
drone.props,
|
|
611
|
+
drone.control.prop_controls,
|
|
612
|
+
drone.state.body_q,
|
|
613
|
+
drone.model.body_com,
|
|
614
|
+
),
|
|
615
|
+
outputs=(drone.state.body_f,),
|
|
616
|
+
)
|
|
617
|
+
|
|
618
|
+
self.integrator.simulate(
|
|
619
|
+
drone.model,
|
|
620
|
+
drone.state,
|
|
621
|
+
drone.next_state,
|
|
622
|
+
self.sim_dt,
|
|
623
|
+
drone.control,
|
|
624
|
+
)
|
|
625
|
+
|
|
626
|
+
drone.sim_tick += 1
|
|
627
|
+
|
|
628
|
+
def forward(self):
|
|
629
|
+
# Evaluate the rollouts with their costs.
|
|
630
|
+
self.rollouts.sim_tick = 0
|
|
631
|
+
self.rollout_costs.zero_()
|
|
632
|
+
wp.launch(
|
|
633
|
+
replicate_states,
|
|
634
|
+
dim=self.rollout_count,
|
|
635
|
+
inputs=(
|
|
636
|
+
self.drone.state.body_q,
|
|
637
|
+
self.drone.state.body_qd,
|
|
638
|
+
self.drone.body_count,
|
|
639
|
+
),
|
|
640
|
+
outputs=(
|
|
641
|
+
self.rollouts.state.body_q,
|
|
642
|
+
self.rollouts.state.body_qd,
|
|
643
|
+
),
|
|
644
|
+
)
|
|
645
|
+
|
|
646
|
+
for i in range(self.rollout_step_count):
|
|
647
|
+
for _ in range(self.sim_substep_count):
|
|
648
|
+
self.update_drone(self.rollouts)
|
|
649
|
+
|
|
650
|
+
wp.launch(
|
|
651
|
+
drone_cost,
|
|
652
|
+
dim=self.rollout_count,
|
|
653
|
+
inputs=(
|
|
654
|
+
self.rollouts.state.body_q,
|
|
655
|
+
self.rollouts.state.body_qd,
|
|
656
|
+
self.current_target,
|
|
657
|
+
self.rollouts.control.prop_controls,
|
|
658
|
+
i,
|
|
659
|
+
self.rollout_step_count,
|
|
660
|
+
1e3,
|
|
661
|
+
),
|
|
662
|
+
outputs=(self.rollout_costs,),
|
|
663
|
+
)
|
|
664
|
+
wp.launch(
|
|
665
|
+
collision_cost,
|
|
666
|
+
dim=(
|
|
667
|
+
self.rollout_count,
|
|
668
|
+
self.rollouts.collider_count,
|
|
669
|
+
),
|
|
670
|
+
inputs=(
|
|
671
|
+
self.rollouts.state.body_q,
|
|
672
|
+
self.rollouts.colliders,
|
|
673
|
+
self.rollouts.model.shape_transform,
|
|
674
|
+
self.rollouts.model.shape_geo,
|
|
675
|
+
self.rollouts.collision_radius,
|
|
676
|
+
1e4,
|
|
677
|
+
),
|
|
678
|
+
outputs=(self.rollout_costs,),
|
|
679
|
+
)
|
|
680
|
+
|
|
681
|
+
def step_optimizer(self):
|
|
682
|
+
if self.optim_graph is None:
|
|
683
|
+
self.tape = wp.Tape()
|
|
684
|
+
with self.tape:
|
|
685
|
+
self.forward()
|
|
686
|
+
self.rollout_costs.grad.fill_(1.0)
|
|
687
|
+
self.tape.backward()
|
|
688
|
+
else:
|
|
689
|
+
wp.capture_launch(self.optim_graph)
|
|
690
|
+
|
|
691
|
+
self.optimizer.step([self.rollouts.trajectories.grad.flatten()])
|
|
692
|
+
|
|
693
|
+
# Enforce limits on the control points.
|
|
694
|
+
wp.launch(
|
|
695
|
+
enforce_control_limits,
|
|
696
|
+
dim=self.rollouts.trajectories.shape,
|
|
697
|
+
inputs=(self.control_limits,),
|
|
698
|
+
outputs=(self.rollouts.trajectories,),
|
|
699
|
+
)
|
|
700
|
+
self.tape.zero()
|
|
701
|
+
|
|
702
|
+
def step(self):
|
|
703
|
+
if self.frame % int((self.num_frames / len(self.targets))) == 0:
|
|
704
|
+
if self.verbose:
|
|
705
|
+
print(f"Choosing new flight target: {self.target_idx+1}")
|
|
706
|
+
|
|
707
|
+
self.target_idx += 1
|
|
708
|
+
self.target_idx %= len(self.targets)
|
|
709
|
+
|
|
710
|
+
# Assign the new target to the current target array.
|
|
711
|
+
self.current_target.assign([self.targets[self.target_idx]])
|
|
712
|
+
|
|
713
|
+
if self.use_cuda_graph and self.optim_graph is None:
|
|
714
|
+
with wp.ScopedCapture() as capture:
|
|
715
|
+
self.tape = wp.Tape()
|
|
716
|
+
with self.tape:
|
|
717
|
+
self.forward()
|
|
718
|
+
self.rollout_costs.grad.fill_(1.0)
|
|
719
|
+
self.tape.backward()
|
|
720
|
+
self.optim_graph = capture.graph
|
|
721
|
+
|
|
722
|
+
# Sample control waypoints around the nominal trajectory.
|
|
723
|
+
noise_scale = 0.15
|
|
724
|
+
wp.launch(
|
|
725
|
+
sample_gaussian,
|
|
726
|
+
dim=(
|
|
727
|
+
self.rollouts.trajectories.shape[0] - 1,
|
|
728
|
+
self.rollouts.trajectories.shape[1],
|
|
729
|
+
self.rollouts.trajectories.shape[2],
|
|
730
|
+
),
|
|
731
|
+
inputs=(
|
|
732
|
+
self.drone.trajectories,
|
|
733
|
+
noise_scale,
|
|
734
|
+
self.control_point_data_count,
|
|
735
|
+
self.control_dim,
|
|
736
|
+
self.control_limits,
|
|
737
|
+
self.seed,
|
|
738
|
+
),
|
|
739
|
+
outputs=(self.rollouts.trajectories,),
|
|
740
|
+
)
|
|
741
|
+
|
|
742
|
+
wp.launch(
|
|
743
|
+
increment_seed,
|
|
744
|
+
dim=1,
|
|
745
|
+
inputs=(),
|
|
746
|
+
outputs=(self.seed,),
|
|
747
|
+
)
|
|
748
|
+
|
|
749
|
+
for _ in range(self.optim_step_count):
|
|
750
|
+
self.step_optimizer()
|
|
751
|
+
|
|
752
|
+
# Pick the best trajectory.
|
|
753
|
+
wp.synchronize()
|
|
754
|
+
lowest_cost_id = np.argmin(self.rollout_costs.numpy())
|
|
755
|
+
wp.launch(
|
|
756
|
+
pick_best_trajectory,
|
|
757
|
+
dim=(
|
|
758
|
+
self.control_point_data_count,
|
|
759
|
+
self.control_dim,
|
|
760
|
+
),
|
|
761
|
+
inputs=(
|
|
762
|
+
self.rollouts.trajectories,
|
|
763
|
+
lowest_cost_id,
|
|
764
|
+
),
|
|
765
|
+
outputs=(self.drone.trajectories,),
|
|
766
|
+
)
|
|
767
|
+
self.rollouts.trajectories[-1].assign(self.drone.trajectories[0])
|
|
768
|
+
|
|
769
|
+
# Simulate the drone.
|
|
770
|
+
self.drone.sim_tick = 0
|
|
771
|
+
for _ in range(self.sim_substep_count):
|
|
772
|
+
self.update_drone(self.drone)
|
|
773
|
+
|
|
774
|
+
# Swap the drone's states.
|
|
775
|
+
(self.drone.states[0], self.drone.states[1]) = (self.drone.states[1], self.drone.states[0])
|
|
776
|
+
|
|
777
|
+
def render(self):
|
|
778
|
+
if self.renderer is None:
|
|
779
|
+
return
|
|
780
|
+
|
|
781
|
+
self.renderer.begin_frame(self.frame / self.fps)
|
|
782
|
+
self.renderer.render(self.drone.state)
|
|
783
|
+
|
|
784
|
+
# Render a sphere as the current target.
|
|
785
|
+
self.renderer.render_sphere(
|
|
786
|
+
"target",
|
|
787
|
+
self.targets[self.target_idx],
|
|
788
|
+
wp.quat_identity(),
|
|
789
|
+
0.05,
|
|
790
|
+
color=(1.0, 0.0, 0.0),
|
|
791
|
+
)
|
|
792
|
+
|
|
793
|
+
# Render the rollout trajectories.
|
|
794
|
+
if self.render_rollouts:
|
|
795
|
+
costs = self.rollout_costs.numpy()
|
|
796
|
+
|
|
797
|
+
positions = np.array([x.body_q.numpy()[:, :3] for x in self.rollouts.states])
|
|
798
|
+
|
|
799
|
+
min_cost = np.min(costs)
|
|
800
|
+
max_cost = np.max(costs)
|
|
801
|
+
for i in range(self.rollout_count):
|
|
802
|
+
# Flip colors, so red means best trajectory, blue worst.
|
|
803
|
+
color = wp.render.bourke_color_map(-max_cost, -min_cost, -costs[i])
|
|
804
|
+
self.renderer.render_line_strip(
|
|
805
|
+
name=f"rollout_{i}",
|
|
806
|
+
vertices=positions[:, i],
|
|
807
|
+
color=color,
|
|
808
|
+
radius=0.001,
|
|
809
|
+
)
|
|
810
|
+
|
|
811
|
+
self.renderer.end_frame()
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
if __name__ == "__main__":
|
|
815
|
+
import argparse
|
|
816
|
+
|
|
817
|
+
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
|
818
|
+
parser.add_argument("--device", type=str, default=None, help="Override the default Warp device.")
|
|
819
|
+
parser.add_argument(
|
|
820
|
+
"--stage_path",
|
|
821
|
+
type=lambda x: None if x == "None" else str(x),
|
|
822
|
+
default="example_drone.usd",
|
|
823
|
+
help="Path to the output USD file.",
|
|
824
|
+
)
|
|
825
|
+
parser.add_argument("--num_frames", type=int, default=360, help="Total number of frames.")
|
|
826
|
+
parser.add_argument("--num_rollouts", type=int, default=16, help="Number of drone rollouts.")
|
|
827
|
+
parser.add_argument(
|
|
828
|
+
"--drone_path",
|
|
829
|
+
type=str,
|
|
830
|
+
default=os.path.join(warp.examples.get_asset_directory(), "crazyflie.usd"),
|
|
831
|
+
help="Path to the USD file to use as the reference for the drone prim in the output stage.",
|
|
832
|
+
)
|
|
833
|
+
parser.add_argument("--render_rollouts", action="store_true", help="Add rollout trajectories to the output stage.")
|
|
834
|
+
parser.add_argument(
|
|
835
|
+
"--headless",
|
|
836
|
+
action="store_true",
|
|
837
|
+
help="Run in headless mode, suppressing the opening of any graphical windows.",
|
|
838
|
+
)
|
|
839
|
+
parser.add_argument("--verbose", action="store_true", help="Print out additional status messages during execution.")
|
|
840
|
+
|
|
841
|
+
args = parser.parse_known_args()[0]
|
|
842
|
+
|
|
843
|
+
with wp.ScopedDevice(args.device):
|
|
844
|
+
example = Example(
|
|
845
|
+
stage_path=args.stage_path,
|
|
846
|
+
verbose=args.verbose,
|
|
847
|
+
render_rollouts=args.render_rollouts,
|
|
848
|
+
drone_path=args.drone_path,
|
|
849
|
+
num_frames=args.num_frames,
|
|
850
|
+
num_rollouts=args.num_rollouts,
|
|
851
|
+
headless=args.headless,
|
|
852
|
+
)
|
|
853
|
+
for _i in range(args.num_frames):
|
|
854
|
+
example.step()
|
|
855
|
+
example.render()
|
|
856
|
+
example.frame += 1
|
|
857
|
+
|
|
858
|
+
loss = np.min(example.rollout_costs.numpy())
|
|
859
|
+
print(f"[{example.frame:3d}/{example.num_frames}] loss={loss:.8f}")
|
|
860
|
+
|
|
861
|
+
if example.renderer is not None:
|
|
862
|
+
example.renderer.save()
|