|
1 /* |
|
2 * Name : Accelerometer.cpp |
|
3 * Description : Accelerometer helper class |
|
4 * Project : This file is part of OpenMAR, an Open Mobile Augmented Reality browser |
|
5 * Website : http://OpenMAR.org |
|
6 * |
|
7 * Copyright (c) 2010 David Caabeiro |
|
8 * |
|
9 * All rights reserved. This program and the accompanying materials are made available |
|
10 * under the terms of the Eclipse Public License v1.0 which accompanies this |
|
11 * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html |
|
12 * |
|
13 */ |
|
14 |
|
15 #include "Accelerometer.h" |
|
16 |
|
17 #include <SensrvAccelerometerSensor.h> |
|
18 |
|
19 #include <SensrvChannel.h> |
|
20 #include <SensrvChannelFinder.h> |
|
21 #include <SensrvChannelInfo.h> |
|
22 |
|
23 CAccelerometer* CAccelerometer::NewL() |
|
24 { |
|
25 CAccelerometer* self = new(ELeave) CAccelerometer; |
|
26 CleanupStack::PushL(self); |
|
27 self->ConstructL(); |
|
28 CleanupStack::Pop(self); |
|
29 |
|
30 return self; |
|
31 } |
|
32 |
|
33 CAccelerometer::~CAccelerometer() |
|
34 { |
|
35 iSensorChannel->CloseChannel(); |
|
36 } |
|
37 |
|
38 CAccelerometer::CAccelerometer() |
|
39 {} |
|
40 |
|
41 void CAccelerometer::ConstructL() |
|
42 { |
|
43 CSensrvChannelFinder* sensorChannelFinder = CSensrvChannelFinder::NewLC(); |
|
44 |
|
45 RSensrvChannelInfoList channelInfoList; |
|
46 CleanupClosePushL(channelInfoList); |
|
47 |
|
48 TSensrvChannelInfo mySearchConditions; |
|
49 mySearchConditions.iChannelType = KSensrvChannelTypeIdAccelerometerXYZAxisData; |
|
50 |
|
51 sensorChannelFinder->FindChannelsL(channelInfoList, mySearchConditions); |
|
52 |
|
53 TSensrvChannelInfo channelInfo; |
|
54 |
|
55 if (channelInfoList.Count() > 0) |
|
56 channelInfo = channelInfoList[0]; |
|
57 else |
|
58 User::Leave(KErrNotFound); |
|
59 |
|
60 CleanupStack::PopAndDestroy(&channelInfoList); |
|
61 CleanupStack::PopAndDestroy(sensorChannelFinder); |
|
62 |
|
63 iSensorChannel = CSensrvChannel::NewL(channelInfo); |
|
64 iSensorChannel->OpenChannelL(); |
|
65 } |
|
66 |
|
67 void CAccelerometer::StartL() |
|
68 { |
|
69 iSensorChannel->StartDataListeningL(this, 1, 8, 0); |
|
70 } |
|
71 |
|
72 void CAccelerometer::Stop() |
|
73 { |
|
74 iSensorChannel->StopDataListening(); |
|
75 } |
|
76 |
|
77 void CAccelerometer::DataReceived(CSensrvChannel& aChannel, TInt aCount, TInt aDataLost) |
|
78 { |
|
79 if (aChannel.GetChannelInfo().iChannelType == KSensrvChannelTypeIdAccelerometerXYZAxisData) |
|
80 { |
|
81 TPckgBuf<TSensrvAccelerometerAxisData> dataBuffer; |
|
82 TSensrvAccelerometerAxisData data; |
|
83 |
|
84 for (TInt i = 0; i < aCount; ++i) |
|
85 { |
|
86 aChannel.GetData(dataBuffer); |
|
87 |
|
88 data = dataBuffer(); |
|
89 // Do something with the date in data variable |
|
90 // data.iTimeStamp |
|
91 |
|
92 // Compensate axis for landscape mode |
|
93 TInt x = -data.iAxisY; |
|
94 TInt y = data.iAxisX; |
|
95 TInt z = data.iAxisZ; |
|
96 #if 0 |
|
97 TInt x = data.iAxisX; |
|
98 TInt y = data.iAxisY; |
|
99 TInt z = data.iAxisZ; |
|
100 #endif |
|
101 const TReal K128 = 0.0078125; // 1/128 |
|
102 const TReal KPole1 = 0.97; // Pole of lowpass filter applied to X and Y |
|
103 const TReal KPole2 = 0.90; // Pole of lowpass filter applied to Z |
|
104 const TReal KPole3 = 0.75; // Pole of highpass filter applied to Z |
|
105 |
|
106 // Single-pole lowpass-filtering of X and Y |
|
107 iX1 = KPole1 * iX1 + ( 1.0 - KPole1 ) * ( K128 * x ); |
|
108 iY1 = KPole1 * iY1 + ( 1.0 - KPole1 ) * ( K128 * y ); |
|
109 |
|
110 // Single-pole lowpass-filtering of Z |
|
111 iZ0 = KPole2 * iZ1 + ( 1.0 - KPole2 ) * ( K128 * z ); |
|
112 // 1st order highpass-filtering of Z (zero at 1) |
|
113 iZ2 = KPole3 * iZ2 + ( 1.0 - KPole3 ) * ( iZ0 - iZ1 ); |
|
114 |
|
115 // Update variables for use at next sample |
|
116 iZ1 = iZ0; |
|
117 } |
|
118 } |
|
119 } |
|
120 |
|
121 void CAccelerometer::DataError(CSensrvChannel& aChannel, TSensrvErrorSeverity aError) |
|
122 { |
|
123 } |
|
124 |
|
125 void CAccelerometer::GetDataListenerInterfaceL(TUid aInterfaceUid, TAny*& aInterface) |
|
126 {} |