Skip to content

Commit

Permalink
Setting the intiial velocity for a model or joint (#693)
Browse files Browse the repository at this point in the history
Signed-off-by: Louise Poubel <[email protected]>
  • Loading branch information
chapulina committed Jul 30, 2021
1 parent 12a60f6 commit 57ce159
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 3 deletions.
1 change: 1 addition & 0 deletions examples/worlds/joint_controller.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
filename="ignition-gazebo-joint-controller-system"
name="ignition::gazebo::systems::JointController">
<joint_name>j1</joint_name>
<initial_velocity>5.0</initial_velocity>
</plugin>
</model>

Expand Down
2 changes: 2 additions & 0 deletions examples/worlds/velocity_control.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@
<plugin
filename="ignition-gazebo-velocity-control-system"
name="ignition::gazebo::systems::VelocityControl">
<initial_linear>0.3 0 0</initial_linear>
<initial_angular>0 0 -0.1</initial_angular>
</plugin>

</model>
Expand Down
7 changes: 7 additions & 0 deletions src/systems/joint_controller/JointController.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ void JointController::Configure(const Entity &_entity,
return;
}

if (_sdf->HasElement("initial_velocity"))
{
this->dataPtr->jointVelCmd = _sdf->Get<double>("initial_velocity");
ignmsg << "Joint velocity initialized to ["
<< this->dataPtr->jointVelCmd << "]" << std::endl;
}

if (_sdf->HasElement("use_force_commands") &&
_sdf->Get<bool>("use_force_commands"))
{
Expand Down
5 changes: 5 additions & 0 deletions src/systems/joint_controller/JointController.hh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ namespace systems
/// using force commands. If this parameter is not set or is false, the
/// controller will use velocity commands internally.
///
/// `<topic>` Topic to receive commands in. Defaults to
/// `/model/<model_name>/joint/<joint_name>/cmd_vel`.
///
/// `<initial_velocity>` Velocity to start with.
///
/// ### Velocity mode: No additional parameters are required.
///
/// ### Force mode: The controller accepts the next optional parameters:
Expand Down
19 changes: 19 additions & 0 deletions src/systems/velocity_control/VelocityControl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ void VelocityControl::Configure(const Entity &_entity,
return;
}

if (_sdf->HasElement("initial_linear"))
{
this->dataPtr->linearVelocity = _sdf->Get<math::Vector3d>("initial_linear");
msgs::Set(this->dataPtr->targetVel.mutable_linear(),
this->dataPtr->linearVelocity);
ignmsg << "Linear velocity initialized to ["
<< this->dataPtr->linearVelocity << "]" << std::endl;
}

if (_sdf->HasElement("initial_angular"))
{
this->dataPtr->angularVelocity =
_sdf->Get<math::Vector3d>("initial_angular");
msgs::Set(this->dataPtr->targetVel.mutable_angular(),
this->dataPtr->angularVelocity);
ignmsg << "Angular velocity initialized to ["
<< this->dataPtr->angularVelocity << "]" << std::endl;
}

// Subscribe to model commands
std::string modelTopic{
"/model/" + this->dataPtr->model.Name(_ecm) + "/cmd_vel"};
Expand Down
9 changes: 9 additions & 0 deletions src/systems/velocity_control/VelocityControl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ namespace systems

/// \brief Linear and angular velocity controller
/// which is directly set on a model.
///
/// ## System Parameters
///
/// `<topic>` Topic to receive commands in. Defaults to
/// `/model/<model_name>/cmd_vel`.
///
/// `<initial_linear>` Linear velocity to start with.
///
/// `<initial_angular>` Linear velocity to start with.
class VelocityControl
: public System,
public ISystemConfigure,
Expand Down
15 changes: 12 additions & 3 deletions test/integration/joint_controller_system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,21 @@ TEST_F(JointControllerTestFixture, JointVelocityCommand)

server.AddSystem(testSystem.systemPtr);

const std::size_t initIters = 10;
const std::size_t initIters = 1000;
server.Run(true, initIters, false);
EXPECT_EQ(initIters, angularVelocities.size());
for (const auto &angVel : angularVelocities)
for (auto i = 0u; i < angularVelocities.size(); ++i)
{
EXPECT_NEAR(0, angVel.Length(), TOL);
if (i == 0)
{
EXPECT_NEAR(0.0, angularVelocities[i].Length(), TOL)
<< "Iteration [" << i << "]";
}
else
{
EXPECT_NEAR(5.0, angularVelocities[i].Length(), TOL)
<< "Iteration [" << i << "]";
}
}

angularVelocities.clear();
Expand Down
1 change: 1 addition & 0 deletions test/worlds/joint_controller.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
filename="ignition-gazebo-joint-controller-system"
name="ignition::gazebo::systems::JointController">
<joint_name>j1</joint_name>
<initial_velocity>5.0</initial_velocity>
</plugin>
</model>

Expand Down
2 changes: 2 additions & 0 deletions test/worlds/velocity_control.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@
<plugin
filename="ignition-gazebo-velocity-control-system"
name="ignition::gazebo::systems::VelocityControl">
<initial_linear>0.3 0 0</initial_linear>
<initial_angular>0 0 -0.1</initial_angular>
</plugin>

</model>
Expand Down

0 comments on commit 57ce159

Please sign in to comment.