当前位置: 代码迷 >> 综合 >> 【Luat-esp32】3.陀螺仪-mpu6050
  详细解决方案

【Luat-esp32】3.陀螺仪-mpu6050

热度:56   发布时间:2023-12-05 20:31:17.0

文章目录

  • 1 前言
  • 2 硬件连接
    • 2.1 官网示例--air101+mpu6050
    • 2.2 参照连接--esp32+mpu6050
  • 3 代码 -- 初始化I2C
    • 3.1 I2C接口
    • 3.2 esp32 demp i2c
    • 3.3 I2C官网示例--air101+mpu6050
    • 3.4 结果--报错及改正
  • 4 代码--查看mpu6xxx库(版本错误)
  • 5 代码--正确实现
    • 5.1 示例代码--Wendal
    • 5.2 微调代码--youkai
    • 结果

1 前言

本章加上合宙的mpu6050.

2 硬件连接

建议先查看这篇文章,简单了解一下mpu6050:MPU6050开发 – 基本概念简介
MPU6050-扫盲

购买网址:GY521MPU6050模块 (三轴加速度陀螺仪6DOF)
引脚信息来源于该购买链接。
在这里插入图片描述

到手是这样的
在这里插入图片描述
我的小板比参考连线多了两个作为主机时的i2c,暂时先不连。

2.1 官网示例–air101+mpu6050

正好luatos的i2c教程用的就是mpu6050,参考一下air101+mpu6050的连接方式。
https://wiki.luatos.com/boardGuide/common/i2c.html

在这里插入图片描述

2.2 参照连接–esp32+mpu6050

参照连线对应到esp32
在这里插入图片描述

在这里插入图片描述

PS:INT先不连。AD0连接到GND,可以用pin1。

3 代码 – 初始化I2C

3.1 I2C接口

https://wiki.luatos.com/api/i2c.html
在这里插入图片描述
i2c地址如何查看:I2C设备、读写地址的关系

3.2 esp32 demp i2c

-- esp32demo -- i2c
--1010 000x
--7bit地址,不包含最后一位读写位
local addr = 0x50
-- 按照实际芯片更改编号哦
local i2cid = 0

高位地址默认时1010,(X X A0 R/W)低1位由A0决定,目前A0接GND,所以我们的地址应该是1010 000,即0x50。
注意,是7位地址,所以不取最后一个读写位。

存疑:我这里的理解是否有偏差,查看mpu6xxx的库发现mpu6050地址是0x68

所以我的i2c配置为i2c.setup(5, i2c.FAST, 0x50)
后续再试i2c.setup(0, i2c.FAST, 0x50)

3.3 I2C官网示例–air101+mpu6050

https://wiki.luatos.com/boardGuide/common/i2c.html

在这里插入图片描述

-- 官网i2s示例 air101+mpu6050
sys.taskInit(function()if i2c.setup(0, i2c.FAST, 0x68) == 1 thenlog.info("i2c", "存在 i2c0")elsei2c.close(0) -- 除非i2c的id错误,通常不会失败returnendi2c.send(0, 0x68, string.char(0x75))--发送器件地址sys.wait(50)local revData = i2c.recv(0, 0x68, 1)--读器件地址log.info("recv",revData:toHex())--打印i2c.close(0)
end)

这里地址为i2c id为0,地址为0x68,应该直接仿照着写。

3.4 结果–报错及改正

i2c.setup(5, i2c.FAST, 0x50)

设置i2c号为5,显示错误。
推测esp32的i2c号是固定的0。
在这里插入图片描述

i2c.setup(0, i2c.FAST, 0x50)

在这里插入图片描述
设置为0后,打印正常

4 代码–查看mpu6xxx库(版本错误)

https://doc.openluat.com/wiki/21?wiki_page_id=2706

点击"LuatOS上层脚本",下载代码
script_LuaTask_V2.4.2\demo\peripheral\重力加速度\MPU6XXX\mpu6xxx.lua
查看mpu6xxx库代码

