Sensor Scol plugin
Multi platform sensors for handled devices
SSensor.cpp
1#include "SSensor.h"
2#include <set>
3
4#ifdef ANDROID
5#include "SASensor.h"
6#elif _WIN32
7#include "SWSensor.h"
8#else
9#include "SQSensor.h"
10#endif
11
12#include "tools/SO3Util.h"
13
14extern int SENSOR_DATA_CB;
15
16#if defined(OSX) || defined(APPLE_IOS)
17//Tickcount function
18#include <boost/chrono/chrono.hpp>
19
20int64_t GetTickCountNano()
21{
22 return boost::chrono::duration_cast<boost::chrono::nanoseconds>(boost::chrono::steady_clock::now().time_since_epoch()).count();
23}
24#elif defined(_WIN32)
25int64_t GetTickCountNano()
26{
27 int msec = GetTickCount();
28 return (int64_t)msec * (int64_t)1e-6;
29}
30#else
31int64_t GetTickCountNano()
32{
33 struct timespec now;
34 clock_gettime(CLOCK_MONOTONIC, &now);
35 return (int64_t)now.tv_sec * 1000000000LL + (int64_t)now.tv_nsec;
36}
37
38#endif
39
40/* * * * * * * * * * * * * STATIC SENSOR MANAGER * * * * * * * * * * * * */
41
42SSensorManager* SSensorManager::mSingletonInstance;
43
44SSensorManager* SSensorManager::GetInstance()
45{
46 if (!mSingletonInstance)
47 {
48#ifdef ANDROID
49 mSingletonInstance = new SASensorManager();
50#elif _WIN32
51 mSingletonInstance = new SWSensorManager();
52#else
53 mSingletonInstance = new SQSensorManager();
54#endif
55 }
56 return mSingletonInstance;
57}
58
59void SSensorManager::Kill(){
60 SAFE_DELETE(mSingletonInstance);
61}
62
63
64/* * * * * * * * * * * * * Axis Sensor Callback * * * * * * * * * * * * */
65SSensorCb::SSensorCb(SSensorType type): mType(type)
66{
67}
68
69SSensorCb::~SSensorCb()
70{
71
72}
73
74SSensorType SSensorCb::GetType()
75{
76 return mType;
77}
78
79
80/* * * * * * * * * * * * * * * * Sensor * * * * * * * * * * * * * * * * */
81SSensor::SSensor()
82{
83 mType = SSENSOR_TYPE_UNKNOWN;
84 mEnabled = false;
85 mPaused = false;
86 mLastTimeStamp = 0;
87}
88
89SSensor::SSensor(SSensorType type)
90{
91 mType = type;
92 mEnabled = false;
93 mPaused = false;
94 mLastTimeStamp = 0;
95}
96
97SSensor::~SSensor()
98{
99
100}
101
102SSensorType SSensor::GetType()
103{
104 return mType;
105}
106
107bool SSensor::IsEnabled()
108{
109 return mEnabled;
110}
111
112void SSensor::SetEnabled(bool enabled)
113{
114 if (mEnabled != enabled)
115 {
116 mEnabled = enabled;
117 mLastTimeStamp = 0;
118 }
119}
120
121bool SSensor::IsPaused()
122{
123 return mPaused;
124}
125
126void SSensor::SetPaused(bool paused)
127{
128 if (mPaused != paused)
129 {
130 mPaused = paused;
131 mLastTimeStamp = 0;
132 }
133}
134
135SSensorCb* SSensor::AddCallBack()
136{
137 SSensorCb* sensorCb = new SSensorCb(mType);
138 mAxisCallbacks.push_back(sensorCb);
139
140 return sensorCb;
141}
142
143void SSensor::RemoveCallBack(SSensorCb* sensorCb)
144{
145 if (sensorCb == 0)
146 return;
147
148 mAxisCallbacks.remove(sensorCb);
149 SAFE_DELETE(sensorCb);
150}
151
152double SSensor::GetElapsedTime(int64_t timeStamp)
153{
154 double delta = 0.0;
155 if (mLastTimeStamp != 0)
156 {
157 delta = (timeStamp - mLastTimeStamp) / 1e9;
158 }
159 mLastTimeStamp = timeStamp;
160 return delta;
161}
162
163void SSensor::CallSensorCb(SSensorData data)
164{
165 for (SSensorCb* sensorCb : mAxisCallbacks)
166 {
167 SSensorData* ndata = new SSensorData;
168 ndata->fval = data.fval;
169 ndata->vec3 = Vector3d(data.vec3);
170 ndata->delta = data.delta;
171
172 OBJpostEvent(SENSOR_DATA_CB, SCOL_PTR sensorCb, SCOL_PTR ndata);
173 }
174}
175
176
177/* * * * * * * * * * * * * * * SENSOR MANAGER * * * * * * * * * * * * * * */
178
179SSensorManager::SSensorManager()
180{
181 mOrientationTracker = new OrientationTracker();
182}
183
184SSensorManager::~SSensorManager()
185{
186 SAFE_DELETE(mOrientationTracker);
187}
188
189bool SSensorManager::IsAvailable(SSensorType sensorType)
190{
191 if (sensorType == SSENSOR_TYPE_ORIENTATION)
192 return IsAvailable(SSENSOR_TYPE_ACCELEROMETER) && IsAvailable(SSENSOR_TYPE_GYROSCOPE);
193 else
194 return (GetSensorByType(sensorType) != 0);
195}
196
197bool SSensorManager::IsEnabled(SSensorType sensorType)
198{
199 if (sensorType == SSENSOR_TYPE_ORIENTATION)
200 {
201 return IsEnabled(SSENSOR_TYPE_ACCELEROMETER) && IsEnabled(SSENSOR_TYPE_GYROSCOPE);
202 }
203 else
204 {
205 SSensor* sensor = GetSensorByType(sensorType);
206 return (sensor)? sensor->IsEnabled() : false;
207 }
208}
209
210SSensor* SSensorManager::GetSensorByType(SSensorType sensorType)
211{
212 for (std::set<SSensor*>::iterator it = mSensorList.begin(); it != mSensorList.end(); ++it)
213 {
214 if ((*it) && (*it)->GetType() == sensorType)
215 {
216 return *it;
217 }
218 }
219 return NULL;
220}
221
222void SSensorManager::PauseSensorManager()
223{
224 //TODO remove update thread
225 for (std::set<SSensor*>::iterator it = mSensorList.begin(); it != mSensorList.end(); ++it)
226 {
227 SSensor* sensor = *it;
228 if (sensor && sensor->IsEnabled())
229 {
230 SetSensorEnable(sensor->GetType(), false);
231 sensor->SetPaused(true);
232 }
233 }
234}
235
236void SSensorManager::ResumeSensorManager()
237{
238 //TODO add update thread again
239 for (std::set<SSensor*>::iterator it = mSensorList.begin(); it != mSensorList.end(); ++it)
240 {
241 SSensor* sensor = *it;
242 if (sensor && sensor->IsPaused())
243 {
244 SetSensorEnable(sensor->GetType(), true);
245 sensor->SetPaused(false);
246 }
247 }
248}
249
250std::set<SSensor*> SSensorManager::GetSensorList()
251{
252 return mSensorList;
253}
254
255OrientationTracker* SSensorManager::GetOrientationTracker()
256{
257 return mOrientationTracker;
258}
259
260Quaternion SSensorManager::GetDeviceOrientation(double timeOffsetInSeconds)
261{
262 if (mOrientationTracker && IsEnabled(SSENSOR_TYPE_ORIENTATION))
263 {
264 boost::shared_lock< boost::shared_mutex > lock(mGlobalMutex);
265 return mOrientationTracker->lastHeadView(timeOffsetInSeconds);
266 }
267 else
268 {
269 // TODO: ERROR NO ORIENTATION EKF
270 return Quaternion::IDENTITY;
271 }
272}
273
274SSensorCb* SSensorManager::AddSensorCallBack(SSensorType sensorType)
275{
276 SSensor* sensor = GetSensorByType(sensorType);
277 if (sensor == 0)
278 return 0;
279
280 return sensor->AddCallBack();
281}
282
283void SSensorManager::RemoveSensorCallBack(SSensorCb* sensorCb)
284{
285 if (sensorCb == 0)
286 return;
287
288 SSensor* sensor = GetSensorByType(sensorCb->GetType());
289 if (sensor == 0)
290 return;
291
292 sensor->RemoveCallBack(sensorCb);
293}