r2000_driver.cpp
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 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice, this
9 // list of conditions and the following disclaimer.
10 //
11 // * Redistributions in binary form must reproduce the above copyright notice, this
12 // list of conditions and the following disclaimer in the documentation and/or
13 // other materials provided with the distribution.
14 //
15 // * Neither the name of Pepperl+Fuchs nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #include <ctime>
31 #include <algorithm>
35 #include "scan_data_receiver.h"
36 
37 namespace pepperl_fuchs {
38 
40 {
42  data_receiver_ = 0;
43  is_connected_ = false;
44  is_capturing_ = false;
46 }
47 
48 //-----------------------------------------------------------------------------
49 bool R2000Driver::connect(const std::string hostname, int port)
50 {
51  command_interface_ = new HttpCommandInterface(hostname,port);
52  auto opi = command_interface_->getProtocolInfo();
53  if( !opi || (*opi).version_major != 1 )
54  {
55  std::cerr << "ERROR: Could not connect to laser range finder!" << std::endl;
56  return false;
57  }
58 
59  if( (*opi).version_major != 1 )
60  {
61  std::cerr << "ERROR: Wrong protocol version (version_major=" << (*opi).version_major << ", version_minor=" << (*opi).version_minor << ")" << std::endl;
62  return false;
63  }
64 
65  protocol_info_ = *opi;
67  is_connected_ = true;
68  return true;
69 }
70 
71 //-----------------------------------------------------------------------------
73 {
74  disconnect();
75 }
76 
77 //-----------------------------------------------------------------------------
79 {
80  if( !checkConnection() )
81  return false;
82 
84  if( !handle_info_ )
85  return false;
86 
88  if( !data_receiver_->isConnected() || !command_interface_->startScanOutput((*handle_info_).handle))
89  return false;
90 
91  food_timeout_ = std::floor(std::max((handle_info_->watchdog_timeout/1000.0/3.0),1.0));
92  is_capturing_ = true;
93  return true;
94 }
95 
96 //-----------------------------------------------------------------------------
98 {
99  if( !checkConnection() )
100  return false;
101 
103  if( !data_receiver_->isConnected() )
104  return false;
105  int udp_port = data_receiver_->getUDPPort();
106 
108  if( !handle_info_ || !command_interface_->startScanOutput((*handle_info_).handle) )
109  return false;
110 
111  food_timeout_ = std::floor(std::max((handle_info_->watchdog_timeout/1000.0/3.0),1.0));
112  is_capturing_ = true;
113  return true;
114 }
115 
116 //-----------------------------------------------------------------------------
118 {
120  return false;
121 
122  bool return_val = checkConnection();
123 
124  return_val = return_val && command_interface_->stopScanOutput((*handle_info_).handle);
125 
126  delete data_receiver_;
127  data_receiver_ = 0;
128 
129  is_capturing_ = false;
130  return_val = return_val && command_interface_->releaseHandle(handle_info_->handle);
131  handle_info_ = boost::optional<HandleInfo>();
132  return return_val;
133 }
134 
135 //-----------------------------------------------------------------------------
137 {
139  {
140  std::cerr << "ERROR: No connection to laser range finder or connection lost!" << std::endl;
141  return false;
142  }
143  return true;
144 }
145 
146 //-----------------------------------------------------------------------------
148 {
149  feedWatchdog();
150  if( data_receiver_ )
151  return data_receiver_->getScan();
152  else
153  {
154  std::cerr << "ERROR: No scan capturing started!" << std::endl;
155  return ScanData();
156  }
157 }
158 
159 //-----------------------------------------------------------------------------
161 {
162  feedWatchdog();
163  if( data_receiver_ )
164  return data_receiver_->getFullScan();
165  else
166  {
167  std::cerr << "ERROR: No scan capturing started!" << std::endl;
168  return ScanData();
169  }
170 }
171 
172 //-----------------------------------------------------------------------------
174 {
175  if( data_receiver_ )
177  else
178  {
179  std::cerr << "ERROR: No scan capturing started!" << std::endl;
180  return 0;
181  }
182 }
183 
184 //-----------------------------------------------------------------------------
186 {
187  if( data_receiver_ )
189  else
190  {
191  std::cerr << "ERROR: No scan capturing started!" << std::endl;
192  return 0;
193  }
194 }
195 
196 //-----------------------------------------------------------------------------
198 {
199  if( isCapturing() )
200  stopCapturing();
201 
202  delete data_receiver_;
203  delete command_interface_;
204  data_receiver_ = 0;
205  command_interface_ = 0;
206 
207  is_capturing_ = false;
208  is_connected_ = false;
209 
210  handle_info_ = boost::optional<HandleInfo>();
212  parameters_ = std::map< std::string, std::string >();
213 }
214 
215 //-----------------------------------------------------------------------------
217 {
219 }
220 
221 //-----------------------------------------------------------------------------
222 const std::map< std::string, std::string >& R2000Driver::getParameters()
223 {
224  if( command_interface_ )
226  return parameters_;
227 }
228 
229 //-----------------------------------------------------------------------------
230 bool R2000Driver::setScanFrequency(unsigned int frequency)
231 {
232  if( !command_interface_ )
233  return false;
234  return command_interface_->setParameter("scan_frequency",std::to_string(frequency));
235 }
236 
237 //-----------------------------------------------------------------------------
238 bool R2000Driver::setSamplesPerScan(unsigned int samples)
239 {
240  if( !command_interface_ )
241  return false;
242  return command_interface_->setParameter("samples_per_scan",std::to_string(samples));
243 }
244 
245 //-----------------------------------------------------------------------------
247 {
248  if( !command_interface_ )
249  return false;
251 }
252 
253 //-----------------------------------------------------------------------------
254 bool R2000Driver::resetParameters(const std::vector<std::string> &names)
255 {
256  if( !command_interface_ )
257  return false;
258  return command_interface_->resetParameters(names);
259 }
260 
261 //-----------------------------------------------------------------------------
262 bool R2000Driver::setParameter(const std::string &name, const std::string &value)
263 {
264  if( !command_interface_ )
265  return false;
266  return command_interface_->setParameter(name,value);
267 }
268 
269 //-----------------------------------------------------------------------------
270 void R2000Driver::feedWatchdog(bool feed_always)
271 {
272  const double current_time = std::time(0);
273 
274  if( (feed_always || watchdog_feed_time_<(current_time-food_timeout_)) && handle_info_ && command_interface_ )
275  {
277  std::cerr << "ERROR: Feeding watchdog failed!" << std::endl;
278  watchdog_feed_time_ = current_time;
279  }
280 }
281 
282 //-----------------------------------------------------------------------------
283 //-----------------------------------------------------------------------------
284 } // NS
double watchdog_feed_time_
Last time the watchdog was fed.
Definition: r2000_driver.h:155
ScanDataReceiver * data_receiver_
Asynchronous data receiver.
Definition: r2000_driver.h:146
bool setParameter(const std::string name, const std::string value)
int getUDPPort() const
Get open and receiving UDP port.
bool startScanOutput(const std::string &handle)
Initiate output of scan data.
std::size_t getScansAvailable() const
Get the total number of laserscans available (even scans which are not fully reveived yet) ...
Information about the HTTP/JSON protocol.
Definition: protocol_info.h:19
bool resetParameters(const std::vector< std::string > &names)
boost::optional< HandleInfo > requestHandleTCP(int start_angle=-1800000)
bool resetParameters(const std::vector< std::string > &names)
~R2000Driver()
Cleanly disconnect in case of destruction.
std::size_t getFullScansAvailable() const
Get the total number of fully received laserscans available.
bool setSamplesPerScan(unsigned int samples)
bool releaseHandle(const std::string &handle)
Release handle.
std::size_t getScansAvailable() const
Get the total number of laserscans available (even scans which are not fully reveived yet) ...
bool setScanFrequency(unsigned int frequency)
HttpCommandInterface * command_interface_
HTTP/JSON interface of the scanner.
Definition: r2000_driver.h:143
ProtocolInfo protocol_info_
Cached version of the protocol info.
Definition: r2000_driver.h:164
std::map< std::string, std::string > getParameters(const std::vector< std::string > &names)
bool isConnected()
Return connection status.
Definition: r2000_driver.h:65
void feedWatchdog(bool feed_always=false)
Feed the watchdog with the current handle ID, to keep the data connection alive.
Normally contains one complete laserscan (a full rotation of the scanner head)
R2000Driver()
Initialize driver.
boost::optional< HandleInfo > requestHandleUDP(int port, std::string hostname=std::string(""), int start_angle=-1800000)
void disconnect()
Disconnect from the laserscanner and reset internal state.
Allows accessing the HTTP/JSON interface of the Pepperl+Fuchs Laserscanner R2000. ...
std::map< std::string, std::string > parameters_
Cached version of all parameter values.
Definition: r2000_driver.h:167
boost::optional< ProtocolInfo > getProtocolInfo()
bool connect(const std::string hostname, int port=80)
bool stopScanOutput(const std::string &handle)
Terminate output of scan data.
bool setParameter(const std::string &name, const std::string &value)
std::vector< std::string > getParameterList()
bool is_connected_
Internal connection state.
Definition: r2000_driver.h:149
bool isConnected() const
Return connection status.
boost::optional< HandleInfo > handle_info_
Handle information about data connection.
Definition: r2000_driver.h:161
Receives data of the laser range finder via IP socket Receives the scanner data with asynchronous fun...
std::size_t getFullScansAvailable() const
Get the total number of fully received laserscans available.
bool feedWatchdog(const std::string &handle)
Feed the watchdog to keep the handle alive.
bool is_capturing_
Internal capturing state.
Definition: r2000_driver.h:152
const std::map< std::string, std::string > & getParameters()
double food_timeout_
Feeding interval (in seconds)
Definition: r2000_driver.h:158


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