9 ros::init(argc, argv,
"hebiros_gazebo_plugin_node");
12 this->
n->advertiseService<AddGroupFromNamesSrv::Request, AddGroupFromNamesSrv::Response>(
13 "hebiros_gazebo_plugin/add_group", boost::bind(
17 if (_sdf->HasElement(
"robotNamespace")) {
18 this->robot_namespace = _sdf->GetElement(
"robotNamespace")->Get<std::string>();
20 if (this->robot_namespace ==
"") {
29 ROS_INFO(
"Loaded hebiros gazebo plugin");
37 auto hebiros_group = group_pair.second;
40 ros::Duration iteration_time = current_time - hebiros_group->prev_time;
41 hebiros_group->prev_time = current_time;
42 if (hebiros_group->group_added) {
50 for (
auto joint_pair : hebiros_group->joints) {
52 std::string joint_name = joint_pair.first;
53 std::shared_ptr<HebirosGazeboJoint> hebiros_joint = hebiros_group->joints[joint_name];
55 physics::JointPtr joint = this->
model->GetJoint(joint_name+
"/"+hebiros_joint->model_name);
59 std::shared_ptr<HebirosGazeboJoint> hebiros_joint = joint_pair.second;
62 ros::Duration elapsed_time = current_time - hebiros_group->start_time;
63 ros::Duration feedback_time = current_time - hebiros_group->prev_feedback_time;
65 int i = hebiros_joint->feedback_index;
67 joint->SetProvideFeedback(
true);
68 double position = joint->GetAngle(0).Radian();
69 double velocity = joint->GetVelocity(0);
70 physics::JointWrench wrench = joint->GetForceTorque(0);
71 auto trans = joint->GetChild()->GetInitialRelativePose().rot;
72 double effort = (-1 * (trans * wrench.body1Torque)).z;
74 hebiros_group->feedback.position[i] = position;
75 hebiros_group->feedback.velocity[i] = velocity;
76 hebiros_group->feedback.effort[i] = effort;
78 hebiros_group->feedback.accelerometer[i] = hebiros_joint->accelerometer;
79 hebiros_group->feedback.gyro[i] = hebiros_joint->gyro;
82 hebiros_group->feedback.motor_winding_temperature[i] = hebiros_joint->temperature.getMotorWindingTemperature();
83 hebiros_group->feedback.motor_housing_temperature[i] = hebiros_joint->temperature.getMotorHousingTemperature();
84 hebiros_group->feedback.board_temperature[i] = hebiros_joint->temperature.getActuatorBodyTemperature();
86 if (hebiros_group->command_received) {
88 position, velocity, effort, iteration_time);
90 if ((hebiros_group->command_lifetime == 0) || (
91 elapsed_time.
toSec() <= hebiros_group->command_lifetime/1000.0)) {
93 joint->SetForce(0, force);
96 int j = hebiros_joint->command_index;
97 if (i < hebiros_group->command_target.position.size()) {
98 hebiros_group->feedback.position_command[j] = hebiros_group->command_target.position[j];
100 if (i < hebiros_group->command_target.velocity.size()) {
101 hebiros_group->feedback.velocity_command[j] = hebiros_group->command_target.velocity[j];
103 if (i < hebiros_group->command_target.effort.size()) {
104 hebiros_group->feedback.effort_command[j] = hebiros_group->command_target.effort[j];
108 if (!hebiros_group->feedback_pub.getTopic().empty() &&
109 feedback_time.
toSec() >= 1.0/hebiros_group->feedback_frequency) {
111 hebiros_group->feedback_pub.publish(hebiros_group->feedback);
112 hebiros_group->prev_feedback_time = current_time;
116 ROS_WARN(
"Joint %s not found", joint_name.c_str());
123 AddGroupFromNamesSrv::Response &res) {
126 ROS_WARN(
"Group %s already exists", req.group_name.c_str());
130 std::shared_ptr<HebirosGazeboGroup> hebiros_group =
131 std::make_shared<HebirosGazeboGroup>(req.group_name, this->
n);
135 for (
int i = 0; i < req.families.size(); i++) {
136 for (
int j = 0; j < req.names.size(); j++) {
138 if ((req.families.size() == 1) ||
139 (req.families.size() == req.names.size() && i == j)) {
141 std::string joint_name = req.families[i]+
"/"+req.names[j];
142 hebiros_group->feedback.name.push_back(joint_name);
149 int size = hebiros_group->joints.size();
151 hebiros_group->feedback.position.resize(size);
152 hebiros_group->feedback.motor_winding_temperature.resize(size);
153 hebiros_group->feedback.motor_housing_temperature.resize(size);
154 hebiros_group->feedback.board_temperature.resize(size);
155 hebiros_group->feedback.velocity.resize(size);
156 hebiros_group->feedback.effort.resize(size);
157 hebiros_group->feedback.position_command.resize(size);
158 hebiros_group->feedback.velocity_command.resize(size);
159 hebiros_group->feedback.effort_command.resize(size);
160 hebiros_group->feedback.accelerometer.resize(size);
161 hebiros_group->feedback.gyro.resize(size);
163 hebiros_group->feedback_pub = this->
n->advertise<FeedbackMsg>(
164 "hebiros_gazebo_plugin/feedback/"+req.group_name, 100);
166 hebiros_group->group_added =
true;
173 std::string joint_name) {
175 std::string model_name =
"";
177 if (this->
model->GetJoint(joint_name+
"/X5_1")) {
180 else if (this->
model->GetJoint(joint_name+
"/X5_4")) {
183 else if (this->
model->GetJoint(joint_name+
"/X5_9")) {
186 else if (this->
model->GetJoint(joint_name+
"/X8_3")) {
190 else if (this->
model->GetJoint(joint_name+
"/X8_9")) {
194 else if (this->
model->GetJoint(joint_name+
"/X8_16")) {
195 model_name =
"X8_16";
199 std::shared_ptr<HebirosGazeboJoint> hebiros_joint =
200 std::make_shared<HebirosGazeboJoint>(joint_name, model_name, is_x8, this->
n);
202 hebiros_joint->feedback_index = hebiros_group->joints.size();
203 hebiros_joint->command_index = hebiros_joint->feedback_index;
206 hebiros_group->joints[joint_name] = hebiros_joint;
ros::ServiceServer add_group_srv
bool SrvAddGroup(AddGroupFromNamesSrv::Request &req, AddGroupFromNamesSrv::Response &res)
void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
static void SetSettings(std::shared_ptr< HebirosGazeboGroup > hebiros_group, std::shared_ptr< HebirosGazeboJoint > hebiros_joint)
event::ConnectionPtr update_connection
void OnUpdate(const common::UpdateInfo &_info)
void AddJointToGroup(std::shared_ptr< HebirosGazeboGroup > hebiros_group, std::string joint_name)
std::string robot_namespace
std::map< std::string, std::shared_ptr< HebirosGazeboGroup > > hebiros_groups
void UpdateGroup(std::shared_ptr< HebirosGazeboGroup > hebiros_group, const ros::Duration &iteration_time)
GZ_REGISTER_MODEL_PLUGIN(HebirosGazeboPlugin)
static double ComputeForce(std::shared_ptr< HebirosGazeboGroup > hebiros_group, std::shared_ptr< HebirosGazeboJoint > hebiros_joint, double position, double velocity, double effort, const ros::Duration &iteration_time)
std::shared_ptr< ros::NodeHandle > n