Unitree 4. 遥控器解码

 查看官方库说明:

/*****************************************************************
Copyright (c) 2020, Unitree Robotics.Co.Ltd. All rights reserved.
*****************************************************************/
#ifndef _UNITREE_LEGGED_JOYSTICK_H_
#define _UNITREE_LEGGED_JOYSTICK_H_

#include <stdint.h>
// 16b
typedef union {
  struct {
    uint8_t R1 : 1;
    uint8_t L1 : 1;
    uint8_t start : 1;
    uint8_t select : 1;
    uint8_t R2 : 1;
    uint8_t L2 : 1;
    uint8_t F1 : 1;
    uint8_t F2 : 1;
    uint8_t A : 1;
    uint8_t B : 1;
    uint8_t X : 1;
    uint8_t Y : 1;
    uint8_t up : 1;
    uint8_t right : 1;
    uint8_t down : 1;
    uint8_t left : 1;
  } components;
  uint16_t value;
} xKeySwitchUnion;

// 40 Byte (now used 24B)
typedef struct {
  uint8_t head[2];
  xKeySwitchUnion btn;
  float lx;
  float rx;
  float ry;
  float L2;
  float ly;

  uint8_t idle[16];
} xRockerBtnDataStruct;

#endif  // _UNITREE_LEGGED_JOYSTICK_H_

首先可以确定遥控器有以下按键:

class wirelessRemote:
    buf = []
    l1_switch = 0
    l2_switch = 0
    r1_switch = 0
    r2_switch = 0
    start_switch = 0
    select_switch = 0
    f1_switch = 0
    f2_switch = 0
    left_cross_switch = [0, 0, 0, 0]
    right_cross_switch = [0, 0, 0, 0]
    left_stick = [0., 0.]
    right_stick = [0., 0.]

读取state.wirelessRemote可以得到一个40字节的数据

[85, 81, 0, 0, 64, 49, 173, 186, 30, 155, 58, 186, 97, 226, 125, 187, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0]

0,1两位为帧头,2到24位为数据帧,后续为空数据,最后两位应该是校验和。但是我没找到对应的校验方式。

其余位的数据解析如下:

        if state.wirelessRemote[:2] == [85, 81]:
            buf = bytes(state.wirelessRemote)
            ps.buf = buf

            switch_data = struct.unpack("BB", buf[2:4])
            ps.l1_switch = (switch_data[0] >> 1) & 1
            ps.l2_switch = (switch_data[0] >> 5) & 1
            ps.r1_switch = (switch_data[0] >> 0) & 1
            ps.r2_switch = (switch_data[0] >> 4) & 1
            ps.start_switch = (switch_data[0] >> 2) & 1
            ps.select_switch = (switch_data[0] >> 3) & 1
            ps.f1_switch = (switch_data[0] >> 6) & 1
            ps.f2_switch = (switch_data[0] >> 7) & 1
            ps.left_cross_switch = [(switch_data[1] >> 4) & 1, (switch_data[1] >> 6) & 1, (switch_data[1] >> 5) & 1, (switch_data[1] >> 7) & 1]
            ps.right_cross_switch = [(switch_data[1] >> 3) & 1, (switch_data[1] >> 0) & 1, (switch_data[1] >> 1) & 1, (switch_data[1] >> 1) & 1]

            ps.left_stick[0] = struct.unpack('<f', buf[4:8])[0]
            ps.right_stick[0] = struct.unpack('<f', buf[8:12])[0]
            ps.right_stick[1] = struct.unpack('<f', buf[12:16])[0]
            ps.left_stick[1] = struct.unpack('<f', buf[20:24])[0]

            # print(f"Stamp: {stamp}")
            # print(f"Left Stick: {ps.left_stick[0]:.2f}, {ps.left_stick[1]:.2f}")
            # print(f"Right Stick: {ps.right_stick[0]:.2f}, {ps.right_stick[1]:.2f}")
            # print(f"Switches: {ps.l1_switch}, {ps.l2_switch}, {ps.r1_switch}, {ps.r2_switch}")
            # print(f"Switches: {ps.left_cross_switch}, {ps.right_cross_switch}")
            # print()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值