Gyro/Mpu_Calibration "ESP8266"

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
samerou
Posts: 38
Joined: Mon Feb 11, 2019 12:51 pm

Gyro/Mpu_Calibration "ESP8266"

Post by samerou » Wed Apr 10, 2019 3:40 pm

hello ,
I'm trying to use the Gyro sensor with ESP8266 and finding a trouble doing the calibration but first I used the mpu6050.py see below in order to get the position and temperature but all the values are wrong even when I keep my sensor without moving it, all the values keep changing and I don't know why , So I did some digging in this site on how to calibrate the Gyro sensor and I found many example but I didn't succeed in running them ,


the actual mpu6050.py is :

Code: Select all

import machine
class accel():

    def __init__(self, i2c, addr=0x68):

        self.iic = i2c

        self.addr = addr

        self.iic.start()

        self.iic.writeto(self.addr, bytearray([107, 0]))

        self.iic.stop()

    def get_raw_values(self):

        self.iic.start()

        a = self.iic.readfrom_mem(self.addr, 0x3B, 14)

        self.iic.stop()

        return a



    def get_ints(self):

        b = self.get_raw_values()

        c = []

        for i in b:

            c.append(i)

        return c



    def bytes_toint(self, firstbyte, secondbyte):


        if not firstbyte & 0x80:

            return firstbyte << 8 | secondbyte

        return - (((firstbyte ^ 255) << 8) | (secondbyte ^ 255) + 1)



    def get_values(self):

        raw_ints = self.get_raw_values()

        vals = {}



        vals["AcX"] = self.bytes_toint(raw_ints[0], raw_ints[1])

        vals["AcY"] = self.bytes_toint(raw_ints[2], raw_ints[3])

        vals["AcZ"] = self.bytes_toint(raw_ints[4], raw_ints[5])

        vals["Tmp"] = self.bytes_toint(raw_ints[6], raw_ints[7]) / 340.00 + 36.53

        vals["GyX"] = self.bytes_toint(raw_ints[8], raw_ints[9])

        vals["GyY"] = self.bytes_toint(raw_ints[10], raw_ints[11])

        vals["GyZ"] = self.bytes_toint(raw_ints[12], raw_ints[13])

        return vals  # returned in range of Int16

        # -32768 to 32767



    def val_test(self):  # ONLY FOR TESTING! Also, fast reading sometimes crashes IIC

        from time import sleep

        while 1:

            print(self.get_values())

            sleep(0.05)



    def calibrate(threshold=50, n_samples=100):
       
        while True:
            v1 = get_values(n_samples)
            v2 = get_values(n_samples)
        # Check all consecutive measurements are within
        # the threshold. We use abs() so all calculated 
        # differences are positive.
            if all(abs(v1[key] - v2[key]) < threshold for key in v1.keys()):
               return v1  # Calibrated.

    
    
    def get_smoothed_values(n_samples=10):

        for _ in range(samples):
           data = accel.get_values()

           for key in data.keys():
            # Add on value / samples (to generate an average)
            # with default of 0 for first loop.
               result[m] = result.get(m, 0) + (data[m] / samples)
               return result







this code returns strange values even when the sensor is not moving it keeps giving not stable values(changing)

and for the calibration I followed this code :

Code: Select all


import pyb
import os
from struct import unpack as unp
from math import atan2,degrees,pi


class MPU6050():
   
    mpu_addr = 0x68  # address of MPU6050
    _I2Cerror = "I2C communication failure"
    
    def __init__(self, side=1, disable_interrupts=False):

        # create i2c object
        self._timeout = 10
        self.disable_interrupts = False
        self._mpu_i2c = pyb.I2C(side, pyb.I2C.MASTER)
        self.chip_id = int(unp('>h', self._read(1, 0x75, self.mpu_addr))[0])

        # now apply user setting for interrupts
        self.disable_interrupts = disable_interrupts

        # wake it up
        self.wake()
        self.accel_range(1)
        self._ar = self.accel_range()
        self.gyro_range(0)
        self._gr = self.gyro_range()

    # read from device
    def _read(self, count, memaddr, devaddr):
      
        irq_state = True
        if self.disable_interrupts:
            irq_state = pyb.disable_irq()
        result = self._mpu_i2c.mem_read(count,
                                        devaddr,
                                        memaddr,
                                        timeout=self._timeout)
        pyb.enable_irq(irq_state)
        return result

    # write to device
    def _write(self, data, memaddr, devaddr):
    
        irq_state = True
        if self.disable_interrupts:
            irq_state = pyb.disable_irq()
        result = self._mpu_i2c.mem_write(data,
                                         devaddr,
                                         memaddr,
                                         timeout=self._timeout)
        pyb.enable_irq(irq_state)
        return result

    # wake
    def wake(self):
      
        try:
            self._write(0x01, 0x6B, self.mpu_addr)
        except OSError:
            print(MPU6050._I2Cerror)
        return 'awake'

    # mode
    def sleep(self):
       
        try:
            self._write(0x40, 0x6B, self.mpu_addr)
        except OSError:
            print(MPU6050._I2Cerror)
        return 'asleep'

 
    # sample rate
    def sample_rate(self, rate=None):
   

        gyro_rate = 8000 # Hz

        # set rate
        try:
            if rate is not None:
                rate_div = int( gyro_rate/rate - 1 )
                if rate_div > 255:
                    rate_div = 255
                self._write(rate_div, 0x19, self.mpu_addr)

            # get rate
            rate = gyro_rate/(unp('<H', self._read(1, 0x19, self.mpu_addr))[0]+1)
        except OSError:
            rate = None
        return rate

    # accelerometer range
    def accel_range(self, accel_range=None):
    
        # set range
        try:
            if accel_range is None:
                pass
            else:
                ar = (0x00, 0x08, 0x10, 0x18)
                try:
                    self._write(ar[accel_range], 0x1C, self.mpu_addr)
                except IndexError:
                    print('accel_range can only be 0, 1, 2 or 3')
            # get range
            ari = int(unp('<H', self._read(1, 0x1C, self.mpu_addr))[0]/8)
        except OSError:
            ari = None
        if ari is not None:
            self._ar = ari
        return ari

    # gyroscope range
    def gyro_range(self, gyro_range=None):
      
        # set range
        try:
            if gyro_range is None:
                pass
            else:
                gr = (0x00, 0x08, 0x10, 0x18)
                try:
                    self._write(gr[gyro_range], 0x1B, self.mpu_addr)
                except IndexError:
                    print('gyro_range can only be 0, 1, 2 or 3')
            # get range
            gri = int(unp('<H', self._read(1, 0x1B, self.mpu_addr))[0]/8)
        except OSError:
            gri = None

        if gri is not None:
            self._gr = gri
        return gri

    # get raw acceleration
    def get_accel_raw(self):
      
        try:
            axyz = self._read(6, 0x3B, self.mpu_addr)
        except OSError:
            axyz = b'\x00\x00\x00\x00\x00\x00'
        return axyz


    # get acceleration
    def get_acc(self, xyz=None):
        if xyz is None:
            xyz = 'xyz'
        scale = (16384, 8192, 4096, 2048)
        raw = self.get_accel_raw()
        axyz = {'x': unp('>h', raw[0:2])[0]/scale[self._ar],
                'y': unp('>h', raw[2:4])[0]/scale[self._ar],
                'z': unp('>h', raw[4:6])[0]/scale[self._ar]}

        aout = []
        for char in xyz:
            aout.append(axyz[char])
        return aout

    # get pitch  
    def pitch(self):   
        scale = (16384, 8192, 4096, 2048)
        raw = self.get_accel_raw()
        x = unp('>h', raw[0:2])[0]/scale[self._ar]
        z = unp('>h', raw[4:6])[0]/scale[self._ar]
        pitch = degrees(pi+atan2(-x,-z))
        if (pitch>=180) and (pitch<=360):
            pitch-=360
        return -pitch

    # get raw gyro
    def get_gyro_raw(self):
       
        try:
            gxyz = self._read(6, 0x43, self.mpu_addr)
        except OSError:
            gxyz = b'\x00\x00\x00\x00\x00\x00'
        return gxyz

    # get gyro
    def get_gyro(self, xyz=None, use_radians=False):
      
        if xyz is None:
            xyz = 'xyz'
        if use_radians:
            scale = (7150, 3755, 1877.5, 938.75)
        else:
            scale = (131.0, 65.5, 32.8, 16.4)
        raw = self.get_gyro_raw()
        gxyz = {'x': unp('>h', raw[0:2])[0]/scale[self._gr],
                'y': unp('>h', raw[2:4])[0]/scale[self._gr],
                'z': unp('>h', raw[4:6])[0]/scale[self._gr]}

        gout = []
        for char in xyz:
            gout.append(gxyz[char])
        return gout

    # get gyro pitch - y - axis in degrees
    def get_gy(self):
        scale = (131.0, 65.5, 32.8, 16.4)
        raw = self.get_gyro_raw()
        gy =  unp('>h', raw[2:4])[0]/scale[self._gr]
        return gy
