STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
gyroscope_calibration.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""
3Gyroscope Calibration Script
4
5Calibrates gyroscope using zero-velocity method.
6Requires sensor to be stationary for extended period.
7
8Usage:
9 python gyroscope_calibration.py --sensor-id imu_0 --duration 60
10"""
11
12import argparse
13import numpy as np
14import time
15from imu_calibration import GyroscopeCalibrator, CalibrationPoint
16
17
18def collect_zero_velocity_readings(duration: float = 60.0):
19 """Collect gyroscope readings during zero-velocity period"""
20 print("Gyroscope Zero-Velocity Calibration")
21 print("=" * 50)
22 print(f"Keep sensor stationary for {duration} seconds...")
23 print("Collecting readings...")
24
25 calibrator = GyroscopeCalibrator("imu_0")
26
27 readings = []
28 start_time = time.time()
29 sample_rate = 10.0 # Hz
30
31 while time.time() - start_time < duration:
32 # In real implementation, read from sensor
33 # For now, simulate with small bias
34 raw = np.array([0.01, -0.02, 0.015]) + np.random.normal(0, 0.001, 3)
35 readings.append(raw)
36 time.sleep(1.0 / sample_rate)
37
38 # Create calibration points (reference is zero)
39 zero_ref = np.array([0.0, 0.0, 0.0])
40
41 for raw in readings:
42 point = CalibrationPoint(
43 raw_value=raw,
44 reference_value=zero_ref,
45 temperature=25.0,
46 timestamp=time.time(),
47 )
48 calibrator.add_calibration_point(point)
49
50 print(f"Collected {len(readings)} readings")
51 return calibrator
52
53
54def main():
55 parser = argparse.ArgumentParser(description="Gyroscope Calibration")
56 parser.add_argument("--sensor-id", default="imu_0")
57 parser.add_argument(
58 "--duration", type=float, default=60.0, help="Collection duration in seconds"
59 )
60 parser.add_argument("--output", default="gyro_calibration.json")
61
62 args = parser.parse_args()
63
64 calibrator = collect_zero_velocity_readings(args.duration)
65
66 # Perform calibration
67 try:
68 params = calibrator.calibrate()
69 print("\nCalibration Results:")
70 print(f" Bias: {params.bias}")
71 print(f" Scale Matrix:\n{params.scale_matrix}")
72 print(f" Quality: {params.calibration_quality:.3f}")
73
74 # Save calibration
75 calibrator.save_calibration(args.output)
76 print(f"\nSaved calibration to {args.output}")
77 except Exception as e:
78 print(f"Calibration failed: {e}")
79
80
81if __name__ == "__main__":
82 main()
collect_zero_velocity_readings(float duration=60.0)
TOML_NODISCARD_CTOR time() noexcept=default