PS:这里的代码不兼容esp32

5 代码–正确实现

5.1 示例代码–Wendal

我使用luatask的代码报错,问了梦程大佬才知道是不兼容,luatask是蜂窝版本。

在这里插入图片描述
所以我尝试去找了air101的demo。在Wendal的git中找到了demo,这版适用,可能因为加了前面的package吧,没有细究。

https://gitee.com/openLuat/LuatOS/blob/master/demo/i2c/mpu6xxx.lua

-- demo https://gitee.com/openLuat/LuatOS/blob/master/demo/i2c/mpu6xxx.lua
--- 模块功能:mpu6xxx
-- @module mpu6xxx
-- @author Dozingfiretruck
-- @license MIT
-- @copyright OpenLuat.com
-- @release 2020.12.22--支持mpu6500,mpu6050,mpu9250,icm2068g,icm20608d,自动判断器件id,只需要配置i2c id就可以local sys = require "sys"--pm.wake("mpu6xxx")local i2cid = 0 --i2cidlocal i2cslaveaddr
local deviceidlocal MPU6XXX_ADDRESS_AD0_LOW     =   0x68 -- address pin low (GND), default for InvenSense evaluation board
local MPU6XXX_ADDRESS_AD0_HIGH    =   0x69 -- address pin high (VCC)---器件通讯地址
local MPU6050_WHO_AM_I            =   0x68 -- mpu6050
local MPU6500_WHO_AM_I            =   0x70 -- mpu6500
local MPU9250_WHO_AM_I            =   0x71 -- mpu9250
local ICM20608G_WHO_AM_I          =   0xAF -- icm20608G
local ICM20608D_WHO_AM_I          =   0xAE -- icm20608Dlocal MPU6XXX_ACCEL_SEN           =   16384
local MPU6XXX_GYRO_SEN            =   1310local MPU60X0_TEMP_SEN            =   340
local MPU60X0_TEMP_OFFSET         =   36.5local MPU6500_TEMP_SEN            =   333.87
local MPU6500_TEMP_OFFSET         =   21---MPU6XXX所用地址
local MPU6XXX_RA_ACCEL_XOUT_H     =   0x3B
local MPU6XXX_RA_ACCEL_XOUT_L     =   0x3C
local MPU6XXX_RA_ACCEL_YOUT_H     =   0x3D
local MPU6XXX_RA_ACCEL_YOUT_L     =   0x3E
local MPU6XXX_RA_ACCEL_ZOUT_H     =   0x3F
local MPU6XXX_RA_ACCEL_ZOUT_L     =   0x40
local MPU6XXX_RA_TEMP_OUT_H       =   0x41
local MPU6XXX_RA_TEMP_OUT_L       =   0x42
local MPU6XXX_RA_GYRO_XOUT_H      =   0x43
local MPU6XXX_RA_GYRO_XOUT_L      =   0x44
local MPU6XXX_RA_GYRO_YOUT_H      =   0x45
local MPU6XXX_RA_GYRO_YOUT_L      =   0x46
local MPU6XXX_RA_GYRO_ZOUT_H      =   0x47
local MPU6XXX_RA_GYRO_ZOUT_L      =   0x48local MPU6XXX_RA_SMPLRT_DIV     =   0x19   --陀螺仪采样率,典型值:0x07(125Hz)
local MPU6XXX_RA_CONFIG         =   0x1A   --低通滤波频率,典型值:0x06(5Hz)
local MPU6XXX_RA_GYRO_CONFIG    =   0x1B   --陀螺仪自检及测量范围,典型值:0x18(不自检,2000deg/s)
local MPU6XXX_RA_ACCEL_CONFIG   =   0x1C   --加速计自检、测量范围及高通滤波频率,典型值:0x01(不自检,2G,5Hz)
local MPU6XXX_RA_FIFO_EN        =   0x23   --fifo使能
local MPU6XXX_RA_INT_PIN_CFG    =   0x37   --int引脚有效电平
local MPU6XXX_RA_INT_ENABLE     =   0x38   --中断使能
local MPU6XXX_RA_USER_CTRL      =   0x6A
local MPU6XXX_RA_PWR_MGMT_1     =   0x6B   --电源管理,典型值:0x00(正常启用)
local MPU6XXX_RA_PWR_MGMT_2     =   0x6C
local MPU6XXX_RA_WHO_AM_I       =   0x75
--器件ID检测
local function mpu6xxx_check()i2c.send(i2cid, MPU6XXX_ADDRESS_AD0_LOW, MPU6XXX_RA_WHO_AM_I)--读器件地址sys.wait(50)local revData = i2c.recv(i2cid, MPU6XXX_ADDRESS_AD0_LOW, 1)if revData:byte() ~= nil theni2cslaveaddr = MPU6XXX_ADDRESS_AD0_LOWelsei2c.send(i2cid, MPU6XXX_ADDRESS_AD0_HIGH, MPU6XXX_RA_WHO_AM_I)--读器件地址sys.wait(50)local revData = i2c.recv(i2cid, MPU6XXX_ADDRESS_AD0_HIGH, 1)if revData:byte() ~= nil theni2cslaveaddr = MPU6XXX_ADDRESS_AD0_HIGHelselog.info("i2c", "Can't find device")return 1endendi2c.send(i2cid, i2cslaveaddr, MPU6XXX_RA_WHO_AM_I)--读器件地址sys.wait(50)local revData = i2c.recv(i2cid, i2cslaveaddr, 1)log.info("Device i2c address is#:", revData:toHex())if revData:byte() == MPU6050_WHO_AM_I thendeviceid = MPU6050_WHO_AM_Ilog.info("Device i2c id is: MPU6050")elseif revData:byte() == MPU6500_WHO_AM_I thendeviceid = MPU6500_WHO_AM_Ilog.info("Device i2c id is: MPU6500")elseif revData:byte() == MPU9250_WHO_AM_I thendeviceid = MPU9250_WHO_AM_Ilog.info("Device i2c id is: MPU9250")elseif revData:byte() == ICM20608G_WHO_AM_I thendeviceid = ICM20608G_WHO_AM_Ilog.info("Device i2c id is: ICM20608G")elseif revData:byte() == ICM20608D_WHO_AM_I thendeviceid = ICM20608D_WHO_AM_Ilog.info("Device i2c id is: ICM20608D")elselog.info("i2c", "Can't find device")return 1endreturn 0
end--器件初始化
local function mpu6xxx_init()i2c.send(i2cid, i2cslaveaddr, {
    MPU6XXX_RA_PWR_MGMT_1, 0x80})--复位sys.wait(100)i2c.send(i2cid, i2cslaveaddr, {
    MPU6XXX_RA_PWR_MGMT_1, 0x00})--唤醒sys.wait(100)i2c.send(i2cid, i2cslaveaddr, {
    MPU6XXX_RA_SMPLRT_DIV, 0x07})--陀螺仪采样率,典型值:0x07(125Hz)i2c.send(i2cid, i2cslaveaddr, {
    MPU6XXX_RA_CONFIG, 0x06})--低通滤波频率,典型值:0x06(5Hz)i2c.send(i2cid, i2cslaveaddr, {
    MPU6XXX_RA_GYRO_CONFIG, 0x18})--陀螺仪自检及测量范围,典型值:0x18(不自检,2000deg/s)i2c.send(i2cid, i2cslaveaddr, {
    MPU6XXX_RA_ACCEL_CONFIG, 0x01})--加速计自检、测量范围及高通滤波频率,典型值:0x01(不自检,2G,5Hz)--i2c.send(i2cid, i2cslaveaddr, {MPU6XXX_RA_FIFO_EN, 0x00})--关闭fifo--i2c.send(i2cid, i2cslaveaddr, {MPU6XXX_RA_INT_ENABLE, 0x00})--关闭所有中断--i2c.send(i2cid, i2cslaveaddr, {MPU6XXX_RA_USER_CTRL, 0x00})--I2C主模式关闭i2c.send(i2cid, i2cslaveaddr, {
    MPU6XXX_RA_PWR_MGMT_1, 0x01})--设置x轴的pll为参考i2c.send(i2cid, i2cslaveaddr, {
    MPU6XXX_RA_PWR_MGMT_2, 0x00})--加速度计与陀螺仪开启log.info("i2c init_ok")
