adafruit_mpu6050.py
def perform_calibration(self,
                            averaging_size: int = 1000,
                            discarding_size: int = 100,
                            accelerometer_tolerance: int = 8,
                            gyroscope_tolerance: int = 1,
                            accelerometer_step: int = 8,
                            gyroscope_step: int = 3,
                            accelerometer_x_goal=0,
                            accelerometer_y_goal=0,
                            accelerometer_z_goal=16384,
                            gyroscope_x_goal=0,
                            gyroscope_y_goal=0,
                            gyroscope_z_goal=0
                            ) -> Tuple[int, int, int, int, int, int]:

        """
         This method calculates the sensor offsets for the accelerometer and gyroscope by averaging values
         while the sensor is NOT in motion and the PCB is placed on a flat surface, facing upwards.
         (Be aware of the fact, that the calibration offsets are not persistent,
         they have to be set manually, after each new i2c connection.)


         :param averaging_size:             Number of reading sensor values used to compute average.
                                            Make it higher to get more precision but the sketch will be lower.
                                            Default value is set to 1000.
         :param discarding_size:            Number of reading sensor values to discard after setting a new offset.
                                            Default value is set to 100.
         :param accelerometer_tolerance:    Error range allowed for accelerometer calibration offsets.
                                            Make it lower to get more precision, but sketch may not converge.
                                            Default value is set to 8.
         :param gyroscope_tolerance:        Error range allowed for gyroscope calibration offsets.
                                            Make it lower to get more precision, but sketch may not converge.
                                            Default value is set to 1.
         :param accelerometer_step:         Step value, tuned for accelerometer, used in each step of calibration.
                                            Default value is set to 8.
         :param gyroscope_step:             Step value, tuned for gyroscope, used in each step of calibration.
                                            Default value is set to 3.
         :param accelerometer_x_goal:       The goal value for the x-axis of the accelerometer.
                                            Default value is 0.
         :param accelerometer_y_goal:       The goal value for the x-axis of the accelerometer.
                                            Default value is 0.
         :param accelerometer_z_goal:       The goal value for the x-axis of the accelerometer.
                                            Default value is 16384, which is the equivalent of 1g,
                                            indicating that the sensor is under gravity.
         :param gyroscope_z_goal:           The goal value for the x-axis of the gyroscope.
                                            Default value is 0.
         :param gyroscope_y_goal:           The goal value for the y-axis of the gyroscope.
                                            Default value is 0.
         :param gyroscope_x_goal:           The goal value for the z-axis of the gyroscope.
                                            Default value is 0.
        """

        offsets = {
            "accelerometer": {"x": 0, "y": 0, "z": 0},
            "gyroscope": {"x": 0, "y": 0, "z": 0}
        }

        calibration_states = {
            "accelerometer": {"x": False, "y": False, "z": False},
            "gyroscope": {"x": False, "y": False, "z": False}
        }

        goals = {
            "accelerometer": {"x": accelerometer_x_goal, "y": accelerometer_y_goal, "z": accelerometer_z_goal},
            "gyroscope": {"x": gyroscope_x_goal, "y": gyroscope_y_goal, "z": gyroscope_z_goal}
        }

        tolerances = {
            "accelerometer": accelerometer_tolerance,
            "gyroscope": gyroscope_tolerance
        }

        steps = {
            "accelerometer": accelerometer_step,
            "gyroscope": gyroscope_step
        }

        self._logger.info('Current sensors offsets:')
        self._logger.info(f'Accelerometer X: {self._raw_accel_offset_x}, Y: {self._raw_accel_offset_y}, '
                          f'Z: {self._raw_accel_offset_z}')
        self._logger.info(f'Gyroscope X: {self._raw_gyro_offset_x}, Y: {self._raw_gyro_offset_y}, '
                          f'Z: {self._raw_gyro_offset_z}')

        while not self.__is_calibrated(calibration_states):
            self.__write_offsets_to_sensor(offsets)
            self.__discard_unreliable_values(discarding_size)
            averages = self.__calculate_average_values(averaging_size)
            self.__update_offsets(offsets, averages, steps, goals, tolerances, calibration_states, "accelerometer")
            self.__update_offsets(offsets, averages, steps, goals, tolerances, calibration_states, "gyroscope")

        self._logger.info(f'New offsets:')
        self._logger.info(f'Accelerometer X: {self._raw_accel_offset_x}, Y: {self._raw_accel_offset_y}, '
                          f'Z: {self._raw_accel_offset_z}')
        self._logger.info(f'Gyroscope X: {self._raw_gyro_offset_x}, Y: {self._raw_gyro_offset_y}, '
                          f'Z: {self._raw_gyro_offset_z}')
        return (offsets['accelerometer']['x'], offsets['accelerometer']['y'], offsets['accelerometer']['z'],
                offsets['gyroscope']['x'], offsets['gyroscope']['y'], offsets['gyroscope']['z'])

         This method calculates the sensor offsets for the accelerometer and gyroscope by averaging values
         while the sensor is NOT in motion and the PCB is placed on a flat surface, facing upwards.
         (Be aware of the fact, that the calibration offsets are not persistent,
         they have to be set manually, after each new i2c connection.)


         :param averaging_size:             Number of reading sensor values used to compute average.
                                            Make it higher to get more precision but the sketch will be lower.
                                            Default value is set to 1000.
         :param discarding_size:            Number of reading sensor values to discard after setting a new offset.
                                            Default value is set to 100.
         :param accelerometer_tolerance:    Error range allowed for accelerometer calibration offsets.
                                            Make it lower to get more precision, but sketch may not converge.
                                            Default value is set to 8.
         :param gyroscope_tolerance:        Error range allowed for gyroscope calibration offsets.
                                            Make it lower to get more precision, but sketch may not converge.
                                            Default value is set to 1.
         :param accelerometer_step:         Step value, tuned for accelerometer, used in each step of calibration.
                                            Default value is set to 8.
         :param gyroscope_step:             Step value, tuned for gyroscope, used in each step of calibration.
                                            Default value is set to 3.
         :param accelerometer_x_goal:       The goal value for the x-axis of the accelerometer.
                                            Default value is 0.
         :param accelerometer_y_goal:       The goal value for the x-axis of the accelerometer.
                                            Default value is 0.
         :param accelerometer_z_goal:       The goal value for the x-axis of the accelerometer.
                                            Default value is 16384, which is the equivalent of 1g,
                                            indicating that the sensor is under gravity.
         :param gyroscope_z_goal:           The goal value for the x-axis of the gyroscope.
                                            Default value is 0.
         :param gyroscope_y_goal:           The goal value for the y-axis of the gyroscope.
                                            Default value is 0.
         :param gyroscope_x_goal:           The goal value for the z-axis of the gyroscope.
                                            Default value is 0.
        
