RflySimSDK v4.01
RflySimSDK说明文档
载入中...
搜索中...
未找到
rfly_unitree.h
1#pragma once
2
4#include <math.h>
5#include <stdio.h>
6#include <string.h>
7#include <vector>
8#include <algorithm> // std::transform
9#include <cctype>
10#include "rfly_data.h"
11#include "rfly_udp.h"
12
13#define MAX_BUF_LEN 300 // More than max mavlink msg len
14
15enum UnitreeDogMode{
16 Damp = 1,
17 BalanceStand,
18 StopMove,
19 PStandUp,
20 StandDown,
21 RecoveryStand,
22 Sit,
23 RiseSit,
24 SwitchGait,
25 BodyHeight,
26 FootRaiseHeight,
27 SpeedLevel,
28 Hello,
29 Stretch,
30 SwitchJoystick,
31 ContinuousGait,
32 Wallow,
33 Pose,
34 Scrape,
35 FrontFlip,
36 FrontJump,
37 FrontPounce,
38 Dance
39};
40
41class RflyUnitree {
42 public:
43
44 RflySimData data;
45 UDPServer udp;
46
47 //MavHealthData phm;
48 std::string TargetIP;
49 int TargetPort;
50 int CopterID;
51 int RecvPort;
52 std::string RecvIP;
53 char buf[MAX_BUF_LEN+1];
54 bool isUpdate=false;
55 bool isRcc=false;
56 double GpsOrin[3]={0};
57
58
59 int SelfCheckMainState=0;
60 int SelfCheckSubState=0;
61 double lastTime=0;
62
63
64 int offCtrlState=0;
65 double offLastTime=0;
66
67 //SOut2Simulator m_show3d;
68 uint64_t m_start_time = 0;
69 GenericInOut28 m_cmd;
70 GenericInOut28 m_recv;
71
72 RflyUnitree(){
73 m_cmd.ioSilInts[0] = 5;
74 }
75 ~RflyUnitree(){
76
77 }
78
79 bool RecvNoblock(){
80 int retry = 0;
81 while(true){ // Read all data in the upd buf zone
82 int recvlen=udp.RecvNoblock(buf,RecvIP,RecvPort,MAX_BUF_LEN);
83
84 // Five times has no useful message, will break
85 if (udp.needFilter && (RecvIP != TargetIP)){
86 retry++;
87 if (retry > 5)
88 break;
89 continue;
90 }
91 retry = 0;
92
93 if(recvlen>0){
94 onUdpMessage((uint8_t*)buf, recvlen);
95 }else{
96 break;
97 }
98 }
99 return isUpdate;
100 }
101
102 void onUdpMessage(uint8_t *buf, int recvlen){
103 if(recvlen==112){
104 unpackStruct(buf, m_recv);
105 }
106 }
107
108 void SendMsg(){
109 unsigned int length = packStruct(m_cmd, (uint8_t*)buf);
110 if(length>0){
111 buf[length]='0';
112 udp.SendTo((const char *)buf,length,TargetIP,TargetPort);
113 // std::cout << "send message to TargetIP \t"<<TargetIP << "TargetPort\t" << TargetPort << std::endl;
114 }
115 }
116
117 void setBit(int &num, int n) {
118 // 生成掩码:第n位为1,其余位为0
119 int mask = 1 << n;
120 // 执行按位或操作,将第n位设为1
121 num = num | mask;
122 }
123
124
125 void SendSetAction(int mode)
126 {
127 m_cmd.ioSilInts[1] = 0;
128 setBit(m_cmd.ioSilInts[1], 0);
129 setBit(m_cmd.ioSilInts[1], 5);
130 m_cmd.ioSilInts[2] = mode;
131 SendMsg();
132 }
133
134 void SendMove(float vx, float vy, float yaw_rate)
135 {
136 m_cmd.ioSilInts[1] = 0;
137 setBit(m_cmd.ioSilInts[1], 0);
138 setBit(m_cmd.ioSilInts[1], 19);
139 m_cmd.ioSilFloats[3] = vx;
140 m_cmd.ioSilFloats[4] = vy;
141 m_cmd.ioSilFloats[14] = yaw_rate;
142 SendMsg();
143 }
144
145 bool isBitTrue(uint32_t value, int bitIndex) {
146 return (value & (1U << bitIndex)) != 0;
147 }
148
149 // input vector, input dim, sim time (s)
150 void SendMavlinkReal(double **u, double t){
151
152 int cmdBitmap = (int)*u[1];
153 int typeMask = (int)*u[2];
154
155 if (isBitTrue(cmdBitmap, 0)){
156 if (isBitTrue(cmdBitmap, 5)){
157 // std::cout << "SendSetAction: " <<typeMask << std::endl;
158 SendSetAction(typeMask);
159 }
160 }
161 if (isBitTrue(cmdBitmap, 19)){
162 // std::cout << "SendMove: " <<(float)*u[11] << "\t" << (float)*u[12] << "\t" << (float)*u[22] << std::endl;
163 SendMove((float)*u[11], (float)*u[12], (float)*u[22]);
164 }
165 }
166
167};
定义 rfly_udp.h:558
定义 rfly_udp.h:195
定义 rfly_data.h:93