Skip to content

Commit 1323b4b

Browse files
AndyZelivanov93
authored andcommitted
Various bug fixes (#114)
1 parent 4298fdf commit 1323b4b

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

ign_ros2_control/src/ign_system.cpp

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,13 @@ class ign_ros2_control::IgnitionSystemPrivate
148148

149149
/// \brief mapping of mimicked joints to index of joint they mimic
150150
std::vector<MimicJoint> mimic_joints_;
151+
152+
/// \brief Gain which converts position error to a velocity command
153+
double position_proportional_gain_;
151154
};
152155

153156
namespace ign_ros2_control
154157
{
155-
156158
bool IgnitionSystem::initSim(
157159
rclcpp::Node::SharedPtr & model_nh,
158160
std::map<std::string, ignition::gazebo::Entity> & enableJoints,
@@ -173,8 +175,19 @@ bool IgnitionSystem::initSim(
173175

174176
this->dataPtr->joints_.resize(this->dataPtr->n_dof_);
175177

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+
176189
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");
178191
return false;
179192
}
180193

@@ -531,14 +544,12 @@ IgnitionSystem::perform_command_mode_switch(
531544
{
532545
this->dataPtr->joints_[j].joint_control_method &=
533546
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
536548
hardware_interface::HW_IF_VELOCITY))
537549
{
538550
this->dataPtr->joints_[j].joint_control_method &=
539551
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
542553
hardware_interface::HW_IF_EFFORT))
543554
{
544555
this->dataPtr->joints_[j].joint_control_method &=
@@ -552,13 +563,11 @@ IgnitionSystem::perform_command_mode_switch(
552563
hardware_interface::HW_IF_POSITION))
553564
{
554565
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
557567
hardware_interface::HW_IF_VELOCITY))
558568
{
559569
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
562571
hardware_interface::HW_IF_EFFORT))
563572
{
564573
this->dataPtr->joints_[j].joint_control_method |= EFFORT;
@@ -588,16 +597,14 @@ hardware_interface::return_type IgnitionSystem::write(
588597
*jointVelCmd = ignition::gazebo::components::JointVelocityCmd(
589598
{this->dataPtr->joints_[i].joint_velocity_cmd});
590599
}
591-
}
592-
593-
if (this->dataPtr->joints_[i].joint_control_method & POSITION) {
600+
} else if (this->dataPtr->joints_[i].joint_control_method & POSITION) {
594601
// Get error in position
595602
double error;
596603
error = (this->dataPtr->joints_[i].joint_position -
597604
this->dataPtr->joints_[i].joint_position_cmd) * *this->dataPtr->update_rate;
598605

599606
// Calculate target velcity
600-
double targetVel = -error;
607+
double target_vel = -this->dataPtr->position_proportional_gain_ * error;
601608

602609
auto vel =
603610
this->dataPtr->ecm->Component<ignition::gazebo::components::JointVelocityCmd>(
@@ -606,13 +613,11 @@ hardware_interface::return_type IgnitionSystem::write(
606613
if (vel == nullptr) {
607614
this->dataPtr->ecm->CreateComponent(
608615
this->dataPtr->joints_[i].sim_joint,
609-
ignition::gazebo::components::JointVelocityCmd({targetVel}));
616+
ignition::gazebo::components::JointVelocityCmd({target_vel}));
610617
} else if (!vel->Data().empty()) {
611-
vel->Data()[0] = targetVel;
618+
vel->Data()[0] = target_vel;
612619
}
613-
}
614-
615-
if (this->dataPtr->joints_[i].joint_control_method & EFFORT) {
620+
} else if (this->dataPtr->joints_[i].joint_control_method & EFFORT) {
616621
if (!this->dataPtr->ecm->Component<ignition::gazebo::components::JointForceCmd>(
617622
this->dataPtr->joints_[i].sim_joint))
618623
{
@@ -626,6 +631,22 @@ hardware_interface::return_type IgnitionSystem::write(
626631
*jointEffortCmd = ignition::gazebo::components::JointForceCmd(
627632
{this->dataPtr->joints_[i].joint_effort_cmd});
628633
}
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+
}
629650
}
630651
}
631652

0 commit comments

Comments
 (0)