end
--获取温度的原始数据
local function mpu6xxx_get_temp_raw()i2c.send(i2cid, i2cslaveaddr,MPU6XXX_RA_TEMP_OUT_H)--获取的地址local buffer = i2c.recv(i2cid, i2cslaveaddr, 2)--获取2字节local temp = string.unpack(">h",buffer)--log.info("get_temp_raw type: "..type(buffer).." hex: "..buffer:toHex().." temp: "..temp)return temp or 0
end
--获取加速度计的原始数据
local function mpu6xxx_get_accel_raw()local accel={
    x=nil,y=nil,z=nil}i2c.send(i2cid, i2cslaveaddr,MPU6XXX_RA_ACCEL_XOUT_H)--获取的地址local x = i2c.recv(i2cid, i2cslaveaddr, 2)--获取6字节accel.x = string.unpack(">h",x)i2c.send(i2cid, i2cslaveaddr,MPU6XXX_RA_ACCEL_YOUT_H)--获取的地址local y = i2c.recv(i2cid, i2cslaveaddr, 2)--获取6字节accel.y = string.unpack(">h",y)i2c.send(i2cid, i2cslaveaddr,MPU6XXX_RA_ACCEL_ZOUT_H)--获取的地址local z = i2c.recv(i2cid, i2cslaveaddr, 2)--获取6字节accel.z = string.unpack(">h",z)--log.info("get_accel_raw: x="..x:toHex().." y="..y:toHex().." z="..z:toHex())return accel or 0
