@@ -148,11 +148,13 @@ class ign_ros2_control::IgnitionSystemPrivate
148
148
149
149
// / \brief mapping of mimicked joints to index of joint they mimic
150
150
std::vector<MimicJoint> mimic_joints_;
151
+
152
+ // / \brief Gain which converts position error to a velocity command
153
+ double position_proportional_gain_;
151
154
};
152
155
153
156
namespace ign_ros2_control
154
157
{
155
-
156
158
bool IgnitionSystem::initSim (
157
159
rclcpp::Node::SharedPtr & model_nh,
158
160
std::map<std::string, ignition::gazebo::Entity> & enableJoints,
@@ -173,8 +175,19 @@ bool IgnitionSystem::initSim(
173
175
174
176
this ->dataPtr ->joints_ .resize (this ->dataPtr ->n_dof_ );
175
177
178
+ constexpr double default_gain = 0.1 ;
179
+ if (!this ->nh_ ->get_parameter_or (
180
+ " position_proportional_gain" ,
181
+ this ->dataPtr ->position_proportional_gain_ , default_gain))
182
+ {
183
+ RCLCPP_WARN_STREAM (
184
+ this ->nh_ ->get_logger (),
185
+ " The position_proportional_gain parameter was not defined, defaulting to: " <<
186
+ default_gain);
187
+ }
188
+
176
189
if (this ->dataPtr ->n_dof_ == 0 ) {
177
- RCLCPP_WARN_STREAM (this ->nh_ ->get_logger (), " There is not joint available " );
190
+ RCLCPP_ERROR_STREAM (this ->nh_ ->get_logger (), " There is no joint available" );
178
191
return false ;
179
192
}
180
193
@@ -531,14 +544,12 @@ IgnitionSystem::perform_command_mode_switch(
531
544
{
532
545
this ->dataPtr ->joints_ [j].joint_control_method &=
533
546
static_cast <ControlMethod_>(VELOCITY & EFFORT);
534
- }
535
- if (interface_name == (this ->dataPtr ->joints_ [j].name + " /" +
547
+ } else if (interface_name == (this ->dataPtr ->joints_ [j].name + " /" + // NOLINT
536
548
hardware_interface::HW_IF_VELOCITY))
537
549
{
538
550
this ->dataPtr ->joints_ [j].joint_control_method &=
539
551
static_cast <ControlMethod_>(POSITION & EFFORT);
540
- }
541
- if (interface_name == (this ->dataPtr ->joints_ [j].name + " /" +
552
+ } else if (interface_name == (this ->dataPtr ->joints_ [j].name + " /" + // NOLINT
542
553
hardware_interface::HW_IF_EFFORT))
543
554
{
544
555
this ->dataPtr ->joints_ [j].joint_control_method &=
@@ -552,13 +563,11 @@ IgnitionSystem::perform_command_mode_switch(
552
563
hardware_interface::HW_IF_POSITION))
553
564
{
554
565
this ->dataPtr ->joints_ [j].joint_control_method |= POSITION;
555
- }
556
- if (interface_name == (this ->dataPtr ->joints_ [j].name + " /" +
566
+ } else if (interface_name == (this ->dataPtr ->joints_ [j].name + " /" + // NOLINT
557
567
hardware_interface::HW_IF_VELOCITY))
558
568
{
559
569
this ->dataPtr ->joints_ [j].joint_control_method |= VELOCITY;
560
- }
561
- if (interface_name == (this ->dataPtr ->joints_ [j].name + " /" +
570
+ } else if (interface_name == (this ->dataPtr ->joints_ [j].name + " /" + // NOLINT
562
571
hardware_interface::HW_IF_EFFORT))
563
572
{
564
573
this ->dataPtr ->joints_ [j].joint_control_method |= EFFORT;
@@ -588,16 +597,14 @@ hardware_interface::return_type IgnitionSystem::write(
588
597
*jointVelCmd = ignition::gazebo::components::JointVelocityCmd (
589
598
{this ->dataPtr ->joints_ [i].joint_velocity_cmd });
590
599
}
591
- }
592
-
593
- if (this ->dataPtr ->joints_ [i].joint_control_method & POSITION) {
600
+ } else if (this ->dataPtr ->joints_ [i].joint_control_method & POSITION) {
594
601
// Get error in position
595
602
double error;
596
603
error = (this ->dataPtr ->joints_ [i].joint_position -
597
604
this ->dataPtr ->joints_ [i].joint_position_cmd ) * *this ->dataPtr ->update_rate ;
598
605
599
606
// Calculate target velcity
600
- double targetVel = -error;
607
+ double target_vel = -this -> dataPtr -> position_proportional_gain_ * error;
601
608
602
609
auto vel =
603
610
this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointVelocityCmd>(
@@ -606,13 +613,11 @@ hardware_interface::return_type IgnitionSystem::write(
606
613
if (vel == nullptr ) {
607
614
this ->dataPtr ->ecm ->CreateComponent (
608
615
this ->dataPtr ->joints_ [i].sim_joint ,
609
- ignition::gazebo::components::JointVelocityCmd ({targetVel }));
616
+ ignition::gazebo::components::JointVelocityCmd ({target_vel }));
610
617
} else if (!vel->Data ().empty ()) {
611
- vel->Data ()[0 ] = targetVel ;
618
+ vel->Data ()[0 ] = target_vel ;
612
619
}
613
- }
614
-
615
- if (this ->dataPtr ->joints_ [i].joint_control_method & EFFORT) {
620
+ } else if (this ->dataPtr ->joints_ [i].joint_control_method & EFFORT) {
616
621
if (!this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointForceCmd>(
617
622
this ->dataPtr ->joints_ [i].sim_joint ))
618
623
{
@@ -626,6 +631,22 @@ hardware_interface::return_type IgnitionSystem::write(
626
631
*jointEffortCmd = ignition::gazebo::components::JointForceCmd (
627
632
{this ->dataPtr ->joints_ [i].joint_effort_cmd });
628
633
}
634
+ } else {
635
+ // Fallback case is a velocity command of zero
636
+ double target_vel = 0.0 ;
637
+ auto vel =
638
+ this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointVelocityCmd>(
639
+ this ->dataPtr ->joints_ [i].sim_joint );
640
+
641
+ if (vel == nullptr ) {
642
+ this ->dataPtr ->ecm ->CreateComponent (
643
+ this ->dataPtr ->joints_ [i].sim_joint ,
644
+ ignition::gazebo::components::JointVelocityCmd ({target_vel}));
645
+ } else if (!vel->Data ().empty ()) {
646
+ vel->Data ()[0 ] = target_vel;
647
+ } else if (!vel->Data ().empty ()) {
648
+ vel->Data ()[0 ] = target_vel;
649
+ }
629
650
}
630
651
}
631
652
0 commit comments