Skip to content

Commit c11b195

Browse files
committed
Testing clamping.
1 parent 87496ff commit c11b195

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

ign_ros2_control/src/ign_system.cpp

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include <ignition/gazebo/components/JointVelocity.hh>
3232
#include <ignition/gazebo/components/JointVelocityCmd.hh>
3333
#include <ignition/gazebo/components/JointAxis.hh>
34+
#include <ignition/gazebo/components/JointType.hh>
35+
#include <ignition/gazebo/components/Joint.hh>
3436

3537
#include <ignition/gazebo/components/LinearAcceleration.hh>
3638
#include <ignition/gazebo/components/Name.hh>
@@ -46,6 +48,7 @@
4648
#include <iostream>
4749

4850
#include <hardware_interface/hardware_info.hpp>
51+
#include <normApi.h>
4952

5053
struct jointData
5154
{
@@ -241,11 +244,6 @@ bool IgnitionSystem::initSim(
241244
_ecm.CreateComponent(simjoint, ignition::gazebo::components::JointForce());
242245
}
243246

244-
// init PID
245-
// assuming every joint has axis
246-
const auto * jointAxis =
247-
this->dataPtr->ecm->Component<ignition::gazebo::components::JointAxis>(
248-
this->dataPtr->joints_[j].sim_joint);
249247
// PID parameters
250248
double p_gain =
251249
(hardware_info.joints[j].parameters.find("p") ==
@@ -752,10 +750,6 @@ hardware_interface::return_type IgnitionSystem::write(
752750
error, std::chrono::duration<double>(
753751
period.to_chrono<std::chrono::nanoseconds>()));
754752

755-
igndbg << "target_force ( joint [" <<
756-
this->dataPtr->joints_[i].name << "] ) = " << target_force <<
757-
"\n";
758-
759753
auto forceCmd =
760754
this->dataPtr->ecm->Component<ignition::gazebo::components::JointForceCmd>(
761755
this->dataPtr->joints_[i].sim_joint);
@@ -794,6 +788,10 @@ hardware_interface::return_type IgnitionSystem::write(
794788
this->dataPtr->ecm->Component<ignition::gazebo::components::JointAxis>(
795789
this->dataPtr->joints_[mimic_joint.joint_index].sim_joint);
796790

791+
const auto * jointAxisMimicked =
792+
this->dataPtr->ecm->Component<ignition::gazebo::components::JointAxis>(
793+
this->dataPtr->joints_[mimic_joint.mimicked_joint_index].sim_joint);
794+
797795
if (mimic_interface == "position") {
798796
// Get the joint position
799797
double position_mimicked_joint =
@@ -806,33 +804,33 @@ hardware_interface::return_type IgnitionSystem::write(
806804
double position_error =
807805
position_mimic_joint - std::clamp(
808806
position_mimicked_joint * mimic_joint.multiplier,
809-
jointAxis->Data().Lower(), jointAxis->Data().Upper());
807+
jointAxisMimicked->Data().Lower(), jointAxisMimicked->Data().Upper());
810808

811809
position_error = copysign(1.0, position_error) * std::clamp(
812810
std::abs(
813-
position_error), 0.0, std::abs(
814-
jointAxis->Data().Upper() - jointAxis->Data().Lower()));
811+
position_error), 0.0, std::abs(jointAxisMimicked->Data().Upper() - jointAxisMimicked->Data().Lower()));
815812

816813
// calculate target force/torque
817-
double target_force = this->dataPtr->joints_[mimic_joint.joint_index].pid.Update(
814+
double target_vel = this->dataPtr->joints_[mimic_joint.joint_index].pid.Update(
818815
position_error,
819816
std::chrono::duration<double>(period.to_chrono<std::chrono::nanoseconds>()));
820817

821-
igndbg << "target_force ( mimic joint [" <<
822-
this->dataPtr->joints_[mimic_joint.joint_index].name << "] ) = " << target_force <<
823-
"\n";
824-
825-
auto forceCmd =
826-
this->dataPtr->ecm->Component<ignition::gazebo::components::JointForceCmd>(
818+
auto velCmd =
819+
this->dataPtr->ecm->Component<ignition::gazebo::components::JointVelocityCmd>(
827820
this->dataPtr->joints_[mimic_joint.joint_index].sim_joint);
828821

829-
if (forceCmd == nullptr) {
822+
// igndbg<<"position_error ["<<this->dataPtr->joints_[mimic_joint.joint_index].name<<"] = "<<position_error<<"\n";
823+
// igndbg<<"target_force ["<<this->dataPtr->joints_[mimic_joint.joint_index].name<<"] = "<<target_force<<"\n";
824+
825+
826+
if (velCmd == nullptr) {
830827
this->dataPtr->ecm->CreateComponent(
831828
this->dataPtr->joints_[mimic_joint.joint_index].sim_joint,
832-
ignition::gazebo::components::JointForceCmd({0}));
829+
ignition::gazebo::components::JointVelocityCmd({0}));
830+
833831
} else {
834-
*forceCmd = ignition::gazebo::components::JointForceCmd(
835-
{target_force});
832+
*velCmd = ignition::gazebo::components::JointVelocityCmd(
833+
{target_vel});
836834
}
837835
}/*
838836
if (mimic_interface == "velocity") {

0 commit comments

Comments
 (0)