end
--获取陀螺仪的原始数据
local function mpu6xxx_get_gyro_raw()local gyro={
    x=nil,y=nil,z=nil}i2c.send(i2cid, i2cslaveaddr,MPU6XXX_RA_GYRO_XOUT_H)--获取的地址local x = i2c.recv(i2cid, i2cslaveaddr, 2)--获取6字节gyro.x = string.unpack(">h",x)i2c.send(i2cid, i2cslaveaddr,MPU6XXX_RA_GYRO_YOUT_H)--获取的地址local y = i2c.recv(i2cid, i2cslaveaddr, 2)--获取6字节gyro.y = string.unpack(">h",y)i2c.send(i2cid, i2cslaveaddr,MPU6XXX_RA_GYRO_ZOUT_H)--获取的地址local z = i2c.recv(i2cid, i2cslaveaddr, 2)--获取6字节gyro.z = string.unpack(">h",z)return gyro or 0
end
--获取温度的原始数据
local function mpu6xxx_get_temp()local temp=nillocal tmp = mpu6xxx_get_temp_raw()if deviceid == MPU6050_WHO_AM_I thentemp = tmp / MPU60X0_TEMP_SEN + MPU60X0_TEMP_OFFSETelsetemp = tmp / MPU6500_TEMP_SEN + MPU6500_TEMP_OFFSETendreturn temp
end
--获取加速度计的数据,单位: mg
local function mpu6xxx_get_accel()local accel={
    x=nil,y=nil,z=nil}local tmp = mpu6xxx_get_accel_raw()accel.x = tmp.x*1000/MPU6XXX_ACCEL_SENaccel.y = tmp.y*1000/MPU6XXX_ACCEL_SENaccel.z = tmp.z*1000/MPU6XXX_ACCEL_SENreturn accel
