HokuyoAIST  3.0.1
sensor.h
Go to the documentation of this file.
1 /* HokuyoAIST
2  *
3  * Header file for the sensor object.
4  *
5  * Copyright 2008-2011 Geoffrey Biggs geoffrey.biggs@aist.go.jp
6  * RT-Synthesis Research Group
7  * Intelligent Systems Research Institute,
8  * National Institute of Advanced Industrial Science and Technology (AIST),
9  * Japan
10  * All rights reserved.
11  *
12  * This file is part of HokuyoAIST.
13  *
14  * HokuyoAIST is free software; you can redistribute it and/or modify it
15  * under the terms of the GNU Lesser General Public License as published
16  * by the Free Software Foundation; either version 2.1 of the License,
17  * or (at your option) any later version.
18  *
19  * HokuyoAIST is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with HokuyoAIST. If not, see
26  * <http://www.gnu.org/licenses/>.
27  */
28 
29 #ifndef SENSOR_H__
30 #define SENSOR_H__
31 
32 #include <string>
33 
34 #if defined(WIN32)
35  typedef unsigned char uint8_t;
36  typedef unsigned int uint32_t;
37  #if defined(HOKUYOAIST_STATIC)
38  #define HOKUYOAIST_EXPORT
39  #elif defined(hokuyoaist_EXPORTS)
40  #define HOKUYOAIST_EXPORT __declspec(dllexport)
41  #else
42  #define HOKUYOAIST_EXPORT __declspec(dllimport)
43  #endif
44 #else
45  #include <stdint.h>
46  #define HOKUYOAIST_EXPORT
47 #endif
48 
49 namespace flexiport
50 {
51  class Port;
52 }
53 
58 namespace hokuyoaist
59 {
60 
74 {
80 };
81 
82 
84 {
85  switch(mode)
86  {
87  case ME_OFF:
88  return "Off";
89  case ME_FRONT:
90  return "Front";
91  case ME_MIDDLE:
92  return "Middle";
93  case ME_REAR:
94  return "Rear";
95  case ME_AVERAGE:
96  return "Average";
97  default:
98  return "Unknown";
99  }
100 }
101 
102 
104 typedef struct IPAddr
105 {
107  unsigned int first;
109  unsigned int second;
111  unsigned int third;
113  unsigned int fourth;
114 } IPAddr;
115 
116 
131 {
132  public:
133  Sensor();
134  Sensor(std::ostream& err_output);
135  ~Sensor();
136 
138  void open(std::string port_options);
139 
141  void close();
142 
144  bool is_open() const;
145 
147  void set_power(bool on);
148 
155  void set_ip(IPAddr const& addr, IPAddr const& subnet,
156  IPAddr const& gateway);
157 
159  void reset();
160 
164  void semi_reset();
165 
175  void set_motor_speed(unsigned int speed);
176 
179  void set_high_sensitivity(bool on);
180 
182  void get_sensor_info(SensorInfo& info);
183 
185  unsigned long long get_time();
186 
188  unsigned int get_raw_time();
189 
218  long long calibrate_time(unsigned int skew_sleep_time=0,
219  unsigned int samples=10);
220 
222  long long time_offset() const { return time_offset_; }
223 
225  void set_time_offset(long long time_offset)
226  { time_offset_ = time_offset; }
228  float drift_rate() const { return time_drift_rate_; }
244  void set_drift_rate(float drift_rate)
245  { time_drift_rate_ = drift_rate; }
246 
248  float skew_alpha() const { return time_skew_alpha_; }
261  void set_skew_alpha(float alpha) { time_skew_alpha_ = alpha; }
262 
285  unsigned int get_ranges(ScanData& data, int start_step = -1,
286  int end_step = -1, unsigned int cluster_count = 1);
287 
302  unsigned int get_ranges_by_angle(ScanData& data, double start_angle,
303  double end_angle, unsigned int cluster_count = 1);
304 
327  unsigned int get_ranges_intensities(ScanData& data,
328  int start_step = -1, int end_step = -1,
329  unsigned int cluster_count = 1);
330 
345  unsigned int get_ranges_intensities_by_angle(ScanData& data,
346  double start_angle, double end_angle,
347  unsigned int cluster_count = 1);
348 
375  unsigned int get_new_ranges(ScanData& data, int start_step = -1,
376  int end_step = -1, unsigned int cluster_count = 1);
377 
392  unsigned int get_new_ranges_by_angle(ScanData& data,
393  double start_angle, double end_angle,
394  unsigned int cluster_count = 1);
395 
421  unsigned int get_new_ranges_intensities(ScanData& data,
422  int start_step = -1, int end_step = -1,
423  unsigned int cluster_count = 1);
424 
439  unsigned int get_new_ranges_intensities_by_angle(ScanData& data,
440  double start_angle, double end_angle,
441  unsigned int cluster_count = 1);
442 
444  uint8_t scip_version() const { return scip_version_; }
445 
448  void set_verbose(bool verbose) { verbose_ = verbose; }
449 
452  void ignore_unknowns(bool ignore) { ignore_unknowns_ = ignore; }
453 
455  void set_multiecho_mode(MultiechoMode mode) { multiecho_mode_ = mode; }
456 
458  double step_to_angle(unsigned int step);
461  unsigned int angle_to_step(double angle);
462 
463  private:
464  flexiport::Port* port_;
465  std::ostream& err_output_;
466 
467  uint8_t scip_version_;
468  LaserModel model_;
469  bool verbose_, enable_checksum_workaround_,
470  ignore_unknowns_;
471  MultiechoMode multiecho_mode_;
472  double min_angle_, max_angle_, resolution_;
473  int first_step_, last_step_, front_step_;
474  unsigned int max_range_;
476  unsigned int time_resolution_;
479  long long time_offset_;
482  unsigned int last_timestamp_;
484  unsigned int wrap_count_;
486  float time_drift_rate_;
488  float time_skew_alpha_;
489 
490  void clear_read_buffer();
491  int read_line(char* buffer, int expected_length=-1);
492  int read_line_with_check(char* buffer, int expected_length=-1,
493  bool has_semicolon=false);
494  bool read_data_block(char* buffer, int& block_size);
495  void skip_lines(int count);
496  int send_command(char const* cmd, char const* param, int param_length,
497  char const* extra_ok);
498 
499  void enter_timing_mode();
500  void leave_timing_mode();
502  unsigned int get_timing_mode_time(unsigned long long* reception_time=0);
504  unsigned long long get_computer_time();
506  unsigned int wrap_timestamp(unsigned int timestamp);
509  unsigned long long offset_timestamp(unsigned int timestamp);
512  unsigned int step_to_time_offset(int start_step);
513 
514  void find_model(char const* buffer);
515  void get_and_set_scip_version();
516  void get_defaults();
517  void process_vv_line(char const* buffer, SensorInfo& info);
518  void process_pp_line(char const* buffer, SensorInfo& info);
519  void process_ii_line(char const* buffer, SensorInfo& info);
520 
521  uint32_t process_echo_buffer(int const* buffer, int num_echos);
522  void read_2_byte_range_data(ScanData& data, unsigned int num_steps);
523  void read_3_byte_range_data(ScanData& data, unsigned int num_steps);
524  void read_3_byte_range_and_intensity_data(ScanData& data,
525  unsigned int num_steps);
526 
527  int confirm_checksum(char const* buffer, int length,
528  int expected_sum);
529 }; // class Sensor
530 
531 } // namespace hokuyoaist
532 
535 #endif // SENSOR_H__
536