Skip to content

Commit 77f4c00

Browse files
authored
Physics: remove *VelocityCmd at each time step (#2228)
This implements a suggestion from #1926 to delete all `*VelocityCmd` components after each time step. This also updates the logic for the following two systems to handle this change: * MulticopterMotorModel: handle missing component Since JointVelocityCmd components are deleted after each timestep, don't skip updating forces and moments if the component does not exist, and use the SetComponent API to more cleanly handle the component creation logic. A syntax error in the the quadcopter test worlds was fixed as well. * TrajectoryFollower: don't need to remove commands Now that the physics system is removing AngularVelocityCmd components at every timestep, that logic can be removed from the trajectory follower system. Signed-off-by: Steve Peters <scpeters@openrobotics.org>
1 parent 301832d commit 77f4c00

File tree

6 files changed

+66
-61
lines changed

6 files changed

+66
-61
lines changed

Migration.md

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ Deprecated code produces compile-time warnings. These warning serve as
55
notification to users that their code should be upgraded. The next major
66
release will remove the deprecated code.
77

8+
## Gazebo Sim 8.x to 9.0
9+
10+
* **Modified**:
11+
+ In the Physics system, all `*VelocityCmd` components are now deleted after
12+
each time step, whereas previously the component values were set to `0`
13+
after each time step. Persistent velocity commands should be reapplied at
14+
each time step.
15+
816
## Gazebo Sim 7.x to 8.0
917
* **Deprecated**
1018
+ `gz::sim::components::Factory::Register(const std::string &_type, ComponentDescriptorBase *_compDesc)` and

src/systems/multicopter_motor_model/MulticopterMotorModel.cc

+4-13
Original file line numberDiff line numberDiff line change
@@ -447,14 +447,6 @@ void MulticopterMotorModel::PreUpdate(const UpdateInfo &_info,
447447
doUpdateForcesAndMoments = false;
448448
}
449449

450-
if (!_ecm.Component<components::JointVelocityCmd>(
451-
this->dataPtr->jointEntity))
452-
{
453-
_ecm.CreateComponent(this->dataPtr->jointEntity,
454-
components::JointVelocityCmd({0}));
455-
doUpdateForcesAndMoments = false;
456-
}
457-
458450
if (!_ecm.Component<components::WorldPose>(this->dataPtr->linkEntity))
459451
{
460452
_ecm.CreateComponent(this->dataPtr->linkEntity, components::WorldPose());
@@ -682,11 +674,10 @@ void MulticopterMotorModelPrivate::UpdateForcesAndMoments(
682674
refMotorRotVel = this->rotorVelocityFilter->UpdateFilter(
683675
this->refMotorInput, this->samplingTime);
684676

685-
const auto jointVelCmd = _ecm.Component<components::JointVelocityCmd>(
686-
this->jointEntity);
687-
*jointVelCmd = components::JointVelocityCmd(
688-
{this->turningDirection * refMotorRotVel
689-
/ this->rotorVelocitySlowdownSim});
677+
_ecm.SetComponentData<components::JointVelocityCmd>(
678+
this->jointEntity,
679+
{this->turningDirection * refMotorRotVel
680+
/ this->rotorVelocitySlowdownSim});
690681
}
691682
}
692683
}

src/systems/physics/Physics.cc

+43-19
Original file line numberDiff line numberDiff line change
@@ -3628,34 +3628,58 @@ void PhysicsPrivate::UpdateSim(EntityComponentManager &_ecm,
36283628
return true;
36293629
});
36303630

3631-
_ecm.Each<components::JointVelocityCmd>(
3632-
[&](const Entity &, components::JointVelocityCmd *_vel) -> bool
3633-
{
3634-
std::fill(_vel->Data().begin(), _vel->Data().end(), 0.0);
3635-
return true;
3636-
});
3631+
{
3632+
std::vector<Entity> entitiesJointVelocityCmd;
3633+
_ecm.Each<components::JointVelocityCmd>(
3634+
[&](const Entity &_entity, components::JointVelocityCmd *) -> bool
3635+
{
3636+
entitiesJointVelocityCmd.push_back(_entity);
3637+
return true;
3638+
});
3639+
3640+
for (const auto entity : entitiesJointVelocityCmd)
3641+
{
3642+
_ecm.RemoveComponent<components::JointVelocityCmd>(entity);
3643+
}
3644+
}
36373645