end
--获取陀螺仪的数据,单位: deg / 10s
local function mpu6xxx_get_gyro()local gyro={
    x=nil,y=nil,z=nil}local tmp = mpu6xxx_get_gyro_raw()gyro.x = tmp.x*100/MPU6XXX_GYRO_SENgyro.y = tmp.y*100/MPU6XXX_GYRO_SENgyro.z = tmp.z*100/MPU6XXX_GYRO_SENreturn gyro
end
local function mpu6xxx()sys.wait(4000)if i2c.setup(i2cid,i2c.SLOW) ~= i2c.SLOW thenlog.error("testI2c.init","fail")returnendif mpu6xxx_check()~= 0 thenreturnendmpu6xxx_init()while true dolocal t = mpu6xxx_get_temp()log.info("6050temptest", t)local a = mpu6xxx_get_accel()log.info("6050acceltest", "accel.x",a.x,"accel.y",a.y,"accel.z",a.z)local g = mpu6xxx_get_gyro()log.info("6050gyrotest", "gyro.x",g.x,"gyro.y",g.y,"gyro.z",g.z)sys.wait(1000)end
endsys.taskInit(mpu6xxx)

这版代码可以使用,不过注意一下需要将local去掉才能在main.lua中调用。

5.2 微调代码–youkai

基本上是把wendal的代码移植过来了,只微调了一点。
包了两个lua文件。
在这里插入图片描述

