warp-lang 1.9.1__py3-none-manylinux_2_34_aarch64.whl → 1.10.0rc2__py3-none-manylinux_2_34_aarch64.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.

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