Skip to content

Commit

Permalink
ForceTorqueSensor: add API for newest measurement
Browse files Browse the repository at this point in the history
This allows the most recent Wrench message to be
accessed via the API instead of only via gz-transport.

Signed-off-by: Steve Peters <[email protected]>
  • Loading branch information
scpeters committed Jul 19, 2024
1 parent b91047f commit dacbd2e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
7 changes: 7 additions & 0 deletions include/gz/sensors/ForceTorqueSensor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#include <gz/math/Pose3.hh>

#include <gz/msgs/wrench.pb.h>

#include <gz/sensors/config.hh>
#include <gz/sensors/force_torque/Export.hh>

Expand Down Expand Up @@ -93,6 +95,11 @@ namespace gz
/// \param[in] _torque torque vector in newton.
public: void SetTorque(const math::Vector3d &_torque);

/// \brief Get the most recent wrench measurement. This matches the data
/// published over the gz-transport topic.
/// \return The most recent wrench measurement.
public: const msgs::Wrench &MeasuredWrench() const;

/// \brief Set the rotation of the joint parent relative to the sensor
/// frame.
/// \return The current rotation the parent in the sensor frame.
Expand Down
11 changes: 10 additions & 1 deletion src/ForceTorqueSensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class gz::sensors::ForceTorqueSensorPrivate
/// \brief Noise free torque as set by SetTorque
public: gz::math::Vector3d torque{0, 0, 0};

/// \brief Most recent wrench measurement.
public: gz::msgs::Wrench measuredWrench;

/// \brief Frame in which we return the measured force torque info.
public: sdf::ForceTorqueFrame measureFrame;

Expand Down Expand Up @@ -247,7 +250,7 @@ bool ForceTorqueSensor::Update(const std::chrono::steady_clock::duration &_now)
applyNoise(TORQUE_Y_NOISE_N_M, measuredTorque.Y());
applyNoise(TORQUE_Z_NOISE_N_M, measuredTorque.Z());

msgs::Wrench msg;
msgs::Wrench &msg = this->dataPtr->measuredWrench;
*msg.mutable_header()->mutable_stamp() = msgs::Convert(_now);
auto frame = msg.mutable_header()->add_data();
frame->set_key("frame_id");
Expand Down Expand Up @@ -288,6 +291,12 @@ void ForceTorqueSensor::SetTorque(const math::Vector3d &_torque)
this->dataPtr->torque = _torque;
}

//////////////////////////////////////////////////
const msgs::Wrench &ForceTorqueSensor::MeasuredWrench() const
{
return this->dataPtr->measuredWrench;
}

//////////////////////////////////////////////////
math::Quaterniond ForceTorqueSensor::RotationParentInSensor() const
{
Expand Down
4 changes: 4 additions & 0 deletions test/integration/force_torque.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*
*/

#include <google/protobuf/util/message_differencer.h>
#include <gtest/gtest.h>

#include <sdf/ForceTorque.hh>
Expand Down Expand Up @@ -245,6 +246,9 @@ TEST_P(ForceTorqueSensorTest, SensorReadings)
sensor->Update(dt, false);
EXPECT_TRUE(msgHelper.WaitForMessage()) << msgHelper;
auto msg = msgHelper.Message();
EXPECT_TRUE(
google::protobuf::util::MessageDifferencer::Equals(
msg, sensor->MeasuredWrench()));
EXPECT_EQ(1, msg.header().stamp().sec());
EXPECT_EQ(0, msg.header().stamp().nsec());

Expand Down

0 comments on commit dacbd2e

Please sign in to comment.