warp-lang 1.7.0__py3-none-manylinux_2_28_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 +139 -0
- warp/__init__.pyi +1 -0
- warp/autograd.py +1142 -0
- warp/bin/warp-clang.so +0 -0
- warp/bin/warp.so +0 -0
- warp/build.py +557 -0
- warp/build_dll.py +405 -0
- warp/builtins.py +6855 -0
- warp/codegen.py +3969 -0
- warp/config.py +158 -0
- warp/constants.py +57 -0
- warp/context.py +6812 -0
- warp/dlpack.py +462 -0
- warp/examples/__init__.py +24 -0
- warp/examples/assets/bear.usd +0 -0
- warp/examples/assets/bunny.usd +0 -0
- warp/examples/assets/cartpole.urdf +110 -0
- warp/examples/assets/crazyflie.usd +0 -0
- warp/examples/assets/cube.usd +0 -0
- warp/examples/assets/nonuniform.usd +0 -0
- warp/examples/assets/nv_ant.xml +92 -0
- warp/examples/assets/nv_humanoid.xml +183 -0
- warp/examples/assets/nvidia_logo.png +0 -0
- warp/examples/assets/pixel.jpg +0 -0
- warp/examples/assets/quadruped.urdf +268 -0
- warp/examples/assets/rocks.nvdb +0 -0
- warp/examples/assets/rocks.usd +0 -0
- warp/examples/assets/sphere.usd +0 -0
- warp/examples/assets/square_cloth.usd +0 -0
- warp/examples/benchmarks/benchmark_api.py +389 -0
- warp/examples/benchmarks/benchmark_cloth.py +296 -0
- warp/examples/benchmarks/benchmark_cloth_cupy.py +96 -0
- warp/examples/benchmarks/benchmark_cloth_jax.py +105 -0
- warp/examples/benchmarks/benchmark_cloth_numba.py +161 -0
- warp/examples/benchmarks/benchmark_cloth_numpy.py +85 -0
- warp/examples/benchmarks/benchmark_cloth_paddle.py +94 -0
- warp/examples/benchmarks/benchmark_cloth_pytorch.py +94 -0
- warp/examples/benchmarks/benchmark_cloth_taichi.py +120 -0
- warp/examples/benchmarks/benchmark_cloth_warp.py +153 -0
- warp/examples/benchmarks/benchmark_gemm.py +164 -0
- warp/examples/benchmarks/benchmark_interop_paddle.py +166 -0
- warp/examples/benchmarks/benchmark_interop_torch.py +166 -0
- warp/examples/benchmarks/benchmark_launches.py +301 -0
- warp/examples/benchmarks/benchmark_tile_load_store.py +103 -0
- warp/examples/browse.py +37 -0
- warp/examples/core/example_cupy.py +86 -0
- warp/examples/core/example_dem.py +241 -0
- warp/examples/core/example_fluid.py +299 -0
- warp/examples/core/example_graph_capture.py +150 -0
- warp/examples/core/example_marching_cubes.py +194 -0
- warp/examples/core/example_mesh.py +180 -0
- warp/examples/core/example_mesh_intersect.py +211 -0
- warp/examples/core/example_nvdb.py +182 -0
- warp/examples/core/example_raycast.py +111 -0
- warp/examples/core/example_raymarch.py +205 -0
- warp/examples/core/example_render_opengl.py +193 -0
- warp/examples/core/example_sample_mesh.py +300 -0
- warp/examples/core/example_sph.py +411 -0
- warp/examples/core/example_torch.py +211 -0
- warp/examples/core/example_wave.py +269 -0
- warp/examples/fem/example_adaptive_grid.py +286 -0
- warp/examples/fem/example_apic_fluid.py +423 -0
- warp/examples/fem/example_burgers.py +261 -0
- warp/examples/fem/example_convection_diffusion.py +178 -0
- warp/examples/fem/example_convection_diffusion_dg.py +204 -0
- warp/examples/fem/example_deformed_geometry.py +172 -0
- warp/examples/fem/example_diffusion.py +196 -0
- warp/examples/fem/example_diffusion_3d.py +225 -0
- warp/examples/fem/example_diffusion_mgpu.py +220 -0
- warp/examples/fem/example_distortion_energy.py +228 -0
- warp/examples/fem/example_magnetostatics.py +240 -0
- warp/examples/fem/example_mixed_elasticity.py +291 -0
- warp/examples/fem/example_navier_stokes.py +261 -0
- warp/examples/fem/example_nonconforming_contact.py +298 -0
- warp/examples/fem/example_stokes.py +213 -0
- warp/examples/fem/example_stokes_transfer.py +262 -0
- warp/examples/fem/example_streamlines.py +352 -0
- warp/examples/fem/utils.py +1000 -0
- warp/examples/interop/example_jax_callable.py +116 -0
- warp/examples/interop/example_jax_ffi_callback.py +132 -0
- warp/examples/interop/example_jax_kernel.py +205 -0
- warp/examples/optim/example_bounce.py +266 -0
- warp/examples/optim/example_cloth_throw.py +228 -0
- warp/examples/optim/example_diffray.py +561 -0
- warp/examples/optim/example_drone.py +870 -0
- warp/examples/optim/example_fluid_checkpoint.py +497 -0
- warp/examples/optim/example_inverse_kinematics.py +182 -0
- warp/examples/optim/example_inverse_kinematics_torch.py +191 -0
- warp/examples/optim/example_softbody_properties.py +400 -0
- warp/examples/optim/example_spring_cage.py +245 -0
- warp/examples/optim/example_trajectory.py +227 -0
- warp/examples/sim/example_cartpole.py +143 -0
- warp/examples/sim/example_cloth.py +225 -0
- warp/examples/sim/example_cloth_self_contact.py +322 -0
- warp/examples/sim/example_granular.py +130 -0
- warp/examples/sim/example_granular_collision_sdf.py +202 -0
- warp/examples/sim/example_jacobian_ik.py +244 -0
- warp/examples/sim/example_particle_chain.py +124 -0
- warp/examples/sim/example_quadruped.py +203 -0
- warp/examples/sim/example_rigid_chain.py +203 -0
- warp/examples/sim/example_rigid_contact.py +195 -0
- warp/examples/sim/example_rigid_force.py +133 -0
- warp/examples/sim/example_rigid_gyroscopic.py +115 -0
- warp/examples/sim/example_rigid_soft_contact.py +140 -0
- warp/examples/sim/example_soft_body.py +196 -0
- warp/examples/tile/example_tile_cholesky.py +87 -0
- warp/examples/tile/example_tile_convolution.py +66 -0
- warp/examples/tile/example_tile_fft.py +55 -0
- warp/examples/tile/example_tile_filtering.py +113 -0
- warp/examples/tile/example_tile_matmul.py +85 -0
- warp/examples/tile/example_tile_mlp.py +383 -0
- warp/examples/tile/example_tile_nbody.py +199 -0
- warp/examples/tile/example_tile_walker.py +327 -0
- warp/fabric.py +355 -0
- warp/fem/__init__.py +106 -0
- warp/fem/adaptivity.py +508 -0
- warp/fem/cache.py +572 -0
- warp/fem/dirichlet.py +202 -0
- warp/fem/domain.py +411 -0
- warp/fem/field/__init__.py +125 -0
- warp/fem/field/field.py +619 -0
- warp/fem/field/nodal_field.py +326 -0
- warp/fem/field/restriction.py +37 -0
- warp/fem/field/virtual.py +848 -0
- warp/fem/geometry/__init__.py +32 -0
- warp/fem/geometry/adaptive_nanogrid.py +857 -0
- warp/fem/geometry/closest_point.py +84 -0
- warp/fem/geometry/deformed_geometry.py +221 -0
- warp/fem/geometry/element.py +776 -0
- warp/fem/geometry/geometry.py +362 -0
- warp/fem/geometry/grid_2d.py +392 -0
- warp/fem/geometry/grid_3d.py +452 -0
- warp/fem/geometry/hexmesh.py +911 -0
- warp/fem/geometry/nanogrid.py +571 -0
- warp/fem/geometry/partition.py +389 -0
- warp/fem/geometry/quadmesh.py +663 -0
- warp/fem/geometry/tetmesh.py +855 -0
- warp/fem/geometry/trimesh.py +806 -0
- warp/fem/integrate.py +2335 -0
- warp/fem/linalg.py +419 -0
- warp/fem/operator.py +293 -0
- warp/fem/polynomial.py +229 -0
- warp/fem/quadrature/__init__.py +17 -0
- warp/fem/quadrature/pic_quadrature.py +299 -0
- warp/fem/quadrature/quadrature.py +591 -0
- warp/fem/space/__init__.py +228 -0
- warp/fem/space/basis_function_space.py +468 -0
- warp/fem/space/basis_space.py +667 -0
- warp/fem/space/dof_mapper.py +251 -0
- warp/fem/space/function_space.py +309 -0
- warp/fem/space/grid_2d_function_space.py +177 -0
- warp/fem/space/grid_3d_function_space.py +227 -0
- warp/fem/space/hexmesh_function_space.py +257 -0
- warp/fem/space/nanogrid_function_space.py +201 -0
- warp/fem/space/partition.py +367 -0
- warp/fem/space/quadmesh_function_space.py +223 -0
- warp/fem/space/restriction.py +179 -0
- warp/fem/space/shape/__init__.py +143 -0
- warp/fem/space/shape/cube_shape_function.py +1105 -0
- warp/fem/space/shape/shape_function.py +133 -0
- warp/fem/space/shape/square_shape_function.py +926 -0
- warp/fem/space/shape/tet_shape_function.py +834 -0
- warp/fem/space/shape/triangle_shape_function.py +672 -0
- warp/fem/space/tetmesh_function_space.py +271 -0
- warp/fem/space/topology.py +424 -0
- warp/fem/space/trimesh_function_space.py +194 -0
- warp/fem/types.py +99 -0
- warp/fem/utils.py +420 -0
- warp/jax.py +187 -0
- warp/jax_experimental/__init__.py +16 -0
- warp/jax_experimental/custom_call.py +351 -0
- warp/jax_experimental/ffi.py +698 -0
- warp/jax_experimental/xla_ffi.py +602 -0
- warp/math.py +244 -0
- warp/native/array.h +1145 -0
- warp/native/builtin.h +1800 -0
- warp/native/bvh.cpp +492 -0
- warp/native/bvh.cu +791 -0
- warp/native/bvh.h +554 -0
- warp/native/clang/clang.cpp +536 -0
- warp/native/coloring.cpp +613 -0
- warp/native/crt.cpp +51 -0
- warp/native/crt.h +362 -0
- warp/native/cuda_crt.h +1058 -0
- warp/native/cuda_util.cpp +646 -0
- warp/native/cuda_util.h +307 -0
- warp/native/error.cpp +77 -0
- warp/native/error.h +36 -0
- warp/native/exports.h +1878 -0
- warp/native/fabric.h +245 -0
- warp/native/hashgrid.cpp +311 -0
- warp/native/hashgrid.cu +87 -0
- warp/native/hashgrid.h +240 -0
- warp/native/initializer_array.h +41 -0
- warp/native/intersect.h +1230 -0
- warp/native/intersect_adj.h +375 -0
- warp/native/intersect_tri.h +339 -0
- warp/native/marching.cpp +19 -0
- warp/native/marching.cu +514 -0
- warp/native/marching.h +19 -0
- warp/native/mat.h +2220 -0
- warp/native/mathdx.cpp +87 -0
- warp/native/matnn.h +343 -0
- warp/native/mesh.cpp +266 -0
- warp/native/mesh.cu +404 -0
- warp/native/mesh.h +1980 -0
- warp/native/nanovdb/GridHandle.h +366 -0
- warp/native/nanovdb/HostBuffer.h +590 -0
- warp/native/nanovdb/NanoVDB.h +6624 -0
- warp/native/nanovdb/PNanoVDB.h +3390 -0
- warp/native/noise.h +859 -0
- warp/native/quat.h +1371 -0
- warp/native/rand.h +342 -0
- warp/native/range.h +139 -0
- warp/native/reduce.cpp +174 -0
- warp/native/reduce.cu +364 -0
- warp/native/runlength_encode.cpp +79 -0
- warp/native/runlength_encode.cu +61 -0
- warp/native/scan.cpp +47 -0
- warp/native/scan.cu +53 -0
- warp/native/scan.h +23 -0
- warp/native/solid_angle.h +466 -0
- warp/native/sort.cpp +251 -0
- warp/native/sort.cu +277 -0
- warp/native/sort.h +33 -0
- warp/native/sparse.cpp +378 -0
- warp/native/sparse.cu +524 -0
- warp/native/spatial.h +657 -0
- warp/native/svd.h +702 -0
- warp/native/temp_buffer.h +46 -0
- warp/native/tile.h +2584 -0
- warp/native/tile_reduce.h +264 -0
- warp/native/vec.h +1426 -0
- warp/native/volume.cpp +501 -0
- warp/native/volume.cu +67 -0
- warp/native/volume.h +969 -0
- warp/native/volume_builder.cu +477 -0
- warp/native/volume_builder.h +52 -0
- warp/native/volume_impl.h +70 -0
- warp/native/warp.cpp +1082 -0
- warp/native/warp.cu +3636 -0
- warp/native/warp.h +381 -0
- warp/optim/__init__.py +17 -0
- warp/optim/adam.py +163 -0
- warp/optim/linear.py +1137 -0
- warp/optim/sgd.py +112 -0
- warp/paddle.py +407 -0
- warp/render/__init__.py +18 -0
- warp/render/render_opengl.py +3518 -0
- warp/render/render_usd.py +784 -0
- warp/render/utils.py +160 -0
- warp/sim/__init__.py +65 -0
- warp/sim/articulation.py +793 -0
- warp/sim/collide.py +2395 -0
- warp/sim/graph_coloring.py +300 -0
- warp/sim/import_mjcf.py +790 -0
- warp/sim/import_snu.py +227 -0
- warp/sim/import_urdf.py +579 -0
- warp/sim/import_usd.py +894 -0
- warp/sim/inertia.py +324 -0
- warp/sim/integrator.py +242 -0
- warp/sim/integrator_euler.py +1997 -0
- warp/sim/integrator_featherstone.py +2101 -0
- warp/sim/integrator_vbd.py +2048 -0
- warp/sim/integrator_xpbd.py +3292 -0
- warp/sim/model.py +4791 -0
- warp/sim/particles.py +121 -0
- warp/sim/render.py +427 -0
- warp/sim/utils.py +428 -0
- warp/sparse.py +2057 -0
- warp/stubs.py +3333 -0
- warp/tape.py +1203 -0
- warp/tests/__init__.py +1 -0
- warp/tests/__main__.py +4 -0
- warp/tests/assets/curlnoise_golden.npy +0 -0
- warp/tests/assets/mlp_golden.npy +0 -0
- warp/tests/assets/pixel.npy +0 -0
- warp/tests/assets/pnoise_golden.npy +0 -0
- warp/tests/assets/spiky.usd +0 -0
- warp/tests/assets/test_grid.nvdb +0 -0
- warp/tests/assets/test_index_grid.nvdb +0 -0
- warp/tests/assets/test_int32_grid.nvdb +0 -0
- warp/tests/assets/test_vec_grid.nvdb +0 -0
- warp/tests/assets/torus.nvdb +0 -0
- warp/tests/assets/torus.usda +105 -0
- warp/tests/aux_test_class_kernel.py +34 -0
- warp/tests/aux_test_compile_consts_dummy.py +18 -0
- warp/tests/aux_test_conditional_unequal_types_kernels.py +29 -0
- warp/tests/aux_test_dependent.py +29 -0
- warp/tests/aux_test_grad_customs.py +29 -0
- warp/tests/aux_test_instancing_gc.py +26 -0
- warp/tests/aux_test_module_unload.py +23 -0
- warp/tests/aux_test_name_clash1.py +40 -0
- warp/tests/aux_test_name_clash2.py +40 -0
- warp/tests/aux_test_reference.py +9 -0
- warp/tests/aux_test_reference_reference.py +8 -0
- warp/tests/aux_test_square.py +16 -0
- warp/tests/aux_test_unresolved_func.py +22 -0
- warp/tests/aux_test_unresolved_symbol.py +22 -0
- warp/tests/cuda/__init__.py +0 -0
- warp/tests/cuda/test_async.py +676 -0
- warp/tests/cuda/test_ipc.py +124 -0
- warp/tests/cuda/test_mempool.py +233 -0
- warp/tests/cuda/test_multigpu.py +169 -0
- warp/tests/cuda/test_peer.py +139 -0
- warp/tests/cuda/test_pinned.py +84 -0
- warp/tests/cuda/test_streams.py +634 -0
- warp/tests/geometry/__init__.py +0 -0
- warp/tests/geometry/test_bvh.py +200 -0
- warp/tests/geometry/test_hash_grid.py +221 -0
- warp/tests/geometry/test_marching_cubes.py +74 -0
- warp/tests/geometry/test_mesh.py +316 -0
- warp/tests/geometry/test_mesh_query_aabb.py +399 -0
- warp/tests/geometry/test_mesh_query_point.py +932 -0
- warp/tests/geometry/test_mesh_query_ray.py +311 -0
- warp/tests/geometry/test_volume.py +1103 -0
- warp/tests/geometry/test_volume_write.py +346 -0
- warp/tests/interop/__init__.py +0 -0
- warp/tests/interop/test_dlpack.py +729 -0
- warp/tests/interop/test_jax.py +371 -0
- warp/tests/interop/test_paddle.py +800 -0
- warp/tests/interop/test_torch.py +1001 -0
- warp/tests/run_coverage_serial.py +39 -0
- warp/tests/sim/__init__.py +0 -0
- warp/tests/sim/disabled_kinematics.py +244 -0
- warp/tests/sim/flaky_test_sim_grad.py +290 -0
- warp/tests/sim/test_collision.py +604 -0
- warp/tests/sim/test_coloring.py +258 -0
- warp/tests/sim/test_model.py +224 -0
- warp/tests/sim/test_sim_grad_bounce_linear.py +212 -0
- warp/tests/sim/test_sim_kinematics.py +98 -0
- warp/tests/sim/test_vbd.py +597 -0
- warp/tests/test_adam.py +163 -0
- warp/tests/test_arithmetic.py +1096 -0
- warp/tests/test_array.py +2972 -0
- warp/tests/test_array_reduce.py +156 -0
- warp/tests/test_assert.py +250 -0
- warp/tests/test_atomic.py +153 -0
- warp/tests/test_bool.py +220 -0
- warp/tests/test_builtins_resolution.py +1298 -0
- warp/tests/test_closest_point_edge_edge.py +327 -0
- warp/tests/test_codegen.py +810 -0
- warp/tests/test_codegen_instancing.py +1495 -0
- warp/tests/test_compile_consts.py +215 -0
- warp/tests/test_conditional.py +252 -0
- warp/tests/test_context.py +42 -0
- warp/tests/test_copy.py +238 -0
- warp/tests/test_ctypes.py +638 -0
- warp/tests/test_dense.py +73 -0
- warp/tests/test_devices.py +97 -0
- warp/tests/test_examples.py +482 -0
- warp/tests/test_fabricarray.py +996 -0
- warp/tests/test_fast_math.py +74 -0
- warp/tests/test_fem.py +2003 -0
- warp/tests/test_fp16.py +136 -0
- warp/tests/test_func.py +454 -0
- warp/tests/test_future_annotations.py +98 -0
- warp/tests/test_generics.py +656 -0
- warp/tests/test_grad.py +893 -0
- warp/tests/test_grad_customs.py +339 -0
- warp/tests/test_grad_debug.py +341 -0
- warp/tests/test_implicit_init.py +411 -0
- warp/tests/test_import.py +45 -0
- warp/tests/test_indexedarray.py +1140 -0
- warp/tests/test_intersect.py +73 -0
- warp/tests/test_iter.py +76 -0
- warp/tests/test_large.py +177 -0
- warp/tests/test_launch.py +411 -0
- warp/tests/test_lerp.py +151 -0
- warp/tests/test_linear_solvers.py +193 -0
- warp/tests/test_lvalue.py +427 -0
- warp/tests/test_mat.py +2089 -0
- warp/tests/test_mat_lite.py +122 -0
- warp/tests/test_mat_scalar_ops.py +2913 -0
- warp/tests/test_math.py +178 -0
- warp/tests/test_mlp.py +282 -0
- warp/tests/test_module_hashing.py +258 -0
- warp/tests/test_modules_lite.py +44 -0
- warp/tests/test_noise.py +252 -0
- warp/tests/test_operators.py +299 -0
- warp/tests/test_options.py +129 -0
- warp/tests/test_overwrite.py +551 -0
- warp/tests/test_print.py +339 -0
- warp/tests/test_quat.py +2315 -0
- warp/tests/test_rand.py +339 -0
- warp/tests/test_reload.py +302 -0
- warp/tests/test_rounding.py +185 -0
- warp/tests/test_runlength_encode.py +196 -0
- warp/tests/test_scalar_ops.py +105 -0
- warp/tests/test_smoothstep.py +108 -0
- warp/tests/test_snippet.py +318 -0
- warp/tests/test_sparse.py +582 -0
- warp/tests/test_spatial.py +2229 -0
- warp/tests/test_special_values.py +361 -0
- warp/tests/test_static.py +592 -0
- warp/tests/test_struct.py +734 -0
- warp/tests/test_tape.py +204 -0
- warp/tests/test_transient_module.py +93 -0
- warp/tests/test_triangle_closest_point.py +145 -0
- warp/tests/test_types.py +562 -0
- warp/tests/test_utils.py +588 -0
- warp/tests/test_vec.py +1487 -0
- warp/tests/test_vec_lite.py +80 -0
- warp/tests/test_vec_scalar_ops.py +2327 -0
- warp/tests/test_verify_fp.py +100 -0
- warp/tests/tile/__init__.py +0 -0
- warp/tests/tile/test_tile.py +780 -0
- warp/tests/tile/test_tile_load.py +407 -0
- warp/tests/tile/test_tile_mathdx.py +208 -0
- warp/tests/tile/test_tile_mlp.py +402 -0
- warp/tests/tile/test_tile_reduce.py +447 -0
- warp/tests/tile/test_tile_shared_memory.py +247 -0
- warp/tests/tile/test_tile_view.py +173 -0
- warp/tests/unittest_serial.py +47 -0
- warp/tests/unittest_suites.py +427 -0
- warp/tests/unittest_utils.py +468 -0
- warp/tests/walkthrough_debug.py +93 -0
- warp/thirdparty/__init__.py +0 -0
- warp/thirdparty/appdirs.py +598 -0
- warp/thirdparty/dlpack.py +145 -0
- warp/thirdparty/unittest_parallel.py +570 -0
- warp/torch.py +391 -0
- warp/types.py +5230 -0
- warp/utils.py +1137 -0
- warp_lang-1.7.0.dist-info/METADATA +516 -0
- warp_lang-1.7.0.dist-info/RECORD +429 -0
- warp_lang-1.7.0.dist-info/WHEEL +5 -0
- warp_lang-1.7.0.dist-info/licenses/LICENSE.md +202 -0
- warp_lang-1.7.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,870 @@
|
|
|
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()
|