36383646
_ecm.Each<components::SlipComplianceCmd>(
36393647
[&](const Entity &, components::SlipComplianceCmd *_slip) -> bool
36403648
{
36413649
std::fill(_slip->Data().begin(), _slip->Data().end(), 0.0);
36423650
return true;
36433651
});
3644-
GZ_PROFILE_END();
36453652

3646-
_ecm.Each<components::AngularVelocityCmd>(
3647-
[&](const Entity &, components::AngularVelocityCmd *_vel) -> bool
3648-
{
3649-
_vel->Data() = math::Vector3d::Zero;
3650-
return true;
3651-
});
3653+
{
3654+
std::vector<Entity> entitiesAngularVelocityCmd;
3655+
_ecm.Each<components::AngularVelocityCmd>(
3656+
[&](const Entity &_entity, components::AngularVelocityCmd *) -> bool
3657+
{
3658+
entitiesAngularVelocityCmd.push_back(_entity);
3659+
return true;
3660+
});
36523661

3653-
_ecm.Each<components::LinearVelocityCmd>(
3654-
[&](const Entity &, components::LinearVelocityCmd *_vel) -> bool
3655-
{
3656-
_vel->Data() = math::Vector3d::Zero;
3657-
return true;
3658-
});
3662+
for (const auto entity : entitiesAngularVelocityCmd)
3663+
{
3664+
_ecm.RemoveComponent<components::AngularVelocityCmd>(entity);
3665+
}
3666+
}
3667+
3668+
{
3669+
std::vector<Entity> entitiesLinearVelocityCmd;
3670+
_ecm.Each<components::LinearVelocityCmd>(
3671+
[&](const Entity &_entity, components::LinearVelocityCmd *) -> bool
3672+
{
3673+
entitiesLinearVelocityCmd.push_back(_entity);
3674+
return true;
3675+
});
3676+
3677+
for (const auto entity : entitiesLinearVelocityCmd)
3678+
{
3679+
_ecm.RemoveComponent<components::LinearVelocityCmd>(entity);
3680+
}
3681+
}
3682+
GZ_PROFILE_END();
36593683

36603684
// Update joint positions
36613685
GZ_PROFILE_BEGIN("Joints");

src/systems/trajectory_follower/TrajectoryFollower.cc

+3-21
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,6 @@ class gz::sim::systems::TrajectoryFollowerPrivate
111111
/// \brief Whether the trajectory follower behavior should be paused or not.
112112
public: bool paused = false;
113113

