Micropython——九轴传感器(MPU6050)的使用及算法(二)

本文介绍了如何对MPU6050九轴传感器的数据偏移进行校准,通过收集大量数据并计算平均值来确定偏移量,从而提高数据准确性。在校准过程中,作者提供了驱动代码示例,包括获取原始数据、转换为整数及进行偏移校准的步骤,并讨论了在启动时平衡准确度与时间之间的权衡。此外,还提供了一个测试函数用于验证校准效果。
摘要由CSDN通过智能技术生成

前言:

在上篇文章中,简单地实现了九轴传感器(MPU6050)的获取加速度、角速度以及温度的数值。但是,我们知道,对于MPU6050来说,其提供的数据会夹杂有严重的噪音,在芯片处理静止状态时数据摆动都可能超过2%。除了噪音以外,其数据还含有偏移现象。这对于我们来说是无法忍受的。所以,我们要先对其生成的数据偏移进行校准,然后我们在处理其噪音现象。


1、对数据偏移进行校准

如何校准是我们所要关注的重点。对于数据来说,比较准确的偏移量要对大量的数据进行统计才能获知,数据量越大越准,但统计的时间也就越慢。一般校准可以在每次启动系统时进行,那么你应当在准确度和启动时间之间做一个权衡。

更改后的驱动代码实现

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):
        self.AcX_high_bite = self.get_raw_values[0]
        self.AcX_low_bite = self.get_raw_values[1]
        self.AcX = self.bytes_toint(self.AcX_high_bite,self.AcX_low_bite)
        self.AcX_calibre = self.AcX - self.AcX_calibration
        
        self.AcY_high_bite = self.get_raw_values[2]
        self.AcY_low_bite = self.get_raw_values[3]
        self.AcY = self.bytes_toint(self.AcY_high_bite,self.AcY_low_bite)
        self.AcY_calibre = self.AcY - self.AcY_calibration
        
        self.AcZ_high_bite = self.get_raw_values[4]
        self.AcZ_low_bite = self.get_raw_values[5]
        self.AcZ = self.bytes_toint(self.AcZ_high_bite,self.AcZ_low_bite)
        self.AcZ_calibre = self.AcZ - self.AcZ_calibration
        
        self.temp_high_byte = self.get_raw_values[6]
        self.temp_low_byte = self.get_raw_values[7]
        self.temp = self.bytes_toint( self.temp_high_byte,self.temp_low_byte)
        self.temperature = self.temp / 340.00 + 36.53
        
        
        self.GyX_high_byte = self.captures[8] 
        self.GyX_low_byte = self.captures[9]
        self.GyX = self.bytes_to_int(self.GyX_high_byte, self.GyX_low_byte)#100ms陀螺仪输出一次值
        self.GyX_calibre = self.GyX - self.GyX_calibration
        
        self.GyY_high_byte = self.captures[10] 
        self.GyY_low_byte = self.captures[11]
        self.GyY = self.bytes_to_int(self.GyY_high_byte, self.GyY_low_byte)#100ms陀螺仪输出一次值
        self.GyY_calibre = self.GyY - self.GyY_calibration
        
        
        self.GyZ_high_byte = self.captures[12] 
        self.GyZ_low_byte = self.captures[13]
        self.GyZ = self.bytes_to_int(self.GyZ_high_byte, self.GyZ_low_byte)#100ms陀螺仪输出一次值
        self.GyZ_calibre = self.GyZ - self.GyZ_calibration
              
        # -32768 to 32767
 
 
    #移位合并
    def bytes_toint(self, firstbyte, secondbyte):
        if not firstbyte & 0x80:
            return firstbyte << 8 | secondbyte
        return - (((firstbyte ^ 255) << 8) | (secondbyte ^ 255) + 1)
 
#读取100次求出平均值,第一次初始化时求出陀螺仪6轴初始值,不动的情况下,理论初始值等于0
    def calibration(self):
        i = 0
        self.GyX_calibration = 0
        self.GyY_calibration = 0
        self.GyZ_calibration = 0
        self.AcX_calibration = 0
        self.AcY_calibration = 0
        self.AcZ_calibration = 0
        while i < 100:
            self.i2c.readform_mem_into(self.address,0x3B,14)
            self.get_values()
            self.AcX_calibration += self.AcX
            self.AcY_calibration += self.AcY
            self.AcZ_calibration += self.AcZ
            
            self.GyX_calibration += self.GyX
            self.GyY_calibration += self.GyY
            self.GyZ_calibration += self.GyZ
            i+=1
            time.sleep(0.1)
        self.GyX_calibration /= 100
        self.GyY_calibration /= 100
        self.GyZ_calibration /= 100
        self.AcX_calibration /= 100
        self.AcY_calibration /= 100
        self.AcZ_calibration /= 100

    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)

这里我们对于每次对100份数据进行处理,以达到处理其数据偏移的问题。

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xa_L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值