@@ -680,6 +680,9 @@ def __init__(self, i2c_bus, address):
680680 gyro_range = "500dps" ,
681681 gyro_data_rate = "104Hz"
682682 )
683+ # Software calibration offsets
684+ self .accel_offset = [0.0 , 0.0 , 0.0 ]
685+ self .gyro_offset = [0.0 , 0.0 , 0.0 ]
683686
684687 def read_acceleration (self ):
685688 """Read acceleration in m/s² (converts from mg)."""
@@ -705,55 +708,62 @@ def read_temperature(self):
705708 return self .sensor .temperature
706709
707710 def calibrate_accelerometer (self , samples ):
708- """Calibrate accelerometer using hardware calibration."""
709- 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 } " )
711+ """Calibrate accelerometer (device must be stationary)."""
712+ sum_x , sum_y , sum_z = 0.0 , 0.0 , 0.0
713+
714+ for _ in range (samples ):
715+ ax , ay , az = self .sensor ._read_raw_accelerations ()
716+ sum_x += (ax / 1000.0 ) * _GRAVITY
717+ sum_y += (ay / 1000.0 ) * _GRAVITY
718+ sum_z += (az / 1000.0 ) * _GRAVITY
719+ time .sleep_ms (10 )
720+
721+ print (f"sumz: { sum_z } " )
722+ z_offset = 0
714723 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 } " )
719- # Return offsets in m/s² (convert from raw offsets)
720- return (return_x , return_y , return_z )
724+ sum_z *= - 1
725+ z_offset = (1000 / samples ) + _GRAVITY
726+ print (f"sumz: { sum_z } " )
727+
728+ # Average offsets (assuming Z-axis should read +9.8 m/s²)
729+ self .accel_offset [0 ] = sum_x / samples
730+ self .accel_offset [1 ] = sum_y / samples
731+ self .accel_offset [2 ] = (sum_z / samples ) - _GRAVITY - z_offset
732+ print (f"offsets: { self .accel_offset } " )
733+
734+ return tuple (self .accel_offset )
721735
722736 def calibrate_gyroscope (self , samples ):
723- """Calibrate gyroscope using hardware calibration."""
724- self .sensor .gyro_calibrate (samples )
725- # Return offsets in deg/s (convert from raw offsets)
726- return (
727- (self .sensor .gyro_offset_x * self .sensor .gyro_sensitivity ) / 1000.0 ,
728- (self .sensor .gyro_offset_y * self .sensor .gyro_sensitivity ) / 1000.0 ,
729- (self .sensor .gyro_offset_z * self .sensor .gyro_sensitivity ) / 1000.0
730- )
737+ """Calibrate gyroscope (device must be stationary)."""
738+ sum_x , sum_y , sum_z = 0.0 , 0.0 , 0.0
739+
740+ for _ in range (samples ):
741+ gx , gy , gz = self .sensor ._read_raw_angular_velocities ()
742+ sum_x += gx
743+ sum_y += gy
744+ sum_z += gz
745+ time .sleep_ms (10 )
746+
747+ # Average offsets (should be 0 when stationary)
748+ self .gyro_offset [0 ] = sum_x / samples
749+ self .gyro_offset [1 ] = sum_y / samples
750+ self .gyro_offset [2 ] = sum_z / samples
751+
752+ return tuple (self .gyro_offset )
731753
732754 def get_calibration (self ):
733- """Get current calibration (raw offsets from hardware) ."""
755+ """Get current calibration."""
734756 return {
735- 'accel_offsets' : [
736- self .sensor .acc_offset_x ,
737- self .sensor .acc_offset_y ,
738- self .sensor .acc_offset_z
739- ],
740- 'gyro_offsets' : [
741- self .sensor .gyro_offset_x ,
742- self .sensor .gyro_offset_y ,
743- self .sensor .gyro_offset_z
744- ]
757+ 'accel_offsets' : self .accel_offset ,
758+ 'gyro_offsets' : self .gyro_offset
745759 }
746760
747761 def set_calibration (self , accel_offsets , gyro_offsets ):
748- """Set calibration from saved values (raw offsets) ."""
762+ """Set calibration from saved values."""
749763 if accel_offsets :
750- self .sensor .acc_offset_x = accel_offsets [0 ]
751- self .sensor .acc_offset_y = accel_offsets [1 ]
752- self .sensor .acc_offset_z = accel_offsets [2 ]
764+ self .accel_offset = list (accel_offsets )
753765 if gyro_offsets :
754- self .sensor .gyro_offset_x = gyro_offsets [0 ]
755- self .sensor .gyro_offset_y = gyro_offsets [1 ]
756- self .sensor .gyro_offset_z = gyro_offsets [2 ]
766+ self .gyro_offset = list (gyro_offsets )
757767
758768
759769# ============================================================================
0 commit comments