Skip to content

Commit 40bb49c

Browse files
use gz-physics#283 to implement joint_states/effort feedback (backport #186) (#607)
Co-authored-by: Andreas Bihlmaier <andreas.bihlmaier@gmx.de>
1 parent c9e6bef commit 40bb49c

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

gz_ros2_control/src/gz_system.cpp

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@
2424
#ifdef GZ_HEADERS
2525
#include <gz/msgs/imu.pb.h>
2626

27+
#include <gz/physics/Geometry.hh>
2728
#include <gz/sim/components/AngularVelocity.hh>
2829
#include <gz/sim/components/Imu.hh>
29-
#include <gz/sim/components/JointForce.hh>
30+
#include <gz/sim/components/JointAxis.hh>
3031
#include <gz/sim/components/JointForceCmd.hh>
3132
#include <gz/sim/components/JointPosition.hh>
3233
#include <gz/sim/components/JointPositionReset.hh>
33-
#include <gz/sim/components/JointVelocity.hh>
34+
#include <gz/sim/components/JointTransmittedWrench.hh>
35+
#include <gz/sim/components/JointType.hh>
3436
#include <gz/sim/components/JointVelocityCmd.hh>
37+
#include <gz/sim/components/JointVelocity.hh>
3538
#include <gz/sim/components/JointVelocityReset.hh>
3639
#include <gz/sim/components/LinearAcceleration.hh>
3740
#include <gz/sim/components/Name.hh>
@@ -41,17 +44,22 @@
4144
#include <gz/transport/Node.hh>
4245
#define GZ_TRANSPORT_NAMESPACE gz::transport::
4346
#define GZ_MSGS_NAMESPACE gz::msgs::
47+
#define GZ_PHYSICS_NAMESPACE gz::physics::
48+
#define GZ_VECTOR_DOT dot
4449
#else
4550
#include <ignition/msgs/imu.pb.h>
4651

52+
#include <ignition/math/Vector3.hh>
4753
#include <ignition/gazebo/components/AngularVelocity.hh>
4854
#include <ignition/gazebo/components/Imu.hh>
49-
#include <ignition/gazebo/components/JointForce.hh>
55+
#include <ignition/gazebo/components/JointAxis.hh>
5056
#include <ignition/gazebo/components/JointForceCmd.hh>
5157
#include <ignition/gazebo/components/JointPosition.hh>
5258
#include <ignition/gazebo/components/JointPositionReset.hh>
53-
#include <ignition/gazebo/components/JointVelocity.hh>
59+
#include <ignition/gazebo/components/JointTransmittedWrench.hh>
60+
#include <ignition/gazebo/components/JointType.hh>
5461
#include <ignition/gazebo/components/JointVelocityCmd.hh>
62+
#include <ignition/gazebo/components/JointVelocity.hh>
5563
#include <ignition/gazebo/components/JointVelocityReset.hh>
5664
#include <ignition/gazebo/components/LinearAcceleration.hh>
5765
#include <ignition/gazebo/components/Name.hh>
@@ -61,6 +69,8 @@
6169
#include <ignition/transport/Node.hh>
6270
#define GZ_TRANSPORT_NAMESPACE ignition::transport::
6371
#define GZ_MSGS_NAMESPACE ignition::msgs::
72+
#define GZ_PHYSICS_NAMESPACE ignition::math::
73+
#define GZ_VECTOR_DOT Dot
6474
#endif
6575

6676
#include <hardware_interface/hardware_info.hpp>
@@ -70,6 +80,12 @@ struct jointData
7080
/// \brief Joint's names.
7181
std::string name;
7282

83+
/// \brief Joint's type.
84+
sdf::JointType joint_type;
85+
86+
/// \brief Joint's axis.
87+
sdf::JointAxis joint_axis;
88+
7389
/// \brief Current joint position
7490
double joint_position;
7591

@@ -236,6 +252,10 @@ bool GazeboSimSystem::initSim(
236252

237253
sim::Entity simjoint = enableJoints[joint_name];
238254
this->dataPtr->joints_[j].sim_joint = simjoint;
255+
this->dataPtr->joints_[j].joint_type = _ecm.Component<sim::components::JointType>(
256+
simjoint)->Data();
257+
this->dataPtr->joints_[j].joint_axis = _ecm.Component<sim::components::JointAxis>(
258+
simjoint)->Data();
239259

240260
// Create joint position component if one doesn't exist
241261
if (!_ecm.EntityHasComponentType(
@@ -253,12 +273,12 @@ bool GazeboSimSystem::initSim(
253273
_ecm.CreateComponent(simjoint, sim::components::JointVelocity());
254274
}
255275

256-
// Create joint force component if one doesn't exist
276+
// Create joint transmitted wrench component if one doesn't exist
257277
if (!_ecm.EntityHasComponentType(
258278
simjoint,
259-
sim::components::JointForce().TypeId()))
279+
sim::components::JointTransmittedWrench().TypeId()))
260280
{
261-
_ecm.CreateComponent(simjoint, sim::components::JointForce());
281+
_ecm.CreateComponent(simjoint, sim::components::JointTransmittedWrench());
262282
}
263283

264284
// Accept this joint and continue configuration
@@ -576,11 +596,10 @@ hardware_interface::return_type GazeboSimSystem::read(
576596
this->dataPtr->ecm->Component<sim::components::JointVelocity>(
577597
this->dataPtr->joints_[i].sim_joint);
578598

579-
// TODO(ahcorde): Revisit this part ignitionrobotics/ign-physics#124
580-
// Get the joint force
581-
// const auto * jointForce =
582-
// _ecm.Component<sim::components::JointForce>(
583-
// this->dataPtr->sim_joints_[j]);
599+
// Get the joint force via joint transmitted wrench
600+
const auto * jointWrench =
601+
this->dataPtr->ecm->Component<sim::components::JointTransmittedWrench>(
602+
this->dataPtr->joints_[i].sim_joint);
584603

585604
// Get the joint position
586605
const auto * jointPositions =
@@ -589,7 +608,19 @@ hardware_interface::return_type GazeboSimSystem::read(
589608

590609
this->dataPtr->joints_[i].joint_position = jointPositions->Data()[0];
591610
this->dataPtr->joints_[i].joint_velocity = jointVelocity->Data()[0];
592-
// this->dataPtr->joint_effort_[j] = jointForce->Data()[0];
611+
GZ_PHYSICS_NAMESPACE Vector3d force_or_torque;
612+
if (this->dataPtr->joints_[i].joint_type == sdf::JointType::PRISMATIC) {
613+
force_or_torque = {jointWrench->Data().force().x(),
614+
jointWrench->Data().force().y(), jointWrench->Data().force().z()};
615+
} else { // REVOLUTE and CONTINUOUS
616+
force_or_torque = {jointWrench->Data().torque().x(),
617+
jointWrench->Data().torque().y(), jointWrench->Data().torque().z()};
618+
}
619+
// Calculate the scalar effort along the joint axis
620+
this->dataPtr->joints_[i].joint_effort = force_or_torque.GZ_VECTOR_DOT(
621+
GZ_PHYSICS_NAMESPACE Vector3d{this->dataPtr->joints_[i].joint_axis.Xyz()[0],
622+
this->dataPtr->joints_[i].joint_axis.Xyz()[1],
623+
this->dataPtr->joints_[i].joint_axis.Xyz()[2]});
593624
}
594625

595626
for (unsigned int i = 0; i < this->dataPtr->imus_.size(); ++i) {

0 commit comments

Comments
 (0)