首页 > 编程语言 >python 串口读取IMU

python 串口读取IMU

时间:2023-05-06 20:12:29浏览次数:38  
标签:acc angle python gyro datahex Bytenum IMU 串口 data

# coding:UTF-8
# Version: V1.0.1
import serial

ACCData = [0.0]*8
GYROData = [0.0]*8
AngleData = [0.0]*8
FrameState = 0  # What is the state of the judgment
Bytenum = 0  # Read the number of digits in this paragraph
CheckSum = 0  # Sum check bit

a = [0.0]*3
w = [0.0]*3
Angle = [0.0]*3


def DueData(inputdata):  # New core procedures, read the data partition, each read to the corresponding array 
    global FrameState    # Declare global variables
    global Bytenum
    global CheckSum
    global acc
    global gyro
    global Angle
    for data in inputdata:  # Traversal the input data
        if FrameState == 0:  # When the state is not determined, enter the following judgment
            if data == 0x55 and Bytenum == 0:  # When 0x55 is the first digit, start reading data and increment bytenum
                CheckSum = data
                Bytenum = 1
                continue
            elif data == 0x51 and Bytenum == 1:  # Change the frame if byte is not 0 and 0x51 is identified
                CheckSum += data
                FrameState = 1
                Bytenum = 2
            elif data == 0x52 and Bytenum == 1:
                CheckSum += data
                FrameState = 2
                Bytenum = 2
            elif data == 0x53 and Bytenum == 1:
                CheckSum += data
                FrameState = 3
                Bytenum = 2
        elif FrameState == 1:  # acc

            if Bytenum < 10:            # Read 8 data
                ACCData[Bytenum-2] = data  # Starting from 0
                CheckSum += data
                Bytenum += 1
            else:
                if data == (CheckSum & 0xff):  # verify check bit
                    acc = get_acc(ACCData)
                CheckSum = 0  # Each data is zeroed and a new circular judgment is made
                Bytenum = 0
                FrameState = 0
        elif FrameState == 2:  # gyro

            if Bytenum < 10:
                GYROData[Bytenum-2] = data
                CheckSum += data
                Bytenum += 1
            else:
                if data == (CheckSum & 0xff):
                    gyro = get_gyro(GYROData)
                CheckSum = 0
                Bytenum = 0
                FrameState = 0
        elif FrameState == 3:  # angle

            if Bytenum < 10:
                AngleData[Bytenum-2] = data
                CheckSum += data
                Bytenum += 1
            else:
                if data == (CheckSum & 0xff):
                    Angle = get_angle(AngleData)
                    result = acc+gyro+Angle
                    print(
                        "acc:%10.3f %10.3f %10.3f \ngyro:%10.3f %10.3f %10.3f \nangle:%10.3f %10.3f %10.3f" % result)
                CheckSum = 0
                Bytenum = 0
                FrameState = 0


def get_acc(datahex):
    axl = datahex[0]
    axh = datahex[1]
    ayl = datahex[2]
    ayh = datahex[3]
    azl = datahex[4]
    azh = datahex[5]
    k_acc = 16.0
    acc_x = (axh << 8 | axl) / 32768.0 * k_acc
    acc_y = (ayh << 8 | ayl) / 32768.0 * k_acc
    acc_z = (azh << 8 | azl) / 32768.0 * k_acc
    if acc_x >= k_acc:
        acc_x -= 2 * k_acc
    if acc_y >= k_acc:
        acc_y -= 2 * k_acc
    if acc_z >= k_acc:
        acc_z -= 2 * k_acc
    return acc_x, acc_y, acc_z


def get_gyro(datahex):
    wxl = datahex[0]
    wxh = datahex[1]
    wyl = datahex[2]
    wyh = datahex[3]
    wzl = datahex[4]
    wzh = datahex[5]
    k_gyro = 2000.0
    gyro_x = (wxh << 8 | wxl) / 32768.0 * k_gyro
    gyro_y = (wyh << 8 | wyl) / 32768.0 * k_gyro
    gyro_z = (wzh << 8 | wzl) / 32768.0 * k_gyro
    if gyro_x >= k_gyro:
        gyro_x -= 2 * k_gyro
    if gyro_y >= k_gyro:
        gyro_y -= 2 * k_gyro
    if gyro_z >= k_gyro:
        gyro_z -= 2 * k_gyro
    return gyro_x, gyro_y, gyro_z


