stream.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 The urg_stamped Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SCIP2_RESPONSE_STREAM_H
18 #define SCIP2_RESPONSE_STREAM_H
19 
20 #include <boost/asio.hpp>
21 
22 #include <map>
23 #include <string>
24 #include <vector>
25 
26 #include <scip2/decode.h>
28 
29 namespace scip2
30 {
31 class ScanData
32 {
33 public:
34  uint32_t timestamp_;
35  std::vector<int32_t> ranges_;
36  std::vector<int32_t> intensities_;
37 };
38 
39 class ResponseStream : public Response
40 {
41 public:
42  using Callback = boost::function<void(
43  const boost::posix_time::ptime &,
44  const std::string &,
45  const std::string &,
46  const ScanData &)>;
47 
48 protected:
50 
51 public:
52  virtual std::string getCommandCode() const = 0;
53  virtual void operator()(
54  const boost::posix_time::ptime &,
55  const std::string &,
56  const std::string &,
57  std::istream &) = 0;
58 
60  const boost::posix_time::ptime &time_read,
61  const std::string &echo_back,
62  const std::string &status,
63  std::istream &stream,
64  ScanData &scan)
65  {
66  if (status == "00")
67  {
68  return false;
69  }
70  if (status != "99")
71  {
72  if (cb_)
73  cb_(time_read, echo_back, status, scan);
74  std::cout << echo_back << " errored with " << status << std::endl;
75  return false;
76  }
77  std::string stamp;
78  if (!std::getline(stream, stamp))
79  {
80  std::cerr << "Failed to get timestamp" << std::endl;
81  return false;
82  }
83  const uint8_t checksum = stamp.back();
84  stamp.pop_back(); // remove checksum
85  if (stamp.size() < 4)
86  {
87  std::cerr << "Wrong timestamp format" << std::endl;
88  return false;
89  }
90 
91  auto dec = Decoder<4>(stamp);
92  auto it = dec.begin();
93  scan.timestamp_ = *it;
94  if ((dec.getChecksum() & 0x3F) + 0x30 != checksum)
95  {
96  std::cerr << "Checksum mismatch" << std::endl;
97  return false;
98  }
99  return true;
100  }
102  {
103  cb_ = cb;
104  }
105 };
106 
108 {
109 public:
110  std::string getCommandCode() const
111  {
112  return std::string("MD");
113  }
115  const boost::posix_time::ptime &time_read,
116  const std::string &echo_back,
117  const std::string &status,
118  std::istream &stream) override
119  {
120  ScanData scan;
121  if (!readTimestamp(time_read, echo_back, status, stream, scan))
122  return;
123  scan.ranges_.reserve(512);
124 
125  std::string line;
126  scip2::DecoderRemain remain;
127  while (std::getline(stream, line))
128  {
129  if (line.size() == 0)
130  break;
131 
132  const uint8_t checksum = line.back();
133  line.pop_back(); // remove checksum
134  if (line.size() < 3)
135  {
136  std::cerr << "Wrong stream format" << std::endl;
137  return;
138  }
139  auto dec = Decoder<3>(line, remain);
140  auto it = dec.begin();
141  for (; it != dec.end(); ++it)
142  {
143  scan.ranges_.push_back(*it);
144  }
145  remain = it.getRemain();
146  if ((dec.getChecksum() & 0x3F) + 0x30 != checksum)
147  {
148  std::cerr << "Checksum mismatch; scan dropped" << std::endl
149  << line << std::endl;
150  return;
151  }
152  }
153  if (cb_)
154  cb_(time_read, echo_back, status, scan);
155  }
156 };
157 
159 {
160 public:
161  std::string getCommandCode() const
162  {
163  return std::string("ME");
164  }
166  const boost::posix_time::ptime &time_read,
167  const std::string &echo_back,
168  const std::string &status,
169  std::istream &stream) override
170  {
171  ScanData scan;
172  if (!readTimestamp(time_read, echo_back, status, stream, scan))
173  return;
174  scan.ranges_.reserve(512);
175  scan.intensities_.reserve(512);
176 
177  std::string line;
178  scip2::DecoderRemain remain;
179  while (std::getline(stream, line))
180  {
181  if (line.size() == 0)
182  break;
183 
184  const uint8_t checksum = line.back();
185  line.pop_back(); // remove checksum
186  if (line.size() < 3)
187  {
188  std::cerr << "Wrong stream format" << std::endl;
189  return;
190  }
191  auto dec = Decoder<6>(line, remain);
192  auto it = dec.begin();
193  for (; it != dec.end(); ++it)
194  {
195  scan.ranges_.push_back((*it) >> 18);
196  scan.intensities_.push_back((*it) & 0x3FFFF);
197  }
198  remain = it.getRemain();
199  if ((dec.getChecksum() & 0x3F) + 0x30 != checksum)
200  {
201  std::cerr << "Checksum mismatch; scan dropped" << std::endl
202  << line << std::endl;
203  return;
204  }
205  }
206  if (cb_)
207  cb_(time_read, echo_back, status, scan);
208  }
209 };
210 
211 } // namespace scip2
212 
213 #endif // SCIP2_RESPONSE_STREAM_H
std::vector< int32_t > ranges_
Definition: stream.h:35
void registerCallback(Callback cb)
Definition: stream.h:101
bool readTimestamp(const boost::posix_time::ptime &time_read, const std::string &echo_back, const std::string &status, std::istream &stream, ScanData &scan)
Definition: stream.h:59
uint32_t timestamp_
Definition: stream.h:34
Iterator end()
Definition: decode.h:119
std::vector< int32_t > intensities_
Definition: stream.h:36
std::string getCommandCode() const
Definition: stream.h:161
void operator()(const boost::posix_time::ptime &time_read, const std::string &echo_back, const std::string &status, std::istream &stream) override
Definition: stream.h:114
std::string getCommandCode() const
Definition: stream.h:110
Iterator begin()
Definition: decode.h:115
boost::function< void(const boost::posix_time::ptime &, const std::string &, const std::string &, const ScanData &)> Callback
Definition: stream.h:46
void operator()(const boost::posix_time::ptime &time_read, const std::string &echo_back, const std::string &status, std::istream &stream) override
Definition: stream.h:165


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Thu Jun 6 2019 19:55:58