From e41eb493cbd9f6464e7f7fb68dda667b30bb8ec5 Mon Sep 17 00:00:00 2001 From: Martin Pecka Date: Mon, 16 Sep 2024 16:25:35 +0200 Subject: [PATCH] apply_joint_force: Simplified using ComponentDefault Signed-off-by: Martin Pecka --- src/systems/apply_joint_force/ApplyJointForce.cc | 15 +++------------ tutorials/component_jointforcecmd.md | 12 +++++++----- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/systems/apply_joint_force/ApplyJointForce.cc b/src/systems/apply_joint_force/ApplyJointForce.cc index b7ea5eba18..d1bc8cf0b9 100644 --- a/src/systems/apply_joint_force/ApplyJointForce.cc +++ b/src/systems/apply_joint_force/ApplyJointForce.cc @@ -156,23 +156,14 @@ void ApplyJointForce::PreUpdate(const UpdateInfo &_info, // Update joint force //! [jointForceComponent] - auto force = _ecm.Component( - this->dataPtr->jointEntity); + auto force = _ecm.ComponentDefault( + this->dataPtr->jointEntity, {0.0}); //! [jointForceComponent] std::lock_guard lock(this->dataPtr->jointForceCmdMutex); //! [modifyComponent] - if (force == nullptr) - { - _ecm.CreateComponent( - this->dataPtr->jointEntity, - components::JointForceCmd({this->dataPtr->jointForceCmd})); - } - else - { - force->Data()[0] += this->dataPtr->jointForceCmd; - } + force->Data()[0] += this->dataPtr->jointForceCmd; //! [modifyComponent] } diff --git a/tutorials/component_jointforcecmd.md b/tutorials/component_jointforcecmd.md index e3a86134ac..9264a93b01 100644 --- a/tutorials/component_jointforcecmd.md +++ b/tutorials/component_jointforcecmd.md @@ -6,12 +6,12 @@ This component allows us to set the force command on a joint. Programmatic usage of this component can be found in the source code for systems and integration tests, such as the -[joint integration test](https://github.com/gazebosim/gz-sim/blob/gz-sim8/test/integration/joint.cc), +[joint integration test](https://github.com/gazebosim/gz-sim/blob/gz-sim9/test/integration/joint.cc), the \ref gz::sim::systems::ApplyJointForce system -([source code](https://github.com/gazebosim/gz-sim/tree/gz-sim8/src/systems/apply_joint_force)), +([source code](https://github.com/gazebosim/gz-sim/tree/gz-sim9/src/systems/apply_joint_force)), and others. -The corresponding world SDF is [`apply_joint_force.sdf`](https://github.com/gazebosim/gz-sim/blob/gz-sim8/examples/worlds/apply_joint_force.sdf), which you can look at in Gazebo: +The corresponding world SDF is [`apply_joint_force.sdf`](https://github.com/gazebosim/gz-sim/blob/gz-sim9/examples/worlds/apply_joint_force.sdf), which you can look at in Gazebo: ```bash gz sim apply_joint_force.sdf @@ -56,11 +56,13 @@ In this case, we use the joint entity found above to look for and modify its `JointForceCmd` component. This will apply a force command to the joint. -In `PreUpdate()`, look for the component: +In `PreUpdate()`, look for the component. If it does not exist yet, create it, +setting its default value to `{0.0}` (which means, in this particular case, +a 1-element `std::vector` with value 0.0 in its only element): \snippet src/systems/apply_joint_force/ApplyJointForce.cc jointForceComponent -Create it if it does not exist yet, and modify it: +Now modify the value stored in the component: \snippet src/systems/apply_joint_force/ApplyJointForce.cc modifyComponent