Regilo
A simple C++ library for controlling the Neato XV robot and the Hokuyo scanner.
scancontroller.hpp
1 /*
2  * Regilo
3  * Copyright (C) 2015-2016 Branislav HolĂ˝ <branoholy@gmail.com>
4  *
5  * This file is part of Regilo.
6  *
7  * Regilo is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Regilo is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Regilo. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef REGILO_SCANCONTROLLER_HPP
23 #define REGILO_SCANCONTROLLER_HPP
24 
25 #include "controller.hpp"
26 #include "scandata.hpp"
27 #include "utils.hpp"
28 
29 namespace regilo {
30 
34 class IScanController : public virtual IController
35 {
36 public:
40  virtual ~IScanController() = default;
41 
47  virtual ScanData getScan(bool fromDevice = true) = 0;
48 };
49 
53 template<typename ProtocolController>
54 class ScanController : public virtual IScanController, public ProtocolController
55 {
56 protected:
57  std::size_t lastScanId = 0;
58 
63  virtual std::string getScanCommand() const = 0;
64 
71  virtual bool parseScanData(std::istream& in, ScanData& data) = 0;
72 
73 public:
74  using ProtocolController::ProtocolController;
75 
79  virtual ~ScanController() = default;
80 
81  virtual ScanData getScan(bool fromDevice = true) override final;
82 };
83 
84 template<typename ProtocolController>
85 ScanData ScanController<ProtocolController>::getScan(bool fromDevice)
86 {
87  ScanData data;
88 
89  if(fromDevice)
90  {
91  ProtocolController::template sendCommand<>(getScanCommand());
92  data.time = epoch<std::chrono::milliseconds>().count();
93 
94  parseScanData(this->deviceOutput, data);
95  }
96  else
97  {
98  std::istringstream response(this->log->readCommand(getScanCommand()));
99  if(std::shared_ptr<const ITimedLog> timedLog = std::dynamic_pointer_cast<const ITimedLog>(this->getLog()))
100  {
101  data.time = timedLog->getLastCommandTimeAs<std::chrono::milliseconds>().count();
102  }
103 
104  parseScanData(response, data);
105  }
106  if(!data.empty()) data.scanId = lastScanId++;
107 
108  return data;
109 }
110 
111 }
112 
113 #endif // REGILO_SCANCONTROLLER_HPP
Definition: controller.hpp:35
The IScanController interface is used for all controller classes that implement scanning functionalit...
Definition: scancontroller.hpp:34
long time
The scan time (milliseconds since epoch).
Definition: scandata.hpp:39
virtual ScanData getScan(bool fromDevice=true)=0
Get a scan from the device.
std::size_t scanId
The scan id (starting from zero).
Definition: scandata.hpp:37
The IController interface is used for all controller classes.
Definition: controller.hpp:42
virtual std::shared_ptr< ILog > getLog()=0
Get the current Log.
The ScanController class implements parsing of scanned laser data.
Definition: scancontroller.hpp:54
The ScanData class is used to store laser data.
Definition: scandata.hpp:34
virtual ~IScanController()=default
Default destructor.