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

Allow specifying gz_frame_id as an alternative to ignition_frame_id #409

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions src/Sensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ bool SensorPrivate::PopulateFromSDF(const sdf::Sensor &_sdf)
if (element->HasElement("ignition_frame_id"))
{
this->frame_id = 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"))
{
ignwarn << "Found both `ignition_frame_id` and `gz_frame_id` in sensor"
<< this->name << ". Only `ignition_frame_id` will be used\n";
}
}
else if (element->HasElement("gz_frame_id"))
{
// 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");
}
else
{
Expand Down
55 changes: 55 additions & 0 deletions src/Sensor_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,61 @@ TEST(Sensor_TEST, SetRateZeroService)
EXPECT_FLOAT_EQ(20.0, sensor.UpdateRate());
}

//////////////////////////////////////////////////
TEST(Sensor_TEST, FrameIdFromSdf)
{
auto loadSensorWithSdfParam =
[](TestSensor &_testSensor, const std::string &_sensorParam)
{
const std::string sensorSdf = R"(
<sdf version="1.9">
<model name="m1">
<link name="link1">
<sensor name="test" type="imu">)" +
_sensorParam + R"(
</sensor>
</link>
</model>
</sdf>
)";
sdf::Root root;
sdf::Errors errors = root.LoadSdfString(sensorSdf);
ASSERT_TRUE(errors.empty()) << errors;
auto *model = root.Model();
ASSERT_NE(model, nullptr);
auto *link = model->LinkByIndex(0);
ASSERT_NE(link, nullptr);
auto *sensor = link->SensorByIndex(0);
ASSERT_NE(sensor, nullptr);

_testSensor.Load(*sensor);
};

{
TestSensor testSensor;
loadSensorWithSdfParam(testSensor, "");
EXPECT_EQ("test", testSensor.FrameId());
}
{
TestSensor testSensor;
loadSensorWithSdfParam(
testSensor, "<ignition_frame_id>custom_frame_id</ignition_frame_id>");
EXPECT_EQ("custom_frame_id", testSensor.FrameId());
}
{
TestSensor testSensor;
loadSensorWithSdfParam(testSensor,
"<gz_frame_id>custom_frame_id</gz_frame_id>");
EXPECT_EQ("custom_frame_id", testSensor.FrameId());
}
{
TestSensor testSensor;
loadSensorWithSdfParam(testSensor, R"(
<ignition_frame_id>custom_frame_id</ignition_frame_id>
<gz_frame_id>other_custom_frame_id</gz_frame_id>)");
EXPECT_EQ("custom_frame_id", testSensor.FrameId());
}
}

//////////////////////////////////////////////////
int main(int argc, char **argv)
Expand Down