IMUDevice.h
Go to the documentation of this file.
1 /*
2  * IMU.h
3  *
4  * Created on: Mar 16, 2014
5  * Author: Dr.-Ing. David Israel González Aguirre
6  * Mail: david.gonzalez@kit.edu
7  */
8 
9 #pragma once
10 
11 #include "Includes.h"
12 #include "IMUHelpers.h"
13 #include "IMUEvent.h"
14 #include "IMUState.h"
15 
16 #ifdef _IMU_USE_XSENS_DEVICE_
17 
18 #include "Xsens/Xsens.h"
19 #include "Xsens/XsensMTiModule.h"
20 
21 #endif
22 
23 #define _IMU_DEVICE_DEFAUL_CONNECTION_ std::string("/dev/ttyUSB0")
24 #define _IMU_DEVICE_DEFAUL_FREQUENCY_ IMU::CIMUDevice::eSamplingFrequency_120HZ
25 #define _IMU_DEVICE_DEFAUL_CHECK_PERIOD_FACTOR_ 0.5f
26 #define _IMU_DEVICE_DEFAUL_NORMALIZED_THREAD_PRIORITY_ 0.125f
27 
28 namespace IMU
29 {
30  //Forward definition
31  class IIMUEventDispatcher;
32 
33  /*!
34  \class CIMUDevice
35  \ingroup IMU
36  \brief This class contains the the devices module and the thread for read the measurements.
37 
38  CIMUDevice encapsulates the device details for the rest of the library and applications.
39  This also includes a thread which is in charge of generate the IMU events
40  */
41  class CIMUDevice
42  {
43  public:
44 
45  /*!
46  \brief Enum specifying the running thread policy.
47  */
49  {
50  eRealTime = SCHED_FIFO, eRoundRobinPriorityBased = SCHED_RR, eBatch = SCHED_BATCH, eIdle = SCHED_IDLE
51  };
52 
53  /*!
54  \brief Enum specifying the supported sampling frequencies.
55  */
57  {
101  };
102 
104  {
106  };
107 
108  /*!
109  \brief The default constructor.
110  The default constructor sets all member variables to zero, i.e. after construction no valid device nor thread are represented.
111  */
112  CIMUDevice();
113 
114  /*!
115  \brief The destructor.
116  */
117  virtual ~CIMUDevice();
118 
119  uint64_t GetDeviceId() const;
120 
121  inline IMUState GetIMUState() const
122  {
123 
124 #ifdef _IMU_USE_XSENS_DEVICE_
125 
126  return m_XsensMTiFrame.m_IMUState;
127 
128 #endif
129 
130  }
131 
132  bool Connect(const std::string& PortName, const SamplingFrequency Frequency);
133 
134  bool Start(const bool Blocking = true);
135 
136  bool SetThreadRunnigMode(const ThreadPolicyType ThreadPolicy, const float NormalizedPriority);
137 
138  void Stop(const bool Blocking = true);
139 
140  bool SetFusion(const FusionStrategy Strategy, const ushort SamplesPerFusion);
141 
142  bool IsActive() const;
143 
144  bool RegisterEventDispatcher(IIMUEventDispatcher* pIMUEventDispatcher);
145 
146  bool UnregisterEventDispatcher(IIMUEventDispatcher* pIMUEventDispatcher);
147 
148  inline const timeval& GetReferenceTimeStamp() const
149  {
150  return m_ReferenceTimeStamp;
151  }
152 
153  protected:
154 
155  bool LoadCurrentState();
156 
157  bool MeanFuseCurrentState();
159  void MeanFusion();
160 
163  void GaussianFusion();
164 
165  bool IntegrateCurrentState();
166  bool IntegrateWithOutFusion();
167  bool IntegrateWithFusion();
168 
169  private:
170 
171  void UnregisterEventDispatchers();
172 
173  bool InitializeDevice(const std::string& PortName, const SamplingFrequency Frequency);
174  void FinalizeModuleDevice();
175  void ShouldYield();
176  bool DispatchCylcle();
177  void SendEvent(const CIMUEvent& Event);
178  void SetReferenceTimeStamps();
179 
180  static void* ThreadLoop(void* pData);
181 
182  uint64_t m_DeviceId;
183  SamplingFrequency m_SamplingFrequency;
184  int m_PeriodMicroSeconds;
185  FusionStrategy m_FusionStrategy;
186  int m_SamplesPerFusion;
187  int m_CollectedFusionSamples;
188  volatile bool m_IsActive;
189  volatile bool m_IsDispatching;
190  bool m_IsInitialized;
191  pthread_t m_pInternalThreadHandel;
192  pthread_mutex_t m_IsActiveMutex;
193  pthread_mutex_t m_IsDispatchingMutex;
194  pthread_mutex_t m_EventDispatchersMutex;
195  pthread_mutex_t m_DeviceMutex;
196  std::set<IIMUEventDispatcher*> m_IMUEventDispatchers;
197  timeval m_ReferenceTimeStamp;
198  timeval m_LastFrameTimeStamp;
199 
200  IMUState::PhysicalData m_FusedPhysicalData;
201  IMUState m_FusedIMUState;
202  IMUState m_IntegratedIMUState;
203 
204 #ifdef _IMU_USE_XSENS_DEVICE_
205 
206  bool InitializeXsensDevice(const std::string& PortName, const SamplingFrequency Frequency);
207  void FinalizeXsensModuleDevice();
208  void DestroyXsensModuleDevice();
209 
210  Xsens::CXsensMTiModule* m_pXsensMTiModule;
211  Xsens::XsensMTiFrame m_XsensMTiFrame;
212 
213 #endif
214 
215  };
216 }
217 
IMU::CIMUDevice::eSamplingFrequency_24HZ
@ eSamplingFrequency_24HZ
Definition: IMUDevice.h:64
IMU::CIMUDevice::eSamplingFrequency_256HZ
@ eSamplingFrequency_256HZ
Definition: IMUDevice.h:91
IMU::CIMUDevice::CIMUDevice
CIMUDevice()
The default constructor. The default constructor sets all member variables to zero,...
Definition: IMUDevice.cpp:14
IMU::CIMUDevice::eSamplingFrequency_150HZ
@ eSamplingFrequency_150HZ
Definition: IMUDevice.h:84
IMU::CIMUDevice::eSamplingFrequency_192HZ
@ eSamplingFrequency_192HZ
Definition: IMUDevice.h:87
IMU::CIMUDevice::eSamplingFrequency_90HZ
@ eSamplingFrequency_90HZ
Definition: IMUDevice.h:78
IMU::CIMUDevice::GetIMUState
IMUState GetIMUState() const
Definition: IMUDevice.h:121
IMU::CIMUDevice::IntegrateCurrentState
bool IntegrateCurrentState()
Definition: IMUDevice.cpp:404
IMU::CIMUDevice::eSamplingFrequency_30HZ
@ eSamplingFrequency_30HZ
Definition: IMUDevice.h:66
IMU::CIMUDevice::eSamplingFrequency_320HZ
@ eSamplingFrequency_320HZ
Definition: IMUDevice.h:94
IMU::CIMUDevice::SetThreadRunnigMode
bool SetThreadRunnigMode(const ThreadPolicyType ThreadPolicy, const float NormalizedPriority)
Definition: IMUDevice.cpp:563
IMU::Xsens::CXsensMTiModule
Definition: XsensMTiModule.h:925
IMU::CIMUDevice::eSamplingFrequency_20HZ
@ eSamplingFrequency_20HZ
Definition: IMUDevice.h:63
IMU::CIMUDevice::eSamplingFrequency_200HZ
@ eSamplingFrequency_200HZ
Definition: IMUDevice.h:88
XsensMTiModule.h
IMU::CIMUDevice::eSamplingFrequency_50HZ
@ eSamplingFrequency_50HZ
Definition: IMUDevice.h:72
IMU::CIMUDevice::eSamplingFrequency_450HZ
@ eSamplingFrequency_450HZ
Definition: IMUDevice.h:98
IMU::CIMUDevice::eSamplingFrequency_80HZ
@ eSamplingFrequency_80HZ
Definition: IMUDevice.h:77
IMU::CIMUDevice::eSamplingFrequency_180HZ
@ eSamplingFrequency_180HZ
Definition: IMUDevice.h:86
IMU::CIMUDevice::eSamplingFrequency_64HZ
@ eSamplingFrequency_64HZ
Definition: IMUDevice.h:74
IMU::CIMUDevice::eSamplingFrequency_60HZ
@ eSamplingFrequency_60HZ
Definition: IMUDevice.h:73
IMU::CIMUDevice::ThreadPolicyType
ThreadPolicyType
Enum specifying the running thread policy.
Definition: IMUDevice.h:48
IMU::CIMUDevice::eBatch
@ eBatch
Definition: IMUDevice.h:50
IMU::CIMUDevice::eSamplingFrequency_12HZ
@ eSamplingFrequency_12HZ
Definition: IMUDevice.h:59
IMU::CIMUDevice::eSamplingFrequency_120HZ
@ eSamplingFrequency_120HZ
Definition: IMUDevice.h:81
IMU::CIMUDevice::eSamplingFrequency_10HZ
@ eSamplingFrequency_10HZ
Definition: IMUDevice.h:58
IMU::CIMUDevice::MeanFuseCurrentState
bool MeanFuseCurrentState()
Definition: IMUDevice.cpp:329
IMU::CIMUDevice::eSamplingFrequency_18HZ
@ eSamplingFrequency_18HZ
Definition: IMUDevice.h:62
IMU::CIMUDevice::FusionStrategy
FusionStrategy
Definition: IMUDevice.h:103
IMU::CIMUDevice::RegisterEventDispatcher
bool RegisterEventDispatcher(IIMUEventDispatcher *pIMUEventDispatcher)
Definition: IMUDevice.cpp:118
Includes.h
IMU::CIMUDevice::eRoundRobinPriorityBased
@ eRoundRobinPriorityBased
Definition: IMUDevice.h:50
IMU::CIMUDevice::GaussianFuseCurrentState
bool GaussianFuseCurrentState()
Definition: IMUDevice.cpp:366
IMU::CIMUDevice::eSamplingFrequency_15HZ
@ eSamplingFrequency_15HZ
Definition: IMUDevice.h:60
IMU::CIMUDevice::IncorporateCurrentStateMeanFusion
void IncorporateCurrentStateMeanFusion()
Definition: IMUDevice.cpp:342
IMU::CIMUEvent
Definition: IMUEvent.h:17
IMU::CIMUDevice::eSamplingFrequency_32HZ
@ eSamplingFrequency_32HZ
Definition: IMUDevice.h:67
IMU::CIMUDevice::IntegrateWithOutFusion
bool IntegrateWithOutFusion()
Definition: IMUDevice.cpp:416
IMU::CIMUDevice::eSamplingFrequency_40HZ
@ eSamplingFrequency_40HZ
Definition: IMUDevice.h:69
IMU::CIMUDevice::eSamplingFrequency_400HZ
@ eSamplingFrequency_400HZ
Definition: IMUDevice.h:97
IMU::IIMUEventDispatcher
Definition: IIMUEventDispatcher.h:18
IMU::CIMUDevice
This class contains the the devices module and the thread for read the measurements.
Definition: IMUDevice.h:41
IMU::CIMUDevice::eSamplingFrequency_100HZ
@ eSamplingFrequency_100HZ
Definition: IMUDevice.h:80
IMU::IMUState
Definition: IMUState.h:15
IMU::Xsens::XsensMTiFrame
Definition: Xsens.h:19
IMU::CIMUDevice::eSamplingFrequency_240HZ
@ eSamplingFrequency_240HZ
Definition: IMUDevice.h:90
IMU::CIMUDevice::eSamplingFrequency_144HZ
@ eSamplingFrequency_144HZ
Definition: IMUDevice.h:83
IMU::CIMUDevice::Connect
bool Connect(const std::string &PortName, const SamplingFrequency Frequency)
Definition: IMUDevice.cpp:39
IMU::CIMUDevice::eRealTime
@ eRealTime
Definition: IMUDevice.h:50
IMU::CIMUDevice::Stop
void Stop(const bool Blocking=true)
Definition: IMUDevice.cpp:77
IMU::CIMUDevice::eSamplingFrequency_16HZ
@ eSamplingFrequency_16HZ
Definition: IMUDevice.h:61
IMU::Xsens::XsensMTiFrame::m_IMUState
IMUState m_IMUState
Definition: Xsens.h:29
IMU::CIMUDevice::eIdle
@ eIdle
Definition: IMUDevice.h:50
IMU::CIMUDevice::IncorporateCurrentStateGaussianFusion
void IncorporateCurrentStateGaussianFusion()
Definition: IMUDevice.cpp:379
IMU::CIMUDevice::eSamplingFrequency_72HZ
@ eSamplingFrequency_72HZ
Definition: IMUDevice.h:75
IMU::CIMUDevice::eSamplingFrequency_128HZ
@ eSamplingFrequency_128HZ
Definition: IMUDevice.h:82
IMU::CIMUDevice::eMeanFusion
@ eMeanFusion
Definition: IMUDevice.h:105
IMU::CIMUDevice::eSamplingFrequency_360HZ
@ eSamplingFrequency_360HZ
Definition: IMUDevice.h:95
IMU::CIMUDevice::GetDeviceId
uint64_t GetDeviceId() const
Definition: IMUDevice.cpp:34
IMU
Definition: IIMUEventDispatcher.cpp:12
IMUHelpers.h
IMU::CIMUDevice::eSamplingFrequency_160HZ
@ eSamplingFrequency_160HZ
Definition: IMUDevice.h:85
IMU::CIMUDevice::eNoFusion
@ eNoFusion
Definition: IMUDevice.h:105
IMU::CIMUDevice::MeanFusion
void MeanFusion()
Definition: IMUDevice.cpp:350
IMU::CIMUDevice::eSamplingFrequency_384HZ
@ eSamplingFrequency_384HZ
Definition: IMUDevice.h:96
Xsens.h
IMU::CIMUDevice::~CIMUDevice
virtual ~CIMUDevice()
The destructor.
Definition: IMUDevice.cpp:29
IMU::CIMUDevice::UnregisterEventDispatcher
bool UnregisterEventDispatcher(IIMUEventDispatcher *pIMUEventDispatcher)
Definition: IMUDevice.cpp:139
IMUEvent.h
IMU::CIMUDevice::SamplingFrequency
SamplingFrequency
Enum specifying the supported sampling frequencies.
Definition: IMUDevice.h:56
IMU::CIMUDevice::LoadCurrentState
bool LoadCurrentState()
Definition: IMUDevice.cpp:268
IMU::CIMUDevice::IntegrateWithFusion
bool IntegrateWithFusion()
Definition: IMUDevice.cpp:421
IMU::CIMUDevice::eSamplingFrequency_75HZ
@ eSamplingFrequency_75HZ
Definition: IMUDevice.h:76
IMU::CIMUDevice::eSamplingFrequency_25HZ
@ eSamplingFrequency_25HZ
Definition: IMUDevice.h:65
IMU::CIMUDevice::eSamplingFrequency_225HZ
@ eSamplingFrequency_225HZ
Definition: IMUDevice.h:89
IMUState.h
IMU::CIMUDevice::eSamplingFrequency_96HZ
@ eSamplingFrequency_96HZ
Definition: IMUDevice.h:79
IMU::CIMUDevice::eSamplingFrequency_45HZ
@ eSamplingFrequency_45HZ
Definition: IMUDevice.h:70
IMU::CIMUDevice::eSamplingFrequency_512HZ
@ eSamplingFrequency_512HZ
Definition: IMUDevice.h:100
IMU::CIMUDevice::eGaussianFusion
@ eGaussianFusion
Definition: IMUDevice.h:105
IMU::CIMUDevice::eSamplingFrequency_48HZ
@ eSamplingFrequency_48HZ
Definition: IMUDevice.h:71
IMU::IMUState::PhysicalData
Definition: IMUState.h:37
IMU::CIMUDevice::Start
bool Start(const bool Blocking=true)
Definition: IMUDevice.cpp:57
IMU::CIMUDevice::eSamplingFrequency_36HZ
@ eSamplingFrequency_36HZ
Definition: IMUDevice.h:68
IMU::CIMUDevice::SetFusion
bool SetFusion(const FusionStrategy Strategy, const ushort SamplesPerFusion)
Definition: IMUDevice.cpp:93
IMU::CIMUDevice::GetReferenceTimeStamp
const timeval & GetReferenceTimeStamp() const
Definition: IMUDevice.h:148
IMU::CIMUDevice::eSamplingFrequency_288HZ
@ eSamplingFrequency_288HZ
Definition: IMUDevice.h:92
IMU::CIMUDevice::GaussianFusion
void GaussianFusion()
Definition: IMUDevice.cpp:388
IMU::CIMUDevice::eSamplingFrequency_300HZ
@ eSamplingFrequency_300HZ
Definition: IMUDevice.h:93
IMU::CIMUDevice::eSamplingFrequency_480HZ
@ eSamplingFrequency_480HZ
Definition: IMUDevice.h:99
IMU::CIMUDevice::IsActive
bool IsActive() const
Definition: IMUDevice.cpp:113