-
Notifications
You must be signed in to change notification settings - Fork 10
/
RMTT_Protocol.cpp
362 lines (317 loc) · 7.31 KB
/
RMTT_Protocol.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Copyright (C) 2020 DJI.
*
* SPDX-License-Identifier: BSD-3-Clause-Clear
*
* Change Logs:
* Date Author Notes
* 2020-08-25 robomaster first version
*/
#include "RMTT_Libs.h"
#include "RMTT_Protocol.h"
static uint16_t sdk_time = 0;
RMTT_Protocol::RMTT_Protocol()
{
}
RMTT_Protocol::RMTT_Protocol(uint16_t time)
{
sdk_time = time;
}
RMTT_Protocol::~RMTT_Protocol()
{
}
void RMTT_Protocol::SendCMD(char *cmd)
{
Serial1.printf("[TELLO] %s", cmd);
if(sdk_time)
{
delay(sdk_time);
}
}
void RMTT_Protocol::SDKOn()
{
SendCMD((char*)"command");
}
void RMTT_Protocol::SDKOff()
{
}
void RMTT_Protocol::TakeOff()
{
SendCMD((char*)"takeoff");
}
void RMTT_Protocol::Land()
{
SendCMD((char*)"land");
}
void RMTT_Protocol::Emergency()
{
SendCMD((char*)"emergency");
}
void RMTT_Protocol::Up(int16_t x)
{
char s[20] = {0};
snprintf(s, sizeof(s), "up %d", x);
SendCMD((char*)s);
}
void RMTT_Protocol::Down(int16_t x)
{
char s[20] = {0};
snprintf(s, sizeof(s), "down %d", x);
SendCMD((char*)s);
}
void RMTT_Protocol::Left(int16_t x)
{
char s[20] = {0};
snprintf(s, sizeof(s), "left %d", x);
SendCMD((char*)s);
}
void RMTT_Protocol::Right(int16_t x)
{
char s[20] = {0};
snprintf(s, sizeof(s), "right %d", x);
SendCMD((char*)s);
}
void RMTT_Protocol::Forward(int16_t x)
{
char s[20] = {0};
snprintf(s, sizeof(s), "forward %d", x);
SendCMD((char*)s);
}
void RMTT_Protocol::Back(int16_t x)
{
char s[20] = {0};
snprintf(s, sizeof(s), "back %d", x);
SendCMD((char*)s);
}
void RMTT_Protocol::CW(uint16_t x)
{
char s[20] = {0};
snprintf(s, sizeof(s), "cw %d", x);
SendCMD((char*)s);
}
void RMTT_Protocol::CCW(uint16_t x)
{
char s[20] = {0};
snprintf(s, sizeof(s), "ccw %d", x);
SendCMD((char*)s);
}
void RMTT_Protocol::Flip(char x)
{
char s[20] = {0};
snprintf(s, sizeof(s), "flip %c", x);
SendCMD((char*)s);
}
void RMTT_Protocol::Go(int16_t x, int16_t y, int16_t z, uint16_t speed)
{
char s[40] = {0};
snprintf(s, sizeof(s), "go %d %d %d %d", x, y, z, speed);
SendCMD((char*)s);
}
void RMTT_Protocol::Go(int16_t x, int16_t y, int16_t z, uint16_t speed, char *mid)
{
char s[40] = {0};
snprintf(s, sizeof(s), "go %d %d %d %d %s", x, y, z, speed, mid);
SendCMD((char*)s);
}
void RMTT_Protocol::Stop()
{
SendCMD((char*)"stop");
}
void RMTT_Protocol::Curve(int16_t x1, int16_t y1, int16_t z1, int16_t x2, int16_t y2, int16_t z2, uint16_t speed)
{
char s[60] = {0};
snprintf(s, sizeof(s), "curve %d %d %d %d %d %d %d", x1, y1, z1, x2, y2, z2, speed);
SendCMD((char*)s);
}
void RMTT_Protocol::Curve(int16_t x1, int16_t y1, int16_t z1, int16_t x2, int16_t y2, int16_t z2, uint16_t speed, char *mid)
{
char s[60] = {0};
snprintf(s, sizeof(s), "curve %d %d %d %d %d %d %d %s", x1, y1, z1, x2, y2, z2, speed, mid);
SendCMD((char*)s);
}
void RMTT_Protocol::Jump(int16_t x, int16_t y, int16_t z, uint16_t speed, int16_t yaw, char *mid1, char *mid2)
{
char s[60] = {0};
snprintf(s, sizeof(s), "jump %d %d %d %d %d %s %s", x, y, z, speed, yaw, mid1, mid2);
SendCMD((char*)s);
}
void RMTT_Protocol::SetSpeed(int16_t x)
{
char s[20] = {0};
snprintf(s, sizeof(s), "speed %d", x);
SendCMD((char*)s);
}
void RMTT_Protocol::SetRC(int16_t a, int16_t b, int16_t c, int16_t d)
{
char s[40] = {0};
snprintf(s, sizeof(s), "rc %d %d %d %d", a, b, c, d);
SendCMD((char*)s);
}
void RMTT_Protocol::SetMon()
{
SendCMD((char*)"mon");
}
void RMTT_Protocol::SetMoff()
{
SendCMD((char*)"moff");
}
void RMTT_Protocol::SetMdirection(uint8_t x)
{
char s[20] = {0};
snprintf(s, sizeof(s), "mdirection %d", x);
SendCMD((char*)s);
}
void RMTT_Protocol::ReadSpeed()
{
SendCMD((char*)"speed?");
}
void RMTT_Protocol::ReadBattery()
{
SendCMD((char*)"battery?");
}
void RMTT_Protocol::ReadTime()
{
SendCMD((char*)"time?");
}
void RMTT_Protocol::ReadSN()
{
SendCMD((char*)"sn?");
}
void RMTT_Protocol::ReadSDKVersion()
{
SendCMD((char*)"sdk?");
}
void RMTT_Protocol::startUntilControl(){
pinMode(34, INPUT_PULLUP);
RMTT_RGB::Init();
while(!(getTelloMsgString("[TELLO] command",1000)==String("ETT ok"))){}
RMTT_RGB::SetRGB(0,255,0);
while (!((digitalRead(34))==0)) {}
RMTT_RGB::SetRGB(0,0,0);
delay(1000);
RMTT_RGB::SetRGB(0,255,0);
delay(1000);
RMTT_RGB::SetRGB(0,0,0);
}
// 静态函数
String RMTT_Protocol::getTelloMsgString(char* cmd, uint32_t timeout){
while(Serial1.available()){
Serial1.read();
}
String back;
Serial1.printf(cmd);
long oldtime = millis();
while(!Serial1.available()){
long newtime = millis();
if((newtime-oldtime)>timeout){
back = "timeout";
return back;
}
}
while(Serial1.available()){
back += String(char(Serial1.read()));
}
if(back.endsWith("\r\n")){
back = back.substring(0,back.indexOf("\r\n"));
}
return back;
}
int RMTT_Protocol::getTelloMsgInt(char* cmd, uint32_t timeout){
while(Serial1.available()){
Serial1.read();
}
Serial1.printf(cmd);
long oldtime = millis();
while(!Serial1.available()){
long newtime = millis();
if((newtime-oldtime)>timeout){
return -1;
}
}
String back;
while(Serial1.available()){
back += String(char(Serial1.read()));
}
if(back.endsWith("\r\n")){
back = back.substring(0,back.indexOf("\r\n"));
}
return back.substring(back.indexOf(' ') + 1, back.length()).toInt();
}
String RMTT_Protocol::getTelloResponseString(uint32_t timeout){
String back;
long oldtime = millis();
while(!Serial1.available()){
long newtime = millis();
if((newtime-oldtime)>timeout){
back = "timeout";
return back;
}
}
while(Serial1.available()){
back += String(char(Serial1.read()));
}
if(back.endsWith("\r\n")){
back = back.substring(0,back.indexOf("\r\n"));
}
return back;
}
int RMTT_Protocol::getTelloResponseInt(uint32_t timeout){
String back;
long oldtime = millis();
while(!Serial1.available()){
long newtime = millis();
if((newtime-oldtime)>timeout){
return -1;
}
}
while(Serial1.available()){
back += String(char(Serial1.read()));
}
if(back.endsWith("\r\n")){
back = back.substring(0,back.indexOf("\r\n"));
}
return back.substring(back.indexOf(' ') + 1, back.length()).toInt();
}
uint8_t re_tag = 0;
uint8_t re_cnt = 0;
int RMTT_Protocol::sendTelloCtrlMsg(char *cmd_str)
{
re_cnt = 0;
while(true)
{
while(Serial1.available()){
Serial1.read();
}
Serial1.printf("[TELLO] Re%02x%02x %s", re_tag, re_cnt++, cmd_str);
// Serial.printf("[TELLO] Re%02x%02x %s", re_tag, re_cnt++, cmd_str);
long oldtime = millis();
while(!Serial1.available()){
long newtime = millis();
if((newtime-oldtime)>1000){
Serial1.printf("[TELLO] Re%02x%02x %s", re_tag, re_cnt++, cmd_str);
// Serial.printf("[TELLO] Re%02x%02x %s", re_tag, re_cnt++, cmd_str);
oldtime = newtime;
}
}
String back;
while(Serial1.available()){
back += String(char(Serial1.read()));
}
// ETT Re[tag][id] ok/error
// ETT Rexxxx ok/error
// Serial.printf(back.c_str());
if (back.length() >= 12)
{
if ((back.c_str()[11] == 'o') && ((back.c_str()[12] == 'k')))
{
break;
}
}
else
{
delay(100);
}
}
re_tag++;
return 0;
}