I changed Pyb with machine and I tried to run the code but getting error when I try to use

Code: Select all

 from mpu6050 import MPU6050
the error is : " Module is not found and I don't know why "

Anyone have an idea how to calibrate Gyro sensor (mpu6050) , I also Tried to use mpu9250 9150 but I get the same error when using

Code: Select all

from mpu9150 import MPU9150
ESP8266 Vendor
sensor : MPU -92/65_____GY 9250/9255/6500/6555

Anyidea is welcome ,
Best Regards,
Samerou

User avatar
Roberthh
Posts: 1468
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Gyro/Mpu_Calibration "ESP8266"

Post by Roberthh » Sun Apr 21, 2019 7:57 pm

I edited the two scripts such that they run on my Wemos D1 Mini and return some useful results. By chance, I had a MPU6050 sensor. Please not that the methods calibrate() and get_smoothed_value() of accel.py are still wrong. They look unfinished and cause obvious errors when called.

accel.py:

Code: Select all

import machine
class accel():

    def __init__(self, i2c, addr=0x68):

        self.iic = i2c
        self.addr = addr
        self.iic.writeto(self.addr, bytearray([107, 0]))

    def get_raw_values(self):

        a = self.iic.readfrom_mem(self.addr, 0x3B, 14)
        return a

    def get_ints(self):

        b = self.get_raw_values()
        c = []
        for i in b:
            c.append(i)
        return c

    def bytes_toint(self, firstbyte, secondbyte):

        if not firstbyte & 0x80:
            return firstbyte << 8 | secondbyte
        return - (((firstbyte ^ 255) << 8) | (secondbyte ^ 255) + 1)

    def get_values(self):

        raw_ints = self.get_raw_values()
        vals = {}

        vals["AcX"] = self.bytes_toint(raw_ints[0], raw_ints[1])
        vals["AcY"] = self.bytes_toint(raw_ints[2], raw_ints[3])
        vals["AcZ"] = self.bytes_toint(raw_ints[4], raw_ints[5])
        vals["Tmp"] = self.bytes_toint(raw_ints[6], raw_ints[7]) / 340.00 + 36.53
        vals["GyX"] = self.bytes_toint(raw_ints[8], raw_ints[9])
        vals["GyY"] = self.bytes_toint(raw_ints[10], raw_ints[11])
        vals["GyZ"] = self.bytes_toint(raw_ints[12], raw_ints[13])

        return vals  # returned in range of Int16

        # -32768 to 32767

    def val_test(self):  # ONLY FOR TESTING! Also, fast reading sometimes crashes IIC

        from time import sleep
        while 1:
            print(self.get_values())
            sleep(0.05)

    def calibrate(threshold=50, n_samples=100):

        while True:
            v1 = get_values(n_samples)
            v2 = get_values(n_samples)
        # Check all consecutive measurements are within
        # the threshold. We use abs() so all calculated
        # differences are positive.
            if all(abs(v1[key] - v2[key]) < threshold for key in v1.keys()):
                return v1  # Calibrated.

    def get_smoothed_values(n_samples=10):

        for _ in range(samples):
            data = accel.get_values()
            for key in data.keys():
                # Add on value / samples (to generate an average)
                # with default of 0 for first loop.
                result[m] = result.get(m, 0) + (data[m] / samples)
                return result
mpu6050.py:

Code: Select all

import machine
import os
from struct import unpack as unp
from math import atan2, degrees, pi


class MPU6050():

    mpu_addr = 0x68  # address of MPU6050
    _I2Cerror = "I2C communication failure"

    def __init__(self, side=1, disable_interrupts=False):

        # create i2c object
        self._timeout = 10
        self.disable_interrupts = False
        self._mpu_i2c = machine.I2C(scl=machine.Pin(12),
                                    sda=machine.Pin(13),
                                    freq=400000)

        self.chip_id = self._read(1, 0x75, self.mpu_addr)[0]

        # now apply user setting for interrupts
        self.disable_interrupts = disable_interrupts

        # wake it up
        self.wake()
        self.accel_range(1)
        self._ar = self.accel_range()
        self.gyro_range(0)
        self._gr = self.gyro_range()

    # read from device
    def _read(self, count, memaddr, devaddr):

        irq_state = True
        result = self._mpu_i2c.readfrom_mem(devaddr,
                                            memaddr,
                                            count)
        return result

    # write to device
    def _write(self, data, memaddr, devaddr):

        irq_state = True
        result = self._mpu_i2c.writeto_mem(devaddr,
                                           memaddr,
                                           bytearray([data]))
        return result

    # wake
    def wake(self):

        try:
            self._write(0x01, 0x6B, self.mpu_addr)
        except OSError:
            print(MPU6050._I2Cerror)
        return 'awake'

    # mode
    def sleep(self):

        try:
            self._write(0x40, 0x6B, self.mpu_addr)
        except OSError:
            print(MPU6050._I2Cerror)
        return 'asleep'

    #  sample rate
    def sample_rate(self, rate=None):

        gyro_rate = 8000  # Hz

        # set rate
        try:
            if rate is not None:
                rate_div = int(gyro_rate/rate - 1)
                if rate_div > 255:
                    rate_div = 255
                self._write(rate_div, 0x19, self.mpu_addr)

            # get rate
            rate = gyro_rate/(unp('<H', self._read(1, 0x19, self.mpu_addr))[0] + 1)
        except OSError:
            rate = None
        return rate

    # accelerometer range
    def accel_range(self, accel_range=None):

        # set range
        try:
            if accel_range is None:
                pass
            else:
                ar = (0x00, 0x08, 0x10, 0x18)
                try:
                    self._write(ar[accel_range], 0x1C, self.mpu_addr)
                except IndexError:
                    print('accel_range can only be 0, 1, 2 or 3')
            # get range
            ari = self._read(1, 0x1C, self.mpu_addr)[0] // 8
        except OSError:
            ari = None
        if ari is not None:
            self._ar = ari
        return ari

    # gyroscope range
    def gyro_range(self, gyro_range=None):

        # set range
        try:
            if gyro_range is None:
                pass
            else:
                gr = (0x00, 0x08, 0x10, 0x18)
                try:
                    self._write(gr[gyro_range], 0x1B, self.mpu_addr)
                except IndexError:
                    print('gyro_range can only be 0, 1, 2 or 3')
            # get range
            gri = self._read(1, 0x1B, self.mpu_addr)[0] // 8
        except OSError:
            gri = None

        if gri is not None:
            self._gr = gri
        return gri

    # get raw acceleration
    def get_accel_raw(self):

        try:
            axyz = self._read(6, 0x3B, self.mpu_addr)
        except OSError:
            axyz = b'\x00\x00\x00\x00\x00\x00'
        return axyz

    # get acceleration
    def get_acc(self, xyz=None):
        if xyz is None:
            xyz = 'xyz'
        scale = (16384, 8192, 4096, 2048)
        raw = self.get_accel_raw()
        axyz = {'x': unp('>h', raw[0:2])[0]/scale[self._ar],
                'y': unp('>h', raw[2:4])[0]/scale[self._ar],
                'z': unp('>h', raw[4:6])[0]/scale[self._ar]}

        aout = []
        for char in xyz:
            aout.append(axyz[char])
        return aout

    # get pitch
    def pitch(self):
        scale = (16384, 8192, 4096, 2048)
        raw = self.get_accel_raw()
        x = unp('>h', raw[0:2])[0]/scale[self._ar]
        z = unp('>h', raw[4:6])[0]/scale[self._ar]
        pitch = degrees(pi + atan2(-x, -z))
        if (pitch >= 180) and (pitch <= 360):
            pitch -= 360
        return -pitch

    # get raw gyro
    def get_gyro_raw(self):

        try:
            gxyz = self._read(6, 0x43, self.mpu_addr)
        except OSError:
            gxyz = b'\x00\x00\x00\x00\x00\x00'
        return gxyz

    # get gyro
    def get_gyro(self, xyz=None, use_radians=False):

        if xyz is None:
            xyz = 'xyz'
        if use_radians:
            scale = (7150, 3755, 1877.5, 938.75)
        else:
            scale = (131.0, 65.5, 32.8, 16.4)
        raw = self.get_gyro_raw()
        gxyz = {'x': unp('>h', raw[0:2])[0]/scale[self._gr],
                'y': unp('>h', raw[2:4])[0]/scale[self._gr],
                'z': unp('>h', raw[4:6])[0]/scale[self._gr]}

        gout = []
        for char in xyz:
            gout.append(gxyz[char])
        return gout

    # get gyro pitch - y - axis in degrees
    def get_gy(self):
        scale = (131.0, 65.5, 32.8, 16.4)
        raw = self.get_gyro_raw()
        gy = unp('>h', raw[2:4])[0]/scale[self._gr]
        return gy

