camera.h
Go to the documentation of this file.
1 #pragma once
2 
10 
18 
28 
30 #include <tf2_ros/buffer.h>
33 
34 #include <fstream>
35 #include <map>
36 #include <memory>
37 #include <mutex>
38 #include <string>
39 #include <vector>
40 
41 #include "nxLib.h"
42 
46 double const STATUS_INTERVAL = 3.0; // Seconds.
47 
52 double const POSE_TF_INTERVAL = 1; // Seconds.
53 
57 double const TF_REQUEST_TIMEOUT = 10.; // Seconds.
58 
62 std::string const DEFAULT_PARAMETER_SET = "default";
63 
67 std::string const TARGET_FRAME_LINK = "Workspace";
68 
69 // The ROS node gives back error codes from the NxLib. Additionally, we use the following error codes to indicate errors
70 // from the ROS node itself.
72 int const ERROR_CODE_TF = 101;
73 
74 #define LOG_NXLIB_EXCEPTION(EXCEPTION) \
75  try \
76  { \
77  if (EXCEPTION.getErrorCode() == NxLibExecutionFailed) \
78  { \
79  NxLibItem executionNode(EXCEPTION.getItemPath()); \
80  ENSENSO_ERROR(nh, "%s: %s", executionNode[itmResult][itmErrorSymbol].asString().c_str(), \
81  executionNode[itmResult][itmErrorText].asString().c_str()); \
82  } /* NOLINT */ \
83  } /* NOLINT */ \
84  catch (...) \
85  { \
86  } /* NOLINT */ \
87  ENSENSO_DEBUG(nh, "Current NxLib tree: %s", NxLibItem().asJson(true).c_str());
88 
89 // The following macros are called at the beginning and end of each action handler that uses the NxLib. In case of an
90 // NxLib exception they automatically abort the action and return the corresponding error code and message.
91 // This assumes that all of our actions have the property error that represents an NxLibException.
92 
93 #define START_NXLIB_ACTION(ACTION_NAME, ACTION_SERVER) \
94  ENSENSO_DEBUG(nh, "Received a " #ACTION_NAME " request."); \
95  auto& server = ACTION_SERVER; \
96  if (server->isPreemptRequested()) \
97  { \
98  server->setPreempted(); \
99  return; \
100  } /* NOLINT */ \
101  std::lock_guard<std::mutex> lock(nxLibMutex); \
102  try \
103  {
104 #define FINISH_NXLIB_ACTION(ACTION_NAME) \
105  } /* NOLINT */ \
106  catch (NxLibException & e) \
107  { \
108  ENSENSO_ERROR(nh, "NxLibException %d (%s) for item %s", e.getErrorCode(), e.getErrorText().c_str(), \
109  e.getItemPath().c_str()); \
110  LOG_NXLIB_EXCEPTION(e) \
111  ensenso::action::ACTION_NAME##Result result; \
112  result.error.code = e.getErrorCode(); \
113  result.error.message = e.getErrorText(); \
114  server->setAborted(std::move(result)); \
115  return; \
116  } /* NOLINT */ \
117  catch (tf2::TransformException & e) \
118  { \
119  ENSENSO_ERROR(nh, "tf Exception: %s", e.what()); \
120  ensenso::action::ACTION_NAME##Result result; \
121  result.error.code = ERROR_CODE_TF; \
122  result.error.message = e.what(); \
123  server->setAborted(std::move(result)); \
124  return; \
125  } /* NOLINT */ \
126  catch (std::exception & e) \
127  { \
128  ENSENSO_ERROR(nh, "Unknown Exception: %s", e.what()); \
129  ensenso::action::ACTION_NAME##Result result; \
130  result.error.code = ERROR_CODE_UNKNOWN_EXCEPTION; \
131  result.error.message = e.what(); \
132  server->setAborted(std::move(result)); \
133  return; \
134  }
135 
136 #define PREEMPT_ACTION_IF_REQUESTED \
137  if (server->isPreemptRequested()) \
138  { \
139  server->setPreempted(); \
140  return; \
141  }
142 
147 {
151  NxLibItem node;
152 
156  bool useROI = false;
157 
162 
167  bool autoProjector = true;
168 
169  ParameterSet(std::string const& name, NxLibItem const& defaultParameters);
170 };
171 
176 {
180  std::string serial;
181 
186  bool isFileCamera = false;
187 
191  std::string fileCameraPath;
192 
196  bool fixed = false;
197 
201  bool wait_for_camera = false;
202 
206  std::string cameraFrame;
207 
219  std::string linkFrame;
220 
227  std::string targetFrame;
228 
234  std::string robotFrame;
235 
240  std::string wristFrame;
241 
245  int captureTimeout = 0;
246 
250  std::unique_ptr<ensenso_camera::VirtualObjectHandler> virtualObjectHandler = nullptr;
251 
252  CameraParameters(ensenso::ros::NodeHandle& nh, std::string const& cameraType, std::string serial);
253 };
254 
255 class Camera
256 {
257 protected:
259 
260  // Whether the camera is a file camera and was created by this node. If it was created by this node, it will be
261  // deleted after it got closed.
262  bool createdFileCamera = false;
263 
265 
266  NxLibItem cameraNode;
267 
268  // Controls parallel access to the NxLib.
269  mutable std::mutex nxLibMutex;
270 
272 
276 
277  // tf buffer, that will store transformations for 10 seconds and dropping transformation afterwards.
278  std::unique_ptr<tf2_ros::Buffer> tfBuffer;
279  std::unique_ptr<tf2_ros::TransformListener> transformListener;
280  std::unique_ptr<tf2_ros::TransformBroadcaster> transformBroadcaster;
281 
282  std::unique_ptr<AccessTreeServer> accessTreeServer;
283  std::unique_ptr<ExecuteCommandServer> executeCommandServer;
284  std::unique_ptr<GetParameterServer> getParameterServer;
285  std::unique_ptr<SetParameterServer> setParameterServer;
286 
287  // Saves the latest transforms for a specific frame.
288  mutable std::map<std::string, geometry_msgs::msg::TransformStamped> transformationCache;
289 
290  // Contains a parameter tree that is used for creating new parameter sets.
291  NxLibItem defaultParameters;
292 
293  std::map<std::string, ParameterSet> parameterSets;
294  std::string currentParameterSet;
295 
296 public:
298 
302  bool open();
303 
308  {
309  }
310 
314  virtual void init() = 0;
315 
319  void close();
320 
325  virtual void startServers() const;
326 
333  bool loadSettings(std::string const& jsonFile, bool saveAsDefaultParameters = false);
334 
338  void onAccessTree(ensenso::action::AccessTreeGoalConstPtr const& goal);
339 
343  void onExecuteCommand(ensenso::action::ExecuteCommandGoalConstPtr const& goal);
344 
348  void onGetParameter(ensenso::action::GetParameterGoalConstPtr const& goal);
349 
353  virtual void onSetParameter(ensenso::action::SetParameterGoalConstPtr const& goal) = 0;
354 
355 protected:
359  virtual void initTfPublishTimer();
360 
364  virtual void initStatusTimer();
365 
372  void saveParameterSet(std::string name);
373 
377  bool cameraIsAvailable() const;
378 
382  bool cameraIsOpen() const;
383 
387  bool hasLink() const;
388 
392  void publishStatus(TIMER_CALLBACK_DECLARATION_ARGS);
393 
398  void saveDefaultParameterSet();
403  void loadParameterSet(std::string name);
404 
408  virtual ensenso::ros::Time capture() const = 0;
409 
418  virtual geometry_msgs::msg::PoseStamped estimatePatternPose(ensenso::ros::Time imageTimestamp,
419  std::string const& targetFrame = "",
420  bool latestPatternOnly = false) const = 0;
421 
425  virtual std::vector<geometry_msgs::msg::PoseStamped>
426  estimatePatternPoses(ensenso::ros::Time imageTimestamp, std::string const& targetFrame = "") const = 0;
427 
434  void updateGlobalLink(ensenso::ros::Time time, std::string targetFrame = "",
435  bool useCachedTransformation = false) const;
436 
440  std::string getNxLibTargetFrameName() const;
441 
446  void updateTransformations(tf2::Transform const& targetFrameTransformation) const;
447 
451  void fillBasicCameraInfoFromNxLib(sensor_msgs::msg::CameraInfoPtr const& info) const;
452 
456  virtual void updateCameraInfo() = 0;
457 
461  virtual ensenso::msg::ParameterPtr readParameter(std::string const& key) const;
462 
466  virtual void writeParameter(ensenso::msg::Parameter const& parameter);
467 
473  void publishCurrentLinks(TIMER_CALLBACK_DECLARATION_ARGS);
474 
478  void publishCameraLink();
479 
483  geometry_msgs::msg::TransformStamped stampedLinkToCamera();
484 
488  tf2::Transform getCameraToLinkTransform();
489 };
std::string const DEFAULT_PARAMETER_SET
Definition: camera.h:62
bool useROI
Definition: camera.h:156
std::map< std::string, ParameterSet > parameterSets
Definition: camera.h:293
std::string cameraFrame
Definition: camera.h:206
NxLibItem node
Definition: camera.h:151
NxLibVersion nxLibVersion
Definition: camera.h:264
::ros::Time Time
Definition: time.h:67
void init(const M_string &remappings)
double const POSE_TF_INTERVAL
Definition: camera.h:52
CameraParameters params
Definition: camera.h:258
std::string const TARGET_FRAME_LINK
Definition: camera.h:67
double const STATUS_INTERVAL
Definition: camera.h:46
bool autoProjector
Definition: camera.h:167
int const ERROR_CODE_UNKNOWN_EXCEPTION
Definition: camera.h:71
NxLibItem defaultParameters
Definition: camera.h:291
ensenso::ros::Timer cameraPosePublisher
Definition: camera.h:275
std::mutex nxLibMutex
Definition: camera.h:269
::ros::Timer Timer
Definition: time.h:68
std::string serial
Definition: camera.h:180
ensenso::ros::NodeHandle nh
Definition: camera.h:271
PointCloudROI roi
Definition: camera.h:161
double const TF_REQUEST_TIMEOUT
Definition: camera.h:57
std::unique_ptr< SetParameterServer > setParameterServer
Definition: camera.h:285
std::string fileCameraPath
Definition: camera.h:191
std::unique_ptr< tf2_ros::Buffer > tfBuffer
Definition: camera.h:278
std::string wristFrame
Definition: camera.h:240
virtual void updateCameraTypeSpecifics()
Definition: camera.h:307
std::map< std::string, geometry_msgs::msg::TransformStamped > transformationCache
Definition: camera.h:288
ensenso::ros::Timer statusTimer
Definition: camera.h:274
ensenso::ros::Publisher< diagnostic_msgs::msg::DiagnosticArray > statusPublisher
Definition: camera.h:273
std::string linkFrame
Definition: camera.h:219
::ros::NodeHandle NodeHandle
Definition: node.h:214
std::unique_ptr< ExecuteCommandServer > executeCommandServer
Definition: camera.h:283
NxLibItem cameraNode
Definition: camera.h:266
std::string targetFrame
Definition: camera.h:227
::std::unique_ptr< ::ros::Publisher > Publisher
Definition: core.h:115
ParameterSet(std::string const &name, NxLibItem const &defaultParameters)
Definition: camera.cpp:12
#define TIMER_CALLBACK_DECLARATION_ARGS
Definition: time.h:94
std::unique_ptr< AccessTreeServer > accessTreeServer
Definition: camera.h:282
std::unique_ptr< GetParameterServer > getParameterServer
Definition: camera.h:284
int const ERROR_CODE_TF
Definition: camera.h:72
Definition: camera.h:255
std::string currentParameterSet
Definition: camera.h:294
std::string robotFrame
Definition: camera.h:234
std::unique_ptr< tf2_ros::TransformListener > transformListener
Definition: camera.h:279
std::unique_ptr< tf2_ros::TransformBroadcaster > transformBroadcaster
Definition: camera.h:280


ensenso_camera
Author(s): Ensenso
autogenerated on Sat Jun 3 2023 02:17:04