SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SENSSimulated< T > Class Template Referenceabstract

#include <SENSSimulated.h>

Inheritance diagram for SENSSimulated< T >:
[legend]

Public Member Functions

virtual ~SENSSimulated ()
 
- Public Member Functions inherited from SENSSimulatedBase
virtual ~SENSSimulatedBase ()
 

Protected Types

using StartSimCB = std::function< void(void)>
 
using SensorSimStoppedCB = std::function< void(void)>
 

Protected Member Functions

 SENSSimulated (const std::string name, StartSimCB startSimCB, SensorSimStoppedCB sensorSimStoppedCB, std::vector< std::pair< SENSTimePt, T >> &&data, const SENSSimClock &clock)
 
void startSim ()
 start the sensor simulation thread More...
 
void stopSim ()
 stop the sensor simulation thread More...
 
bool getErrorMsg (std::string &msg) override
 get error msg (valid if function returns true) More...
 
bool isThreadRunning () const override
 indicates if simulation thread is running More...
 
virtual void feedSensorData (const int counter)=0
 feed new sensor data to sensor More...
 
virtual void prepareSensorData (const int counter)
 
virtual void onLatencyProblem (int &counter)
 special treatment when there is latency (e.g. when feeding camera frames from cv::videocapture updating the frame pos may take ages) More...
 

Protected Attributes

std::vector< std::pair< SENSTimePt, T > > _data
 
SensorSimStoppedCB _sensorSimStoppedCB
 
StartSimCB _startSimCB
 
std::thread _thread
 
std::condition_variable _condVar
 
std::mutex _mutex
 
bool _stop = false
 
SENSTimePt _commonSimStartTimePt
 
bool _threadIsRunning = false
 
const SENSSimClock_clock
 
std::string _name
 
std::mutex _msgMutex
 
std::string _errorMsg
 

Private Member Functions

void feedSensor ()
 thread run routine to feed sensor with data. More...
 

Detailed Description

template<typename T>
class SENSSimulated< T >

SENSSimulated Implements the sensor simulation backend. SENSSimulated runs a thread (see feedSensor()) that calls feedSensorData() interface function which feeds the next data value to a sensor (e.g. SENSGps). The current simulation time is retrieved from the SENSSimulator clock and depending on this the next data value is selected and fed a the respective time. This class contains common functionality for SENSSimulated implementations.

Definition at line 39 of file SENSSimulated.h.

Member Typedef Documentation

◆ SensorSimStoppedCB

template<typename T >
using SENSSimulated< T >::SensorSimStoppedCB = std::function<void(void)>
protected

Definition at line 46 of file SENSSimulated.h.

◆ StartSimCB

template<typename T >
using SENSSimulated< T >::StartSimCB = std::function<void(void)>
protected

Definition at line 45 of file SENSSimulated.h.

Constructor & Destructor Documentation

◆ ~SENSSimulated()

template<typename T >
virtual SENSSimulated< T >::~SENSSimulated ( )
inlinevirtual

Definition at line 42 of file SENSSimulated.h.

42 {}

◆ SENSSimulated()

template<typename T >
SENSSimulated< T >::SENSSimulated ( const std::string  name,
StartSimCB  startSimCB,
SensorSimStoppedCB  sensorSimStoppedCB,
std::vector< std::pair< SENSTimePt, T >> &&  data,
const SENSSimClock clock 
)
protected

Transfer callbacks to SENSSimulator during construction. The SENSSimulator is informed by these if the SENSSimulated state changes (start, stop). We use callbacks to avoid circular dependencies.

Definition at line 6 of file SENSSimulated.cpp.

11  : _name(name),
12  _startSimCB(startSimCB),
13  _sensorSimStoppedCB(sensorSimStoppedCB),
14  _data(data),
15  _clock(clock)
16 {
17 }
StartSimCB _startSimCB
Definition: SENSSimulated.h:83
std::vector< std::pair< SENSTimePt, T > > _data
Definition: SENSSimulated.h:79
const SENSSimClock & _clock
Definition: SENSSimulated.h:94
std::string _name
Definition: SENSSimulated.h:96
SensorSimStoppedCB _sensorSimStoppedCB
Definition: SENSSimulated.h:82

Member Function Documentation

◆ feedSensor()

template<typename T >
void SENSSimulated< T >::feedSensor
private

thread run routine to feed sensor with data.

Definition at line 60 of file SENSSimulated.cpp.

