-
Notifications
You must be signed in to change notification settings - Fork 33
Glowing led decks in sim. #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9a52470
Add change_material function in visualize utils.
yufei4hua 29398f4
Fix linting.
yufei4hua f1df346
Add example for rendering led decks.
yufei4hua d5ca60d
Simplify code.
yufei4hua 33c844c
Use lagecy drones in example.
yufei4hua b3e3d5e
Add test cases for change_material.
yufei4hua bb59cff
Clean up test error function.
yufei4hua c0c4c96
Final cleaning up.
yufei4hua File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import numpy as np | ||
|
|
||
| from crazyflow.control.control import Control | ||
| from crazyflow.sim import Sim | ||
| from crazyflow.sim.visualize import change_material | ||
|
|
||
|
|
||
| def main(): | ||
| """Spawn 25 drones in one world and activate led decks.""" | ||
| sim = Sim(n_drones=25, drone_model="cf21B_500", control=Control.state) | ||
| fps = 60 | ||
| cmd = np.zeros((sim.n_worlds, sim.n_drones, 4)) | ||
| cmd[..., 3] = sim.data.params.mass[0, 0, 0] * 9.81 | ||
| rgbas = np.random.default_rng(0).uniform(0, 1, (sim.n_drones, 4)) | ||
| rgbas[..., 3] = 1.0 | ||
|
|
||
| init_pos = np.array(sim.data.states.pos[0, :, :]) | ||
| cmd = np.zeros((sim.n_worlds, sim.n_drones, 13)) | ||
| cmd[:, :, :3] = init_pos | ||
| cmd[:, :, 2] += 1.5 | ||
|
|
||
| for i in range(int(10 * sim.control_freq)): | ||
| sim.state_control(cmd) | ||
| sim.step(sim.freq // sim.control_freq) | ||
| if ((i * fps) % sim.control_freq) < fps: | ||
| even_ids = np.arange(0, sim.n_drones, 2) | ||
| odd_ids = np.arange(1, sim.n_drones, 2) | ||
| emission = np.sin(i / sim.control_freq * np.pi) | ||
| change_material( | ||
| sim, | ||
| mat_name="led_top", | ||
| drone_ids=even_ids, | ||
| rgba=rgbas[even_ids, :], | ||
| emission=emission, | ||
| ) | ||
| change_material( | ||
| sim, | ||
| mat_name="led_bot", | ||
| drone_ids=odd_ids, | ||
| rgba=rgbas[odd_ids, :], | ||
| emission=emission, | ||
| ) | ||
| sim.render() | ||
| sim.close() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Submodule drone-models
updated
from 73a083 to cce86c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |||||
|
|
||||||
| import jax | ||||||
| import jax.numpy as jnp | ||||||
| import mujoco | ||||||
| import numpy as np | ||||||
| import pytest | ||||||
| from jax import Array | ||||||
|
|
@@ -16,6 +17,7 @@ | |||||
| from crazyflow.sim import Physics, Sim | ||||||
| from crazyflow.sim.data import ControlData | ||||||
| from crazyflow.sim.sim import sync_sim2mjx | ||||||
| from crazyflow.sim.visualize import change_material | ||||||
|
|
||||||
| if TYPE_CHECKING: | ||||||
| from typing import Any | ||||||
|
|
@@ -493,3 +495,63 @@ def test_scan_results(physics: Physics): | |||||
| assert np.all(pos_loop_steps[..., 2] > 0.1), "Drones should have moved" | ||||||
| assert np.allclose(pos_scan_steps, pos_loop_steps), "Scan results should be identical" | ||||||
| sim.close() | ||||||
|
|
||||||
|
|
||||||
| @pytest.mark.unit | ||||||
| @pytest.mark.parametrize("drone_model", ["cf2x_L250", "cf2x_P250", "cf2x_T350", "cf21B_500"]) | ||||||
| @pytest.mark.parametrize("mat_name", ["led_top", "led_bot"]) | ||||||
| def test_change_material(device: str, drone_model: str, mat_name: str): | ||||||
| """change_material should broadcast RGBA/emission and update MuJoCo materials appropriately.""" | ||||||
| n_drones = 2 | ||||||
|
|
||||||
| sim = Sim(n_drones=n_drones, drone_model=drone_model, device=device) | ||||||
|
|
||||||
| drone_ids = np.array([0, 1], dtype=int) | ||||||
| rgba = 0.42 * np.ones((n_drones, 4), dtype=float) | ||||||
| emission = 0.42 * np.ones((n_drones,), dtype=float) | ||||||
|
|
||||||
| change_material(sim, mat_name=mat_name, drone_ids=drone_ids, rgba=rgba, emission=emission) | ||||||
|
|
||||||
| mj_model = sim.mj_model | ||||||
| mat0 = mujoco.mj_name2id(mj_model, mujoco.mjtObj.mjOBJ_MATERIAL, f"{mat_name}:0") | ||||||
| mat1 = mujoco.mj_name2id(mj_model, mujoco.mjtObj.mjOBJ_MATERIAL, f"{mat_name}:1") | ||||||
| expected_rgba = 0.42 * np.ones((4,), dtype=float) | ||||||
| expected_emission = 0.42 | ||||||
| np.testing.assert_allclose(mj_model.mat_rgba[mat0], expected_rgba) | ||||||
| np.testing.assert_allclose(mj_model.mat_rgba[mat1], expected_rgba) | ||||||
| assert mj_model.mat_emission[mat0] == pytest.approx(expected_emission) | ||||||
| assert mj_model.mat_emission[mat1] == pytest.approx(expected_emission) | ||||||
|
|
||||||
|
|
||||||
| @pytest.mark.unit | ||||||
| def test_change_material_errors(device: str): | ||||||
| """Test that change_material raises the expected errors for bad inputs.""" | ||||||
| n_drones = 2 | ||||||
| sim = Sim(n_drones=n_drones, device=device) | ||||||
|
|
||||||
| drone_ids = np.array([0, 1], dtype=int) | ||||||
| rgba = np.ones((n_drones, 4), dtype=float) | ||||||
| emission = np.ones((n_drones,), dtype=float) | ||||||
|
|
||||||
| with pytest.raises(ValueError): | ||||||
| change_material( | ||||||
| sim, mat_name="bad_mat", drone_ids=drone_ids, rgba=rgba, emission=emission | ||||||
| ) | ||||||
|
|
||||||
| with pytest.raises(ValueError, match="drone_ids must be 1D array"): | ||||||
| change_material( | ||||||
| sim, | ||||||
| mat_name="led_top", | ||||||
| drone_ids=np.array(2, dtype=int), | ||||||
| rgba=rgba, | ||||||
| emission=emission, | ||||||
| ) | ||||||
|
|
||||||
| with pytest.raises(ValueError, match=r"drone_ids must be in range \[0, 1\]"): | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, but again, please check.
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked up, so "r" s needed when "[]" appears in a regular expression. |
||||||
| change_material( | ||||||
| sim, | ||||||
| mat_name="led_top", | ||||||
| drone_ids=np.arange(3, dtype=int), | ||||||
| rgba=rgba, | ||||||
| emission=emission, | ||||||
| ) | ||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add tests for this function! Even if we are not rendering in the tests, we should at least try to see if this errors as expected and succeeds for the correct input.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added 2 small tests under test_sim.py
One mainly to make sure all the drone models have "led_top" and "led_bot" in xml file.
One to test the error output of the function.