Skip to content

Commit d83d135

Browse files
peci1azeey
andauthored
apply_joint_force: Simplified using ComponentDefault (gazebosim#2630)
Signed-off-by: Martin Pecka <peci1@seznam.cz> Co-authored-by: Addisu Z. Taddese <addisu@openrobotics.org>
1 parent a8c5ec8 commit d83d135

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

src/systems/apply_joint_force/ApplyJointForce.cc

+3-12
Original file line numberDiff line numberDiff line change
@@ -156,23 +156,14 @@ void ApplyJointForce::PreUpdate(const UpdateInfo &_info,
156156

157157
// Update joint force
158158
//! [jointForceComponent]
159-
auto force = _ecm.Component<components::JointForceCmd>(
160-
this->dataPtr->jointEntity);
159+
auto force = _ecm.ComponentDefault<components::JointForceCmd>(
160+
this->dataPtr->jointEntity, {0.0});
161161
//! [jointForceComponent]
162162

163163
std::lock_guard<std::mutex> lock(this->dataPtr->jointForceCmdMutex);
164164

165165
//! [modifyComponent]
166-
if (force == nullptr)
167-
{
168-
_ecm.CreateComponent(
169-
this->dataPtr->jointEntity,
170-
components::JointForceCmd({this->dataPtr->jointForceCmd}));
171-
}
172-
else
173-
{
174-
force->Data()[0] += this->dataPtr->jointForceCmd;
175-
}
166+
force->Data()[0] += this->dataPtr->jointForceCmd;
176167
//! [modifyComponent]
177168
}
178169

tutorials/component_jointforcecmd.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ This component allows us to set the force command on a joint.
66

77
Programmatic usage of this component can be found in the source code for
88
systems and integration tests, such as the
9-
[joint integration test](https://github.com/gazebosim/gz-sim/blob/gz-sim8/test/integration/joint.cc),
9+
[joint integration test](https://github.com/gazebosim/gz-sim/blob/gz-sim9/test/integration/joint.cc),
1010
the \ref gz::sim::systems::ApplyJointForce system
11-
([source code](https://github.com/gazebosim/gz-sim/tree/gz-sim8/src/systems/apply_joint_force)),
11+
([source code](https://github.com/gazebosim/gz-sim/tree/gz-sim9/src/systems/apply_joint_force)),
1212
and others.
1313

14-
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:
14+
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:
1515

1616
```bash
1717
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
5656
`JointForceCmd` component.
5757
This will apply a force command to the joint.
5858

59-
In `PreUpdate()`, look for the component:
59+
In `PreUpdate()`, look for the component. If it does not exist yet, create it,
60+
setting its default value to `{0.0}` (which means, in this particular case,
61+
a 1-element `std::vector<double>` with value 0.0 in its only element):
6062

6163
\snippet src/systems/apply_joint_force/ApplyJointForce.cc jointForceComponent
6264

63-
Create it if it does not exist yet, and modify it:
65+
Now modify the value stored in the component:
6466

6567
\snippet src/systems/apply_joint_force/ApplyJointForce.cc modifyComponent
6668

0 commit comments

Comments
 (0)