-
Notifications
You must be signed in to change notification settings - Fork 206
[DRAFT] Add Model.joint_act that reads from MjData.qfrc_actuator
#871
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
base: main
Are you sure you want to change the base?
Conversation
📝 WalkthroughWalkthroughAdds per-DOF joint activation tracking across builder, model, and state as joint_act. Wires joint_act through builder creation, collapse/restore, add_builder transfer, and finalize export. Extends Model and State to store joint_act. Updates MuJoCo solver to convert and synchronize joint_act with MuJoCo qact in both MJ→Newton and Newton→MJ flows and kernels. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant MJ as MuJoCo mj_data
participant ConvF as convert_mj_coords_to_warp_kernel
participant NW as Newton State/Model
Note over MJ,NW: MJ → Newton update (read)
MJ->>ConvF: qpos, qvel, qact
ConvF->>NW: joint_q, joint_qd, joint_act
Note right of NW: State now holds joint_act aligned with DOFs
sequenceDiagram
autonumber
participant NW as Newton State/Model
participant ConvB as convert_warp_coords_to_mj_kernel
participant MJ as MuJoCo mj_data
Note over NW,MJ: Newton → MJ update (write)
NW->>ConvB: joint_q, joint_qd, joint_act
ConvB->>MJ: qpos, qvel, qact
Note right of MJ: qact (e.g., qfrc_actuator) updated from joint_act
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Actionable comments posted: 1
📜 Review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
newton/_src/sim/builder.py(6 hunks)newton/_src/sim/model.py(3 hunks)newton/_src/sim/state.py(1 hunks)newton/_src/solvers/mujoco/solver_mujoco.py(13 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
newton/_src/sim/builder.py (2)
newton/_src/sim/control.py (1)
clear(67-77)newton/_src/sim/state.py (1)
requires_grad(81-87)
newton/_src/solvers/mujoco/solver_mujoco.py (2)
newton/_src/sim/state.py (2)
joint_coord_count(104-108)joint_dof_count(111-115)newton/_src/sim/model.py (1)
state(443-478)
🔇 Additional comments (10)
newton/_src/sim/state.py (1)
64-65: LGTM!The new
joint_actfield follows the established pattern for joint state attributes (matchingjoint_qandjoint_qd). The documentation clearly specifies the shape and dtype, maintaining consistency with other joint-related fields.newton/_src/sim/model.py (3)
229-230: LGTM!The
joint_actfield is properly added to the Model class with clear documentation following the same pattern asjoint_qandjoint_qd.
410-410: LGTM!Correctly registers
joint_actin the attribute frequency mapping as a "joint_dof" attribute, consistent withjoint_qdand other per-DOF attributes.
476-476: LGTM!The
state()method properly clonesjoint_actwhen creating a new State object, maintaining consistency with the cloning ofjoint_qandjoint_qd.newton/_src/sim/builder.py (6)
420-420: LGTM!The
joint_actlist is properly initialized inModelBuilder.__init__, following the same pattern asjoint_qdandjoint_f.
1105-1105: LGTM!Including
joint_actinmore_builder_attrsensures it is correctly copied when usingadd_builder()to compose models, maintaining consistency withjoint_qdandjoint_f.
1341-1341: LGTM!The per-DOF initialization of
joint_actwith0.0inadd_jointis consistent with the initialization ofjoint_qdandjoint_fon adjacent lines (1340, 1342).
2090-2090: LGTM!The
joint_actslice is properly captured in the collapsed joint data structure withincollapse_fixed_joints, matching the handling ofqdon line 2089.
2284-2284: LGTM!Clearing
joint_actduring thecollapse_fixed_jointsrestructuring is necessary and consistent with the clearing of other joint attributes likejoint_qd(line 2283).
4339-4339: LGTM!The finalization of
joint_actinto awp.arraywithdtype=wp.float32andrequires_gradparameter is correct and follows the same pattern asjoint_qd(line 4321) andjoint_f(line 4340).
| joint_act[wqd_i + 0] = qact[worldid, q_i + 0] | ||
| joint_act[wqd_i + 1] = qact[worldid, q_i + 1] | ||
| joint_act[wqd_i + 2] = qact[worldid, q_i + 2] | ||
| joint_act[wqd_i + 3] = qact[worldid, q_i + 3] | ||
| joint_act[wqd_i + 4] = qact[worldid, q_i + 4] | ||
| joint_act[wqd_i + 5] = qact[worldid, q_i + 5] | ||
|
|
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.
Fix qact indexing for free joints.
qact/qfrc_actuator follow the velocity layout (stride = nv), but here we index them with q_i (qpos stride). On the second and later free joints this walks past the end of the actuator array, corrupting data. Use qd_i just like we already do for velocities.
Apply this diff:
- joint_act[wqd_i + 0] = qact[worldid, q_i + 0]
- joint_act[wqd_i + 1] = qact[worldid, q_i + 1]
- joint_act[wqd_i + 2] = qact[worldid, q_i + 2]
- joint_act[wqd_i + 3] = qact[worldid, q_i + 3]
- joint_act[wqd_i + 4] = qact[worldid, q_i + 4]
- joint_act[wqd_i + 5] = qact[worldid, q_i + 5]
+ joint_act[wqd_i + 0] = qact[worldid, qd_i + 0]
+ joint_act[wqd_i + 1] = qact[worldid, qd_i + 1]
+ joint_act[wqd_i + 2] = qact[worldid, qd_i + 2]
+ joint_act[wqd_i + 3] = qact[worldid, qd_i + 3]
+ joint_act[wqd_i + 4] = qact[worldid, qd_i + 4]
+ joint_act[wqd_i + 5] = qact[worldid, qd_i + 5]📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| joint_act[wqd_i + 0] = qact[worldid, q_i + 0] | |
| joint_act[wqd_i + 1] = qact[worldid, q_i + 1] | |
| joint_act[wqd_i + 2] = qact[worldid, q_i + 2] | |
| joint_act[wqd_i + 3] = qact[worldid, q_i + 3] | |
| joint_act[wqd_i + 4] = qact[worldid, q_i + 4] | |
| joint_act[wqd_i + 5] = qact[worldid, q_i + 5] | |
| joint_act[wqd_i + 0] = qact[worldid, qd_i + 0] | |
| joint_act[wqd_i + 1] = qact[worldid, qd_i + 1] | |
| joint_act[wqd_i + 2] = qact[worldid, qd_i + 2] | |
| joint_act[wqd_i + 3] = qact[worldid, qd_i + 3] | |
| joint_act[wqd_i + 4] = qact[worldid, qd_i + 4] | |
| joint_act[wqd_i + 5] = qact[worldid, qd_i + 5] |
Description
Adds attribute
State.joint_actto read actuation fromMjData.qfrc_actuator. This PR is a draft to illustrate the current challenge of modifying (ModelBuilder,Model,State,SolverMujoco) in order to read attributes fromMjDatathat are not currently supported in Newton.Newton Migration Guide
Please ensure the migration guide for warp.sim users is up-to-date with the changes made in this PR.
docs/migration.rstis up-to dateBefore your PR is "Ready for review"
newton/tests/test_examples.py)pre-commit run -aSummary by CodeRabbit