STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
accelerometer_calibration.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""
3Accelerometer Calibration Script
4
5Calibrates accelerometer using static position method.
6Requires sensor to be placed in 6+ known orientations.
7
8Usage:
9 python accelerometer_calibration.py --sensor-id imu_0
10"""
11
12import argparse
13import numpy as np
14import time
15from imu_calibration import AccelerometerCalibrator, SensorType, CalibrationPoint
16
17
19 """Collect accelerometer readings at static positions"""
20 print("Accelerometer Static Position Calibration")
21 print("=" * 50)
22 print("Place sensor in the following orientations:")
23 print("1. +X axis up (gravity in +X)")
24 print("2. -X axis up (gravity in -X)")
25 print("3. +Y axis up (gravity in +Y)")
26 print("4. -Y axis up (gravity in -X)")
27 print("5. +Z axis up (gravity in +Z)")
28 print("6. -Z axis up (gravity in -Z)")
29 print()
30
31 calibrator = AccelerometerCalibrator("imu_0")
32
33 orientations = [
34 ("+X", np.array([9.81, 0.0, 0.0])),
35 ("-X", np.array([-9.81, 0.0, 0.0])),
36 ("+Y", np.array([0.0, 9.81, 0.0])),
37 ("-Y", np.array([0.0, -9.81, 0.0])),
38 ("+Z", np.array([0.0, 0.0, 9.81])),
39 ("-Z", np.array([0.0, 0.0, -9.81])),
40 ]
41
42 for i, (name, ref_gravity) in enumerate(orientations, 1):
43 input(f"Place sensor in {name} orientation and press Enter...")
44
45 # Collect multiple readings and average
46 print("Collecting readings (5 seconds)...")
47 readings = []
48 start_time = time.time()
49 while time.time() - start_time < 5.0:
50 # In real implementation, read from sensor
51 # For now, simulate
52 raw = np.array([0.0, 0.0, 0.0]) # Would read from sensor
53 readings.append(raw)
54 time.sleep(0.1)
55
56 # Average readings
57 avg_reading = np.mean(readings, axis=0)
58
59 # Create calibration point
60 point = CalibrationPoint(
61 raw_value=avg_reading,
62 reference_value=ref_gravity,
63 temperature=25.0,
64 timestamp=time.time(),
65 )
66 calibrator.add_calibration_point(point)
67
68 print(f" Collected {name}: {avg_reading}")
69
70 return calibrator
71
72
73def main():
74 parser = argparse.ArgumentParser(description="Accelerometer Calibration")
75 parser.add_argument("--sensor-id", default="imu_0")
76 parser.add_argument("--output", default="accel_calibration.json")
77 parser.add_argument(
78 "--interactive",
79 action="store_true",
80 help="Interactive mode (collect positions)",
81 )
82
83 args = parser.parse_args()
84
85 if args.interactive:
86 calibrator = collect_static_positions()
87 else:
88 # Load from file or use existing data
89 calibrator = AccelerometerCalibrator(args.sensor_id)
90 print("Add calibration points programmatically or use --interactive")
91 return
92
93 # Perform calibration
94 try:
95 params = calibrator.calibrate()
96 print("\nCalibration Results:")
97 print(f" Bias: {params.bias}")
98 print(f" Scale Matrix:\n{params.scale_matrix}")
99 print(f" Quality: {params.calibration_quality:.3f}")
100
101 # Save calibration
102 calibrator.save_calibration(args.output)
103 print(f"\nSaved calibration to {args.output}")
104 except Exception as e:
105 print(f"Calibration failed: {e}")
106
107
108if __name__ == "__main__":
109 main()
TOML_NODISCARD_CTOR time() noexcept=default