4040# Gravity constant for unit conversions
4141_GRAVITY = 9.80665 # m/s²
4242
43+ IMU_CALIBRATION_FILENAME = "imu_calibration.json"
44+
4345# Module state
4446_initialized = False
4547_imu_driver = None
@@ -227,7 +229,7 @@ def read_sensor(sensor):
227229 if _imu_driver :
228230 ax , ay , az = _imu_driver .read_acceleration ()
229231 if _mounted_position == FACING_EARTH :
230- az += _GRAVITY
232+ az *= - 1
231233 return (ax , ay , az )
232234 elif sensor .type == TYPE_GYROSCOPE :
233235 if _imu_driver :
@@ -622,6 +624,9 @@ def calibrate_accelerometer(self, samples):
622624 sum_z += az * _GRAVITY
623625 time .sleep_ms (10 )
624626
627+ if _mounted_position == FACING_EARTH :
628+ sum_z *= - 1
629+
625630 # Average offsets (assuming Z-axis should read +9.8 m/s²)
626631 self .accel_offset [0 ] = sum_x / samples
627632 self .accel_offset [1 ] = sum_y / samples
@@ -702,12 +707,17 @@ def read_temperature(self):
702707 def calibrate_accelerometer (self , samples ):
703708 """Calibrate accelerometer using hardware calibration."""
704709 self .sensor .acc_calibrate (samples )
710+ return_x = (self .sensor .acc_offset_x * self .sensor .acc_sensitivity / 1000.0 ) * _GRAVITY
711+ return_y = (self .sensor .acc_offset_y * self .sensor .acc_sensitivity / 1000.0 ) * _GRAVITY
712+ return_z = (self .sensor .acc_offset_z * self .sensor .acc_sensitivity / 1000.0 ) * _GRAVITY
713+ print (f"normal return_z: { return_z } " )
714+ if _mounted_position == FACING_EARTH :
715+ return_z *= - 1
716+ print (f"sensor is facing earth so returning inverse: { return_z } " )
717+ return_z -= _GRAVITY
718+ print (f"returning: { return_x } ,{ return_y } ,{ return_z } " )
705719 # Return offsets in m/s² (convert from raw offsets)
706- return (
707- (self .sensor .acc_offset_x * self .sensor .acc_sensitivity / 1000.0 ) * _GRAVITY ,
708- (self .sensor .acc_offset_y * self .sensor .acc_sensitivity / 1000.0 ) * _GRAVITY ,
709- (self .sensor .acc_offset_z * self .sensor .acc_sensitivity / 1000.0 ) * _GRAVITY
710- )
720+ return (return_x , return_y , return_z )
711721
712722 def calibrate_gyroscope (self , samples ):
713723 """Calibrate gyroscope using hardware calibration."""
@@ -847,25 +857,10 @@ def _load_calibration():
847857 from mpos .config import SharedPreferences
848858
849859 # Try NEW location first
850- prefs_new = SharedPreferences ("com.micropythonos.settings" , filename = "sensors.json" )
860+ prefs_new = SharedPreferences ("com.micropythonos.settings" , filename = IMU_CALIBRATION_FILENAME )
851861 accel_offsets = prefs_new .get_list ("accel_offsets" )
852862 gyro_offsets = prefs_new .get_list ("gyro_offsets" )
853863
854- # If not found, try OLD location and migrate
855- if not accel_offsets and not gyro_offsets :
856- prefs_old = SharedPreferences ("com.micropythonos.sensors" )
857- accel_offsets = prefs_old .get_list ("accel_offsets" )
858- gyro_offsets = prefs_old .get_list ("gyro_offsets" )
859-
860- if accel_offsets or gyro_offsets :
861- # Save to new location
862- editor = prefs_new .edit ()
863- if accel_offsets :
864- editor .put_list ("accel_offsets" , accel_offsets )
865- if gyro_offsets :
866- editor .put_list ("gyro_offsets" , gyro_offsets )
867- editor .commit ()
868-
869864 if accel_offsets or gyro_offsets :
870865 _imu_driver .set_calibration (accel_offsets , gyro_offsets )
871866 except :
@@ -879,7 +874,7 @@ def _save_calibration():
879874
880875 try :
881876 from mpos .config import SharedPreferences
882- prefs = SharedPreferences ("com.micropythonos.settings" , filename = "sensors.json" )
877+ prefs = SharedPreferences ("com.micropythonos.settings" , filename = IMU_CALIBRATION_FILENAME )
883878 editor = prefs .edit ()
884879
885880 cal = _imu_driver .get_calibration ()
0 commit comments