-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSensorDataManager.kt.rtf
More file actions
228 lines (227 loc) · 7.69 KB
/
Copy pathSensorDataManager.kt.rtf
File metadata and controls
228 lines (227 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
{\rtf1\ansi\ansicpg1252\cocoartf2865
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\froman\fcharset0 Times-Roman;}
{\colortbl;\red255\green255\blue255;\red0\green0\blue0;}
{\*\expandedcolortbl;;\cssrgb\c0\c0\c0;}
\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\deftab720
\pard\pardeftab720\partightenfactor0
\f0\fs24 \cf0 \expnd0\expndtw0\kerning0
\outl0\strokewidth0 \strokec2 \
package com.suspension.analyzer.sensors\
\
import android.content.Context\
import android.hardware.Sensor\
import android.hardware.SensorEvent\
import android.hardware.SensorEventListener\
import android.hardware.SensorManager\
import kotlin.math.sqrt\
\
/**\
* Manages sensor data collection and fusion.\
* Handles accelerometer, gyroscope, and magnetometer with basic Kalman filtering.\
*/\
class SensorDataManager(context: Context) : SensorEventListener \{\
\
private val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager\
private val accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)\
private val gyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE)\
private val magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)\
private val barometer = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE)\
\
// Current sensor readings\
private var accelX = 0f\
private var accelY = 0f\
private var accelZ = 0f\
\
private var gyroX = 0f\
private var gyroY = 0f\
private var gyroZ = 0f\
\
private var magX = 0f\
private var magY = 0f\
private var magZ = 0f\
\
private var pressure = 0f\
private var lastTimestamp = 0L\
\
// Kalman filter state for gyroscope integration\
private val gyroKalmanX = KalmanFilter(0.02f, 0.01f) // Process noise, measurement noise\
private val gyroKalmanY = KalmanFilter(0.02f, 0.01f)\
private val gyroKalmanZ = KalmanFilter(0.02f, 0.01f)\
\
// Orientation angles (pitch, roll, yaw) in radians\
private var pitch = 0f\
private var roll = 0f\
private var yaw = 0f\
\
// Calibration offsets\
private var accelOffsetX = 0f\
private var accelOffsetY = 0f\
private var accelOffsetZ = 0f\
\
private var gyroOffsetX = 0f\
private var gyroOffsetY = 0f\
private var gyroOffsetZ = 0f\
\
// Listener for data updates\
var onDataUpdated: ((SensorReading) -> Unit)? = null\
\
data class SensorReading(\
val timestamp: Long,\
val accelX: Float,\
val accelY: Float,\
val accelZ: Float,\
val gyroX: Float,\
val gyroY: Float,\
val gyroZ: Float,\
val pitch: Float,\
val roll: Float,\
val yaw: Float,\
val pressure: Float,\
val peakLateralG: Float,\
val peakLongitudinalG: Float,\
val peakVerticalG: Float\
)\
\
fun startListening() \{\
accelerometer?.let \{\
sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_FASTEST)\
\}\
gyroscope?.let \{\
sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_FASTEST)\
\}\
magnetometer?.let \{\
sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_FASTEST)\
\}\
barometer?.let \{\
sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_NORMAL)\
\}\
\}\
\
fun stopListening() \{\
sensorManager.unregisterListener(this)\
\}\
\
/**\
* Capture current sensor readings for calibration (static phone, level surface)\
*/\
fun calibrate(numSamples: Int = 100) \{\
var sumAccelX = 0f\
var sumAccelY = 0f\
var sumAccelZ = 0f\
var sumGyroX = 0f\
var sumGyroY = 0f\
var sumGyroZ = 0f\
\
repeat(numSamples) \{\
sumAccelX += accelX\
sumAccelY += accelY\
sumAccelZ += accelZ\
sumGyroX += gyroX\
sumGyroY += gyroY\
sumGyroZ += gyroZ\
Thread.sleep(10)\
\}\
\
accelOffsetX = sumAccelX / numSamples\
accelOffsetY = sumAccelY / numSamples\
accelOffsetZ = (sumAccelZ / numSamples) - 9.81f // Gravity offset\
\
gyroOffsetX = sumGyroX / numSamples\
gyroOffsetY = sumGyroY / numSamples\
gyroOffsetZ = sumGyroZ / numSamples\
\
gyroKalmanX.setState(0f)\
gyroKalmanY.setState(0f)\
gyroKalmanZ.setState(0f)\
\}\
\
override fun onSensorChanged(event: SensorEvent) \{\
val currentTimestamp = System.currentTimeMillis()\
val deltaTime = if (lastTimestamp == 0L) 0.01f else (currentTimestamp - lastTimestamp) / 1000f\
lastTimestamp = currentTimestamp\
\
when (event.sensor.type) \{\
Sensor.TYPE_ACCELEROMETER -> \{\
accelX = event.values[0] - accelOffsetX\
accelY = event.values[1] - accelOffsetY\
accelZ = event.values[2] - accelOffsetZ\
\}\
Sensor.TYPE_GYROSCOPE -> \{\
gyroX = event.values[0] - gyroOffsetX\
gyroY = event.values[1] - gyroOffsetY\
gyroZ = event.values[2] - gyroOffsetZ\
\
// Integrate gyro data with Kalman filter for orientation\
if (deltaTime > 0) \{\
pitch = gyroKalmanX.update(pitch + gyroY * deltaTime, gyroY)\
roll = gyroKalmanY.update(roll - gyroX * deltaTime, gyroX)\
yaw = gyroKalmanZ.update(yaw + gyroZ * deltaTime, gyroZ)\
\}\
\}\
Sensor.TYPE_MAGNETIC_FIELD -> \{\
magX = event.values[0]\
magY = event.values[1]\
magZ = event.values[2]\
\}\
Sensor.TYPE_PRESSURE -> \{\
pressure = event.values[0]\
\}\
\}\
\
// Emit current reading\
val peakLateralG = sqrt(accelX * accelX + accelZ * accelZ) / 9.81f\
val peakLongitudinalG = accelY / 9.81f\
val peakVerticalG = accelZ / 9.81f\
\
val reading = SensorReading(\
timestamp = currentTimestamp,\
accelX = accelX,\
accelY = accelY,\
accelZ = accelZ,\
gyroX = gyroX,\
gyroY = gyroY,\
gyroZ = gyroZ,\
pitch = pitch,\
roll = roll,\
yaw = yaw,\
pressure = pressure,\
peakLateralG = peakLateralG,\
peakLongitudinalG = peakLongitudinalG,\
peakVerticalG = peakVerticalG\
)\
\
onDataUpdated?.invoke(reading)\
\}\
\
override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) \{\}\
\
/**\
* Simple Kalman filter for smoothing noisy sensor data\
*/\
private class KalmanFilter(\
private val processNoise: Float,\
private val measurementNoise: Float\
) \{\
private var estimate = 0f\
private var errorEstimate = 1f\
\
fun setState(value: Float) \{\
estimate = value\
errorEstimate = 1f\
\}\
\
fun update(measurement: Float, rate: Float): Float \{\
// Predict\
val predictedEstimate = estimate\
val predictedError = errorEstimate + processNoise\
\
// Update\
val kalmanGain = predictedError / (predictedError + measurementNoise)\
estimate = predictedEstimate + kalmanGain * (measurement - predictedEstimate)\
errorEstimate = (1 - kalmanGain) * predictedError\
\
return estimate\
\}\
\}\
\}}