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

Added Camera Info topic support for cameras #285

Merged
merged 6 commits into from
Feb 15, 2023
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
23 changes: 14 additions & 9 deletions src/CameraSensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ bool CameraSensor::Load(const sdf::Sensor &_sdf)
if (this->Topic().empty())
this->SetTopic("/camera");

if (!_sdf.CameraSensor()->CameraInfoTopic().empty())
{
this->dataPtr->infoTopic = _sdf.CameraSensor()->CameraInfoTopic();
}

this->dataPtr->pub =
this->dataPtr->node.Advertise<msgs::Image>(
this->Topic());
Expand Down Expand Up @@ -491,17 +496,17 @@ std::string CameraSensor::InfoTopic() const
//////////////////////////////////////////////////
bool CameraSensor::AdvertiseInfo()
{
// TODO(anyone) Make info topic configurable from SDF
// Info topic must be at same level as image topic
auto parts = common::Split(this->Topic(), '/');
parts.pop_back();

for (const auto &part : parts)
if (this->dataPtr->infoTopic.empty())
{
if (!part.empty())
this->dataPtr->infoTopic += "/" + part;
auto parts = common::Split(this->Topic(), '/');
parts.pop_back();
for (const auto &part : parts)
{
if (!part.empty())
this->dataPtr->infoTopic += "/" + part;
}
this->dataPtr->infoTopic += "/camera_info";
}
this->dataPtr->infoTopic += "/camera_info";

return this->AdvertiseInfo(this->dataPtr->infoTopic);
}
Expand Down
19 changes: 12 additions & 7 deletions src/Camera_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ sdf::ElementPtr cameraToBadSdf()

sdf::ElementPtr CameraToSdf(const std::string &_type,
const std::string &_name, double _updateRate,
const std::string &_topic, bool _alwaysOn, bool _visualize)
const std::string &_topic, const std::string &_cameraInfoTopic,
bool _alwaysOn, bool _visualize)
{
std::ostringstream stream;
stream
Expand All @@ -58,6 +59,7 @@ sdf::ElementPtr CameraToSdf(const std::string &_type,
<< " <link name='link1'>"
<< " <sensor name='" << _name << "' type='" << _type << "'>"
<< " <topic>" << _topic << "</topic>"
<< " <topic>" << _cameraInfoTopic << "</topic>"
<< " <update_rate>"<< _updateRate <<"</update_rate>"
<< " <always_on>"<< _alwaysOn <<"</always_on>"
<< " <visualize>" << _visualize << "</visualize>"
Expand Down Expand Up @@ -146,8 +148,8 @@ TEST(Camera_TEST, CreateCamera)
{
gz::sensors::Manager mgr;

sdf::ElementPtr camSdf = CameraToSdf("camera", "my_camera", 60.0, "/cam",
true, true);
sdf::ElementPtr camSdf = CameraToSdf("camera", "my_camera", 60.0,
"/cam", "my_camera/camera_info", true, true);

// Create a CameraSensor
gz::sensors::CameraSensor *cam =
Expand Down Expand Up @@ -190,8 +192,9 @@ TEST(Camera_TEST, Topic)
// Default topic
{
const std::string topic;
auto cameraSdf = CameraToSdf(type, name, updateRate, topic, alwaysOn,
visualize);
const std::string cameraInfoTopic;
auto cameraSdf = CameraToSdf(type, name, updateRate, topic, cameraInfoTopic,
alwaysOn, visualize);

auto sensorId = mgr.CreateSensor(cameraSdf);
EXPECT_NE(gz::sensors::NO_SENSOR, sensorId);
Expand All @@ -203,12 +206,13 @@ TEST(Camera_TEST, Topic)
ASSERT_NE(nullptr, camera);

EXPECT_EQ("/camera", camera->Topic());
EXPECT_EQ("/camera_info", camera->InfoTopic());
}

// Convert to valid topic
{
const std::string topic = "/topic with spaces/@~characters//";
auto cameraSdf = CameraToSdf(type, name, updateRate, topic, alwaysOn,
auto cameraSdf = CameraToSdf(type, name, updateRate, topic, "", alwaysOn,
visualize);

auto sensorId = mgr.CreateSensor(cameraSdf);
Expand All @@ -221,12 +225,13 @@ TEST(Camera_TEST, Topic)
ASSERT_NE(nullptr, camera);

EXPECT_EQ("/topic_with_spaces/characters", camera->Topic());
EXPECT_EQ("/topic_with_spaces/camera_info", camera->InfoTopic());
}

// Invalid topic
{
const std::string topic = "@@@";
auto cameraSdf = CameraToSdf(type, name, updateRate, topic, alwaysOn,
auto cameraSdf = CameraToSdf(type, name, updateRate, topic, "", alwaysOn,
visualize);

auto sensorId = mgr.CreateSensor(cameraSdf);
Expand Down