114-
/// \brief Angular velocity set to zero
115-
public: bool zeroAngVelSet = false;
116-
117114
/// \brief Force angular velocity to be zero when bearing is reached
118115
public: bool forceZeroAngVel = false;
119116
};
@@ -390,37 +387,22 @@ void TrajectoryFollower::PreUpdate(
390387
// Transform the force and torque to the world frame.
391388
// Move commands. The vehicle always move forward (X direction).
392389
gz::math::Vector3d forceWorld;
390+
gz::math::Vector3d torqueWorld;
393391
if (std::abs(bearing.Degree()) <= this->dataPtr->bearingTolerance)
394392
{
395393
forceWorld = (*comPose).Rot().RotateVector(
396394
gz::math::Vector3d(this->dataPtr->forceToApply, 0, 0));
397395

398396
// force angular velocity to be zero when bearing is reached
399-
if (this->dataPtr->forceZeroAngVel && !this->dataPtr->zeroAngVelSet &&
397+
if (this->dataPtr->forceZeroAngVel &&
400398
math::equal (std::abs(bearing.Degree()), 0.0,
401399
this->dataPtr->bearingTolerance * 0.5))
402400
{
403401
this->dataPtr->link.SetAngularVelocity(_ecm, math::Vector3d::Zero);
404-
this->dataPtr->zeroAngVelSet = true;
405402
}
406403
}
407-
gz::math::Vector3d torqueWorld;
408-
if (std::abs(bearing.Degree()) > this->dataPtr->bearingTolerance)
404+
else
409405
{
410-
// remove angular velocity component otherwise the physics system will set
411-
// the zero ang vel command every iteration
412-
if (this->dataPtr->forceZeroAngVel && this->dataPtr->zeroAngVelSet)
413-
{
414-
auto angVelCmdComp = _ecm.Component<components::AngularVelocityCmd>(
415-
this->dataPtr->link.Entity());
416-
if (angVelCmdComp)
417-
{
418-
_ecm.RemoveComponent<components::AngularVelocityCmd>(
419-
this->dataPtr->link.Entity());
420-
this->dataPtr->zeroAngVelSet = false;
421-
}
422-
}
423-
424406
int sign = static_cast<int>(std::abs(bearing.Degree()) / bearing.Degree());
425407
torqueWorld = (*comPose).Rot().RotateVector(
426408
gz::math::Vector3d(0, 0, sign * this->dataPtr->torqueToApply));

test/worlds/quadcopter.sdf

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
</geometry>
9292
</collision>
9393
<visual name="rotor_0_visual">
94-
<pose>0 0 0 1.57 0 0 0</pose>
94+
<pose>0 0 0 1.57 0 0</pose>
9595
<geometry>
9696
<cylinder>
9797
<length>0.2</length>
@@ -136,7 +136,7 @@
136136
</geometry>
137137
</collision>
138138
<visual name="rotor_1_visual">
139-
<pose>0 0 0 1.57 0 0 0</pose>
139+
<pose>0 0 0 1.57 0 0</pose>
140140
<geometry>
141141
<cylinder>
142142
<length>0.2</length>
@@ -181,7 +181,7 @@
181181
</geometry>
182182
</collision>
183183
<visual name="rotor_2_visual">
184-
<pose>0 0 0 1.57 0 0 0</pose>
184+
<pose>0 0 0 1.57 0 0</pose>
185185
<geometry>
186186
<cylinder>
187187
<length>0.2</length>
@@ -226,7 +226,7 @@
226226
</geometry>
227227
</collision>
228228
<visual name="rotor_3_visual">
229-
<pose>0 0 0 1.57 0 0 0</pose>
229+
<pose>0 0 0 1.57 0 0</pose>
230230
<geometry>
231231
<cylinder>
232232
<length>0.2</length>

test/worlds/quadcopter_velocity_control.sdf

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
</geometry>
9292
</collision>
9393
<visual name="rotor_0_visual">
94-
<pose>0 0 0 1.57 0 0 0</pose>
94+
<pose>0 0 0 1.57 0 0</pose>
9595
<geometry>
9696
<cylinder>
9797
<length>0.2</length>
@@ -136,7 +136,7 @@
136136
</geometry>
137137
</collision>
138138
<visual name="rotor_1_visual">
139-
<pose>0 0 0 1.57 0 0 0</pose>
139+
<pose>0 0 0 1.57 0 0</pose>
140140
<geometry>
141141
<cylinder>
142142
<length>0.2</length>
@@ -181,7 +181,7 @@
181181
</geometry>
182182
</collision>
183183
<visual name="rotor_2_visual">
184-
<pose>0 0 0 1.57 0 0 0</pose>
184+
<pose>0 0 0 1.57 0 0</pose>
185185
<geometry>
186186
<cylinder>
187187
<length>0.2</length>
@@ -226,7 +226,7 @@
226226
</geometry>
227227
</collision>
228228
<visual name="rotor_3_visual">
229-
<pose>0 0 0 1.57 0 0 0</pose>
229+
<pose>0 0 0 1.57 0 0</pose>
230230
<geometry>
231231
<cylinder>
232232
<length>0.2</length>

0 commit comments

Comments
 (0)