Skip to content

Commit

Permalink
Backport frame id fixes (#446)
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Chen <[email protected]>
  • Loading branch information
iche033 committed Aug 7, 2024
1 parent aad7957 commit d9d2fc7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
2 changes: 0 additions & 2 deletions src/Lidar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,6 @@ bool Lidar::PublishLidarScan(const std::chrono::steady_clock::duration &_now)
this->dataPtr->laserMsg.mutable_header()->clear_data();
auto frame = this->dataPtr->laserMsg.mutable_header()->add_data();
frame->set_key("frame_id");
// keeping here the sensor name instead of frame_id because the visualizeLidar
// plugin relies on this value to get the position of the lidar.
// the ros_ign plugin is using the laserscan.proto 'frame' field
frame->add_value(this->FrameId());
this->dataPtr->laserMsg.set_frame(this->FrameId());
Expand Down
12 changes: 6 additions & 6 deletions src/Sensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class gz::sensors::SensorPrivate
public: std::map<std::string, uint64_t> sequences;

/// \brief frame id
public: std::string frame_id;
public: std::string frameId;

/// \brief If sensor is active or not.
public: bool active = true;
Expand Down Expand Up @@ -147,7 +147,7 @@ bool SensorPrivate::PopulateFromSDF(const sdf::Sensor &_sdf)
{
if (element->HasElement("ignition_frame_id"))
{
this->frame_id = element->Get<std::string>("ignition_frame_id");
this->frameId = element->Get<std::string>("ignition_frame_id");
// Warn if both ignition_frame_id and gz_frame_id are specified
if (element->HasElement("gz_frame_id"))
{
Expand All @@ -159,11 +159,11 @@ bool SensorPrivate::PopulateFromSDF(const sdf::Sensor &_sdf)
{
// Also read gz_frame_id to support SDF that's compatible with newer
// versions of Gazebo.
this->frame_id = element->Get<std::string>("gz_frame_id");
this->frameId = element->Get<std::string>("gz_frame_id");
}
else
{
this->frame_id = this->name;
this->frameId = this->name;
}
}

Expand Down Expand Up @@ -260,13 +260,13 @@ std::string Sensor::Name() const
//////////////////////////////////////////////////
std::string Sensor::FrameId() const
{
return this->dataPtr->frame_id;
return this->dataPtr->frameId;
}

//////////////////////////////////////////////////
void Sensor::SetFrameId(const std::string &_frameId)
{
this->dataPtr->frame_id = _frameId;
this->dataPtr->frameId = _frameId;
}

//////////////////////////////////////////////////
Expand Down
37 changes: 25 additions & 12 deletions test/integration/gpu_lidar_sensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ sdf::ElementPtr GpuLidarToSdf(const std::string &name,
const double vertResolution, const double vertMinAngle,
const double vertMaxAngle, const double rangeResolution,
const double rangeMin, const double rangeMax, const bool alwaysOn,
const bool visualize)
const bool visualize, const std::string &frameId)
{
std::ostringstream stream;
stream
Expand All @@ -79,6 +79,7 @@ sdf::ElementPtr GpuLidarToSdf(const std::string &name,
<< " <model name='m1'>"
<< " <link name='link1'>"
<< " <sensor name='" << name << "' type='gpu_lidar'>"
<< " <gz_frame_id>" << frameId << "</gz_frame_id>"
<< " <pose>" << pose << "</pose>"
<< " <topic>" << topic << "</topic>"
<< " <updateRate>"<< updateRate <<"</updateRate>"
Expand Down Expand Up @@ -186,14 +187,15 @@ void GpuLidarSensorTest::CreateGpuLidar(const std::string &_renderEngine)
const double rangeMax = 10.0;
const bool alwaysOn = 1;
const bool visualize = 1;
const std::string frameId = "TestGpuLidar_frame";

// Create sensor description in SDF
gz::math::Pose3d testPose(gz::math::Vector3d(0, 0, 0.1),
gz::math::Quaterniond::Identity);
sdf::ElementPtr lidarSdf = GpuLidarToSdf(name, testPose, updateRate, topic,
horzSamples, horzResolution, horzMinAngle, horzMaxAngle,
vertSamples, vertResolution, vertMinAngle, vertMaxAngle,
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize);
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize, frameId);

// Setup ign-rendering with an empty scene
auto *engine = gz::rendering::engine(_renderEngine);
Expand Down Expand Up @@ -305,14 +307,15 @@ void GpuLidarSensorTest::DetectBox(const std::string &_renderEngine)
const double rangeMax = 10.0;
const bool alwaysOn = 1;
const bool visualize = 1;
const std::string frameId = "TestGpuLidar_frame";

// Create sensor SDF
gz::math::Pose3d testPose(gz::math::Vector3d(0.0, 0.0, 0.1),
gz::math::Quaterniond::Identity);
sdf::ElementPtr lidarSdf = GpuLidarToSdf(name, testPose, updateRate, topic,
horzSamples, horzResolution, horzMinAngle, horzMaxAngle,
vertSamples, vertResolution, vertMinAngle, vertMaxAngle,
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize);
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize, frameId);

// Create and populate scene
gz::rendering::RenderEngine *engine =
Expand Down Expand Up @@ -393,7 +396,7 @@ void GpuLidarSensorTest::DetectBox(const std::string &_renderEngine)
LASER_TOL);
EXPECT_DOUBLE_EQ(laserMsgs.back().ranges(last), gz::math::INF_D);

EXPECT_EQ(laserMsgs.back().frame(), name);
EXPECT_EQ(laserMsgs.back().frame(), frameId);
EXPECT_NEAR(laserMsgs.back().angle_min(), horzMinAngle, 1e-4);
EXPECT_NEAR(laserMsgs.back().angle_max(), horzMaxAngle, 1e-4);
EXPECT_NEAR(laserMsgs.back().count(), horzSamples, 1e-4);
Expand Down Expand Up @@ -425,6 +428,12 @@ void GpuLidarSensorTest::DetectBox(const std::string &_renderEngine)
EXPECT_FALSE(pointMsgs.back().is_dense());
EXPECT_EQ(32u * horzSamples * vertSamples, pointMsgs.back().data().size());

EXPECT_TRUE(pointMsgs.back().has_header());
EXPECT_LT(1, pointMsgs.back().header().data().size());
EXPECT_EQ("frame_id", pointMsgs.back().header().data(0).key());
ASSERT_EQ(1, pointMsgs.back().header().data(0).value().size());
EXPECT_EQ(frameId, pointMsgs.back().header().data(0).value(0));

// Clean up rendering ptrs
visualBox1.reset();

Expand Down Expand Up @@ -462,22 +471,23 @@ void GpuLidarSensorTest::TestThreeBoxes(const std::string &_renderEngine)
const double rangeMax = 10.0;
const bool alwaysOn = 1;
const bool visualize = 1;
const std::string frameId = "TestGpuLidar_frame";

// Create sensor SDF
gz::math::Pose3d testPose1(gz::math::Vector3d(0, 0, 0.1),
gz::math::Quaterniond::Identity);
sdf::ElementPtr lidarSdf1 = GpuLidarToSdf(name1, testPose1, updateRate,
topic1, horzSamples, horzResolution, horzMinAngle, horzMaxAngle,
vertSamples, vertResolution, vertMinAngle, vertMaxAngle,
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize);
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize, frameId);

