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 "IMUEvent.h"
12 #include "IMUHelpers.h"
13 #include "IMUState.h"
14 #include "Includes.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  \brief Enum specifying the running thread policy.
46  */
48  {
49  eRealTime = SCHED_FIFO,
51  eBatch = SCHED_BATCH,
52  eIdle = SCHED_IDLE
53  };
54 
55  /*!
56  \brief Enum specifying the supported sampling frequencies.
57  */
59  {
103  };
104 
106  {
110  };
111 
112  /*!
113  \brief The default constructor.
114  The default constructor sets all member variables to zero, i.e. after construction no valid device nor thread are represented.
115  */
116  CIMUDevice();
117 
118  /*!
119  \brief The destructor.
120  */
121  virtual ~CIMUDevice();
122 
123  uint64_t GetDeviceId() const;
124 
125  inline IMUState
126  GetIMUState() const
127  {
128 
129 #ifdef _IMU_USE_XSENS_DEVICE_
130 
131  return m_XsensMTiFrame.m_IMUState;
132 
133 #endif
134  }
135 
136  bool Connect(const std::string& PortName, const SamplingFrequency Frequency);
137 
138  bool Start(const bool Blocking = true);
139 
140  bool SetThreadRunnigMode(const ThreadPolicyType ThreadPolicy,
141  const float NormalizedPriority);
142 
143  void Stop(const bool Blocking = true);
144 
145  bool SetFusion(const FusionStrategy Strategy, const ushort SamplesPerFusion);
146 
147  bool IsActive() const;
148 
149  bool RegisterEventDispatcher(IIMUEventDispatcher* pIMUEventDispatcher);
150 
151  bool UnregisterEventDispatcher(IIMUEventDispatcher* pIMUEventDispatcher);
152 
153  inline const timeval&
155  {
156  return m_ReferenceTimeStamp;
157  }
158 
159  protected:
160  bool LoadCurrentState();
161 
162  bool MeanFuseCurrentState();
164  void MeanFusion();
165 
168  void GaussianFusion();
169 
170  bool IntegrateCurrentState();
171  bool IntegrateWithOutFusion();
172  bool IntegrateWithFusion();
173 
174  private:
175  void UnregisterEventDispatchers();
176 
177  bool InitializeDevice(const std::string& PortName, const SamplingFrequency Frequency);
178  void FinalizeModuleDevice();
179  void ShouldYield();
180  bool DispatchCylcle();
181  void SendEvent(const CIMUEvent& Event);
182  void SetReferenceTimeStamps();
183 
184  static void* ThreadLoop(void* pData);
185 
186  uint64_t m_DeviceId;
187  SamplingFrequency m_SamplingFrequency;
188  int m_PeriodMicroSeconds;
189  FusionStrategy m_FusionStrategy;
190  int m_SamplesPerFusion;
191  int m_CollectedFusionSamples;
192  volatile bool m_IsActive;
193  volatile bool m_IsDispatching;
194  bool m_IsInitialized;
195  pthread_t m_pInternalThreadHandel;
196  pthread_mutex_t m_IsActiveMutex;
197  pthread_mutex_t m_IsDispatchingMutex;
198  pthread_mutex_t m_EventDispatchersMutex;
199  pthread_mutex_t m_DeviceMutex;
200  std::set<IIMUEventDispatcher*> m_IMUEventDispatchers;
201  timeval m_ReferenceTimeStamp;
202  timeval m_LastFrameTimeStamp;
203 
204  IMUState::PhysicalData m_FusedPhysicalData;
205  IMUState m_FusedIMUState;
206  IMUState m_IntegratedIMUState;
207 
208 #ifdef _IMU_USE_XSENS_DEVICE_
209 
210  bool InitializeXsensDevice(const std::string& PortName, const SamplingFrequency Frequency);
211  void FinalizeXsensModuleDevice();
212  void DestroyXsensModuleDevice();
213 
214  Xsens::CXsensMTiModule* m_pXsensMTiModule;
215  Xsens::XsensMTiFrame m_XsensMTiFrame;
216 
217 #endif
218  };
219 } // namespace IMU
IMU::CIMUDevice::eSamplingFrequency_24HZ
@ eSamplingFrequency_24HZ
Definition: IMUDevice.h:66
IMU::CIMUDevice::eSamplingFrequency_256HZ
@ eSamplingFrequency_256HZ
Definition: IMUDevice.h:93
IMU::CIMUDevice::CIMUDevice
CIMUDevice()
The default constructor. The default constructor sets all member variables to zero,...
Definition: IMUDevice.cpp:15
IMU::CIMUDevice::eSamplingFrequency_150HZ
@ eSamplingFrequency_150HZ
Definition: IMUDevice.h:86
IMU::CIMUDevice::eSamplingFrequency_192HZ
@ eSamplingFrequency_192HZ
Definition: IMUDevice.h:89
IMU::CIMUDevice::eSamplingFrequency_90HZ
@ eSamplingFrequency_90HZ
Definition: IMUDevice.h:80
IMU::CIMUDevice::GetIMUState
IMUState GetIMUState() const
Definition: IMUDevice.h:126
IMU::CIMUDevice::IntegrateCurrentState
bool IntegrateCurrentState()
Definition: IMUDevice.cpp:521
IMU::CIMUDevice::eSamplingFrequency_30HZ
@ eSamplingFrequency_30HZ
Definition: IMUDevice.h:68
IMU::CIMUDevice::eSamplingFrequency_320HZ
@ eSamplingFrequency_320HZ
Definition: IMUDevice.h:96
IMU::CIMUDevice::SetThreadRunnigMode
bool SetThreadRunnigMode(const ThreadPolicyType ThreadPolicy, const float NormalizedPriority)
Definition: IMUDevice.cpp:707
IMU::Xsens::CXsensMTiModule
Definition: XsensMTiModule.h:927
IMU::CIMUDevice::eSamplingFrequency_20HZ
@ eSamplingFrequency_20HZ
Definition: IMUDevice.h:65
IMU::CIMUDevice::eSamplingFrequency_200HZ
@ eSamplingFrequency_200HZ
Definition: IMUDevice.h:90
XsensMTiModule.h
IMU::CIMUDevice::eSamplingFrequency_50HZ
@ eSamplingFrequency_50HZ
Definition: IMUDevice.h:74
IMU::CIMUDevice::eSamplingFrequency_450HZ
@ eSamplingFrequency_450HZ
Definition: IMUDevice.h:100
IMU::CIMUDevice::eSamplingFrequency_80HZ
@ eSamplingFrequency_80HZ
Definition: IMUDevice.h:79
IMU::CIMUDevice::eSamplingFrequency_180HZ
@ eSamplingFrequency_180HZ
Definition: IMUDevice.h:88
IMU::CIMUDevice::eSamplingFrequency_64HZ
@ eSamplingFrequency_64HZ
Definition: IMUDevice.h:76
IMU::CIMUDevice::eSamplingFrequency_60HZ
@ eSamplingFrequency_60HZ
Definition: IMUDevice.h:75
IMU::CIMUDevice::ThreadPolicyType
ThreadPolicyType
Enum specifying the running thread policy.
Definition: IMUDevice.h:47
IMU::CIMUDevice::eBatch
@ eBatch
Definition: IMUDevice.h:51
IMU::CIMUDevice::eSamplingFrequency_12HZ
@ eSamplingFrequency_12HZ
Definition: IMUDevice.h:61
IMU::CIMUDevice::eSamplingFrequency_120HZ
@ eSamplingFrequency_120HZ
Definition: IMUDevice.h:83
IMU::CIMUDevice::eSamplingFrequency_10HZ
@ eSamplingFrequency_10HZ
Definition: IMUDevice.h:60
IMU::CIMUDevice::MeanFuseCurrentState
bool MeanFuseCurrentState()
Definition: IMUDevice.cpp:428
IMU::CIMUDevice::eSamplingFrequency_18HZ
@ eSamplingFrequency_18HZ
Definition: IMUDevice.h:64
IMU::CIMUDevice::FusionStrategy
FusionStrategy
Definition: IMUDevice.h:105
IMU::CIMUDevice::RegisterEventDispatcher
bool RegisterEventDispatcher(IIMUEventDispatcher *pIMUEventDispatcher)
Definition: IMUDevice.cpp:144
Includes.h
IMU::CIMUDevice::eRoundRobinPriorityBased
@ eRoundRobinPriorityBased
Definition: IMUDevice.h:50
IMU::CIMUDevice::GaussianFuseCurrentState
bool GaussianFuseCurrentState()
Definition: IMUDevice.cpp:474
IMU::CIMUDevice::eSamplingFrequency_15HZ
@ eSamplingFrequency_15HZ
Definition: IMUDevice.h:62
IMU::CIMUDevice::IncorporateCurrentStateMeanFusion
void IncorporateCurrentStateMeanFusion()
Definition: IMUDevice.cpp:442
IMU::CIMUEvent
Definition: IMUEvent.h:18
IMU::CIMUDevice::eSamplingFrequency_32HZ
@ eSamplingFrequency_32HZ
Definition: IMUDevice.h:69
IMU::CIMUDevice::IntegrateWithOutFusion
bool IntegrateWithOutFusion()
Definition: IMUDevice.cpp:534
IMU::CIMUDevice::eSamplingFrequency_40HZ
@ eSamplingFrequency_40HZ
Definition: IMUDevice.h:71
IMU::CIMUDevice::eSamplingFrequency_400HZ
@ eSamplingFrequency_400HZ
Definition: IMUDevice.h:99
IMU::IIMUEventDispatcher
Definition: IIMUEventDispatcher.h:19
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:82
IMU::IMUState
Definition: IMUState.h:15
IMU::Xsens::XsensMTiFrame
Definition: Xsens.h:19
IMU::CIMUDevice::eSamplingFrequency_240HZ
@ eSamplingFrequency_240HZ
Definition: IMUDevice.h:92
IMU::CIMUDevice::eSamplingFrequency_144HZ
@ eSamplingFrequency_144HZ
Definition: IMUDevice.h:85
IMU::CIMUDevice::Connect
bool Connect(const std::string &PortName, const SamplingFrequency Frequency)
Definition: IMUDevice.cpp:55
IMU::CIMUDevice::eRealTime
@ eRealTime
Definition: IMUDevice.h:49
IMU::CIMUDevice::Stop
void Stop(const bool Blocking=true)
Definition: IMUDevice.cpp:98
IMU::CIMUDevice::eSamplingFrequency_16HZ
@ eSamplingFrequency_16HZ
Definition: IMUDevice.h:63
IMU::Xsens::XsensMTiFrame::m_IMUState
IMUState m_IMUState
Definition: Xsens.h:28
IMU::CIMUDevice::eIdle
@ eIdle
Definition: IMUDevice.h:52
IMU::CIMUDevice::IncorporateCurrentStateGaussianFusion
void IncorporateCurrentStateGaussianFusion()
Definition: IMUDevice.cpp:488
IMU::CIMUDevice::eSamplingFrequency_72HZ
@ eSamplingFrequency_72HZ
Definition: IMUDevice.h:77
IMU::CIMUDevice::eSamplingFrequency_128HZ
@ eSamplingFrequency_128HZ
Definition: IMUDevice.h:84
IMU::CIMUDevice::eMeanFusion
@ eMeanFusion
Definition: IMUDevice.h:108
IMU::CIMUDevice::eSamplingFrequency_360HZ
@ eSamplingFrequency_360HZ
Definition: IMUDevice.h:97
IMU::CIMUDevice::GetDeviceId
uint64_t GetDeviceId() const
Definition: IMUDevice.cpp:49
IMU
Definition: IIMUEventDispatcher.cpp:13
IMUHelpers.h
IMU::CIMUDevice::eSamplingFrequency_160HZ
@ eSamplingFrequency_160HZ
Definition: IMUDevice.h:87
IMU::CIMUDevice::eNoFusion
@ eNoFusion
Definition: IMUDevice.h:107
IMU::CIMUDevice::MeanFusion
void MeanFusion()
Definition: IMUDevice.cpp:454
IMU::CIMUDevice::eSamplingFrequency_384HZ
@ eSamplingFrequency_384HZ
Definition: IMUDevice.h:98
Xsens.h
IMU::CIMUDevice::~CIMUDevice
virtual ~CIMUDevice()
The destructor.
Definition: IMUDevice.cpp:43
IMU::CIMUDevice::UnregisterEventDispatcher
bool UnregisterEventDispatcher(IIMUEventDispatcher *pIMUEventDispatcher)
Definition: IMUDevice.cpp:167
IMUEvent.h
IMU::CIMUDevice::SamplingFrequency
SamplingFrequency
Enum specifying the supported sampling frequencies.
Definition: IMUDevice.h:58
IMU::CIMUDevice::LoadCurrentState
bool LoadCurrentState()
Definition: IMUDevice.cpp:319
IMU::CIMUDevice::IntegrateWithFusion
bool IntegrateWithFusion()
Definition: IMUDevice.cpp:540
IMU::CIMUDevice::eSamplingFrequency_75HZ
@ eSamplingFrequency_75HZ
Definition: IMUDevice.h:78
IMU::CIMUDevice::eSamplingFrequency_25HZ
@ eSamplingFrequency_25HZ
Definition: IMUDevice.h:67
IMU::CIMUDevice::eSamplingFrequency_225HZ
@ eSamplingFrequency_225HZ
Definition: IMUDevice.h:91
IMUState.h
IMU::CIMUDevice::eSamplingFrequency_96HZ
@ eSamplingFrequency_96HZ
Definition: IMUDevice.h:81
IMU::CIMUDevice::eSamplingFrequency_45HZ
@ eSamplingFrequency_45HZ
Definition: IMUDevice.h:72
IMU::CIMUDevice::eSamplingFrequency_512HZ
@ eSamplingFrequency_512HZ
Definition: IMUDevice.h:102
IMU::CIMUDevice::eGaussianFusion
@ eGaussianFusion
Definition: IMUDevice.h:109
IMU::CIMUDevice::eSamplingFrequency_48HZ
@ eSamplingFrequency_48HZ
Definition: IMUDevice.h:73
IMU::IMUState::PhysicalData
Definition: IMUState.h:39
IMU::CIMUDevice::Start
bool Start(const bool Blocking=true)
Definition: IMUDevice.cpp:76
IMU::CIMUDevice::eSamplingFrequency_36HZ
@ eSamplingFrequency_36HZ
Definition: IMUDevice.h:70
IMU::CIMUDevice::SetFusion
bool SetFusion(const FusionStrategy Strategy, const ushort SamplesPerFusion)
Definition: IMUDevice.cpp:115
IMU::CIMUDevice::GetReferenceTimeStamp
const timeval & GetReferenceTimeStamp() const
Definition: IMUDevice.h:154
IMU::CIMUDevice::eSamplingFrequency_288HZ
@ eSamplingFrequency_288HZ
Definition: IMUDevice.h:94
IMU::CIMUDevice::GaussianFusion
void GaussianFusion()
Definition: IMUDevice.cpp:501
IMU::CIMUDevice::eSamplingFrequency_300HZ
@ eSamplingFrequency_300HZ
Definition: IMUDevice.h:95
IMU::CIMUDevice::eSamplingFrequency_480HZ
@ eSamplingFrequency_480HZ
Definition: IMUDevice.h:101
IMU::CIMUDevice::IsActive
bool IsActive() const
Definition: IMUDevice.cpp:138