GIF 2023-2-17 13-15-11.gif

首先,在esp32上连接MPU6050,并下载DMP示例程序(删除多余文本print,只print RPY数据)。

参考这个:
https://blog.nbzch.cn:5081/index.php/archives/123/

使用虚拟串口连接,匿名上位机用com1,esp32用com6,python获取com6的数据,转换后发送给com1

参考代码:


#匿名上位机V7串口控制实验
#输入ROL、PIT、YAW(姿态角,依次为横滚、俯仰、航向,精确到 0.01)
#串口发送数据帧至匿名上位机V7
#作者:玩机器人的赵老师

import serial
import time
#自定义函数——姿态角设定
def rpy_set(ROL_value,PIT_value,YAW_value):

    ROL_H = (ROL_value >> 8) & 0xFF #获取高八位
    ROL_L = ROL_value & 0xFF        #获取低八位
    PIT_H = (PIT_value >> 8) & 0xFF
    PIT_L = PIT_value & 0xFF
    YAW_H = (YAW_value >> 8) & 0xFF
    YAW_L = YAW_value & 0xFF
    rpy_data=[0xAA, 0xFF, 0x03, 0x07,ROL_L,ROL_H,PIT_L,PIT_H,YAW_L,YAW_H,0x00] 
    
    SC = 0  #和校验
    AC = 0  #附加校验
    for i in rpy_data:
        SC += i
        AC +=SC
    rpy_data.append(SC & 0xFF)
    rpy_data.append(AC & 0xFF)
    ser.write(rpy_data)#输出功率设定值

ser = serial.Serial("com2", 115200)   # 选择串口,并设置波特率
ser1 = serial.Serial('com6', 115200, timeout=1)  # 打开串口
if ser1.is_open:
    print("port open success")
else:
    print("port open failed")
    
def main():
    ROL = 0
    PIT = 0
    YAW = 0
    ser1.write(1)
    time.sleep(5)
    while True:
        data = ser1.readline().decode().strip()       # 读取一行数据并解码
        numbers = data.split('\t')                   # 将数据按制表符分割成三个数
        YAW,ROL,PIT  = map(float, numbers)            # 将三个数转换为浮点数
        rpy_set(int(100*ROL),int(100*PIT),int(100*YAW))
        print(YAW,ROL,PIT)

if __name__=='__main__':
    try:
        main()
    except KeyboardInterrupt:
        if ser != None:
            ser.close()

发表评论