scan_data_receiver.h
Go to the documentation of this file.
1 // Copyright (c) 2014, Pepperl+Fuchs GmbH, Mannheim
2 // Copyright (c) 2014, Denis Dillenberger
3 // All rights reserved.
4 //
5 // Use, modification, and distribution is subject to the
6 // 3-clause BSD license ("Revised BSD License",
7 // "New BSD License", or "Modified BSD License")
8 // You should have received a copy of this license
9 // in a file named COPYING or LICENSE.
10 
11 #ifndef SCAN_DATA_RECEIVER_H
12 #define SCAN_DATA_RECEIVER_H
13 
14 #define BOOST_CB_DISABLE_DEBUG
15 #include <string>
16 #include <iostream>
17 #include <mutex>
18 #include <condition_variable>
19 #include <deque>
20 #include <array>
21 #include <boost/bind.hpp>
22 #include <boost/asio.hpp>
23 #include <boost/thread.hpp>
24 #include <boost/circular_buffer.hpp>
26 
27 namespace pepperl_fuchs {
28 
33 {
34 public:
36  ScanDataReceiver(const std::string hostname, const int tcp_port);
37 
40 
43 
45  int getUDPPort() const { return udp_port_; }
46 
48  bool isConnected() const { return is_connected_; }
49 
51  void disconnect();
52 
57  ScanData getScan();
58 
63 
65  std::size_t getScansAvailable() const { return scan_data_.size(); }
66 
68  std::size_t getFullScansAvailable() const;
69 
70 private:
72  void handleSocketRead(const boost::system::error_code& error);
73 
75  void handleSocketRead(const boost::system::error_code& error, std::size_t bytes_transferred);
76 
79  bool handleNextPacket();
80 
83  int findPacketStart();
84 
87  bool retrievePacket( std::size_t start, PacketTypeC* p );
88 
91  bool checkConnection();
92 
96  void readBufferFront(char* dst, std::size_t numbytes );
97 
101  void writeBufferBack(char* src, std::size_t numbytes );
102 
105 
108 
110  boost::thread io_service_thread_;
111  boost::asio::io_service io_service_;
112 
114  boost::asio::streambuf inbuf_;
115 
117  std::istream instream_;
118 
120  boost::asio::ip::tcp::socket* tcp_socket_;
121 
123  boost::asio::ip::udp::socket* udp_socket_;
124 
126  boost::asio::ip::udp::endpoint udp_endpoint_;
127 
129  std::array< char, 65536 > udp_buffer_;
130 
132  boost::circular_buffer<char> ring_buffer_;
133 
135  std::mutex data_mutex_;
136 
138  std::condition_variable data_notifier_;
139 
141  std::deque<ScanData> scan_data_;
142 
145 };
146 
147 }
148 #endif // SCAN_DATA_RECEIVER_H
boost::thread io_service_thread_
Event handler thread.
boost::asio::ip::tcp::socket * tcp_socket_
Receiving socket.
int getUDPPort() const
Get open and receiving UDP port.
ROSCPP_DECL void start()
std::size_t getScansAvailable() const
Get the total number of laserscans available (even scans which are not fully reveived yet) ...
void disconnect()
Disconnect and cleanup.
~ScanDataReceiver()
Disconnect cleanly.
std::size_t getFullScansAvailable() const
Get the total number of fully received laserscans available.
double last_data_time_
time in seconds since epoch, when last data was received
void handleSocketRead(const boost::system::error_code &error)
Asynchronous callback function, called if data has been reveived by the TCP socket.
boost::asio::streambuf inbuf_
Boost::Asio streambuffer.
boost::asio::ip::udp::socket * udp_socket_
Receiving socket.
Normally contains one complete laserscan (a full rotation of the scanner head)
std::mutex data_mutex_
Protection against data races between ROS and IO threads.
boost::asio::ip::udp::endpoint udp_endpoint_
Endpoint in case of UDP receiver.
std::condition_variable data_notifier_
Data notification condition variable.
bool retrievePacket(std::size_t start, PacketTypeC *p)
boost::circular_buffer< char > ring_buffer_
Internal ringbuffer for temporarily storing reveived data.
ScanDataReceiver()
Open an UDP port and listen on it.
boost::asio::io_service io_service_
Structure of a UDP or TCP data packet from the laserscanner.
bool isConnected() const
Return connection status.
Receives data of the laser range finder via IP socket Receives the scanner data with asynchronous fun...
std::array< char, 65536 > udp_buffer_
Buffer in case of UDP receiver.
void readBufferFront(char *dst, std::size_t numbytes)
void writeBufferBack(char *src, std::size_t numbytes)
int udp_port_
Data (UDP) port at local side.
std::deque< ScanData > scan_data_
Double ended queue with sucessfully received and parsed data, organized as single complete scans...
bool is_connected_
Internal connection state.
std::istream instream_
Input stream.


pepperl_fuchs_r2000
Author(s): Denis Dillenberger
autogenerated on Mon Jun 10 2019 14:12:48