SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SENSSimulated.h
Go to the documentation of this file.
1 #ifndef SENS_SIMULATED_H
2 #define SENS_SIMULATED_H
3 
4 #include <functional>
5 #include <memory>
6 #include <chrono>
7 #include <condition_variable>
8 
9 #include <Utils.h>
10 #include <SENSSimClock.h>
11 #include <SENSCamera.h>
12 #include <SENSGps.h>
13 #include <SENSOrientation.h>
14 
15 class SENSSimulator;
16 
17 //-----------------------------------------------------------------------------
18 /*! SENSSimulatedBase
19 This ia pure virtual base class to control SENSSimulated implementations via a common interface in SENSSimulator
20  */
22 {
23 public:
24  virtual ~SENSSimulatedBase() {}
25  //!indicates if simulation thread is running
26  virtual bool isThreadRunning() const = 0;
27 
28  virtual bool getErrorMsg(std::string& msg) = 0;
29 };
30 
31 //-----------------------------------------------------------------------------
32 /*! SENSSimulated
33 Implements the sensor simulation backend. SENSSimulated runs a thread (see feedSensor()) that calls feedSensorData() interface
34 function which feeds the next data value to a sensor (e.g. SENSGps). The current simulation time is retrieved from
35 the SENSSimulator clock and depending on this the next data value is selected and fed a the respective time.
36 This class contains common functionality for SENSSimulated implementations.
37  */
38 template<typename T>
40 {
41 public:
42  virtual ~SENSSimulated() {}
43 
44 protected:
45  using StartSimCB = std::function<void(void)>;
46  using SensorSimStoppedCB = std::function<void(void)>;
47 
48  //!Transfer callbacks to SENSSimulator during construction. The SENSSimulator is informed by
49  //!these if the SENSSimulated state changes (start, stop). We use callbacks to avoid circular dependencies.
50  SENSSimulated(const std::string name,
51  StartSimCB startSimCB,
52  SensorSimStoppedCB sensorSimStoppedCB,
53  std::vector<std::pair<SENSTimePt, T>>&& data,
54  const SENSSimClock& clock);
55 
56  //!start the sensor simulation thread
57  void startSim();
58  //!stop the sensor simulation thread
59  void stopSim();
60 
61  //!get error msg (valid if function returns true)
62  bool getErrorMsg(std::string& msg) override;
63 
64  bool isThreadRunning() const override { return _threadIsRunning; }
65 
66  //!feed new sensor data to sensor
67  virtual void feedSensorData(const int counter) = 0;
68  //!prepare things that may take some time for the next writing of sensor data
69  //!(e.g. for video reading we can already read the frame and do decoding and maybe it also blocks for some reasons..)
70  virtual void prepareSensorData(const int counter){};
71  //!special treatment when there is latency (e.g. when feeding camera frames from cv::videocapture updating the frame pos may take ages)
72  virtual void onLatencyProblem(int& counter) {}
73 
74 private:
75  //!thread run routine to feed sensor with data.
76  void feedSensor();
77 
78 protected:
79  std::vector<std::pair<SENSTimePt, T>> _data;
80 
81  //inform simulator that sensor was stopped
84 
85  std::thread _thread;
86  std::condition_variable _condVar;
87  std::mutex _mutex;
88  bool _stop = false;
89 
91 
92  bool _threadIsRunning = false;
93 
95 
96  std::string _name;
97 
98  std::mutex _msgMutex;
99  std::string _errorMsg;
100 };
101 
102 //-----------------------------------------------------------------------------
103 /*! SENSSimulatedGps
104 SENSSimulated implementation for GPS sensor simulation. Implements the SENSGps interface
105  to make it a full gps sensor and the SENSSimulated interface for the sensor simulation backend.
106  */
107 class SENSSimulatedGps : public SENSGps
108  , public SENSSimulated<SENSGps::Location>
109 {
110  friend class SENSSimulator;
111 
112 public:
114 
115  bool start() override;
116  void stop() override;
117 
118 private:
119  //only SENSSimulator can instantiate
120  SENSSimulatedGps(StartSimCB startSimCB,
121  SensorSimStoppedCB sensorSimStoppedCB,
122  std::vector<std::pair<SENSTimePt, SENSGps::Location>>&& data,
123  const SENSSimClock& clock);
124 
125  void feedSensorData(const int counter) override;
126 };
127 
128 //-----------------------------------------------------------------------------
129 /*! SENSSimulatedOrientation
130 SENSSimulated implementation for Orientation sensor simulation. Implements the SENSOrientation interface
131  to make it a full orientation sensor and the SENSSimulated interface for the sensor simulation backend.
132  */
134  , public SENSSimulated<SENSOrientation::Quat>
135 {
136  friend class SENSSimulator;
137 
138 public:
140 
141  bool start() override;
142  void stop() override;
143 
144 private:
146  SensorSimStoppedCB sensorSimStoppedCB,
147  std::vector<std::pair<SENSTimePt, SENSOrientation::Quat>>&& data,
148  const SENSSimClock& clock);
149 
150  void feedSensorData(const int counter) override;
151 };
152 
153 //-----------------------------------------------------------------------------
154 /*! SENSSimulatedCamera
155 SENSSimulated implementation for Camera sensor simulation. Implements the SENSBaseCamera interface
156 to make it a full camera sensor and the SENSSimulated interface for the sensor simulation backend.
157  */
159  , public SENSSimulated<int>
160 {
161  friend class SENSSimulator;
162 
163 public:
165 
166  const SENSCameraConfig& start(std::string deviceId,
167  const SENSCameraStreamConfig& streamConfig,
168  bool provideIntrinsics = true) override;
169 
170  void stop() override;
171 
172  const SENSCaptureProps& captureProperties() override;
173 
174 private:
175  SENSSimulatedCamera(StartSimCB startSimCB,
176  SensorSimStoppedCB sensorSimStoppedCB,
177  std::vector<std::pair<SENSTimePt, int>>&& data,
178  std::string videoFileName,
179  SENSCameraConfig cameraConfig,
180  const SENSSimClock& clock);
181 
182  void feedSensorData(const int counter) override;
183  void prepareSensorData(const int counter) override;
184  //!when feeding camera frames from cv::videocapture updating the frame pos may take ages. So we skip one more frame and update the next frame counter
185  void onLatencyProblem(int& counter) override;
186 
187  std::string _videoFileName;
188  cv::VideoCapture _cap;
189 
190  cv::Mat _preparedFrame;
192  cv::Mat _frame;
193  std::mutex _frameMutex;
194 };
195 
196 #endif
std::chrono::high_resolution_clock::time_point SENSTimePt
Definition: SENS.h:57
typedef void(SL_STDCALL *cbOnImGuiBuild)(SLScene *s
Callback function typedef for ImGui build function.
Implementation of common functionality and members.
Definition: SENSCamera.h:197
virtual ~SENSSimulatedBase()
Definition: SENSSimulated.h:24
virtual bool getErrorMsg(std::string &msg)=0
virtual bool isThreadRunning() const =0
indicates if simulation thread is running
std::mutex _frameMutex
void onLatencyProblem(int &counter) override
when feeding camera frames from cv::videocapture updating the frame pos may take ages....
std::string _videoFileName
const SENSCaptureProps & captureProperties() override
Get SENSCaptureProps which contains necessary information about all available camera devices and thei...
void prepareSensorData(const int counter) override
const SENSCameraConfig & start(std::string deviceId, const SENSCameraStreamConfig &streamConfig, bool provideIntrinsics=true) override
SENSSimulatedCamera(StartSimCB startSimCB, SensorSimStoppedCB sensorSimStoppedCB, std::vector< std::pair< SENSTimePt, int >> &&data, std::string videoFileName, SENSCameraConfig cameraConfig, const SENSSimClock &clock)
void stop() override
Stop a started camera device.
void feedSensorData(const int counter) override
feed new sensor data to sensor
cv::VideoCapture _cap
bool start() override
void feedSensorData(const int counter) override
feed new sensor data to sensor
void stop() override
SENSSimulatedGps(StartSimCB startSimCB, SensorSimStoppedCB sensorSimStoppedCB, std::vector< std::pair< SENSTimePt, SENSGps::Location >> &&data, const SENSSimClock &clock)
SENSSimulated(const std::string name, StartSimCB startSimCB, SensorSimStoppedCB sensorSimStoppedCB, std::vector< std::pair< SENSTimePt, T >> &&data, const SENSSimClock &clock)
void stopSim()
stop the sensor simulation thread
bool isThreadRunning() const override
indicates if simulation thread is running
Definition: SENSSimulated.h:64
std::thread _thread
Definition: SENSSimulated.h:85
bool getErrorMsg(std::string &msg) override
get error msg (valid if function returns true)
StartSimCB _startSimCB
Definition: SENSSimulated.h:83
std::function< void(void)> SensorSimStoppedCB
Definition: SENSSimulated.h:46
std::vector< std::pair< SENSTimePt, T > > _data
Definition: SENSSimulated.h:79
std::mutex _msgMutex
Definition: SENSSimulated.h:98
virtual void feedSensorData(const int counter)=0
feed new sensor data to sensor
void feedSensor()
thread run routine to feed sensor with data.
virtual ~SENSSimulated()
Definition: SENSSimulated.h:42
virtual void prepareSensorData(const int counter)
Definition: SENSSimulated.h:70
void startSim()
start the sensor simulation thread
const SENSSimClock & _clock
Definition: SENSSimulated.h:94
bool _threadIsRunning
Definition: SENSSimulated.h:92
std::mutex _mutex
Definition: SENSSimulated.h:87
std::condition_variable _condVar
Definition: SENSSimulated.h:86
std::string _name
Definition: SENSSimulated.h:96
SensorSimStoppedCB _sensorSimStoppedCB
Definition: SENSSimulated.h:82
std::function< void(void)> StartSimCB
Definition: SENSSimulated.h:45
std::string _errorMsg
Definition: SENSSimulated.h:99
SENSTimePt _commonSimStartTimePt
Definition: SENSSimulated.h:90
virtual void onLatencyProblem(int &counter)
special treatment when there is latency (e.g. when feeding camera frames from cv::videocapture updati...
Definition: SENSSimulated.h:72
void feedSensorData(const int counter) override
feed new sensor data to sensor
SENSSimulatedOrientation(StartSimCB startSimCB, SensorSimStoppedCB sensorSimStoppedCB, std::vector< std::pair< SENSTimePt, SENSOrientation::Quat >> &&data, const SENSSimClock &clock)