samerou
Posts: 38
Joined: Mon Feb 11, 2019 12:51 pm

Re: Gyro/Mpu_Calibration "ESP8266"

Post by samerou » Mon Apr 22, 2019 5:57 am

Roberthh wrote:
Sun Apr 21, 2019 7:57 pm
I edited the two scripts such that they run on my Wemos D1 Mini and return some useful results. By chance, I had a MPU6050 sensor. Please not that the methods calibrate() and get_smoothed_value() of accel.py are still wrong. They look unfinished and cause obvious errors when called.

accel.py:

Code: Select all

import machine
class accel():

    def __init__(self, i2c, addr=0x68):

        self.iic = i2c
        self.addr = addr
        self.iic.writeto(self.addr, bytearray([107, 0]))

    def get_raw_values(self):

        a = self.iic.readfrom_mem(self.addr, 0x3B, 14)
        return a

    def get_ints(self):

        b = self.get_raw_values()
        c = []
        for i in b:
            c.append(i)
        return c

    def bytes_toint(self, firstbyte, secondbyte):

        if not firstbyte & 0x80:
            return firstbyte << 8 | secondbyte
        return - (((firstbyte ^ 255) << 8) | (secondbyte ^ 255) + 1)

    def get_values(self):

        raw_ints = self.get_raw_values()
        vals = {}

        vals["AcX"] = self.bytes_toint(raw_ints[0], raw_ints[1])
        vals["AcY"] = self.bytes_toint(raw_ints[2], raw_ints[3])
        vals["AcZ"] = self.bytes_toint(raw_ints[4], raw_ints[5])
        vals["Tmp"] = self.bytes_toint(raw_ints[6], raw_ints[7]) / 340.00 + 36.53
        vals["GyX"] = self.bytes_toint(raw_ints[8], raw_ints[9])
        vals["GyY"] = self.bytes_toint(raw_ints[10], raw_ints[11])
        vals["GyZ"] = self.bytes_toint(raw_ints[12], raw_ints[13])

        return vals  # returned in range of Int16

        # -32768 to 32767

    def val_test(self):  # ONLY FOR TESTING! Also, fast reading sometimes crashes IIC

        from time import sleep
        while 1:
            print(self.get_values())
            sleep(0.05)

    def calibrate(threshold=50, n_samples=100):

        while True:
            v1 = get_values(n_samples)
            v2 = get_values(n_samples)
        # Check all consecutive measurements are within
        # the threshold. We use abs() so all calculated
        # differences are positive.
            if all(abs(v1[key] - v2[key]) < threshold for key in v1.keys()):
                return v1  # Calibrated.

    def get_smoothed_values(n_samples=10):

        for _ in range(samples):
            data = accel.get_values()
            for key in data.keys():
                # Add on value / samples (to generate an average)
                # with default of 0 for first loop.
                result[m] = result.get(m, 0) + (data[m] / samples)
                return result
mpu6050.py:

Code: Select all

import machine
import os
from struct import unpack as unp
from math import atan2, degrees, pi


class MPU6050():

    mpu_addr = 0x68  # address of MPU6050
    _I2Cerror = "I2C communication failure"

    def __init__(self, side=1, disable_interrupts=False):

        # create i2c object
        self._timeout = 10
        self.disable_interrupts = False
        self._mpu_i2c = machine.I2C(scl=machine.Pin(12),
                                    sda=machine.Pin(13),
                                    freq=400000)

        self.chip_id = self._read(1, 0x75, self.mpu_addr)[0]

        # now apply user setting for interrupts
        self.disable_interrupts = disable_interrupts

        # wake it up
        self.wake()
        self.accel_range(1)
        self._ar = self.accel_range()
        self.gyro_range(0)
        self._gr = self.gyro_range()

    # read from device
    def _read(self, count, memaddr, devaddr):

        irq_state = True
        result = self._mpu_i2c.readfrom_mem(devaddr,
                                            memaddr,
                                            count)
        return result

    # write to device
    def _write(self, data, memaddr, devaddr):

        irq_state = True
        result = self._mpu_i2c.writeto_mem(devaddr,
                                           memaddr,
                                           bytearray([data]))
        return result

    # wake
    def wake(self):

        try:
            self._write(0x01, 0x6B, self.mpu_addr)
        except OSError:
            print(MPU6050._I2Cerror)
        return 'awake'

    # mode
    def sleep(self):

        try:
            self._write(0x40, 0x6B, self.mpu_addr)
        except OSError:
            print(MPU6050._I2Cerror)
        return 'asleep'

    #  sample rate
    def sample_rate(self, rate=None):

        gyro_rate = 8000  # Hz

        # set rate
        try:
            if rate is not None:
                rate_div = int(gyro_rate/rate - 1)
                if rate_div > 255:
                    rate_div = 255
                self._write(rate_div, 0x19, self.mpu_addr)

            # get rate
            rate = gyro_rate/(unp('<H', self._read(1, 0x19, self.mpu_addr))[0] + 1)
        except OSError:
            rate = None
        return rate

    # accelerometer range
    def accel_range(self, accel_range=None):

        # set range
        try:
            if accel_range is None:
                pass
            else:
                ar = (0x00, 0x08, 0x10, 0x18)
                try:
                    self._write(ar[accel_range], 0x1C, self.mpu_addr)
                except IndexError:
                    print('accel_range can only be 0, 1, 2 or 3')
            # get range
            ari = self._read(1, 0x1C, self.mpu_addr)[0] // 8
        except OSError:
            ari = None
        if ari is not None:
            self._ar = ari
        return ari

    # gyroscope range
    def gyro_range(self, gyro_range=None):

        # set range
        try:
            if gyro_range is None:
                pass
            else:
                gr = (0x00, 0x08, 0x10, 0x18)
                try:
                    self._write(gr[gyro_range], 0x1B, self.mpu_addr)
                except IndexError:
                    print('gyro_range can only be 0, 1, 2 or 3')
            # get range
            gri = self._read(1, 0x1B, self.mpu_addr)[0] // 8
        except OSError:
            gri = None

        if gri is not None:
            self._gr = gri
        return gri

    # get raw acceleration
    def get_accel_raw(self):

        try:
            axyz = self._read(6, 0x3B, self.mpu_addr)
        except OSError:
            axyz = b'\x00\x00\x00\x00\x00\x00'
        return axyz

    # get acceleration
    def get_acc(self, xyz=None):
        if xyz is None:
            xyz = 'xyz'
        scale = (16384, 8192, 4096, 2048)
        raw = self.get_accel_raw()
        axyz = {'x': unp('>h', raw[0:2])[0]/scale[self._ar],
                'y': unp('>h', raw[2:4])[0]/scale[self._ar],
                'z': unp('>h', raw[4:6])[0]/scale[self._ar]}

        aout = []
        for char in xyz:
            aout.append(axyz[char])
        return aout

    # get pitch
    def pitch(self):
        scale = (16384, 8192, 4096, 2048)
        raw = self.get_accel_raw()
        x = unp('>h', raw[0:2])[0]/scale[self._ar]
        z = unp('>h', raw[4:6])[0]/scale[self._ar]
        pitch = degrees(pi + atan2(-x, -z))
        if (pitch >= 180) and (pitch <= 360):
            pitch -= 360
        return -pitch

    # get raw gyro
    def get_gyro_raw(self):

        try:
            gxyz = self._read(6, 0x43, self.mpu_addr)
        except OSError:
            gxyz = b'\x00\x00\x00\x00\x00\x00'
        return gxyz

    # get gyro
    def get_gyro(self, xyz=None, use_radians=False):

        if xyz is None:
            xyz = 'xyz'
        if use_radians:
            scale = (7150, 3755, 1877.5, 938.75)
        else:
            scale = (131.0, 65.5, 32.8, 16.4)
        raw = self.get_gyro_raw()
        gxyz = {'x': unp('>h', raw[0:2])[0]/scale[self._gr],
                'y': unp('>h', raw[2:4])[0]/scale[self._gr],
                'z': unp('>h', raw[4:6])[0]/scale[self._gr]}

        gout = []
        for char in xyz:
            gout.append(gxyz[char])
        return gout

    # get gyro pitch - y - axis in degrees
    def get_gy(self):
        scale = (131.0, 65.5, 32.8, 16.4)
        raw = self.get_gyro_raw()
        gy = unp('>h', raw[2:4])[0]/scale[self._gr]
        return gy


