Repository navigation
Motion Sensor Tutorial
The specialty topic of focus in our project is using the integrated motion sensor in android phones. Specifially, we chose to use the integrated gyroscope to calculate all of the needed data relating to squatting. Our demo app uses the gyroscope to detect a series of phases during the squat, which allows rep count, duration, average speed, and lowest depth to be calculated effectively. The idea of this demo app is to allow people to track basic squat data easily and quickly, and save the data for later comparison.
Requirements for current development: Android Studio Narwhal 3 Feature Drop | 2025.1.3 Gradle 8.13
Development instructions: The gyroscope functionality should be declared in a viewmodel, and resemble something close to the following:
class TrackingScreenViewModel(app: Application) : AndroidViewModel(app), SensorEventListener {
private val _gyro = MutableStateFlow(FloatArray(3))
val gyro = _gyro.asStateFlow()
fun startSensors() {
accelSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
gyroSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE)
accelSensor?.let {
sensorManager.registerListener(
this,
it,
SensorManager.SENSOR_DELAY_GAME
)
}
gyroSensor?.let {
sensorManager.registerListener(
this,
it,
SensorManager.SENSOR_DELAY_GAME
)
}
}
fun stopSensors() {
sensorManager.unregisterListener(this)
}
}
this is