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 #include <scip2/logger.h>
29 
30 namespace scip2
31 {
32 class ScanData
33 {
34 public:
35  uint32_t timestamp_;
36  std::vector<int32_t> ranges_;
37  std::vector<int32_t> intensities_;
38 };
39 
40 class ResponseStream : public Response
41 {
42 public:
43  using Callback = boost::function<void(
44  const boost::posix_time::ptime&,
45  const std::string&,
46  const std::string&,
47  const ScanData&)>;
48 
49 protected:
51 
52 public:
53  virtual std::string getCommandCode() const = 0;
54  virtual void operator()(
55  const boost::posix_time::ptime&,
56  const std::string&,
57  const std::string&,
58  std::istream&) = 0;
59 
61  const boost::posix_time::ptime& time_read,
62  const std::string& echo_back,
63  const std::string& status,
64  std::istream& stream,
65  ScanData& scan)
66  {
67  if (status != "99")
68  {
69  return false;
70  }
71  std::string stamp;
72  if (!std::getline(stream, stamp))
73  {
74  logger::error() << "Failed to get timestamp" << std::endl;
75  return false;
76  }
77  const uint8_t checksum = stamp.back();
78  stamp.pop_back(); // remove checksum
79  if (stamp.size() < 4)
80  {
81  logger::error() << "Wrong timestamp format" << std::endl;
82  return false;
83  }
84 
85  auto dec = Decoder<4>(stamp);
86  auto it = dec.begin();
87  scan.timestamp_ = *it;
88  if ((dec.getChecksum() & 0x3F) + 0x30 != checksum)
89  {
90  logger::error() << "Checksum mismatch" << std::endl;
91  return false;
92  }
93  return true;
94  }
96  {
97  cb_ = cb;
98  }
99 };
100 
102 {
103 public:
104  std::string getCommandCode() const
105  {
106  return std::string("MD");
107  }
109  const boost::posix_time::ptime& time_read,
110  const std::string& echo_back,
111  const std::string& status,
112  std::istream& stream) override
113  {
114  ScanData scan;
115  if (!readTimestamp(time_read, echo_back, status, stream, scan))
116  {
117  if (cb_)
118  cb_(time_read, echo_back, status, scan);
119  return;
120  }
121  scan.ranges_.reserve(512);
122 
123  std::string line;
124  scip2::DecoderRemain remain;
125  while (std::getline(stream, line))
126  {
127  if (line.size() == 0)
128  break;
129 
130  const uint8_t checksum = line.back();
131  line.pop_back(); // remove checksum
132  if (line.size() < 3)
133  {
134  logger::error() << "Wrong stream format" << std::endl;
135  return;
136  }
137  auto dec = Decoder<3>(line, remain);
138  auto it = dec.begin();
139  for (; it != dec.end(); ++it)
140  {
141  scan.ranges_.push_back(*it);
142  }
143  remain = it.getRemain();
144  if ((dec.getChecksum() & 0x3F) + 0x30 != checksum)
145  {
146  logger::error() << "Checksum mismatch; scan dropped" << std::endl
147  << line << std::endl;
148  return;
149  }
150  }
151  if (cb_)
152  cb_(time_read, echo_back, status, scan);
153  }
154 };
155 
157 {
158 public:
159  std::string getCommandCode() const
160  {
161  return std::string("ME");
162  }
164  const boost::posix_time::ptime& time_read,
165  const std::string& echo_back,
166  const std::string& status,
167  std::istream& stream) override
168  {
169  ScanData scan;
170  if (!readTimestamp(time_read, echo_back, status, stream, scan))
171  {
172  if (cb_)
173  cb_(time_read, echo_back, status, scan);
174  return;
175  }
176  scan.ranges_.reserve(512);
177  scan.intensities_.reserve(512);
178 
179  std::string line;
180  scip2::DecoderRemain remain;
181  while (std::getline(stream, line))
182  {
183  if (line.size() == 0)
184  break;
185 
186  const uint8_t checksum = line.back();
187  line.pop_back(); // remove checksum
188  if (line.size() < 3)
189  {
190  logger::error() << "Wrong stream format" << std::endl;
191  return;
192  }
193  auto dec = Decoder<6>(line, remain);
194  auto it = dec.begin();
195  for (; it != dec.end(); ++it)
196  {
197  scan.ranges_.push_back((*it) >> 18);
198  scan.intensities_.push_back((*it) & 0x3FFFF);
199  }
200  remain = it.getRemain();
201  if ((dec.getChecksum() & 0x3F) + 0x30 != checksum)
202  {
203  logger::error() << "Checksum mismatch; scan dropped" << std::endl
204  << line << std::endl;
205  return;
206  }
207  }
208  if (cb_)
209  cb_(time_read, echo_back, status, scan);
210  }
211 };
212 
213 } // namespace scip2
214 
215 #endif // SCIP2_RESPONSE_STREAM_H
std::vector< int32_t > ranges_
Definition: stream.h:36
void registerCallback(Callback cb)
Definition: stream.h:95
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:60
uint32_t timestamp_
Definition: stream.h:35
boost::function< void(const boost::posix_time::ptime &, const std::string &, const std::string &, const ScanData &)> Callback
Definition: stream.h:47
Iterator end()
Definition: decode.h:119
std::vector< int32_t > intensities_
Definition: stream.h:37
std::string getCommandCode() const
Definition: stream.h:159
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:108
std::string getCommandCode() const
Definition: stream.h:104
Iterator begin()
Definition: decode.h:115
std::ostream & error()
Definition: logger.cpp:105
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:163


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Tue May 11 2021 02:14:05