|
| 1 | +#include "ign_ros2_control/MimicJointSystem.hh" |
| 2 | + |
| 3 | +#include "ignition/gazebo/Model.hh" |
| 4 | +#include "ignition/gazebo/components/JointForceCmd.hh" |
| 5 | +#include "ignition/gazebo/components/JointVelocityCmd.hh" |
| 6 | +#include "ignition/gazebo/components/JointPosition.hh" |
| 7 | + |
| 8 | +#include <ignition/math/PID.hh> |
| 9 | + |
| 10 | + |
| 11 | +//! [registerMimicJointSystem] |
| 12 | +#include <ignition/plugin/Register.hh> |
| 13 | + |
| 14 | +IGNITION_ADD_PLUGIN( |
| 15 | + ign_ros2_control::MimicJointSystem, |
| 16 | + ignition::gazebo::System, |
| 17 | + ign_ros2_control::MimicJointSystem::ISystemConfigure, |
| 18 | + ign_ros2_control::MimicJointSystem::ISystemPreUpdate, |
| 19 | + ign_ros2_control::MimicJointSystem::ISystemUpdate, |
| 20 | + ign_ros2_control::MimicJointSystem::ISystemPostUpdate) |
| 21 | +//! [registerMimicJointSystem] |
| 22 | + |
| 23 | +using namespace ign_ros2_control; |
| 24 | + |
| 25 | +class ign_ros2_control::MimicJointSystemPrivate{ |
| 26 | + |
| 27 | +/// \brief Joint Entity |
| 28 | +public: ignition::gazebo::Entity jointEntity{ignition::gazebo::kNullEntity}; |
| 29 | + |
| 30 | + /// \brief ECM pointer |
| 31 | + ignition::gazebo::EntityComponentManager * ecm{nullptr}; |
| 32 | + |
| 33 | +/// \brief Joint name |
| 34 | +public: std::string jointName; |
| 35 | + |
| 36 | +public: std::string mimicJointName; |
| 37 | + |
| 38 | +/// \brief Commanded joint position |
| 39 | +public: double jointPosCmd{0.0}; |
| 40 | + |
| 41 | +public: double multiplier{1.0}; |
| 42 | + |
| 43 | +public: double offset{0.0}; |
| 44 | + |
| 45 | +/// \brief Model interface |
| 46 | +public: ignition::gazebo::Model model{ignition::gazebo::kNullEntity}; |
| 47 | + |
| 48 | +/// \brief Position PID controller. |
| 49 | +public: ignition::math::PID posPid; |
| 50 | + |
| 51 | +/// \brief Joint index to be used. |
| 52 | +public: unsigned int jointIndex = 0u; |
| 53 | + |
| 54 | +/// \brief Operation modes |
| 55 | +enum OperationMode |
| 56 | +{ |
| 57 | + /// \brief Use PID to achieve positional control |
| 58 | + PID, |
| 59 | + /// \brief Bypass PID completely. This means the joint will move to that |
| 60 | + /// position bypassing the physics engine. |
| 61 | + ABS |
| 62 | +}; |
| 63 | + |
| 64 | +/// \brief Joint position mode |
| 65 | +public: OperationMode mode = OperationMode::PID; |
| 66 | + |
| 67 | +}; |
| 68 | + |
| 69 | +MimicJointSystem::MimicJointSystem(): dataPtr(std::make_unique<ign_ros2_control::MimicJointSystemPrivate>()) |
| 70 | +{ |
| 71 | +} |
| 72 | + |
| 73 | +MimicJointSystem::~MimicJointSystem() |
| 74 | +{ |
| 75 | + this->dataPtr.reset(); |
| 76 | +} |
| 77 | + |
| 78 | +void MimicJointSystem::Configure(const ignition::gazebo::Entity &_entity, |
| 79 | + const std::shared_ptr<const sdf::Element> &_sdf, |
| 80 | + ignition::gazebo::EntityComponentManager &_ecm, |
| 81 | + ignition::gazebo::EventManager &_eventMgr) { |
| 82 | + |
| 83 | + // Make sure the controller is attached to a valid model |
| 84 | + const auto model = ignition::gazebo::Model(_entity); |
| 85 | + if (!model.Valid(_ecm)) { |
| 86 | + |
| 87 | + ignerr << "MimicJointSystem Failed to initialize because ["<<model.Name(_ecm) <<"] (Entity="<<_entity <<")] is not a model.. "; |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + this->dataPtr->ecm = &_ecm; |
| 92 | + |
| 93 | + // Get params from SDF |
| 94 | + this->dataPtr->jointName = _sdf->Get<std::string>("joint_name"); |
| 95 | + |
| 96 | + if (this->dataPtr->jointName.empty()) |
| 97 | + { |
| 98 | + ignerr << "MimicJointSystem found an empty joint_name parameter. " |
| 99 | + << "Failed to initialize."; |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + this->dataPtr->mimicJointName = _sdf->Get<std::string>("mimic_joint_name"); |
| 104 | + if (this->dataPtr->mimicJointName.empty()) |
| 105 | + { |
| 106 | + ignerr << "MimicJointSystem found an empty mimic_joint_name parameter. " |
| 107 | + << "Failed to initialize."; |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + if (_sdf->HasElement("multiplier")) |
| 112 | + { |
| 113 | + this->dataPtr->multiplier = _sdf->Get<unsigned int>("multiplier"); |
| 114 | + } |
| 115 | + |
| 116 | + if (_sdf->HasElement("offset")) |
| 117 | + { |
| 118 | + this->dataPtr->offset = _sdf->Get<unsigned int>("offset"); |
| 119 | + } |
| 120 | + |
| 121 | + if (_sdf->HasElement("joint_index")) |
| 122 | + { |
| 123 | + this->dataPtr->jointIndex = _sdf->Get<unsigned int>("joint_index"); |
| 124 | + } |
| 125 | + |
| 126 | + // PID parameters |
| 127 | + double p = 1; |
| 128 | + double i = 0.1; |
| 129 | + double d = 0.01; |
| 130 | + double iMax = 1; |
| 131 | + double iMin = -1; |
| 132 | + double cmdMax = 1000; |
| 133 | + double cmdMin = -1000; |
| 134 | + double cmdOffset = 0; |
| 135 | + |
| 136 | + if (_sdf->HasElement("p_gain")) |
| 137 | + { |
| 138 | + p = _sdf->Get<double>("p_gain"); |
| 139 | + } |
| 140 | + if (_sdf->HasElement("i_gain")) |
| 141 | + { |
| 142 | + i = _sdf->Get<double>("i_gain"); |
| 143 | + } |
| 144 | + if (_sdf->HasElement("d_gain")) |
| 145 | + { |
| 146 | + d = _sdf->Get<double>("d_gain"); |
| 147 | + } |
| 148 | + if (_sdf->HasElement("i_max")) |
| 149 | + { |
| 150 | + iMax = _sdf->Get<double>("i_max"); |
| 151 | + } |
| 152 | + if (_sdf->HasElement("i_min")) |
| 153 | + { |
| 154 | + iMin = _sdf->Get<double>("i_min"); |
| 155 | + } |
| 156 | + if (_sdf->HasElement("cmd_max")) |
| 157 | + { |
| 158 | + cmdMax = _sdf->Get<double>("cmd_max"); |
| 159 | + } |
| 160 | + if (_sdf->HasElement("cmd_min")) |
| 161 | + { |
| 162 | + cmdMin = _sdf->Get<double>("cmd_min"); |
| 163 | + } |
| 164 | + if (_sdf->HasElement("cmd_offset")) |
| 165 | + { |
| 166 | + cmdOffset = _sdf->Get<double>("cmd_offset"); |
| 167 | + } |
| 168 | + if (_sdf->HasElement("use_velocity_commands")) |
| 169 | + { |
| 170 | + auto useVelocityCommands = _sdf->Get<bool>("use_velocity_commands"); |
| 171 | + if (useVelocityCommands) |
| 172 | + { |
| 173 | + this->dataPtr->mode = |
| 174 | + MimicJointSystemPrivate::OperationMode::ABS; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + this->dataPtr->posPid.Init(p, i, d, iMax, iMin, cmdMax, cmdMin, cmdOffset); |
| 179 | + |
| 180 | + |
| 181 | + if (_sdf->HasElement("initial_position")) |
| 182 | + { |
| 183 | + this->dataPtr->jointPosCmd = _sdf->Get<double>("initial_position"); |
| 184 | + } |
| 185 | + |
| 186 | + |
| 187 | +} |
| 188 | + |
| 189 | +void MimicJointSystem::PreUpdate(const ignition::gazebo::UpdateInfo &_info, |
| 190 | + ignition::gazebo::EntityComponentManager &_ecm) |
| 191 | +{ |
| 192 | +// ignmsg << "MimicJointSystem::PreUpdate" << std::endl; |
| 193 | +} |
| 194 | + |
| 195 | +void MimicJointSystem::Update(const ignition::gazebo::UpdateInfo &_info, |
| 196 | + ignition::gazebo::EntityComponentManager &_ecm) |
| 197 | +{ |
| 198 | +// ignmsg << "MimicJointSystem::Update" << std::endl; |
| 199 | +} |
| 200 | + |
| 201 | +void MimicJointSystem::PostUpdate(const ignition::gazebo::UpdateInfo &_info, |
| 202 | + const ignition::gazebo::EntityComponentManager &_ecm) |
| 203 | +{ |
| 204 | +// ignmsg << "MimicJointSystem::PostUpdate" << std::endl; |
| 205 | +} |
0 commit comments