sick_safevisionary.cpp
Go to the documentation of this file.
1 // -- BEGIN LICENSE BLOCK ----------------------------------------------
20 // -- END LICENSE BLOCK ------------------------------------------------
21 
22 //----------------------------------------------------------------------
29 //----------------------------------------------------------------------
30 
32 #include <thread>
33 
35  : priv_nh_("~/")
36  , data_available_(false)
37 {
38  priv_nh_.param<std::string>("frame_id", frame_id_, "camera");
39  priv_nh_.param<int>("udp_port", udp_port_, 6060);
40 
41  data_handle_ = std::make_shared<visionary::SafeVisionaryData>();
42  data_stream_ = std::make_shared<visionary::SafeVisionaryDataStream>(data_handle_);
43 
44  if (data_stream_->openUdpConnection(htons((int)udp_port_)))
45  {
46  ROS_INFO_STREAM("Opening UDP Connection to Sensor.");
47  bool waiting_for_connection = true;
48  while (waiting_for_connection)
49  {
50  ROS_INFO_STREAM("Waiting for camera connection.");
51  if (data_stream_->getNextBlobUdp())
52  {
53  waiting_for_connection = false;
54  ROS_INFO_STREAM("Received first frame, starting to publish data.");
55  }
56  else
57  {
58  ROS_INFO_STREAM("No UDP packages received until now. Please make sure that your sensor is "
59  "connected and correctly configured.");
60  }
61  }
62  }
63  else
64  {
65  ROS_INFO_STREAM("Could not open udp connection");
66  return;
67  }
68 }
69 
71 {
72  receive_thread_ptr_ = std::make_unique<std::thread>(&SickSafeVisionary::receiveThread, this);
73  publish_thread_ptr_ = std::make_unique<std::thread>(&SickSafeVisionary::publishThread, this);
74 }
75 
77 {
78  receive_thread_ptr_->join();
79  publish_thread_ptr_->join();
80  data_stream_->closeUdpConnection();
81 }
82 
84 {
85  while (ros::ok())
86  {
87  if (data_stream_->getNextBlobUdp())
88  {
90  data_available_ = true;
91  }
92  else
93  {
94  ROS_DEBUG_STREAM("UDP stream was incomplete. Skipping frame and waiting for new data.");
95  continue;
96  }
97  }
98 }
99 
101 {
102  while (ros::ok())
103  {
104  if (data_available_)
105  {
106  visionary::SafeVisionaryData frame_data;
107  spsc_queue_.pop(frame_data);
108  std_msgs::Header header;
109  header.frame_id = frame_id_;
110  header.stamp = ros::Time::now();
111  compound_publisher_.publish(header, frame_data);
112  data_available_ = false;
113  }
114  else
115  {
116  std::this_thread::sleep_for(std::chrono::microseconds(10));
117  }
118  }
119 }
SickSafeVisionary::data_handle_
std::shared_ptr< visionary::SafeVisionaryData > data_handle_
Definition: sick_safevisionary.h:70
SickSafeVisionary::priv_nh_
ros::NodeHandle priv_nh_
Definition: sick_safevisionary.h:73
SickSafeVisionary::publishThread
void publishThread()
Thread for publishing sensor data.
Definition: sick_safevisionary.cpp:100
SickSafeVisionary::udp_port_
int udp_port_
Definition: sick_safevisionary.h:75
sick_safevisionary.h
ROS_DEBUG_STREAM
#define ROS_DEBUG_STREAM(args)
ros::ok
ROSCPP_DECL bool ok()
SickSafeVisionary::publish_thread_ptr_
std::unique_ptr< std::thread > publish_thread_ptr_
Definition: sick_safevisionary.h:77
SickSafeVisionary::frame_id_
std::string frame_id_
Definition: sick_safevisionary.h:74
SickSafeVisionary::receiveThread
void receiveThread()
Thread for receiving sensor data.
Definition: sick_safevisionary.cpp:83
SickSafeVisionary::spsc_queue_
boost::lockfree::spsc_queue< visionary::SafeVisionaryData, boost::lockfree::capacity< 10 > > spsc_queue_
Definition: sick_safevisionary.h:81
ROS_INFO_STREAM
#define ROS_INFO_STREAM(args)
SickSafeVisionary::SickSafeVisionary
SickSafeVisionary()
Definition: sick_safevisionary.cpp:34
SickSafeVisionary::receive_thread_ptr_
std::unique_ptr< std::thread > receive_thread_ptr_
Definition: sick_safevisionary.h:76
SickSafeVisionary::compound_publisher_
CompoundPublisher compound_publisher_
Definition: sick_safevisionary.h:68
SickSafeVisionary::data_stream_
std::shared_ptr< visionary::SafeVisionaryDataStream > data_stream_
Definition: sick_safevisionary.h:71
SickSafeVisionary::stop
void stop()
Stops the driver functionality.
Definition: sick_safevisionary.cpp:76
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
SickSafeVisionary::run
void run()
Starts the driver functionality.
Definition: sick_safevisionary.cpp:70
header
const std::string header
CompoundPublisher::publish
void publish(const std_msgs::Header &header, visionary::SafeVisionaryData &frame_data)
Publish once with all registered publishers.
Definition: compound_publisher.cpp:53
ros::Time::now
static Time now()
SickSafeVisionary::data_available_
std::atomic< bool > data_available_
Definition: sick_safevisionary.h:78


sick_safevisionary_driver
Author(s):
autogenerated on Fri Oct 20 2023 02:09:16