def get_angle(datahex):
    rxl = datahex[0]
    rxh = datahex[1]
    ryl = datahex[2]
    ryh = datahex[3]
    rzl = datahex[4]
    rzh = datahex[5]
    k_angle = 180.0
    angle_x = (rxh << 8 | rxl) / 32768.0 * k_angle
    angle_y = (ryh << 8 | ryl) / 32768.0 * k_angle
    angle_z = (rzh << 8 | rzl) / 32768.0 * k_angle
    if angle_x >= k_angle:
        angle_x -= 2 * k_angle
    if angle_y >= k_angle:
        angle_y -= 2 * k_angle
    if angle_z >= k_angle:
        angle_z -= 2 * k_angle
    return angle_x, angle_y, angle_z


if __name__ == '__main__':
    port = '/dev/ttyUSB0' # USB serial port #/dev/ttyS3
    baud = 9600   # Same baud rate as the INERTIAL navigation module
    ser = serial.Serial(port, baud, timeout=0.5)
    print("Serial is Opened:", ser.is_open)
    while(1):
        datahex = ser.read(33)
        DueData(datahex)

  

标签:acc,angle,python,gyro,datahex,Bytenum,IMU,串口,data
From: https://www.cnblogs.com/oftenlin/p/17378353.html

相关文章

  • python - mock 处理
    前言在现在的软件开发过程中,特别是app的部分,需要的很多数据以及内容,都是来自server端的API,但是不能保证在客户端开发的时候,api在server端已经开发完成,专门等着前端来调用,理想的情况是前端在开发的时候,已经有人写好了接口,直接调用就可以了。但往往是页面写好了,后端接口没有写好,这......
  • PAT Advanced 1007. Maximum Subsequence Sum
    PATAdvanced1007.MaximumSubsequenceSum1.ProblemDescription:Givenasequenceof\(K\)integers{\(N_1,N_2,...,N_K\)}.Acontinuoussubsequenceisdefinedtobe{\(N_i,N_{i+1},...,N_j\)}where\(1≤i≤j≤K\).TheMaximumSubsequencei......
  • python 制作奥运五环 (turtle模块)
     importturtle#第一个圈turtle.width(10)#画笔宽度10turtle.color("black")#画笔的颜色turtle.circle(50)#半径50的圆#第二个圈turtle.penup()#抬笔turtle.goto(110,0)#去到坐标(110,0)turtle.pendown()#放笔turtle.width(10)turt......
  • 使用Python扩展PAM(part 1)
    0、使用Python扩展PAM实现一些额外的功能,比如ssh、vpn等二次验证。1、准备工具实现这个功能需要用到pam_python pam-python 注意!是pam-python不是python_pamPam-python是一个开源的Python模块,用于编写自定义PAM模块。Pam-python使用Python脚本来扩展PAM功能。它使用Cython和Pyt......
  • python给onnet上报数据
      #!/usr/bin/envpython#-*-coding:utf-8-*-#@Time:2021/2/2423:01#@Author:LiShan#@Email:[email protected]#@File:OneNet_Post.py#@Note:https://blog.csdn.net/lishan132/article/details/114044902importurllib.requesti......
  • python中怎么输出字典中字典的值
    #T要求输出'./a/text()url_gglb_xpath_dic={'中国邮政储蓄银行':{'发行公告':['https://www.psbc.com/cn/grfw/tzlc/lc/ywgg/lcxsgg/index_页码.html','https://www.psbc.com/cn/grfw/tzlc/lc/ywgg/lcxsgg&#......
  • python中判断多个元素是否在列表中
    判断单个元素是否在列表中时可以通过in>>>'a'in['a','b','c']True但如果是多个元素,就不能通过in进行判断此时我们可以通过集合中的issubset和issuperset方法顾名思义,issubset是判断某集合是否是另外一个集合的子集,issuperset是判断某集合是否是另外一个集合的超集>>>......
  • python操作elasticsearch 记录
    一、环境Elasticsearch5.x, python3.6, 注意Elasticsearch不同版本的区别,比如以下几条:_id 字段变为 id 字段:在Elasticsearch5中,文档的唯一标识符使用 _id 字段。而在Elasticsearch6和7中,唯一标识符改为使用 id 字段。在Elasticsearch7中,_id字段被重新引......
  • lora串口通信实验
    记录一下lora串口通信的测试用的亿佰特E32170T30D(230的大概率也是一样的,后面测一下)+STM32F103(正点原子的mini,后面会改成小的stm32板子方便使用)+lora的TTL工具E15-USB-T2+电脑因为亿佰特的引脚很多,对于M0M1还有AUX这种配置工作状态的引脚先通过上位机软件配置好,然后就可以把lor......
  • 番外篇:分享一道用Python基础+蒙特卡洛算法实现排列组合的题目(附源码)
    今日鸡汤夕阳无限好,只是近黄昏。    大家好,我是Python进阶者。    是不是觉得很诧异?明明上周刚发布了这篇:分享一道用Python基础+蒙特卡洛算法实现排列组合的题目(附源码),今天又来一篇,名曰番外篇!其实今天是想给大家分享【......