performCalibration method

Future<TugCalibrationResult> performCalibration()

Performs the calibration process for the TUG test.

Starts the probe, collects gyroscope data, and determines the maximum turn angle while calibration is active. Returns a TugCalibrationResult with the maximum angle found. Handles cancellation and errors gracefully.

Implementation

Future<TugCalibrationResult> performCalibration() async {
  try {
    isCalibrating = true;
    var maxAngle = 0.0;

    // Start probe
    await probe.start();

    while (isCalibrating) {
      if (completer.isCanceled) {
        return TugCalibrationResult(identifier: identifier, angle: 0.0)
          ..results['error'] = 'Cancelled';
      }
      final result = processor.calculateTurn(probe.gyroscopeData);

      if (maxAngle < result) {
        maxAngle = result;
      }

      await Future.delayed(const Duration(seconds: 1));
    }

    // Stop probe
    await probe.stop();

    // Create TugCalibrationResult
    if (!maxAngle.isNaN && !maxAngle.isInfinite && maxAngle > 0.0) {
      return TugCalibrationResult(identifier: identifier, angle: maxAngle);
    } else {
      return TugCalibrationResult(identifier: identifier, angle: 0.0)
        ..results['error'] = 'No valid results found';
    }
  } catch (e) {
    if (kDebugMode) {
      print('Error in TUGStep.performCalibration: $e');
    }
    return TugCalibrationResult(identifier: identifier, angle: 0.0)
      ..results['error'] = e.toString();
  }
}