Micropython 飞控 驱动 6.匿名上位机v4.34协议

一、介绍
通过连接2.4G的串口无线发射模块,使用匿名上位机的协议,将无人机的各种信息发送到电脑显示
(目前协议未完全移植)
(使用时需要关闭上位机的校验功能)
二、代码

# 匿名上位机 移植上位机、协议版本v4.34
# 移植部分功能
# 关闭了数据的校验功能,提高发送速度
class ANO():
    def  __init__(self,uart):
        self.writechar = uart.writechar
        self.BYTE0 = lambda x : (x>>0)&0xff
        self.BYTE1 = lambda x : (x>>8)&0xff
        self.BYTE2 = lambda x : (x>>16)&0xff
        self.BYTE3 = lambda x : (x>>24)&0xff
        pass

    # 发送数据
    def ANO_Send_Data(self,data):
        for v in data:
            self.writechar(v)

    #send 数据
    def cs(self,data_to_send):
        _cnt = len(data_to_send)
        data_to_send[3] = _cnt - 4
        data_to_send.append(0) # 需要校验功能的将这一行改了就行
        self.ANO_Send_Data(data_to_send)

    # 飞机姿态 高度 飞行模式 是否解锁
    def ANO_DT_Send_Status(self,angle_rol, angle_pit, angle_yaw, alt, fly_model, armed):


        _cnt = 0
        _temp = 0
        _temp2 = alt
        data_to_send  =  []
        data_to_send.append(0xAA)
        data_to_send.append(0xAA)
        data_to_send.append(0x01)
        data_to_send.append(0)

        _temp = int(angle_rol * 100)
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))
        _temp = int(angle_pit * 100)
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))
        _temp = int(angle_yaw * 100)
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))

        data_to_send.append(self.BYTE3(_temp2))
        data_to_send.append(self.BYTE2(_temp2))
        data_to_send.append(self.BYTE1(_temp2))
        data_to_send.append(self.BYTE0(_temp2))

        data_to_send.append(fly_model)

        data_to_send.append(armed)
        self.cs(data_to_send)

    # PID信息
    def ANO_DT_Send_PID(self,group, p1_p, p1_i, p1_d, p2_p, p2_i, p2_d, p3_p, p3_i, p3_d):
        _cnt = 0
        _temp = 0
        data_to_send  =  []
        data_to_send.append(0xAA)
        data_to_send.append(0xAA)
        data_to_send.append(0x10 + group -1)
        data_to_send.append(0)

        _temp = p1_p  * 1000
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))
        _temp = p1_i * 1000
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))
        _temp = p1_d * 1000
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))

        _temp = p2_p * 1000
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))
        _temp = p2_i * 1000
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))
        _temp = p2_d * 1000
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))

        _temp = p3_p * 1000
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))
        _temp = p3_i * 1000
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))
        _temp = p3_d * 1000
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))

        self.cs(data_to_send)

    # 传感器数据
    def ANO_DT_Send_Senser(self,a_x, a_y, a_z, g_x, g_y, g_z, m_x, m_y, m_z):
        _cnt = 0
        _temp = 0
        data_to_send = []
        data_to_send.append(0xAA)
        data_to_send.append(0xAA)
        data_to_send.append(0x02)
        data_to_send.append(0)

        _temp = a_x
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))
        _temp = a_y
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))
        _temp = a_z
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))

        _temp = g_x
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))
        _temp = g_y
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))
        _temp = g_z
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))

        _temp = m_x
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))
        _temp = m_y
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))
        _temp = m_z
        data_to_send.append(self.BYTE1(_temp))
        data_to_send.append(self.BYTE0(_temp))

        self.cs(data_to_send)

    # 电机pwm数据 0-1000
    def ANO_DT_Send_MotoPWM(self, m_1, m_2, m_3, m_4, m_5, m_6, m_7, m_8):
        _cnt = 0

        data_to_send = []
        data_to_send.append(0xAA)
        data_to_send.append(0xAA)
        data_to_send.append(0x06)
        data_to_send.append(0)

        data_to_send.append(self.BYTE1(m_1))
        data_to_send.append(self.BYTE0(m_1))
        data_to_send.append(self.BYTE1(m_2))
        data_to_send.append(self.BYTE0(m_2))
        data_to_send.append(self.BYTE1(m_3))
        data_to_send.append(self.BYTE0(m_3))
        data_to_send.append(self.BYTE1(m_4))
        data_to_send.append(self.BYTE0(m_4))
        data_to_send.append(self.BYTE1(m_5))
        data_to_send.append(self.BYTE0(m_5))
        data_to_send.append(self.BYTE1(m_6))
        data_to_send.append(self.BYTE0(m_6))
        data_to_send.append(self.BYTE1(m_7))
        data_to_send.append(self.BYTE0(m_7))
        data_to_send.append(self.BYTE1(m_8))
        data_to_send.append(self.BYTE0(m_8))

        self.cs(data_to_send)

你可能感兴趣的:(Micropython 飞控 驱动 6.匿名上位机v4.34协议)