This method calculates the sensor offsets for the accelerometer and gyroscope by averaging values
while the sensor is NOT in motion and the PCB is placed on a flat surface, facing upwards.
(Be aware of the fact, that the calibration offsets are not persistent,
they have to be set manually, after each new i2c connection.)
This method calculates the sensor offsets for the accelerometer and gyroscope by averaging values while the sensor is NOT in motion and the PCB is placed on a flat surface facing upwards . ( Be aware of the fact that the calibration offsets are not persistent they have to be set manually after each new i2c connection . )
def perform_calibration(self,
                            averaging_size: int = 1000,
                            discarding_size: int = 100,
                            accelerometer_tolerance: int = 8,
                            gyroscope_tolerance: int = 1,
                            accelerometer_step: int = 8,
                            gyroscope_step: int = 3,
                            accelerometer_x_goal=0,
                            accelerometer_y_goal=0,
                            accelerometer_z_goal=16384,
                            gyroscope_x_goal=0,
                            gyroscope_y_goal=0,
                            gyroscope_z_goal=0
                            ) -> Tuple[int, int, int, int, int, int]:
        offsets = {
            "accelerometer": {"x": 0, "y": 0, "z": 0},
            "gyroscope": {"x": 0, "y": 0, "z": 0}
        }
        calibration_states = {
            "accelerometer": {"x": False, "y": False, "z": False},
            "gyroscope": {"x": False, "y": False, "z": False}
        }
        goals = {
            "accelerometer": {"x": accelerometer_x_goal, "y": accelerometer_y_goal, "z": accelerometer_z_goal},
            "gyroscope": {"x": gyroscope_x_goal, "y": gyroscope_y_goal, "z": gyroscope_z_goal}
        }
        tolerances = {
            "accelerometer": accelerometer_tolerance,
            "gyroscope": gyroscope_tolerance
        }
        steps = {
            "accelerometer": accelerometer_step,
            "gyroscope": gyroscope_step
        }
        self._logger.info('Current sensors offsets:')
        self._logger.info(f'Accelerometer X: {self._raw_accel_offset_x}, Y: {self._raw_accel_offset_y}, '
                          f'Z: {self._raw_accel_offset_z}')
        self._logger.info(f'Gyroscope X: {self._raw_gyro_offset_x}, Y: {self._raw_gyro_offset_y}, '
                          f'Z: {self._raw_gyro_offset_z}')
        while not self.__is_calibrated(calibration_states):
            self.__write_offsets_to_sensor(offsets)
            self.__discard_unreliable_values(discarding_size)
            averages = self.__calculate_average_values(averaging_size)
            self.__update_offsets(offsets, averages, steps, goals, tolerances, calibration_states, "accelerometer")
            self.__update_offsets(offsets, averages, steps, goals, tolerances, calibration_states, "gyroscope")
        self._logger.info(f'New offsets:')
        self._logger.info(f'Accelerometer X: {self._raw_accel_offset_x}, Y: {self._raw_accel_offset_y}, '
                          f'Z: {self._raw_accel_offset_z}')
        self._logger.info(f'Gyroscope X: {self._raw_gyro_offset_x}, Y: {self._raw_gyro_offset_y}, '
                          f'Z: {self._raw_gyro_offset_z}')
        return (offsets['accelerometer']['x'], offsets['accelerometer']['y'], offsets['accelerometer']['z'],
                offsets['gyroscope']['x'], offsets['gyroscope']['y'], offsets['gyroscope']['z'])