HI Roberth ,

Thank you for your respond , I saved both file to my ESP8266 but to run them should I use the usual code ?

Code: Select all

from machine import I2C, Pin 
i2c = I2C(scl =Pin(5) ,sda = Pin(5))
import mpu6050 
gyrodata= mpu6050.accel(i2c)
gyrodata.get_values() 
Thank you in advance :) ;) :D
B.R
SAMEUR

User avatar
pythoncoder
Posts: 3455
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Gyro/Mpu_Calibration "ESP8266"

Post by pythoncoder » Mon Apr 22, 2019 8:25 am

@samerou Perhaps you could explain what you're trying to achieve. There is an established driver for the MPU6050 here. If your aim is to write a simplified version I'd suggest starting with the proven code and removing features you don't require.
Peter Hinch

User avatar
Roberthh
Posts: 1468
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Gyro/Mpu_Calibration "ESP8266"

Post by Roberthh » Mon Apr 22, 2019 8:30 am

If you call the file with the class accel mpy6050.py. then it is correct. attached is a version of the class accel with the methods calibrate() and get_smoothed_values() changes in a way of how I assumed its purpose. It looks as initially these functions were not intended as class members, but now they are. The file with the class MPU6050 is completely independent. Maybe you could tell you intention for that.

Code: Select all

import machine
class accel():

    def __init__(self, i2c, addr=0x68):

        self.iic = i2c
        self.addr = addr
        self.iic.writeto(self.addr, bytearray([107, 0]))

    def get_raw_values(self):

        a = self.iic.readfrom_mem(self.addr, 0x3B, 14)
        return a

    def get_ints(self):

        b = self.get_raw_values()
        c = []
        for i in b:
            c.append(i)
        return c

    def bytes_toint(self, firstbyte, secondbyte):

        if not firstbyte & 0x80:
            return firstbyte << 8 | secondbyte
        return - (((firstbyte ^ 255) << 8) | (secondbyte ^ 255) + 1)

    def get_values(self):

        raw_ints = self.get_raw_values()
        vals = {}

        vals["AcX"] = self.bytes_toint(raw_ints[0], raw_ints[1])
        vals["AcY"] = self.bytes_toint(raw_ints[2], raw_ints[3])
        vals["AcZ"] = self.bytes_toint(raw_ints[4], raw_ints[5])
        vals["Tmp"] = self.bytes_toint(raw_ints[6], raw_ints[7]) / 340.00 + 36.53
        vals["GyX"] = self.bytes_toint(raw_ints[8], raw_ints[9])
        vals["GyY"] = self.bytes_toint(raw_ints[10], raw_ints[11])
        vals["GyZ"] = self.bytes_toint(raw_ints[12], raw_ints[13])

        return vals  # returned in range of Int16

        # -32768 to 32767

    def get_smoothed_values(self, n_samples=10):

        result = self.get_values()
        for _ in range(0, n_samples - 1):
            data = self.get_values()
            for key in data.keys():
                result[key] += data[key]
        for key in data.keys():
            result[key] /= n_samples
        return result

    def calibrate(self, threshold=50, n_samples=100):

        while True:
            v1 = self.get_smoothed_values(n_samples)
            v2 = self.get_smoothed_values(n_samples)
        # Check all consecutive measurements are within
        # the threshold. We use abs() so all calculated
        # differences are positive.
            if all(abs(v1[key] - v2[key]) < threshold for key in v1.keys()):
                return v1  # Calibrated.

    # ONLY FOR TESTING! Also, fast reading sometimes crashes IIC
    def val_test(self):
        from time import sleep
        while 1:
            print(self.get_values())
            sleep(0.05)


User avatar
Roberthh
Posts: 1468
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Gyro/Mpu_Calibration "ESP8266"

Post by Roberthh » Mon Apr 22, 2019 8:53 am

Just playing around with the MPU6050 class it looks as if creates reasonable normalized values.

samerou
Posts: 38
Joined: Mon Feb 11, 2019 12:51 pm

Re: Gyro/Mpu_Calibration "ESP8266"

Post by samerou » Mon Apr 22, 2019 1:43 pm

Roberthh wrote:
Mon Apr 22, 2019 8:53 am
Just playing around with the MPU6050 class it looks as if creates reasonable normalized values.
@Roberth Thank you for the suggestion but I tried I changed my actual mpu6050 ( the class accel ) but when running it it keeps giving a strange values but it's working

strange values which mean : "Unstable X Y Z (GyX GyZ GyY)



@pythoncoder what I trying to achieve is measure the GyX and GyY , GyZ from my sensor in order to make my analyses later but I couldn't move forward with the calibration of mpu6050

I tried the Github provided but when running

Code: Select all

From mpu9250 Import MPU9250 
it keeps saying no module found "MPU9250"

I'm stuck

samerou
Posts: 38
Joined: Mon Feb 11, 2019 12:51 pm

Re: Gyro/Mpu_Calibration "ESP8266"

Post by samerou » Mon Apr 22, 2019 2:02 pm

@ roberthh ,

How did you manage to play with file where the class MPU6050 could you please give me the correct statement in order to extract values from it ?

User avatar
Roberthh
Posts: 1468
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Gyro/Mpu_Calibration "ESP8266"

Post by Roberthh » Mon Apr 22, 2019 3:34 pm

The sequence I used is:

Code: Select all

from machine import I2C, Pin
from accel import accel
# my I2C is connected to Pins 12 & 13
i2c= I2C(scl=Pin(12), sda=Pin(13))
mpu = accel(i2c)

val = mpu.get_values()
print(val)
# or
val = mpu.get_smoothed_values()
print(val)
And no, the values are not stable but have a lot of noise and fluctuations. I'm not familiar with this sensor, so I do not know if that is to be expected. But I'm pretty confident that this is what the sensor tells. Below a few readings, using get_values()

Code: Select all

