Skip to content

Commit

Permalink
Added Camera Info topic support for cameras (#285)
Browse files Browse the repository at this point in the history
* Added Camera Info topic support for cameras

---------

Signed-off-by: ahcorde <ahcorde@gmail.com>
Signed-off-by: Alejandro Hernández Cordero <ahcorde@gmail.com>
Co-authored-by: Ian Chen <ichen@openrobotics.org>
  • Loading branch information
ahcorde and iche033 authored Feb 15, 2023
1 parent 425359e commit 898570d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
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

0 comments on commit 898570d

Please sign in to comment.