-- main.lua
--- 模块功能:gsensor- esp32_mpu6050
-- @module mpu6050
-- @author youkai
-- @release 2022.01.23-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = "esp32_mpu6050"
VERSION = "1.0.0"log.info("main", PROJECT, VERSION)-- sys库是标配
_G.sys = require("sys")require("esp32_st7735")
require("mpu6xxx_nolocal")-- ----------------setup start----------------------
-- 初始化屏幕
init_esp32_st7735 ()-- ----------------setup end-------------------------- ================main start================
sys.taskInit(function()init_mpu6050()      -- 有wait不能放在外面while 1 dolcd.clear()display_str()sys.wait(1500)lcd.clear()display_line()sys.wait(1500)get_mpu6xxx_value()end
end)
-- ================main end==================-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!
-- mpu6xxx_nolocal.lua
--- 模块功能:mpu6xxx
-- @module mpu6xxx
-- @author Dozingfiretruck
-- @license MIT
-- @copyright OpenLuat.com
-- @release 2020.12.22--支持mpu6500,mpu6050,mpu9250,icm2068g,icm20608d,自动判断器件id,只需要配置i2c id就可以sys = require "sys"--pm.wake("mpu6xxx")i2cid = 0 --i2cidi2cslaveaddr = MPU6XXX_ADDRESS_AD0_LOW
deviceid = MPU6050_WHO_AM_IMPU6XXX_ADDRESS_AD0_LOW     =   0x68 -- address pin low (GND), default for InvenSense evaluation board
MPU6XXX_ADDRESS_AD0_HIGH    =   0x69 -- address pin high (VCC)---器件通讯地址
MPU6050_WHO_AM_I            =   0x68 -- mpu6050
MPU6500_WHO_AM_I            =   0x70 -- mpu6500
MPU9250_WHO_AM_I            =   0x71 -- mpu9250
ICM20608G_WHO_AM_I          =   0xAF -- icm20608G
ICM20608D_WHO_AM_I          =   0xAE -- icm20608DMPU6XXX_ACCEL_SEN           =   16384
MPU6XXX_GYRO_SEN            =   1310MPU60X0_TEMP_SEN            =   340
MPU60X0_TEMP_OFFSET         =   36.5MPU6500_TEMP_SEN            =   333.87
MPU6500_TEMP_OFFSET         =   21---MPU6XXX所用地址
MPU6XXX_RA_ACCEL_XOUT_H     =   0x3B
MPU6XXX_RA_ACCEL_XOUT_L     =   0x3C
MPU6XXX_RA_ACCEL_YOUT_H     =   0x3D
MPU6XXX_RA_ACCEL_YOUT_L     =   0x3E
MPU6XXX_RA_ACCEL_ZOUT_H     =   0x3F
MPU6XXX_RA_ACCEL_ZOUT_L     =   0x40
MPU6XXX_RA_TEMP_OUT_H       =   0x41
MPU6XXX_RA_TEMP_OUT_L       =   0x42
MPU6XXX_RA_GYRO_XOUT_H      =   0x43
MPU6XXX_RA_GYRO_XOUT_L      =   0x44
MPU6XXX_RA_GYRO_YOUT_H      =   0x45
MPU6XXX_RA_GYRO_YOUT_L      =   0x46
MPU6XXX_RA_GYRO_ZOUT_H      =   0x47
MPU6XXX_RA_GYRO_ZOUT_L      =   0x48MPU6XXX_RA_SMPLRT_DIV     =   0x19   --陀螺仪采样率,典型值:0x07(125Hz)
MPU6XXX_RA_CONFIG         =   0x1A   --低通滤波频率,典型值:0x06(5Hz)
MPU6XXX_RA_GYRO_CONFIG    =   0x1B   --陀螺仪自检及测量范围,典型值:0x18(不自检,2000deg/s)
MPU6XXX_RA_ACCEL_CONFIG   =   0x1C   --加速计自检、测量范围及高通滤波频率,典型值:0x01(不自检,2G,5Hz)
MPU6XXX_RA_FIFO_EN        =   0x23   --fifo使能
MPU6XXX_RA_INT_PIN_CFG    =   0x37   --int引脚有效电平
MPU6XXX_RA_INT_ENABLE     =   0x38   --中断使能
MPU6XXX_RA_USER_CTRL      =   0x6A
MPU6XXX_RA_PWR_MGMT_1     =   0x6B   --电源管理,典型值:0x00(正常启用)
MPU6XXX_RA_PWR_MGMT_2     =   0x6C
MPU6XXX_RA_WHO_AM_I       =   0x75
--器件ID检测
function mpu6xxx_check()i2c.send(i2cid, MPU6XXX_ADDRESS_AD0_LOW, MPU6XXX_RA_WHO_AM_I)--读器件地址sys.wait(50)revData = i2c.recv(i2cid, MPU6XXX_ADDRESS_AD0_LOW, 1)if revData:byte() ~= nil theni2cslaveaddr = MPU6XXX_ADDRESS_AD0_LOWelsei2c.send(i2cid, MPU6XXX_ADDRESS_AD0_HIGH, MPU6XXX_RA_WHO_AM_I)--读器件地址sys.wait(50)revData = i2c.recv(i2cid, MPU6XXX_ADDRESS_AD0_HIGH, 1)if revData:byte() ~= nil theni2cslaveaddr = MPU6XXX_ADDRESS_AD0_HIGHelselog.info("i2c", "Can't find device")return 1endendi2c.send(i2cid, i2cslaveaddr, MPU6XXX_RA_WHO_AM_I)--读器件地址sys.wait(50)revData = i2c.recv(i2cid, i2cslaveaddr, 1)log.info("Device i2c address is#:", revData:toHex())if revData:byte() == MPU6050_WHO_AM_I thendeviceid = MPU6050_WHO_AM_Ilog.info("Device i2c id is: MPU6050")elseif revData:byte() == MPU6500_WHO_AM_I thendeviceid = MPU6500_WHO_AM_Ilog.info("Device i2c id is: MPU6500")elseif revData:byte() == MPU9250_WHO_AM_I thendeviceid = MPU9250_WHO_AM_Ilog.info("Device i2c id is: MPU9250")elseif revData:byte() == ICM20608G_WHO_AM_I thendeviceid = ICM20608G_WHO_AM_Ilog.info("Device i2c id is: ICM20608G")elseif revData:byte() == ICM20608D_WHO_AM_I thendeviceid = ICM20608D_WHO_AM_Ilog.info("Device i2c id is: ICM20608D")elselog.info("i2c", "Can't find device")return 1endreturn 0
end--器件初始化
function mpu6xxx_init()i2c.send(i2cid, i2cslaveaddr, {
    MPU6XXX_RA_PWR_MGMT_1, 0x80})--复位sys.wait(100)i2c.send(i2cid, i2cslaveaddr, {
    MPU6XXX_RA_PWR_MGMT_1, 0x00})--唤醒sys.wait(100)i2c.send(i2cid, i2cslaveaddr, {
    MPU6XXX_RA_SMPLRT_DIV, 0x07})--陀螺仪采样率,典型值:0x07(125Hz)i2c.send(i2cid, i2cslaveaddr, {
    MPU6XXX_RA_CONFIG, 0x06})--低通滤波频率,典型值:0x06(5Hz)i2c.send(i2cid, i2cslaveaddr, {
    MPU6XXX_RA_GYRO_CONFIG, 0x18})--陀螺仪自检及测量范围,典型值:0x18(不自检,2000deg/s)i2c.send(i2cid, i2cslaveaddr, {
    MPU6XXX_RA_ACCEL_CONFIG, 0x01})--加速计自检、测量范围及高通滤波频率,典型值:0x01(不自检,2G,5Hz)--i2c.send(i2cid, i2cslaveaddr, {MPU6XXX_RA_FIFO_EN, 0x00})--关闭fifo--i2c.send(i2cid, i2cslaveaddr, {MPU6XXX_RA_INT_ENABLE, 0x00})--关闭所有中断--i2c.send(i2cid, i2cslaveaddr, {MPU6XXX_RA_USER_CTRL, 0x00})--I2C主模式关闭i2c.send(i2cid, i2cslaveaddr, {
    MPU6XXX_RA_PWR_MGMT_1, 0x01})--设置x轴的pll为参考i2c.send(i2cid, i2cslaveaddr, {
    MPU6XXX_RA_PWR_MGMT_2, 0x00})--加速度计与陀螺仪开启log.info("i2c init_ok")