{'GyZ': 1, 'GyY': -29, 'GyX': -289, 'Tmp': 30.3182, 'AcZ': -1160, 'AcY': 12, 'AcX': 16464}
{'GyZ': -80, 'GyY': -19, 'GyX': -293, 'Tmp': 30.2241, 'AcZ': -1144, 'AcY': 48, 'AcX': 16412}
{'GyZ': -51, 'GyY': -14, 'GyX': -305, 'Tmp': 30.2241, 'AcZ': -1300, 'AcY': 32, 'AcX': 16460}
{'GyZ': -26, 'GyY': -20, 'GyX': -270, 'Tmp': 30.13, 'AcZ': -1140, 'AcY': 16, 'AcX': 16544}
{'GyZ': -27, 'GyY': -27, 'GyX': -255, 'Tmp': 30.3182, 'AcZ': -1228, 'AcY': 88, 'AcX': 16416}
{'GyZ': -20, 'GyY': -20, 'GyX': -263, 'Tmp': 30.2712, 'AcZ': -1092, 'AcY': 148, 'AcX': 16416}
{'GyZ': -71, 'GyY': -15, 'GyX': -282, 'Tmp': 30.1771, 'AcZ': -1288, 'AcY': 12, 'AcX': 16384}
{'GyZ': -27, 'GyY': -3, 'GyX': -275, 'Tmp': 30.3182, 'AcZ': -1208, 'AcY': 40, 'AcX': 16496}
{'GyZ': -44, 'GyY': -26, 'GyX': -281, 'Tmp': 30.3182, 'AcZ': -1108, 'AcY': 16, 'AcX': 16488}
{'GyZ': -23, 'GyY': -12, 'GyX': -272, 'Tmp': 30.2712, 'AcZ': -1212, 'AcY': 124, 'AcX': 16488}
{'GyZ': -24, 'GyY': -37, 'GyX': -273, 'Tmp': 30.2241, 'AcZ': -1060, 'AcY': 80, 'AcX': 16400}
{'GyZ': -45, 'GyY': -21, 'GyX': -264, 'Tmp': 30.13, 'AcZ': -1236, 'AcY': 72, 'AcX': 16444}
{'GyZ': -36, 'GyY': -27, 'GyX': -294, 'Tmp': 30.2241, 'AcZ': -1228, 'AcY': 48, 'AcX': 16396}
{'GyZ': -37, 'GyY': -21, 'GyX': -281, 'Tmp': 30.2712, 'AcZ': -1148, 'AcY': 192, 'AcX': 16344}
{'GyZ': 6, 'GyY': -11, 'GyX': -272, 'Tmp': 30.2712, 'AcZ': -1276, 'AcY': 84, 'AcX': 16496}
{'GyZ': -38, 'GyY': -32, 'GyX': -288, 'Tmp': 30.2712, 'AcZ': -1244, 'AcY': 132, 'AcX': 16528}
{'GyZ': -56, 'GyY': 1, 'GyX': -274, 'Tmp': 30.2712, 'AcZ': -1148, 'AcY': -16, 'AcX': 16432}
{'GyZ': -50, 'GyY': -18, 'GyX': -319, 'Tmp': 30.2241, 'AcZ': -1204, 'AcY': 140, 'AcX': 16516}
{'GyZ': -56, 'GyY': -13, 'GyX': -284, 'Tmp': 30.1771, 'AcZ': -1220, 'AcY': 152, 'AcX': 16352}
{'GyZ': -12, 'GyY': -15, 'GyX': -284, 'Tmp': 30.1771, 'AcZ': -1132, 'AcY': 48, 'AcX': 16400}
{'GyZ': -51, 'GyY': -10, 'GyX': -273, 'Tmp': 30.2241, 'AcZ': -1156, 'AcY': 44, 'AcX': 16428}
{'GyZ': -48, 'GyY': -25, 'GyX': -271, 'Tmp': 30.1771, 'AcZ': -1152, 'AcY': 68, 'AcX': 16408}
{'GyZ': -50, 'GyY': 6, 'GyX': -251, 'Tmp': 30.3182, 'AcZ': -1096, 'AcY': 132, 'AcX': 16428}
{'GyZ': -49, 'GyY': -4, 'GyX': -283, 'Tmp': 30.1771, 'AcZ': -1148, 'AcY': 72, 'AcX': 16524}
{'GyZ': -31, 'GyY': -7, 'GyX': -263, 'Tmp': 30.2241, 'AcZ': -1112, 'AcY': 76, 'AcX': 16364}
{'GyZ': -9, 'GyY': -17, 'GyX': -283, 'Tmp': 30.1771, 'AcZ': -1160, 'AcY': 48, 'AcX': 16436}
{'GyZ': -15, 'GyY': -18, 'GyX': -301, 'Tmp': 30.1771, 'AcZ': -1152, 'AcY': 128, 'AcX': 16540}
{'GyZ': -58, 'GyY': -34, 'GyX': -288, 'Tmp': 30.2241, 'AcZ': -1140, 'AcY': 52, 'AcX': 16628}
{'GyZ': -35, 'GyY': -26, 'GyX': -293, 'Tmp': 30.2712, 'AcZ': -1168, 'AcY': 100, 'AcX': 16492}
{'GyZ': -24, 'GyY': -19, 'GyX': -253, 'Tmp': 30.13, 'AcZ': -1096, 'AcY': 88, 'AcX': 16480}
{'GyZ': -9, 'GyY': -20, 'GyX': -290, 'Tmp': 30.1771, 'AcZ': -1144, 'AcY': 152, 'AcX': 16500}
{'GyZ': -16, 'GyY': -21, 'GyX': -287, 'Tmp': 30.13, 'AcZ': -1300, 'AcY': 96, 'AcX': 16388}
{'GyZ': -37, 'GyY': -8, 'GyX': -287, 'Tmp': 30.2241, 'AcZ': -1256, 'AcY': -32, 'AcX': 16504}
{'GyZ': -59, 'GyY': -38, 'GyX': -271, 'Tmp': 30.2241, 'AcZ': -1184, 'AcY': 84, 'AcX': 16416}
{'GyZ': -25, 'GyY': -14, 'GyX': -287, 'Tmp': 30.2712, 'AcZ': -1088, 'AcY': 96, 'AcX': 16492}
{'GyZ': -20, 'GyY': -28, 'GyX': -294, 'Tmp': 30.2712, 'AcZ': -1220, 'AcY': 44, 'AcX': 16608}
{'GyZ': -5, 'GyY': -30, 'GyX': -285, 'Tmp': 30.1771, 'AcZ': -1116, 'AcY': 156, 'AcX': 16404}
T
Another list readings using get_smoothed_values(100) (Average over 100 single)

Code: Select all