61 {
62  _threadIsRunning = true;
63  //index of current data set to feed
64  int counter = 0;
65 
66  SENSTimePt simTime = _clock.now();
67  //bring counter close to startTimePt (maybe we skip some values to synchronize simulated sensors)
68  while (counter < _data.size() && _data[counter].first < simTime)
69  counter++;
70 
71  if (counter >= _data.size())
72  {
73  //end of simulation
74  _threadIsRunning = false;
76  return;
77  }
78 
79  while (true)
80  {
81  //estimate current sim time
82  SENSTimePt simTime = _clock.now();
83 
84  //feed latest data
85  int oldCounter = counter;
86  if (counter < _data.size() && _data[counter].first <= simTime)
87  {
88  //SENS_DEBUG("feed sensor index %d with latency: %d us, SimT: %d, ValueT: %d", counter, std::chrono::duration_cast<SENSMicroseconds>(_data[counter].first - simTime).count(), std::chrono::time_point_cast<SENSMicroseconds>(simTime).time_since_epoch().count(), std::chrono::time_point_cast<SENSMicroseconds>(_data[counter].first).time_since_epoch().count());
89  SENS_DEBUG("feed %s sensor index %d with latency: %d us", _name.c_str(), counter, std::chrono::duration_cast<SENSMicroseconds>(_data[counter].first - simTime).count());
90  feedSensorData(counter);
91  if (_stop)
92  break;
93 
94  //update the counter
95  simTime = _clock.now();
96  while (counter < _data.size() && _data[counter].first < simTime)
97  counter++;
98  }
99 
100  if (counter - oldCounter > 1)
101  {
102  //for feeding camera frames from cv::videocapture we need a special treatment in this case
103  //(THIS FUNCTION MAY CHANGE THE COUNTER)
104  onLatencyProblem(counter);
105  //report an error
106  int numberOfSkippedData = counter - oldCounter;
107 
108  std::stringstream ss;
109  ss << "Data feeding is too slow. Skipped " << numberOfSkippedData << " data sets!";
110  SENS_WARN("SENSSimulated feedSensor: %s", ss.str().c_str());
111  {
112  std::lock_guard<std::mutex> lock(_msgMutex);
113  _errorMsg = ss.str();
114  }
115  }
116 
117  //end of simulation
118  if (counter >= _data.size())
119  break;
120 
121  //prepare things that may take some time (e.g. for video reading we can already read the frame and do decoding and maybe it also blocks for some reasons..)
122  prepareSensorData(counter);
123 
124  //WAITING DOES NOT WORK VERY EXACTLY ON WINDOWS (it does wake too late)
125 #ifdef WAIT
126  simTime = _clock.now();
127  //simTime should now be smaller than valueTime because valueTime is in the simulation future
128  SENSMicroseconds waitTimeUs = std::chrono::duration_cast<SENSMicroseconds>(_data[counter].first - simTime);
129  //We reduce the wait time because thread sleep is not very exact (best is not to wait at all)
130  SENSMicroseconds reducedWaitTimeUs((long)(0.01 * (double)waitTimeUs.count()));
131 
132  //HighResTimer t;
133  std::unique_lock<std::mutex> lock(_mutex);
134  _condVar.wait_for(lock, reducedWaitTimeUs, [&]
135  { return _stop == true; });
136  //SENS_DEBUG("wait time %d us", waitTimeUs.count());
137  //SENS_DEBUG("woke after %d us", t.elapsedTimeInMicroSec());
138 #endif
139  if (_stop)
140  break;
141  }
142 
143  _threadIsRunning = false;
145 }
#define SENS_DEBUG(...)
Definition: SENS.h:60
std::chrono::microseconds SENSMicroseconds
Definition: SENS.h:58
#define SENS_WARN(...)
Definition: SENS.h:64
std::chrono::high_resolution_clock::time_point SENSTimePt
Definition: SENS.h:57
SENSTimePt now() const
get current simulation time (const function which should be thread save)
Definition: SENSSimClock.h:71
std::mutex _msgMutex
Definition: SENSSimulated.h:98
virtual void feedSensorData(const int counter)=0
feed new sensor data to sensor
virtual void prepareSensorData(const int counter)
Definition: SENSSimulated.h:70
bool _threadIsRunning
Definition: SENSSimulated.h:92
std::mutex _mutex
Definition: SENSSimulated.h:87
std::condition_variable _condVar
Definition: SENSSimulated.h:86
std::string _errorMsg
Definition: SENSSimulated.h:99
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

◆ feedSensorData()

template<typename T >
virtual void SENSSimulated< T >::feedSensorData ( const int  counter)
protectedpure virtual

feed new sensor data to sensor

Implemented in SENSSimulatedCamera, SENSSimulatedOrientation, and SENSSimulatedGps.

◆ getErrorMsg()

template<typename T >
bool SENSSimulated< T >::getErrorMsg ( std::string &  msg)
overrideprotectedvirtual

get error msg (valid if function returns true)

