Skip to content

Commit fdf8e02

Browse files
authored
ForceTorqueSensor: add API for newest measurement (#449)
This allows the most recent Wrench message to be accessed via the API instead of only via gz-transport. This also reuses the wrench message instead of recreating it at each Update. Signed-off-by: Steve Peters <[email protected]>
1 parent b91047f commit fdf8e02

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

include/gz/sensors/ForceTorqueSensor.hh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#include <gz/math/Pose3.hh>
2828

29+
#include <gz/msgs/wrench.pb.h>
30+
2931
#include <gz/sensors/config.hh>
3032
#include <gz/sensors/force_torque/Export.hh>
3133

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

98+
/// \brief Get the most recent wrench measurement. This matches the data
99+
/// published over the gz-transport topic.
100+
/// \return The most recent wrench measurement.
101+
public: const msgs::Wrench &MeasuredWrench() const;
102+
96103
/// \brief Set the rotation of the joint parent relative to the sensor
97104
/// frame.
98105
/// \return The current rotation the parent in the sensor frame.

src/ForceTorqueSensor.cc

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ class gz::sensors::ForceTorqueSensorPrivate
5555
/// \brief Noise free torque as set by SetTorque
5656
public: gz::math::Vector3d torque{0, 0, 0};
5757

58+
/// \brief Most recent wrench measurement.
59+
public: gz::msgs::Wrench measuredWrench;
60+
5861
/// \brief Frame in which we return the measured force torque info.
5962
public: sdf::ForceTorqueFrame measureFrame;
6063

@@ -92,6 +95,10 @@ class gz::sensors::ForceTorqueSensorPrivate
9295
ForceTorqueSensor::ForceTorqueSensor()
9396
: dataPtr(std::make_unique<ForceTorqueSensorPrivate>())
9497
{
98+
// measuredWrench is reused, so allocate the first header data-value pair
99+
auto frame = this->dataPtr->measuredWrench.mutable_header()->add_data();
100+
frame->set_key("frame_id");
101+
frame->add_value("");
95102
}
96103

97104
//////////////////////////////////////////////////
@@ -247,11 +254,12 @@ bool ForceTorqueSensor::Update(const std::chrono::steady_clock::duration &_now)
247254
applyNoise(TORQUE_Y_NOISE_N_M, measuredTorque.Y());
248255
applyNoise(TORQUE_Z_NOISE_N_M, measuredTorque.Z());
249256

250-
msgs::Wrench msg;
257+
msgs::Wrench &msg = this->dataPtr->measuredWrench;
251258
*msg.mutable_header()->mutable_stamp() = msgs::Convert(_now);
252-
auto frame = msg.mutable_header()->add_data();
253-
frame->set_key("frame_id");
254-
frame->add_value(this->FrameId());
259+
auto frame = msg.mutable_header()->mutable_data(0);
260+
// header.data[0].key is already set to "frame_id" in the constructor
261+
// header.data[0].value[0] is already allocated in the constructor
262+
frame->set_value(0, this->FrameId());
255263

256264
msgs::Set(msg.mutable_force(), measuredForce);
257265
msgs::Set(msg.mutable_torque(), measuredTorque);
@@ -288,6 +296,12 @@ void ForceTorqueSensor::SetTorque(const math::Vector3d &_torque)
288296
this->dataPtr->torque = _torque;
289297
}
290298

299+
//////////////////////////////////////////////////
300+
const msgs::Wrench &ForceTorqueSensor::MeasuredWrench() const
301+
{
302+
return this->dataPtr->measuredWrench;
303+
}
304+
291305
//////////////////////////////////////////////////
292306
math::Quaterniond ForceTorqueSensor::RotationParentInSensor() const
293307
{

test/integration/force_torque.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*
1616
*/
1717

18+
#include <google/protobuf/util/message_differencer.h>
1819
#include <gtest/gtest.h>
1920

2021
#include <sdf/ForceTorque.hh>
@@ -245,6 +246,9 @@ TEST_P(ForceTorqueSensorTest, SensorReadings)
245246
sensor->Update(dt, false);
246247
EXPECT_TRUE(msgHelper.WaitForMessage()) << msgHelper;
247248
auto msg = msgHelper.Message();
249+
EXPECT_TRUE(
250+
google::protobuf::util::MessageDifferencer::Equals(
251+
msg, sensor->MeasuredWrench()));
248252
EXPECT_EQ(1, msg.header().stamp().sec());
249253
EXPECT_EQ(0, msg.header().stamp().nsec());
250254

0 commit comments

Comments
 (0)