{'GyZ': -39.47, 'GyY': -18.32, 'GyX': -277.51, 'Tmp': 30.062, 'AcZ': -1178.0, 'AcY': 78.36, 'AcX': 16423.4}
{'GyZ': -37.76, 'GyY': -19.63, 'GyX': -279.38, 'Tmp': 30.0672, 'AcZ': -1167.6, 'AcY': 72.72, 'AcX': 16447.8}
{'GyZ': -39.37, 'GyY': -17.07, 'GyX': -280.13, 'Tmp': 30.0686, 'AcZ': -1163.36, 'AcY': 79.48, 'AcX': 16442.8}
{'GyZ': -40.22, 'GyY': -19.27, 'GyX': -278.82, 'Tmp': 30.0658, 'AcZ': -1162.92, 'AcY': 65.6, 'AcX': 16452.3}
{'GyZ': -36.57, 'GyY': -20.03, 'GyX': -279.35, 'Tmp': 30.0814, 'AcZ': -1172.48, 'AcY': 80.0, 'AcX': 16446.2}
{'GyZ': -38.39, 'GyY': -18.15, 'GyX': -280.0, 'Tmp': 30.0748, 'AcZ': -1179.92, 'AcY': 72.36, 'AcX': 16450.9}
{'GyZ': -38.18, 'GyY': -19.55, 'GyX': -280.1, 'Tmp': 30.0734, 'AcZ': -1163.08, 'AcY': 76.28, 'AcX': 16449.8}
{'GyZ': -37.37, 'GyY': -20.8, 'GyX': -281.99, 'Tmp': 30.0771, 'AcZ': -1180.6, 'AcY': 78.44, 'AcX': 16456.6}
{'GyZ': -38.27, 'GyY': -17.52, 'GyX': -281.33, 'Tmp': 30.0668, 'AcZ': -1159.12, 'AcY': 74.08, 'AcX': 16436.7}
{'GyZ': -37.14, 'GyY': -21.7, 'GyX': -280.69, 'Tmp': 30.0748, 'AcZ': -1161.08, 'AcY': 74.24, 'AcX': 16458.6}
{'GyZ': -36.53, 'GyY': -21.5, 'GyX': -281.57, 'Tmp': 30.0648, 'AcZ': -1174.68, 'AcY': 68.8, 'AcX': 16453.5}
{'GyZ': -38.4, 'GyY': -17.63, 'GyX': -281.09, 'Tmp': 30.0672, 'AcZ': -1168.72, 'AcY': 72.36, 'AcX': 16455.6}
{'GyZ': -38.61, 'GyY': -20.28, 'GyX': -278.77, 'Tmp': 30.0827, 'AcZ': -1166.6, 'AcY': 75.76, 'AcX': 16455.7}
{'GyZ': -36.66, 'GyY': -20.98, 'GyX': -277.36, 'Tmp': 30.0785, 'AcZ': -1174.76, 'AcY': 76.2, 'AcX': 16458.8}
{'GyZ': -37.63, 'GyY': -19.45, 'GyX': -279.25, 'Tmp': 30.0898, 'AcZ': -1171.4, 'AcY': 73.64, 'AcX': 16445.5}
{'GyZ': -36.57, 'GyY': -19.67, 'GyX': -279.45, 'Tmp': 30.0997, 'AcZ': -1169.44, 'AcY': 67.04, 'AcX': 16456.7}
{'GyZ': -39.71, 'GyY': -17.64, 'GyX': -278.48, 'Tmp': 30.0922, 'AcZ': -1183.8, 'AcY': 73.0, 'AcX': 16454.5}
{'GyZ': -35.68, 'GyY': -17.99, 'GyX': -280.73, 'Tmp': 30.0922, 'AcZ': -1157.48, 'AcY': 67.92, 'AcX': 16452.3}
{'GyZ': -37.44, 'GyY': -17.67, 'GyX': -280.59, 'Tmp': 30.0954, 'AcZ': -1160.64, 'AcY': 66.72, 'AcX': 16448.2}
{'GyZ': -38.29, 'GyY': -16.32, 'GyX': -280.74, 'Tmp': 30.0922, 'AcZ': -1183.96, 'AcY': 73.28, 'AcX': 16441.9}
{'GyZ': -37.37, 'GyY': -17.63, 'GyX': -277.18, 'Tmp': 30.1077, 'AcZ': -1180.64, 'AcY': 70.16, 'AcX': 16446.2}
{'GyZ': -38.84, 'GyY': -20.26, 'GyX': -280.12, 'Tmp': 30.0959, 'AcZ': -1174.36, 'AcY': 70.92, 'AcX': 16450.4}
{'GyZ': -38.11, 'GyY': -16.87, 'GyX': -280.18, 'Tmp': 30.1011, 'AcZ': -1178.12, 'AcY': 77.36, 'AcX': 16449.3}
{'GyZ': -37.26, 'GyY': -17.16, 'GyX': -278.5, 'Tmp': 30.1129, 'AcZ': -1172.88, 'AcY': 69.24, 'AcX': 16443.2}
{'GyZ': -36.19, 'GyY': -18.84, 'GyX': -279.37, 'Tmp': 30.1035, 'AcZ': -1171.48, 'AcY': 71.84, 'AcX': 16456.4}
{'GyZ': -39.31, 'GyY': -19.7, 'GyX': -280.07, 'Tmp': 30.1072, 'AcZ': -1171.96, 'AcY': 77.2, 'AcX': 16454.8}
{'GyZ': -37.98, 'GyY': -17.68, 'GyX': -279.39, 'Tmp': 30.1115, 'AcZ': -1171.32, 'AcY': 72.8, 'AcX': 16442.0}
{'GyZ': -38.06, 'GyY': -19.56, 'GyX': -280.66, 'Tmp': 30.1247, 'AcZ': -1179.56, 'AcY': 68.88, 'AcX': 16447.2}
{'GyZ': -37.13, 'GyY': -17.1, 'GyX': -278.48, 'Tmp': 30.1237, 'AcZ': -1180.08, 'AcY': 76.68, 'AcX': 16450.1}
{'GyZ': -37.34, 'GyY': -18.99, 'GyX': -282.56, 'Tmp': 30.1294, 'AcZ': -1173.12, 'AcY': 77.08, 'AcX': 16448.9}
{'GyZ': -38.02, 'GyY': -18.08, 'GyX': -280.58, 'Tmp': 30.1402, 'AcZ': -1185.48, 'AcY': 72.16, 'AcX': 16448.3}
{'GyZ': -38.49, 'GyY': -18.58, 'GyX': -281.1, 'Tmp': 30.1359, 'AcZ': -1169.28, 'AcY': 79.96, 'AcX': 16449.0}
{'GyZ': -39.0, 'GyY': -19.5, 'GyX': -279.23, 'Tmp': 30.1317, 'AcZ': -1173.76, 'AcY': 80.08, 'AcX': 16439.6}
{'GyZ': -39.55, 'GyY': -19.94, 'GyX': -278.77, 'Tmp': 30.1421, 'AcZ': -1193.52, 'AcY': 70.68, 'AcX': 16455.0}
{'GyZ': -40.41, 'GyY': -18.57, 'GyX': -278.77, 'Tmp': 30.1599, 'AcZ': -1169.6, 'AcY': 77.12, 'AcX': 16454.3}
{'GyZ': -37.6, 'GyY': -19.32, 'GyX': -279.22, 'Tmp': 30.1548, 'AcZ': -1197.84, 'AcY': 76.64, 'AcX': 16456.2}
{'GyZ': -35.73, 'GyY': -20.04, 'GyX': -280.42, 'Tmp': 30.1383, 'AcZ': -1170.44, 'AcY': 69.2, 'AcX': 16450.3}
{'GyZ': -38.7, 'GyY': -18.04, 'GyX': -281.01, 'Tmp': 30.1642, 'AcZ': -1180.88, 'AcY': 73.8, 'AcX': 16448.1}
{'GyZ': -38.9, 'GyY': -19.57, 'GyX': -280.86, 'Tmp': 30.1491, 'AcZ': -1173.28, 'AcY': 63.8, 'AcX': 16452.3}
{'GyZ': -35.12, 'GyY': -19.65, 'GyX': -278.55, 'Tmp': 30.1482, 'AcZ': -1182.8, 'AcY': 76.76, 'AcX': 16457.1}
{'GyZ': -38.28, 'GyY': -19.55, 'GyX': -278.24, 'Tmp': 30.1599, 'AcZ': -1178.32, 'AcY': 73.08, 'AcX': 16447.3}
{'GyZ': -37.78, 'GyY': -17.52, 'GyX': -281.41, 'Tmp': 30.1576, 'AcZ': -1180.2, 'AcY': 76.64, 'AcX': 16449.0}
{'GyZ': -36.95, 'GyY': -19.83, 'GyX': -279.19, 'Tmp': 30.16, 'AcZ': -1181.4, 'AcY': 80.16, 'AcX': 16449.0}
{'GyZ': -37.42, 'GyY': -18.49, 'GyX': -277.39, 'Tmp': 30.1562, 'AcZ': -1174.68, 'AcY': 82.76, 'AcX': 16447.6}
{'GyZ': -30.51, 'GyY': -21.19, 'GyX': -278.27, 'Tmp': 30.1585, 'AcZ': -1165.52, 'AcY': 71.72, 'AcX': 16406.5}
The temperture is off. My desk thermometer shows 26.6°C, and that one is pretty in sync with most other sensors tested.

samerou
Posts: 38
Joined: Mon Feb 11, 2019 12:51 pm

Re: Gyro/Mpu_Calibration "ESP8266"

Post by samerou » Mon Apr 22, 2019 3:45 pm

Roberthh wrote:
Mon Apr 22, 2019 3:34 pm
The sequence I used is:

Code: Select all

from machine import I2C, Pin
from accel import accel
# my I2C is connected to Pins 12 & 13
i2c= I2C(scl=Pin(12), sda=Pin(13))
mpu = accel(i2c)

val = mpu.get_values()
print(val)
# or
val = mpu.get_smoothed_values()
print(val)
And no, the values are not stable but have a lot of noise and fluctuations. I'm not familiar with this sensor, so I do not know if that is to be expected. But I'm pretty confident that this is what the sensor tells. Below a few readings, using get_values()

