SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SENSSimulator.h
Go to the documentation of this file.
1 #ifndef SENS_SIMULATOR_H
2 #define SENS_SIMULATOR_H
3 
4 #include <functional>
5 #include <memory>
6 #include <chrono>
7 
8 #include <Utils.h>
9 #include <SENSSimulated.h>
10 #include <SENSSimClock.h>
11 
12 /*!SENSSimulator
13  This class is owner of SENSSimulated sensor instances. It loads special sensor data from
14  input simulation directory transferred during construction. Depending on if the directory content is valid,
15  a SENSSimulated implementation is instantiated and initialized with loaded sensor data.
16  You can get a sensor pointer to a simulated sensor by calling the getter get..SensorPtr() method and
17  use it like the real sensor, as it implements the original sensor interface.
18  The SENSSimulator is maintainer of the current simulation time. Simulated sensor values are selected
19  depending on this time. The idea is to provide sensor data exactly as it was recorded.
20  ATTENTION: Simulation time starts, as soon as one simulated sensor was started and is resetted when all simulated
21  sensors are stopped.
22  */
24 {
25 public:
26  //!ctor, transfer directory containing sensor data that was recorded with SENSRecorder
27  SENSSimulator(const std::string& simDirName);
29 
30  //!get sensor pointer of simulated gps sensor and use it like a normal sensor (valid if not null)
31  SENSSimulatedGps* getGpsSensorPtr() { return getActiveSensor<SENSSimulatedGps>(); }
32  //!get sensor pointer of simulated orientation sensor and use it like a normal sensor (valid if not null)
33  SENSSimulatedOrientation* getOrientationSensorPtr() { return getActiveSensor<SENSSimulatedOrientation>(); }
34  //!get sensor pointer of simulated camera sensor and use it like a normal sensor (valid if not null)
35  SENSSimulatedCamera* getCameraSensorPtr() { return getActiveSensor<SENSSimulatedCamera>(); }
36  //!indicates if simulator is currently running
37  bool isRunning() const { return _running; }
38 
39  bool getSimulatorErrors(std::vector<std::string>& errorMsgs);
40 
41  //!pause simulation time
42  void pause();
43  bool isPaused();
44  //!resume simulation time
45  void resume();
46  //!reset simulation time
47  SENSTimePt now();
48  //!get passed simulation time
50 
51 private:
52  template<typename T>
54  {
55  for (int i = 0; i < _activeSensors.size(); ++i)
56  {
57  SENSSimulatedBase* base = _activeSensors[i].get();
58  T* derived = dynamic_cast<T*>(base);
59  if (derived)
60  return derived;
61  }
62 
63  return nullptr;
64  }
65  //!callback called from SENSSimulated when the simulated sensor was started
66  void onStart();
67  //!callback called from SENSSimulated when the simulated sensor was stopped
68  void onSensorSimStopped();
69 
70  //!load data from file and instantiate sensor if valid
71  void loadGpsData(const std::string& dirName, std::vector<std::pair<SENSTimePt, SENSGps::Location>>& data);
72  //!load data from file and instantiate sensor if valid
73  void loadOrientationData(const std::string& dirName, std::vector<std::pair<SENSTimePt, SENSOrientation::Quat>>& data);
74  //!load data from file and instantiate sensor if valid
75  void loadCameraData(const std::string& dirName, std::vector<std::pair<SENSTimePt, int>>& data, std::string& videoFileName, SENSCameraConfig& cameraConfig);
76 
77  //!list of currently activated sensors. Which sensors are activated depends on the content of simulation directory
78  std::vector<std::unique_ptr<SENSSimulatedBase>> _activeSensors;
79 
80  std::unique_ptr<SENSSimClock> _clock;
81  //!flags if simulator is currently running
82  std::atomic_bool _running{false};
83 };
84 
85 #endif
std::chrono::microseconds SENSMicroseconds
Definition: SENS.h:58
std::chrono::high_resolution_clock::time_point SENSTimePt
Definition: SENS.h:57
std::atomic_bool _running
flags if simulator is currently running
Definition: SENSSimulator.h:82
SENSSimulatedCamera * getCameraSensorPtr()
get sensor pointer of simulated camera sensor and use it like a normal sensor (valid if not null)
Definition: SENSSimulator.h:35
void loadOrientationData(const std::string &dirName, std::vector< std::pair< SENSTimePt, SENSOrientation::Quat >> &data)
load data from file and instantiate sensor if valid
bool isRunning() const
indicates if simulator is currently running
Definition: SENSSimulator.h:37
bool getSimulatorErrors(std::vector< std::string > &errorMsgs)
std::unique_ptr< SENSSimClock > _clock
Definition: SENSSimulator.h:80
SENSMicroseconds passedTime()
get passed simulation time
SENSSimulatedGps * getGpsSensorPtr()
get sensor pointer of simulated gps sensor and use it like a normal sensor (valid if not null)
Definition: SENSSimulator.h:31
std::vector< std::unique_ptr< SENSSimulatedBase > > _activeSensors
list of currently activated sensors. Which sensors are activated depends on the content of simulation...
Definition: SENSSimulator.h:78
void loadGpsData(const std::string &dirName, std::vector< std::pair< SENSTimePt, SENSGps::Location >> &data)
load data from file and instantiate sensor if valid
T * getActiveSensor()
Definition: SENSSimulator.h:53
SENSSimulatedOrientation * getOrientationSensorPtr()
get sensor pointer of simulated orientation sensor and use it like a normal sensor (valid if not null...
Definition: SENSSimulator.h:33
void pause()
pause simulation time
SENSSimulator(const std::string &simDirName)
ctor, transfer directory containing sensor data that was recorded with SENSRecorder
void resume()
resume simulation time
void onStart()
callback called from SENSSimulated when the simulated sensor was started
void loadCameraData(const std::string &dirName, std::vector< std::pair< SENSTimePt, int >> &data, std::string &videoFileName, SENSCameraConfig &cameraConfig)
load data from file and instantiate sensor if valid
void onSensorSimStopped()
callback called from SENSSimulated when the simulated sensor was stopped
SENSTimePt now()
reset simulation time