end
--获取温度的原始数据
function mpu6xxx_get_temp_raw()i2c.send(i2cid, i2cslaveaddr,MPU6XXX_RA_TEMP_OUT_H)--获取的地址buffer = i2c.recv(i2cid, i2cslaveaddr, 2)--获取2字节temp = string.unpack(">h",buffer)--log.info("get_temp_raw type: "..type(buffer).." hex: "..buffer:toHex().." temp: "..temp)return temp or 0
end
--获取加速度计的原始数据
function mpu6xxx_get_accel_raw()accel={
    x=nil,y=nil,z=nil}i2c.send(i2cid, i2cslaveaddr,MPU6XXX_RA_ACCEL_XOUT_H)--获取的地址x = i2c.recv(i2cid, i2cslaveaddr, 2)--获取6字节accel.x = string.unpack(">h",x)i2c.send(i2cid, i2cslaveaddr,MPU6XXX_RA_ACCEL_YOUT_H)--获取的地址y = i2c.recv(i2cid, i2cslaveaddr, 2)--获取6字节accel.y = string.unpack(">h",y)i2c.send(i2cid, i2cslaveaddr,MPU6XXX_RA_ACCEL_ZOUT_H)--获取的地址z = i2c.recv(i2cid, i2cslaveaddr, 2)--获取6字节accel.z = string.unpack(">h",z)--log.info("get_accel_raw: x="..x:toHex().." y="..y:toHex().." z="..z:toHex())return accel or 0