Implements SENSSimulatedBase.

Definition at line 47 of file SENSSimulated.cpp.

48 {
49  std::lock_guard<std::mutex> lock(_msgMutex);
50  if (_errorMsg.empty())
51  return false;
52  else
53  {
54  msg = _errorMsg;
55  return true;
56  }
57 }

◆ isThreadRunning()

template<typename T >
bool SENSSimulated< T >::isThreadRunning ( ) const
inlineoverrideprotectedvirtual

indicates if simulation thread is running

Implements SENSSimulatedBase.

Definition at line 64 of file SENSSimulated.h.

64 { return _threadIsRunning; }

◆ onLatencyProblem()

template<typename T >
virtual void SENSSimulated< T >::onLatencyProblem ( int &  counter)
inlineprotectedvirtual

special treatment when there is latency (e.g. when feeding camera frames from cv::videocapture updating the frame pos may take ages)

Reimplemented in SENSSimulatedCamera.

Definition at line 72 of file SENSSimulated.h.

72 {}

◆ prepareSensorData()

template<typename T >
virtual void SENSSimulated< T >::prepareSensorData ( const int  counter)
inlineprotectedvirtual

prepare things that may take some time for the next writing of sensor data (e.g. for video reading we can already read the frame and do decoding and maybe it also blocks for some reasons..)

Reimplemented in SENSSimulatedCamera.

Definition at line 70 of file SENSSimulated.h.

70 {};

◆ startSim()

template<typename T >
void SENSSimulated< T >::startSim
protected

start the sensor simulation thread

Definition at line 20 of file SENSSimulated.cpp.

21 {
22  //inform SENSSimulator about start
23  _startSimCB();
24  //stop the local simulation thread if running
25  stopSim();
26  _errorMsg.clear();
27  //start the simulation thread
28  _thread = std::thread(&SENSSimulated::feedSensor, this);
29 }
void stopSim()
stop the sensor simulation thread
std::thread _thread
Definition: SENSSimulated.h:85
void feedSensor()
thread run routine to feed sensor with data.

◆ stopSim()

template<typename T >
void SENSSimulated< T >::stopSim
protected

stop the sensor simulation thread

Definition at line 32 of file SENSSimulated.cpp.

33 {
34  std::unique_lock<std::mutex> lock(_mutex);
35  _stop = true;
36  lock.unlock();
37  _condVar.notify_one();
38 
39  if (_thread.joinable())
40  _thread.join();
41 
42  lock.lock();
43  _stop = false;
44 }

Member Data Documentation

◆ _clock

template<typename T >
const SENSSimClock& SENSSimulated< T >::_clock
protected

Definition at line 94 of file SENSSimulated.h.

◆ _commonSimStartTimePt

template<typename T >
SENSTimePt SENSSimulated< T >::_commonSimStartTimePt
protected

Definition at line 90 of file SENSSimulated.h.

◆ _condVar

template<typename T >
std::condition_variable SENSSimulated< T >::_condVar
protected

Definition at line 86 of file SENSSimulated.h.

◆ _data

template<typename T >
std::vector<std::pair<SENSTimePt, T> > SENSSimulated< T >::_data
protected

Definition at line 79 of file SENSSimulated.h.

◆ _errorMsg

template<typename T >
std::string SENSSimulated< T >::_errorMsg
protected

Definition at line 99 of file SENSSimulated.h.

◆ _msgMutex

template<typename T >
std::mutex SENSSimulated< T >::_msgMutex
protected

Definition at line 98 of file SENSSimulated.h.

◆ _mutex

template<typename T >
std::mutex SENSSimulated< T >::_mutex
protected

Definition at line 87 of file SENSSimulated.h.

◆ _name

template<typename T >
std::string SENSSimulated< T >::_name
protected

Definition at line 96 of file SENSSimulated.h.

◆ _sensorSimStoppedCB

template<typename T >
SensorSimStoppedCB SENSSimulated< T >::_sensorSimStoppedCB
protected

Definition at line 82 of file SENSSimulated.h.

◆ _startSimCB

template<typename T >
StartSimCB SENSSimulated< T >::_startSimCB
protected

Definition at line 83 of file SENSSimulated.h.

◆ _stop

template<typename T >
bool SENSSimulated< T >::_stop = false
protected

Definition at line 88 of file SENSSimulated.h.

◆ _thread

template<typename T >
std::thread SENSSimulated< T >::_thread
protected

Definition at line 85 of file SENSSimulated.h.

◆ _threadIsRunning

template<typename T >
bool SENSSimulated< T >::_threadIsRunning = false
protected

Definition at line 92 of file SENSSimulated.h.


The documentation for this class was generated from the following files: