camera_node.cpp
Go to the documentation of this file.
2 
6 
8 
9 #include <memory>
10 
11 #include "nxLib.h"
12 
13 namespace camera_node
14 {
15 void abortInit(ensenso::ros::NodeHandle& nh, std::string const& errorMsg)
16 {
17  ENSENSO_ERROR(nh, "%s. Shutting down node.", errorMsg.c_str());
18  // nxLibFinalize is implicitly invoked in NxLibInitializeFinalize destructor.
19  exit(EXIT_FAILURE);
20 }
21 
23 {
24  ENSENSO_DEBUG(nh, "Initializing the NxLib...");
25  try
26  {
28  }
29  catch (NxLibException& e)
30  {
31  abortInit(nh, "Error while initializing the NxLib");
32  }
33 
34  int tcpPort;
35  if (ensenso::ros::get_parameter(nh, "tcp_port", tcpPort) && tcpPort != -1)
36  {
37  ENSENSO_DEBUG(nh, "Opening TCP port %d on the NxLib...", tcpPort);
38 
39  int openedPort;
40  try
41  {
42  nxLibOpenTcpPort(tcpPort, &openedPort);
43  ENSENSO_INFO(nh, "Opened TCP port %d on the NxLib.", openedPort);
44  }
45  catch (NxLibException& e)
46  {
47  abortInit(nh, "Error while opening TCP port (NxLib error message: " + e.getErrorText() + ")");
48  }
49  }
50 
51  int threads;
52  if (ensenso::ros::get_parameter(nh, "threads", threads) && threads > 0)
53  {
54  NxLibItem()[itmParameters][itmThreads] = threads;
55  }
56 }
57 
59 {
60  std::string serial;
61 
62  // Try to retrieve the serial as a string.
63  if (ensenso::ros::get_parameter(nh, "serial", serial))
64  {
65  // Delete optional trailing "!" character.
66  std::size_t pos = serial.find("!");
67  if (pos != std::string::npos)
68  {
69  serial.erase(pos);
70  }
71 
72  // Return the serial, because it might be the name for a file camera to be opened, which does not exists yet, and
73  // checking for its existence must be skipped.
74  return serial;
75  }
76 
77  // Try to retrieve the serial as an integer, because rosparam automatically converts numeric strings to integer.
78  int intSerial;
79  if (ensenso::ros::get_parameter(nh, "serial", intSerial))
80  {
81  serial = std::to_string(intSerial);
82  }
83 
84  NxLibItem cameraNode = NxLibItem()[itmCameras][itmBySerialNo][serial];
85  if (!serial.empty() && !cameraNode.exists())
86  {
87  ENSENSO_WARN(nh,
88  "If the camera serial only consists of digits, its numerical value might be too large to be "
89  "interpreted as a 32-bit integer. Append an \"!\" to the serial so that it can be interpreted as a "
90  "string, e.g. _serial:=1234567890!. If you are using a launch file, just define the parameter's type "
91  "as string, e.g. type=\"string\".");
92  abortInit(nh, "Could not find camera with serial " + serial);
93  }
94 
95  // String is empty if no serial was given.
96  return serial;
97 }
98 
99 std::string getSerialOfFirstCamera(ensenso::ros::NodeHandle& nh, std::string const& cameraNodeType)
100 {
101  std::string serial;
102 
103  bool foundAppropriateCamera = false;
104 
105  // Try to find the first camera that matches the type of the camera node.
106  NxLibItem cameras = NxLibItem()[itmCameras];
107  for (int i = 0; i < cameras.count(); i++)
108  {
109  NxLibItem camera = cameras[i];
110  NxLibItem cameraType = camera[itmType];
111  if (camera[itmStatus][itmAvailable].exists() && camera[itmStatus][itmAvailable].asBool() && cameraType.exists() &&
112  cameraType.asString() == cameraNodeType)
113  {
114  foundAppropriateCamera = true;
115  serial = camera.name();
116  break;
117  }
118  }
119 
120  if (!foundAppropriateCamera)
121  {
122  abortInit(nh, "Could not find any camera");
123  }
124 
125  return serial;
126 }
127 
128 std::string getSerial(ensenso::ros::NodeHandle& nh, std::string const& nodeType)
129 {
130  std::string serial = getSerialFromParameterServer(nh);
131 
132  if (!serial.empty())
133  {
134  return serial;
135  }
136 
137  // No serial was given, get the serial of the first camera listed by the NxLib.
138  return getSerialOfFirstCamera(nh, nodeType);
139 }
140 
142 {
143  std::string settingsFile;
144  if (ensenso::ros::get_parameter(nh, "settings", settingsFile))
145  {
146  ENSENSO_DEBUG(nh, "Loading camera settings...");
147  if (!camera.loadSettings(settingsFile, true))
148  {
149  abortInit(nh, "Failed to load the camera settings");
150  }
151  }
152 }
153 
154 template <typename CameraType>
155 std::unique_ptr<CameraType> initCamera(ensenso::ros::NodeHandleWrapper& nhw, std::string const& nodeType)
156 {
157  // Get the serial, either from the parameter server if the serial was given as parameter to the node or use the serial
158  // of the first camera in the list of the NxLib. At this point, the serial either belongs to a mono or stereo camera
159  // and the type matches the camera node's type.
160  std::string serial = getSerial(nhw.getPrivateNodeHandle(), nodeType);
161 
162  CameraParameters params(nhw.getPrivateNodeHandle(), nodeType, serial);
163  auto camera = ensenso::std::make_unique<CameraType>(nhw.getNodeHandle(), std::move(params));
164 
165  if (!camera->open())
166  {
167  abortInit(nhw.getNodeHandle(), "Failed to open the camera");
168  }
169 
171  camera->init();
172 
173  return camera;
174 }
175 
176 template std::unique_ptr<StereoCamera> initCamera<StereoCamera>(ensenso::ros::NodeHandleWrapper& nhw,
177  std::string const& nodeType);
178 template std::unique_ptr<MonoCamera> initCamera<MonoCamera>(ensenso::ros::NodeHandleWrapper& nhw,
179  std::string const& nodeType);
180 
181 } // namespace camera_node
ENSENSO_WARN
void ENSENSO_WARN(T... args)
Definition: logging.h:210
camera_node::getSerialFromParameterServer
std::string getSerialFromParameterServer(ensenso::ros::NodeHandle &nh)
Definition: camera_node.cpp:58
exists
ROSCPP_DECL bool exists(const std::string &service_name, bool print_failure_reason)
camera_node::initCamera< MonoCamera >
template std::unique_ptr< MonoCamera > initCamera< MonoCamera >(ensenso::ros::NodeHandleWrapper &nhw, std::string const &nodeType)
Camera
Definition: camera.h:255
CameraParameters
Definition: camera.h:175
camera_node::initCamera
std::unique_ptr< CameraType > initCamera(ensenso::ros::NodeHandleWrapper &nhw, std::string const &nodeType)
Definition: camera_node.cpp:155
ENSENSO_INFO
void ENSENSO_INFO(T... args)
Definition: logging.h:142
camera_node
Definition: camera_node.h:9
NxLibInitializeFinalize::instance
static NxLibInitializeFinalize & instance()
Definition: nxlib_initialize_finalize.cpp:7
ENSENSO_DEBUG
void ENSENSO_DEBUG(T... args)
Definition: logging.h:74
mono_camera.h
ensenso::ros::NodeHandleWrapper::getPrivateNodeHandle
NodeHandle & getPrivateNodeHandle()
Definition: node.h:229
camera_node::getSerialOfFirstCamera
std::string getSerialOfFirstCamera(ensenso::ros::NodeHandle &nh, std::string const &cameraNodeType)
Definition: camera_node.cpp:99
camera_node::loadCameraSettings
void loadCameraSettings(ensenso::ros::NodeHandle &nh, Camera &camera)
Definition: camera_node.cpp:141
camera_node::getSerial
std::string getSerial(ensenso::ros::NodeHandle &nh, std::string const &nodeType)
Definition: camera_node.cpp:128
ensenso::ros::get_parameter
bool get_parameter(NodeHandle &nh, const char *name, T &parameter)
Definition: core.h:148
Camera::loadSettings
bool loadSettings(std::string const &jsonFile, bool saveAsDefaultParameters=false)
Definition: camera.cpp:129
ensenso::ros::NodeHandle
::ros::NodeHandle NodeHandle
Definition: node.h:215
ensenso::ros::NodeHandleWrapper
Definition: node.h:217
camera_node::initCamera< StereoCamera >
template std::unique_ptr< StereoCamera > initCamera< StereoCamera >(ensenso::ros::NodeHandleWrapper &nhw, std::string const &nodeType)
nxlib_initialize_finalize.h
camera_node::initNxLib
void initNxLib(ensenso::ros::NodeHandle &nh)
Definition: camera_node.cpp:22
logging.h
ensenso::ros::NodeHandleWrapper::getNodeHandle
NodeHandle & getNodeHandle()
Definition: node.h:225
ENSENSO_ERROR
void ENSENSO_ERROR(T... args)
Definition: logging.h:278
camera_node.h
camera_node::abortInit
void abortInit(ensenso::ros::NodeHandle &nh, std::string const &errorMsg)
Definition: camera_node.cpp:15
stereo_camera.h


ensenso_camera
Author(s): Ensenso
autogenerated on Wed Apr 2 2025 02:37:46