def perform_calibration ( self , averaging_size : int = 1000 , discarding_size : int = 100 , accelerometer_tolerance : int = 8 , gyroscope_tolerance : int = 1 , accelerometer_step : int = 8 , gyroscope_step : int = 3 , accelerometer_x_goal = 0 , accelerometer_y_goal = 0 , accelerometer_z_goal = 16384 , gyroscope_x_goal = 0 , gyroscope_y_goal = 0 , gyroscope_z_goal = 0 ) -> Tuple [ int , int , int , int , int , int ] : offsets = { "accelerometer" : { "x" : 0 , "y" : 0 , "z" : 0 } , "gyroscope" : { "x" : 0 , "y" : 0 , "z" : 0 } } calibration_states = { "accelerometer" : { "x" : False , "y" : False , "z" : False } , "gyroscope" : { "x" : False , "y" : False , "z" : False } } goals = { "accelerometer" : { "x" : accelerometer_x_goal , "y" : accelerometer_y_goal , "z" : accelerometer_z_goal } , "gyroscope" : { "x" : gyroscope_x_goal , "y" : gyroscope_y_goal , "z" : gyroscope_z_goal } } tolerances = { "accelerometer" : accelerometer_tolerance , "gyroscope" : gyroscope_tolerance } steps = { "accelerometer" : accelerometer_step , "gyroscope" : gyroscope_step } self . _logger . info ( 'Current sensors offsets:' ) self . _logger . info ( f'Accelerometer X: {self._raw_accel_offset_x}, Y: {self._raw_accel_offset_y}, ' f'Z: {self._raw_accel_offset_z}' ) self . _logger . info ( f'Gyroscope X: {self._raw_gyro_offset_x}, Y: {self._raw_gyro_offset_y}, ' f'Z: {self._raw_gyro_offset_z}' ) while not self . __is_calibrated ( calibration_states ) : self . __write_offsets_to_sensor ( offsets ) self . __discard_unreliable_values ( discarding_size ) averages = self . __calculate_average_values ( averaging_size ) self . __update_offsets ( offsets , averages , steps , goals , tolerances , calibration_states , "accelerometer" ) self . __update_offsets ( offsets , averages , steps , goals , tolerances , calibration_states , "gyroscope" ) self . _logger . info ( f'New offsets:' ) self . _logger . info ( f'Accelerometer X: {self._raw_accel_offset_x}, Y: {self._raw_accel_offset_y}, ' f'Z: {self._raw_accel_offset_z}' ) self . _logger . info ( f'Gyroscope X: {self._raw_gyro_offset_x}, Y: {self._raw_gyro_offset_y}, ' f'Z: {self._raw_gyro_offset_z}' ) return ( offsets [ 'accelerometer' ] [ 'x' ] , offsets [ 'accelerometer' ] [ 'y' ] , offsets [ 'accelerometer' ] [ 'z' ] , offsets [ 'gyroscope' ] [ 'x' ] , offsets [ 'gyroscope' ] [ 'y' ] , offsets [ 'gyroscope' ] [ 'z' ] )
This method calculates the sensor offsets for the accelerometer and gyroscope by averaging values
while the sensor is NOT in motion and the PCB is placed on a flat surface, facing upwards.
This method calculates the sensor offsets for the accelerometer and gyroscope by averaging values while the sensor is NOT in motion and the PCB is placed on a flat surface facing upwards .
"""
         This method calculates the sensor offsets for the accelerometer and gyroscope by averaging values
         while the sensor is NOT in motion and the PCB is placed on a flat surface, facing upwards.
         (Be aware of the fact, that the calibration offsets are not persistent,
         they have to be set manually, after each new i2c connection.)


         :param averaging_size:             Number of reading sensor values used to compute average.
                                            Make it higher to get more precision but the sketch will be lower.
                                            Default value is set to 1000.
         :param discarding_size:            Number of reading sensor values to discard after setting a new offset.
                                            Default value is set to 100.
         :param accelerometer_tolerance:    Error range allowed for accelerometer calibration offsets.
                                            Make it lower to get more precision, but sketch may not converge.
                                            Default value is set to 8.
         :param gyroscope_tolerance:        Error range allowed for gyroscope calibration offsets.
                                            Make it lower to get more precision, but sketch may not converge.
                                            Default value is set to 1.
         :param accelerometer_step:         Step value, tuned for accelerometer, used in each step of calibration.
                                            Default value is set to 8.
         :param gyroscope_step:             Step value, tuned for gyroscope, used in each step of calibration.
                                            Default value is set to 3.
         :param accelerometer_x_goal:       The goal value for the x-axis of the accelerometer.
                                            Default value is 0.
         :param accelerometer_y_goal:       The goal value for the x-axis of the accelerometer.
                                            Default value is 0.
         :param accelerometer_z_goal:       The goal value for the x-axis of the accelerometer.
                                            Default value is 16384, which is the equivalent of 1g,
                                            indicating that the sensor is under gravity.
         :param gyroscope_z_goal:           The goal value for the x-axis of the gyroscope.
                                            Default value is 0.
         :param gyroscope_y_goal:           The goal value for the y-axis of the gyroscope.
                                            Default value is 0.
         :param gyroscope_x_goal:           The goal value for the z-axis of the gyroscope.
                                            Default value is 0.
        """