// Create a second sensor SDF rotated
gz::math::Pose3d testPose2(gz::math::Vector3d(0, 0, 0.1),
gz::math::Quaterniond(IGN_PI/2.0, 0, 0));
sdf::ElementPtr lidarSdf2 = GpuLidarToSdf(name2, testPose2, updateRate,
topic2, horzSamples, horzResolution, horzMinAngle, horzMaxAngle,
vertSamples, vertResolution, vertMinAngle, vertMaxAngle,
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize);
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize, frameId);

// Create and populate scene
gz::rendering::RenderEngine *engine =
Expand Down Expand Up @@ -618,14 +628,15 @@ void GpuLidarSensorTest::VerticalLidar(const std::string &_renderEngine)
const double rangeMax = 10.0;
const bool alwaysOn = 1;
const bool visualize = 1;
const std::string frameId = "TestGpuLidar_frame";

// Create sensor SDF
gz::math::Pose3d testPose(gz::math::Vector3d(0.25, 0.0, 0.5),
gz::math::Quaterniond::Identity);
sdf::ElementPtr lidarSdf = GpuLidarToSdf(name, testPose, updateRate, topic,
horzSamples, horzResolution, horzMinAngle, horzMaxAngle,
vertSamples, vertResolution, vertMinAngle, vertMaxAngle,
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize);
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize, frameId);

