Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide force-torque sensor data through gz_system to controller_manager #273

Open
wants to merge 4 commits into
base: rolling
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions gz_ros2_control/src/gz_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@

#ifdef GZ_HEADERS
#include <gz/msgs/imu.pb.h>
#include <gz/msgs/wrench.pb.h>

#include <gz/sim/components/AngularVelocity.hh>
#include <gz/sim/components/Imu.hh>
#include <gz/sim/components/ForceTorque.hh>
#include <gz/sim/components/JointForce.hh>
#include <gz/sim/components/JointForceCmd.hh>
#include <gz/sim/components/JointPosition.hh>
Expand All @@ -43,9 +45,11 @@
#define GZ_MSGS_NAMESPACE gz::msgs::
#else
#include <ignition/msgs/imu.pb.h>
#include <ignition/msgs/wrench.pb.h>

#include <ignition/gazebo/components/AngularVelocity.hh>
#include <ignition/gazebo/components/Imu.hh>
#include <ignition/gazebo/components/ForceTorque.hh>
#include <ignition/gazebo/components/JointForce.hh>
#include <ignition/gazebo/components/JointForceCmd.hh>
#include <ignition/gazebo/components/JointPosition.hh>
Expand Down Expand Up @@ -108,6 +112,35 @@ struct MimicJoint
std::vector<std::string> interfaces_to_mimic;
};

class ForceTorqueData
{
public:
/// \brief imu's name.
std::string name{};

/// \brief imu's topic name.
std::string topicName{};

/// \brief handles to the force torque from within Gazebo
sim::Entity sim_ft_sensors_ = sim::kNullEntity;

/// \brief An array per FT
std::array<double, 6> ft_sensor_data_;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include <array>


/// \brief callback to get the Force Torque topic values
void OnForceTorque(const GZ_MSGS_NAMESPACE Wrench & _msg);
};

void ForceTorqueData::OnForceTorque(const GZ_MSGS_NAMESPACE Wrench & _msg)
{
this->ft_sensor_data_[0] = _msg.force().x();
this->ft_sensor_data_[1] = _msg.force().y();
this->ft_sensor_data_[2] = _msg.force().z();
this->ft_sensor_data_[3] = _msg.torque().x();
this->ft_sensor_data_[4] = _msg.torque().y();
this->ft_sensor_data_[5] = _msg.torque().z();
}

class ImuData
{
public:
Expand Down Expand Up @@ -159,6 +192,8 @@ class gz_ros2_control::GazeboSimSystemPrivate
/// \brief vector with the imus .
std::vector<std::shared_ptr<ImuData>> imus_;

std::vector<std::shared_ptr<ForceTorqueData>> ft_sensors_;

/// \brief state interfaces that will be exported to the Resource Manager
std::vector<hardware_interface::StateInterface> state_interfaces_;

Expand Down Expand Up @@ -522,6 +557,55 @@ void GazeboSimSystem::registerSensors(
this->dataPtr->imus_.push_back(imuData);
return true;
});

this->dataPtr->ecm->Each<sim::components::ForceTorque,
sim::components::Name>(
[&](const sim::Entity & _entity,
const sim::components::ForceTorque *,
const sim::components::Name * _name) -> bool
{
auto ftData = std::make_shared<ForceTorqueData>();
RCLCPP_INFO_STREAM(this->nh_->get_logger(), "Loading sensor: " << _name->Data());

auto sensorTopicComp = this->dataPtr->ecm->Component<
sim::components::SensorTopic>(_entity);
if (sensorTopicComp) {
RCLCPP_INFO_STREAM(this->nh_->get_logger(), "Topic name: " << sensorTopicComp->Data());
}

RCLCPP_INFO_STREAM(
this->nh_->get_logger(), "\tState:");
ftData->name = _name->Data();
ftData->sim_ft_sensors_ = _entity;

hardware_interface::ComponentInfo component;
for (auto & comp : sensor_components_) {
if (comp.name == _name->Data()) {
component = comp;
}
}

static const std::map<std::string, size_t> interface_name_map = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include <cstddef>

{"force.x", 0},
{"force.y", 1},
{"force.z", 2},
{"torque.x", 3},
{"torque.y", 4},
{"torque.z", 5},
};

for (const auto & state_interface : component.state_interfaces) {
RCLCPP_INFO_STREAM(this->nh_->get_logger(), "\t\t " << state_interface.name);

size_t data_index = interface_name_map.at(state_interface.name);
this->dataPtr->state_interfaces_.emplace_back(
ftData->name,
state_interface.name,
&ftData->ft_sensor_data_[data_index]);
}
this->dataPtr->ft_sensors_.push_back(ftData);
return true;
});
}

CallbackReturn
Expand Down Expand Up @@ -609,6 +693,24 @@ hardware_interface::return_type GazeboSimSystem::read(
}
}
}

for (unsigned int i = 0; i < this->dataPtr->ft_sensors_.size(); ++i) {
if (this->dataPtr->ft_sensors_[i]->topicName.empty()) {
auto sensorTopicComp = this->dataPtr->ecm->Component<
sim::components::SensorTopic>(this->dataPtr->ft_sensors_[i]->sim_ft_sensors_);
if (sensorTopicComp) {
this->dataPtr->ft_sensors_[i]->topicName = sensorTopicComp->Data();
RCLCPP_INFO_STREAM(
this->nh_->get_logger(), "ForceTorque " << this->dataPtr->ft_sensors_[i]->name <<
" has a topic name: " << sensorTopicComp->Data());

this->dataPtr->node.Subscribe(
this->dataPtr->ft_sensors_[i]->topicName, &ForceTorqueData::OnForceTorque,
this->dataPtr->ft_sensors_[i].get());
}
}
}

return hardware_interface::return_type::OK;
}

Expand Down
Loading