self
averaging_size
int
Number of reading sensor values used to compute average.
Make it higher to get more precision but the sketch will be lower.
Default value is set to 1000.
Number of reading sensor values used to compute average . Make it higher to get more precision but the sketch will be lower . Default value is set to 1000 .
discarding_size
int
Number of reading sensor values to discard after setting a new offset.
Default value is set to 100.
Number of reading sensor values to discard after setting a new offset . Default value is set to 100 .
accelerometer_tolerance
int
Error range allowed for accelerometer calibration offsets.
Make it lower to get more precision, but sketch may not converge.
Default value is set to 8.
Error range allowed for accelerometer calibration offsets . Make it lower to get more precision but sketch may not converge . Default value is set to 8 .
gyroscope_tolerance
int
Error range allowed for gyroscope calibration offsets.
Make it lower to get more precision, but sketch may not converge.
Default value is set to 1.
Error range allowed for gyroscope calibration offsets . Make it lower to get more precision but sketch may not converge . Default value is set to 1 .
accelerometer_step
int
Step value, tuned for accelerometer, used in each step of calibration.
Default value is set to 8.
Step value tuned for accelerometer used in each step of calibration . Default value is set to 8 .
gyroscope_step
int
Step value, tuned for gyroscope, used in each step of calibration.
Default value is set to 3.
Step value tuned for gyroscope used in each step of calibration . Default value is set to 3 .
accelerometer_x_goal
The goal value for the x-axis of the accelerometer.
Default value is 0.
The goal value for the x - axis of the accelerometer . Default value is 0 .
accelerometer_y_goal
The goal value for the x-axis of the accelerometer.
Default value is 0.
The goal value for the x - axis of the accelerometer . Default value is 0 .
accelerometer_z_goal
The goal value for the x-axis of the accelerometer.
Default value is 16384, which is the equivalent of 1g,
indicating that the sensor is under gravity.
The goal value for the x - axis of the accelerometer . Default value is 16384 which is the equivalent of 1g indicating that the sensor is under gravity .
gyroscope_x_goal
The goal value for the z-axis of the gyroscope.
Default value is 0.
The goal value for the z - axis of the gyroscope . Default value is 0 .
gyroscope_y_goal
The goal value for the y-axis of the gyroscope.
Default value is 0.
The goal value for the y - axis of the gyroscope . Default value is 0 .
gyroscope_z_goal
The goal value for the x-axis of the gyroscope.
Default value is 0.
The goal value for the x - axis of the gyroscope . Default value is 0 .