// Create and populate scene
gz::rendering::RenderEngine *engine =
Expand Down Expand Up @@ -745,22 +756,23 @@ void GpuLidarSensorTest::ManualUpdate(const std::string &_renderEngine)
const double rangeMax = 10.0;
const bool alwaysOn = 1;
const bool visualize = 1;
const std::string frameId = "TestGpuLidar_frame";

// Create sensor SDF
gz::math::Pose3d testPose1(gz::math::Vector3d(0, 0, 0.1),
gz::math::Quaterniond::Identity);
sdf::ElementPtr lidarSdf1 = GpuLidarToSdf(name1, testPose1, updateRate,
topic1, horzSamples, horzResolution, horzMinAngle, horzMaxAngle,
vertSamples, vertResolution, vertMinAngle, vertMaxAngle,
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize);
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize, frameId);

// Create a second sensor SDF at an xy offset of 1
gz::math::Pose3d testPose2(gz::math::Vector3d(1, 1, 0.1),
gz::math::Quaterniond::Identity);
sdf::ElementPtr lidarSdf2 = GpuLidarToSdf(name2, testPose2, updateRate,
topic2, horzSamples, horzResolution, horzMinAngle, horzMaxAngle,
vertSamples, vertResolution, vertMinAngle, vertMaxAngle,
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize);
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize, frameId);

// Create and populate scene
gz::rendering::RenderEngine *engine =
Expand Down Expand Up @@ -871,6 +883,7 @@ void GpuLidarSensorTest::Topic(const std::string &_renderEngine)
const bool alwaysOn = 1;
const bool visualize = 1;
auto testPose = gz::math::Pose3d();
const std::string frameId = "TestGpuLidar_frame";

// Scene
auto engine = gz::rendering::engine(_renderEngine);
Expand All @@ -892,7 +905,7 @@ void GpuLidarSensorTest::Topic(const std::string &_renderEngine)
auto lidarSdf = GpuLidarToSdf(name, testPose, updateRate, topic,
horzSamples, horzResolution, horzMinAngle, horzMaxAngle,
vertSamples, vertResolution, vertMinAngle, vertMaxAngle,
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize);
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize, frameId);

auto lidar = mgr.CreateSensor<gz::sensors::GpuLidarSensor>(lidarSdf);
ASSERT_NE(nullptr, lidar);
Expand All @@ -906,7 +919,7 @@ void GpuLidarSensorTest::Topic(const std::string &_renderEngine)
auto lidarSdf = GpuLidarToSdf(name, testPose, updateRate, topic,
horzSamples, horzResolution, horzMinAngle, horzMaxAngle,
vertSamples, vertResolution, vertMinAngle, vertMaxAngle,
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize);
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize, frameId);

auto lidar = mgr.CreateSensor<gz::sensors::GpuLidarSensor>(lidarSdf);
ASSERT_NE(nullptr, lidar);
Expand All @@ -921,7 +934,7 @@ void GpuLidarSensorTest::Topic(const std::string &_renderEngine)
auto lidarSdf = GpuLidarToSdf(name, testPose, updateRate, topic,
horzSamples, horzResolution, horzMinAngle, horzMaxAngle,
vertSamples, vertResolution, vertMinAngle, vertMaxAngle,
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize);
rangeResolution, rangeMin, rangeMax, alwaysOn, visualize, frameId);

auto sensor = mgr.CreateSensor<gz::sensors::GpuLidarSensor>(lidarSdf);
EXPECT_EQ(nullptr, sensor);
Expand Down

0 comments on commit d9d2fc7

Please sign in to comment.