Code: Select all

{'GyZ': 1, 'GyY': -29, 'GyX': -289, 'Tmp': 30.3182, 'AcZ': -1160, 'AcY': 12, 'AcX': 16464}
{'GyZ': -80, 'GyY': -19, 'GyX': -293, 'Tmp': 30.2241, 'AcZ': -1144, 'AcY': 48, 'AcX': 16412}
{'GyZ': -51, 'GyY': -14, 'GyX': -305, 'Tmp': 30.2241, 'AcZ': -1300, 'AcY': 32, 'AcX': 16460}
{'GyZ': -26, 'GyY': -20, 'GyX': -270, 'Tmp': 30.13, 'AcZ': -1140, 'AcY': 16, 'AcX': 16544}
{'GyZ': -27, 'GyY': -27, 'GyX': -255, 'Tmp': 30.3182, 'AcZ': -1228, 'AcY': 88, 'AcX': 16416}
{'GyZ': -20, 'GyY': -20, 'GyX': -263, 'Tmp': 30.2712, 'AcZ': -1092, 'AcY': 148, 'AcX': 16416}
{'GyZ': -71, 'GyY': -15, 'GyX': -282, 'Tmp': 30.1771, 'AcZ': -1288, 'AcY': 12, 'AcX': 16384}
{'GyZ': -27, 'GyY': -3, 'GyX': -275, 'Tmp': 30.3182, 'AcZ': -1208, 'AcY': 40, 'AcX': 16496}
{'GyZ': -44, 'GyY': -26, 'GyX': -281, 'Tmp': 30.3182, 'AcZ': -1108, 'AcY': 16, 'AcX': 16488}
{'GyZ': -23, 'GyY': -12, 'GyX': -272, 'Tmp': 30.2712, 'AcZ': -1212, 'AcY': 124, 'AcX': 16488}
{'GyZ': -24, 'GyY': -37, 'GyX': -273, 'Tmp': 30.2241, 'AcZ': -1060, 'AcY': 80, 'AcX': 16400}
{'GyZ': -45, 'GyY': -21, 'GyX': -264, 'Tmp': 30.13, 'AcZ': -1236, 'AcY': 72, 'AcX': 16444}
{'GyZ': -36, 'GyY': -27, 'GyX': -294, 'Tmp': 30.2241, 'AcZ': -1228, 'AcY': 48, 'AcX': 16396}
{'GyZ': -37, 'GyY': -21, 'GyX': -281, 'Tmp': 30.2712, 'AcZ': -1148, 'AcY': 192, 'AcX': 16344}
{'GyZ': 6, 'GyY': -11, 'GyX': -272, 'Tmp': 30.2712, 'AcZ': -1276, 'AcY': 84, 'AcX': 16496}
{'GyZ': -38, 'GyY': -32, 'GyX': -288, 'Tmp': 30.2712, 'AcZ': -1244, 'AcY': 132, 'AcX': 16528}
{'GyZ': -56, 'GyY': 1, 'GyX': -274, 'Tmp': 30.2712, 'AcZ': -1148, 'AcY': -16, 'AcX': 16432}
{'GyZ': -50, 'GyY': -18, 'GyX': -319, 'Tmp': 30.2241, 'AcZ': -1204, 'AcY': 140, 'AcX': 16516}
{'GyZ': -56, 'GyY': -13, 'GyX': -284, 'Tmp': 30.1771, 'AcZ': -1220, 'AcY': 152, 'AcX': 16352}
{'GyZ': -12, 'GyY': -15, 'GyX': -284, 'Tmp': 30.1771, 'AcZ': -1132, 'AcY': 48, 'AcX': 16400}
{'GyZ': -51, 'GyY': -10, 'GyX': -273, 'Tmp': 30.2241, 'AcZ': -1156, 'AcY': 44, 'AcX': 16428}
{'GyZ': -48, 'GyY': -25, 'GyX': -271, 'Tmp': 30.1771, 'AcZ': -1152, 'AcY': 68, 'AcX': 16408}
{'GyZ': -50, 'GyY': 6, 'GyX': -251, 'Tmp': 30.3182, 'AcZ': -1096, 'AcY': 132, 'AcX': 16428}
{'GyZ': -49, 'GyY': -4, 'GyX': -283, 'Tmp': 30.1771, 'AcZ': -1148, 'AcY': 72, 'AcX': 16524}
{'GyZ': -31, 'GyY': -7, 'GyX': -263, 'Tmp': 30.2241, 'AcZ': -1112, 'AcY': 76, 'AcX': 16364}
{'GyZ': -9, 'GyY': -17, 'GyX': -283, 'Tmp': 30.1771, 'AcZ': -1160, 'AcY': 48, 'AcX': 16436}
{'GyZ': -15, 'GyY': -18, 'GyX': -301, 'Tmp': 30.1771, 'AcZ': -1152, 'AcY': 128, 'AcX': 16540}
{'GyZ': -58, 'GyY': -34, 'GyX': -288, 'Tmp': 30.2241, 'AcZ': -1140, 'AcY': 52, 'AcX': 16628}
{'GyZ': -35, 'GyY': -26, 'GyX': -293, 'Tmp': 30.2712, 'AcZ': -1168, 'AcY': 100, 'AcX': 16492}
{'GyZ': -24, 'GyY': -19, 'GyX': -253, 'Tmp': 30.13, 'AcZ': -1096, 'AcY': 88, 'AcX': 16480}
{'GyZ': -9, 'GyY': -20, 'GyX': -290, 'Tmp': 30.1771, 'AcZ': -1144, 'AcY': 152, 'AcX': 16500}
{'GyZ': -16, 'GyY': -21, 'GyX': -287, 'Tmp': 30.13, 'AcZ': -1300, 'AcY': 96, 'AcX': 16388}
{'GyZ': -37, 'GyY': -8, 'GyX': -287, 'Tmp': 30.2241, 'AcZ': -1256, 'AcY': -32, 'AcX': 16504}
{'GyZ': -59, 'GyY': -38, 'GyX': -271, 'Tmp': 30.2241, 'AcZ': -1184, 'AcY': 84, 'AcX': 16416}
{'GyZ': -25, 'GyY': -14, 'GyX': -287, 'Tmp': 30.2712, 'AcZ': -1088, 'AcY': 96, 'AcX': 16492}
{'GyZ': -20, 'GyY': -28, 'GyX': -294, 'Tmp': 30.2712, 'AcZ': -1220, 'AcY': 44, 'AcX': 16608}
{'GyZ': -5, 'GyY': -30, 'GyX': -285, 'Tmp': 30.1771, 'AcZ': -1116, 'AcY': 156, 'AcX': 16404}
T
Another list readings using get_smoothed_values(100) (Average over 100 single)

Code: Select all

