nodelet.cpp
Go to the documentation of this file.
2 
4 #include "nxLib.h"
5 
7 #include <string>
8 
9 #include <fstream>
10 
11 namespace ensenso_camera
12 {
14 {
17 
18  NODELET_DEBUG("Initializing the NxLib...");
19  try
20  {
21  nxLibInitialize(true);
22  }
23  catch (NxLibException& e)
24  {
25  NODELET_ERROR("Error while initializing the NxLib. Shutting down.");
26  exit(EXIT_FAILURE);
27  }
28 
29  int threads;
30  if (nhLocal.getParam("threads", threads))
31  {
32  NxLibItem()[itmParameters][itmThreads] = threads;
33  }
34 
35  std::string serial, fileCameraPath, cameraFrame, targetFrame, linkFrame, robotFrame, wristFrame;
36  bool cameraIsFixed;
37 
38  if (!nhLocal.getParam("serial", serial))
39  {
40  // Try to get the serial as an integer, because rosparam sometimes
41  // automatically converts the serials to numbers.
42  int intSerial;
43  if (nhLocal.getParam("serial", intSerial))
44  {
45  serial = std::to_string(intSerial);
46  }
47  }
48 
49  if (serial.empty())
50  {
51  NxLibItem cameras = NxLibItem()[itmCameras][itmBySerialNo];
52  // Try to find the first stereo camera.
53  bool foundAppropriateCamera = false;
54  for (int i = 0; i < cameras.count(); i++)
55  {
56  if (cameras[i][itmType].exists() && cameras[i][itmType].asString() == valStereo)
57  {
58  foundAppropriateCamera = true;
59  serial = cameras[i].name();
60  break;
61  }
62  }
63  if (!foundAppropriateCamera)
64  {
65  NODELET_ERROR("Could not find any camera. Shutting down.");
66  nxLibFinalize();
67  exit(EXIT_FAILURE);
68  }
69  }
70 
71  nhLocal.param<std::string>("file_camera_path", fileCameraPath, "");
72 
73  // We can only check the type with file cameras, when we create file cameras, so for now we can only check whether the
74  // current physical camera is of the right type
75  if (fileCameraPath.empty())
76  {
77  std::string type = NxLibItem()[itmCameras][itmBySerialNo][serial][itmType].asString();
78  std::string const neededType = valStereo;
79  if (type != neededType)
80  {
81  NODELET_ERROR("The camera to be opened is of the wrong type %s. It should be %s.", type.c_str(),
82  neededType.c_str());
83  nxLibFinalize();
84  exit(EXIT_FAILURE);
85  }
86  }
87 
88  nhLocal.param("fixed", cameraIsFixed, false);
89 
90  if (!nhLocal.getParam("camera_frame", cameraFrame))
91  {
92  cameraFrame = serial + "_optical_frame";
93  }
94 
95  nhLocal.param<std::string>("robot_frame", robotFrame, "");
96  nhLocal.param<std::string>("wrist_frame", wristFrame, "");
97 
98  if (cameraIsFixed && robotFrame.empty())
99  {
100  robotFrame = cameraFrame;
101  }
102  if (!cameraIsFixed && wristFrame.empty())
103  {
104  wristFrame = cameraFrame;
105  }
106 
107  nhLocal.getParam("target_frame", targetFrame);
108  nhLocal.getParam("link_frame", linkFrame);
109 
110  if (!targetFrame.empty() && linkFrame.empty())
111  {
112  linkFrame = targetFrame;
113  }
114  else if (targetFrame.empty() && linkFrame.empty())
115  {
116  targetFrame = cameraFrame;
117  linkFrame = cameraFrame;
118  }
119 
120  int captureTimeout;
121  nhLocal.param("capture_timeout", captureTimeout, 0);
122 
123  // Load virtual objects
124  std::unique_ptr<VirtualObjectHandler> virtualObjectHandler;
125  std::string objectsFile;
126  if (nhLocal.getParam("objects_file", objectsFile) && !objectsFile.empty())
127  {
128  // Get objects frame, default to target
129  std::string objectsFrame = targetFrame;
130  nhLocal.getParam("objects_frame", objectsFrame);
131 
132  NODELET_DEBUG("Loading virtual objects...");
133  try
134  {
135  virtualObjectHandler = ::make_unique<VirtualObjectHandler>(objectsFile, objectsFrame, cameraFrame);
136  }
137  catch (const std::exception &e)
138  {
139  NODELET_WARN("Unable to load virtual objects file '%s'. Error: %s", objectsFile.c_str(), e.what());
140  }
141  }
142 
143  camera = ::make_unique<StereoCamera>(nh, serial, fileCameraPath, cameraIsFixed, cameraFrame, targetFrame, robotFrame,
144  wristFrame, linkFrame, captureTimeout, std::move(virtualObjectHandler));
145  if (!camera->open())
146  {
147  NODELET_ERROR("Failed to open the camera. Shutting down.");
148  nxLibFinalize();
149  exit(EXIT_FAILURE);
150  }
151 
152  std::string settingsFile;
153  if (nhLocal.getParam("settings", settingsFile))
154  {
155  NODELET_DEBUG("Loading camera settings...");
156  if (!camera->loadSettings(settingsFile, true))
157  {
158  NODELET_ERROR("Failed to load the camera settings. Shutting down.");
159  nxLibFinalize();
160  exit(EXIT_FAILURE);
161  }
162  }
163 
164  camera->startServers();
165  camera->initTfPublishTimer();
166  camera->initStatusTimer();
167 }
168 
170 {
171  camera->close();
172  nxLibFinalize();
173 }
174 
175 } // namespace ensenso_camera
176 
#define NODELET_ERROR(...)
#define NODELET_WARN(...)
PLUGINLIB_EXPORT_CLASS(BAGReader, nodelet::Nodelet)
void onInit() override
Definition: nodelet.cpp:13
std::unique_ptr< StereoCamera > camera
Definition: nodelet.h:14
ros::NodeHandle & getPrivateNodeHandle() const
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ros::NodeHandle & getNodeHandle() const
bool getParam(const std::string &key, std::string &s) const
ROSCPP_DECL bool exists(const std::string &service_name, bool print_failure_reason)
#define NODELET_DEBUG(...)


ensenso_camera
Author(s): Ensenso
autogenerated on Thu May 6 2021 02:50:06