Skip to content

Commit d8180d7

Browse files
committed
Fix: Avoids crashing if camera can not be found
Previously if a camera with the given name could not be found, it would still call a function on it. This prevents this and adds some user-friendly output.
1 parent 49ab134 commit d8180d7

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

src/RealSensePlugin.cpp

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@
2828

2929
using namespace gazebo;
3030

31+
template <typename T>
32+
std::shared_ptr<T> GetAndCheckSensor(sensors::SensorManager *smanager, std::string name) {
33+
const gazebo::sensors::SensorPtr s = smanager->GetSensor(name);
34+
if (s == NULL) {
35+
std::cerr << "RealSensePlugin: Sensor '" << name << "' not found. Available sensor are:" << std::endl;
36+
const auto sensors = smanager->GetSensors();
37+
for (const auto& sensor : sensors) {
38+
std::cerr << "\t" << sensor->Name() << std::endl;
39+
}
40+
return NULL;
41+
}
42+
return std::dynamic_pointer_cast<T>(s);
43+
}
44+
45+
3146
/////////////////////////////////////////////////
3247
RealSensePlugin::RealSensePlugin() {
3348
this->depthCam = nullptr;
@@ -117,7 +132,7 @@ void RealSensePlugin::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf) {
117132
else if (name == "robotNamespace")
118133
break;
119134
else
120-
throw std::runtime_error("Ivalid parameter for ReakSensePlugin");
135+
throw std::runtime_error("Invalid parameter for RealSensePlugin");
121136

122137
_sdf = _sdf->GetNextElement();
123138
} while (_sdf);
@@ -132,39 +147,47 @@ void RealSensePlugin::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf) {
132147
sensors::SensorManager *smanager = sensors::SensorManager::Instance();
133148

134149
// Get Cameras Renderers
135-
this->depthCam = std::dynamic_pointer_cast<sensors::DepthCameraSensor>(
136-
smanager->GetSensor(prefix + DEPTH_CAMERA_NAME))
137-
->DepthCamera();
138-
139-
this->ired1Cam = std::dynamic_pointer_cast<sensors::CameraSensor>(
140-
smanager->GetSensor(prefix + IRED1_CAMERA_NAME))
141-
->Camera();
142-
this->ired2Cam = std::dynamic_pointer_cast<sensors::CameraSensor>(
143-
smanager->GetSensor(prefix + IRED2_CAMERA_NAME))
144-
->Camera();
145-
this->colorCam = std::dynamic_pointer_cast<sensors::CameraSensor>(
146-
smanager->GetSensor(prefix + COLOR_CAMERA_NAME))
147-
->Camera();
150+
sensors::DepthCameraSensorPtr depth_cs = GetAndCheckSensor<sensors::DepthCameraSensor>(smanager, std::string(prefix + DEPTH_CAMERA_NAME));
151+
if (depth_cs) {
152+
this->depthCam = depth_cs->DepthCamera();
153+
}
154+
sensors::CameraSensorPtr ired1_cs = GetAndCheckSensor<sensors::CameraSensor>(smanager, std::string(prefix + IRED1_CAMERA_NAME));
155+
if (ired1_cs) {
156+
this->ired1Cam = ired1_cs->Camera();
157+
}
158+
sensors::CameraSensorPtr ired2_cs = GetAndCheckSensor<sensors::CameraSensor>(smanager, std::string(prefix + IRED2_CAMERA_NAME));
159+
if (ired2_cs) {
160+
this->ired2Cam = ired2_cs->Camera();
161+
}
162+
sensors::CameraSensorPtr color_cs = GetAndCheckSensor<sensors::CameraSensor>(smanager, std::string(prefix + COLOR_CAMERA_NAME));
163+
if (color_cs) {
164+
this->colorCam = color_cs->Camera();
165+
}
148166

167+
bool abort{false};
149168
// Check if camera renderers have been found successfuly
150169
if (!this->depthCam) {
151170
std::cerr << "RealSensePlugin: Depth Camera has not been found"
152171
<< std::endl;
153-
return;
172+
abort = true;
154173
}
155174
if (!this->ired1Cam) {
156175
std::cerr << "RealSensePlugin: InfraRed Camera 1 has not been found"
157176
<< std::endl;
158-
return;
177+
abort = true;
159178
}
160179
if (!this->ired2Cam) {
161180
std::cerr << "RealSensePlugin: InfraRed Camera 2 has not been found"
162181
<< std::endl;
163-
return;
182+
abort = true;
164183
}
165184
if (!this->colorCam) {
166185
std::cerr << "RealSensePlugin: Color Camera has not been found"
167186
<< std::endl;
187+
abort = true;
188+
}
189+
if (abort) {
190+
std:cerr << "RealSensePlugin: Aborting loading"
168191
return;
169192
}
170193

0 commit comments

Comments
 (0)