calculateTurn method

double calculateTurn(
  1. List<SensorEvent> gyroData
)

Calculates the total turning angle from gyroscope data.

Integrates angular velocity over the entire data sequence to determine the total angle turned. Used for validation and analysis of turning motion.

Returns the absolute value of the total integrated angle in radians.

Implementation

double calculateTurn(List<SensorEvent> gyroData) {
  if (gyroData.isEmpty) return 0.0;

  double sum = 0.0; // Cumulative angle integration
  for (int i = 1; i < gyroData.length; i++) {
    // Calculate time delta for numerical integration
    final dt =
        gyroData[i].timestamp
            .difference(gyroData[i - 1].timestamp)
            .inMilliseconds /
        1e3;
    if (dt <= 0) continue; // Skip if time difference is zero or negative

    // Integrate angular velocity (y-axis) to get angle
    sum += gyroData[i].y * dt;
  }
  return sum.abs(); // Return absolute angle
}