end
--获取陀螺仪的原始数据
function mpu6xxx_get_gyro_raw()gyro={
    x=nil,y=nil,z=nil}i2c.send(i2cid, i2cslaveaddr,MPU6XXX_RA_GYRO_XOUT_H)--获取的地址x = i2c.recv(i2cid, i2cslaveaddr, 2)--获取6字节gyro.x = string.unpack(">h",x)i2c.send(i2cid, i2cslaveaddr,MPU6XXX_RA_GYRO_YOUT_H)--获取的地址y = i2c.recv(i2cid, i2cslaveaddr, 2)--获取6字节gyro.y = string.unpack(">h",y)i2c.send(i2cid, i2cslaveaddr,MPU6XXX_RA_GYRO_ZOUT_H)--获取的地址z = i2c.recv(i2cid, i2cslaveaddr, 2)--获取6字节gyro.z = string.unpack(">h",z)return gyro or 0
end
--获取温度的原始数据
function mpu6xxx_get_temp()temp=niltmp = mpu6xxx_get_temp_raw()if deviceid == MPU6050_WHO_AM_I thentemp = tmp / MPU60X0_TEMP_SEN + MPU60X0_TEMP_OFFSETelsetemp = tmp / MPU6500_TEMP_SEN + MPU6500_TEMP_OFFSETendreturn temp
end
--获取加速度计的数据,单位: mg
function mpu6xxx_get_accel()accel={
    x=nil,y=nil,z=nil}tmp = mpu6xxx_get_accel_raw()accel.x = tmp.x*1000/MPU6XXX_ACCEL_SENaccel.y = tmp.y*1000/MPU6XXX_ACCEL_SENaccel.z = tmp.z*1000/MPU6XXX_ACCEL_SENreturn accel
end
--获取陀螺仪的数据,单位: deg / 10s
function mpu6xxx_get_gyro()gyro={
    x=nil,y=nil,z=nil}tmp = mpu6xxx_get_gyro_raw()gyro.x = tmp.x*100/MPU6XXX_GYRO_SENgyro.y = tmp.y*100/MPU6XXX_GYRO_SENgyro.z = tmp.z*100/MPU6XXX_GYRO_SENreturn gyro
end
function check_i2c()if i2c.setup(i2cid,i2c.SLOW) ~= i2c.SLOW thenlog.error("testI2c.init","fail")returnendif mpu6xxx_check()~= 0 thenreturnend
end
function get_mpu6xxx_value()t = mpu6xxx_get_temp()log.info("6050temptest", t)a = mpu6xxx_get_accel()log.info("6050acceltest", "accel.x",a.x,"accel.y",a.y,"accel.z",a.z)g = mpu6xxx_get_gyro()log.info("6050gyrotest", "gyro.x",g.x,"gyro.y",g.y,"gyro.z",g.z)
endfunction init_mpu6050()log.info("init_mpu6050 start---------------")sys.wait(1000)check_i2c()mpu6xxx_init()log.info("init_mpu6050 end---------------")
end
-- esp32_st7735.lua
-- 1. display: st735
function init_esp32_st7735 ()-- spi test ok-- spi clk is io2, cs is IO7spi_lcd = spi.deviceSetup(2,7,0,0,8,20000000,spi.MSB,1,1)log.info("SPI OK")log.info("lcd.init",-- st7735 + esp32lcd.init("st7735",{
    port = "device",pin_dc = 6, pin_pwr = 11,pin_rst = 10,direction = 0,w = 128,h = 160,xoffset = 0,yoffset = 0},spi_lcd))log.info("LCD OK")
endfunction display_line ()log.info("lcd.drawLine", lcd.drawLine(20,20,150,20,0x001F))log.info("lcd.drawRectangle", lcd.drawRectangle(20,40,120,70,0xF800))log.info("lcd.drawCircle", lcd.drawCircle(50,50,20,0x0CE0))-- sys.wait(1500)log.info("display demo")
endfunction display_str ()lcd.setFont(lcd.font_opposansm12)lcd.drawStr(40,10,"drawStr")lcd.setFont(lcd.font_opposansm16_chinese)lcd.drawStr(40,40,"drawStr测试")
end

将以上三个lua文件刷入,可以正常运行了。

在这里插入图片描述

结果

在这里插入图片描述
目前屏幕和gsensor可以正常使用了,后续再导入算法。