{'GyZ': -39.47, 'GyY': -18.32, 'GyX': -277.51, 'Tmp': 30.062, 'AcZ': -1178.0, 'AcY': 78.36, 'AcX': 16423.4}
{'GyZ': -37.76, 'GyY': -19.63, 'GyX': -279.38, 'Tmp': 30.0672, 'AcZ': -1167.6, 'AcY': 72.72, 'AcX': 16447.8}
{'GyZ': -39.37, 'GyY': -17.07, 'GyX': -280.13, 'Tmp': 30.0686, 'AcZ': -1163.36, 'AcY': 79.48, 'AcX': 16442.8}
{'GyZ': -40.22, 'GyY': -19.27, 'GyX': -278.82, 'Tmp': 30.0658, 'AcZ': -1162.92, 'AcY': 65.6, 'AcX': 16452.3}
{'GyZ': -36.57, 'GyY': -20.03, 'GyX': -279.35, 'Tmp': 30.0814, 'AcZ': -1172.48, 'AcY': 80.0, 'AcX': 16446.2}
{'GyZ': -38.39, 'GyY': -18.15, 'GyX': -280.0, 'Tmp': 30.0748, 'AcZ': -1179.92, 'AcY': 72.36, 'AcX': 16450.9}
{'GyZ': -38.18, 'GyY': -19.55, 'GyX': -280.1, 'Tmp': 30.0734, 'AcZ': -1163.08, 'AcY': 76.28, 'AcX': 16449.8}
{'GyZ': -37.37, 'GyY': -20.8, 'GyX': -281.99, 'Tmp': 30.0771, 'AcZ': -1180.6, 'AcY': 78.44, 'AcX': 16456.6}
{'GyZ': -38.27, 'GyY': -17.52, 'GyX': -281.33, 'Tmp': 30.0668, 'AcZ': -1159.12, 'AcY': 74.08, 'AcX': 16436.7}
{'GyZ': -37.14, 'GyY': -21.7, 'GyX': -280.69, 'Tmp': 30.0748, 'AcZ': -1161.08, 'AcY': 74.24, 'AcX': 16458.6}
{'GyZ': -36.53, 'GyY': -21.5, 'GyX': -281.57, 'Tmp': 30.0648, 'AcZ': -1174.68, 'AcY': 68.8, 'AcX': 16453.5}
{'GyZ': -38.4, 'GyY': -17.63, 'GyX': -281.09, 'Tmp': 30.0672, 'AcZ': -1168.72, 'AcY': 72.36, 'AcX': 16455.6}
{'GyZ': -38.61, 'GyY': -20.28, 'GyX': -278.77, 'Tmp': 30.0827, 'AcZ': -1166.6, 'AcY': 75.76, 'AcX': 16455.7}
{'GyZ': -36.66, 'GyY': -20.98, 'GyX': -277.36, 'Tmp': 30.0785, 'AcZ': -1174.76, 'AcY': 76.2, 'AcX': 16458.8}
{'GyZ': -37.63, 'GyY': -19.45, 'GyX': -279.25, 'Tmp': 30.0898, 'AcZ': -1171.4, 'AcY': 73.64, 'AcX': 16445.5}
{'GyZ': -36.57, 'GyY': -19.67, 'GyX': -279.45, 'Tmp': 30.0997, 'AcZ': -1169.44, 'AcY': 67.04, 'AcX': 16456.7}
{'GyZ': -39.71, 'GyY': -17.64, 'GyX': -278.48, 'Tmp': 30.0922, 'AcZ': -1183.8, 'AcY': 73.0, 'AcX': 16454.5}
{'GyZ': -35.68, 'GyY': -17.99, 'GyX': -280.73, 'Tmp': 30.0922, 'AcZ': -1157.48, 'AcY': 67.92, 'AcX': 16452.3}
{'GyZ': -37.44, 'GyY': -17.67, 'GyX': -280.59, 'Tmp': 30.0954, 'AcZ': -1160.64, 'AcY': 66.72, 'AcX': 16448.2}
{'GyZ': -38.29, 'GyY': -16.32, 'GyX': -280.74, 'Tmp': 30.0922, 'AcZ': -1183.96, 'AcY': 73.28, 'AcX': 16441.9}
{'GyZ': -37.37, 'GyY': -17.63, 'GyX': -277.18, 'Tmp': 30.1077, 'AcZ': -1180.64, 'AcY': 70.16, 'AcX': 16446.2}
{'GyZ': -38.84, 'GyY': -20.26, 'GyX': -280.12, 'Tmp': 30.0959, 'AcZ': -1174.36, 'AcY': 70.92, 'AcX': 16450.4}
{'GyZ': -38.11, 'GyY': -16.87, 'GyX': -280.18, 'Tmp': 30.1011, 'AcZ': -1178.12, 'AcY': 77.36, 'AcX': 16449.3}
{'GyZ': -37.26, 'GyY': -17.16, 'GyX': -278.5, 'Tmp': 30.1129, 'AcZ': -1172.88, 'AcY': 69.24, 'AcX': 16443.2}
{'GyZ': -36.19, 'GyY': -18.84, 'GyX': -279.37, 'Tmp': 30.1035, 'AcZ': -1171.48, 'AcY': 71.84, 'AcX': 16456.4}
{'GyZ': -39.31, 'GyY': -19.7, 'GyX': -280.07, 'Tmp': 30.1072, 'AcZ': -1171.96, 'AcY': 77.2, 'AcX': 16454.8}
{'GyZ': -37.98, 'GyY': -17.68, 'GyX': -279.39, 'Tmp': 30.1115, 'AcZ': -1171.32, 'AcY': 72.8, 'AcX': 16442.0}
{'GyZ': -38.06, 'GyY': -19.56, 'GyX': -280.66, 'Tmp': 30.1247, 'AcZ': -1179.56, 'AcY': 68.88, 'AcX': 16447.2}
{'GyZ': -37.13, 'GyY': -17.1, 'GyX': -278.48, 'Tmp': 30.1237, 'AcZ': -1180.08, 'AcY': 76.68, 'AcX': 16450.1}
{'GyZ': -37.34, 'GyY': -18.99, 'GyX': -282.56, 'Tmp': 30.1294, 'AcZ': -1173.12, 'AcY': 77.08, 'AcX': 16448.9}
{'GyZ': -38.02, 'GyY': -18.08, 'GyX': -280.58, 'Tmp': 30.1402, 'AcZ': -1185.48, 'AcY': 72.16, 'AcX': 16448.3}
{'GyZ': -38.49, 'GyY': -18.58, 'GyX': -281.1, 'Tmp': 30.1359, 'AcZ': -1169.28, 'AcY': 79.96, 'AcX': 16449.0}
{'GyZ': -39.0, 'GyY': -19.5, 'GyX': -279.23, 'Tmp': 30.1317, 'AcZ': -1173.76, 'AcY': 80.08, 'AcX': 16439.6}
{'GyZ': -39.55, 'GyY': -19.94, 'GyX': -278.77, 'Tmp': 30.1421, 'AcZ': -1193.52, 'AcY': 70.68, 'AcX': 16455.0}
{'GyZ': -40.41, 'GyY': -18.57, 'GyX': -278.77, 'Tmp': 30.1599, 'AcZ': -1169.6, 'AcY': 77.12, 'AcX': 16454.3}
{'GyZ': -37.6, 'GyY': -19.32, 'GyX': -279.22, 'Tmp': 30.1548, 'AcZ': -1197.84, 'AcY': 76.64, 'AcX': 16456.2}
{'GyZ': -35.73, 'GyY': -20.04, 'GyX': -280.42, 'Tmp': 30.1383, 'AcZ': -1170.44, 'AcY': 69.2, 'AcX': 16450.3}
{'GyZ': -38.7, 'GyY': -18.04, 'GyX': -281.01, 'Tmp': 30.1642, 'AcZ': -1180.88, 'AcY': 73.8, 'AcX': 16448.1}
{'GyZ': -38.9, 'GyY': -19.57, 'GyX': -280.86, 'Tmp': 30.1491, 'AcZ': -1173.28, 'AcY': 63.8, 'AcX': 16452.3}
{'GyZ': -35.12, 'GyY': -19.65, 'GyX': -278.55, 'Tmp': 30.1482, 'AcZ': -1182.8, 'AcY': 76.76, 'AcX': 16457.1}
{'GyZ': -38.28, 'GyY': -19.55, 'GyX': -278.24, 'Tmp': 30.1599, 'AcZ': -1178.32, 'AcY': 73.08, 'AcX': 16447.3}
{'GyZ': -37.78, 'GyY': -17.52, 'GyX': -281.41, 'Tmp': 30.1576, 'AcZ': -1180.2, 'AcY': 76.64, 'AcX': 16449.0}
{'GyZ': -36.95, 'GyY': -19.83, 'GyX': -279.19, 'Tmp': 30.16, 'AcZ': -1181.4, 'AcY': 80.16, 'AcX': 16449.0}
{'GyZ': -37.42, 'GyY': -18.49, 'GyX': -277.39, 'Tmp': 30.1562, 'AcZ': -1174.68, 'AcY': 82.76, 'AcX': 16447.6}
{'GyZ': -30.51, 'GyY': -21.19, 'GyX': -278.27, 'Tmp': 30.1585, 'AcZ': -1165.52, 'AcY': 71.72, 'AcX': 16406.5}
The temperture is off. My desk thermometer shows 26.6°C, and that one is pretty in sync with most other sensors tested.

For

Code: Select all

val.get_smoothed_values()
I get error samples isn't defined line 66

Post Reply