-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.TX_Tello_Test_4 (With Flip Control).c
229 lines (196 loc) · 5.67 KB
/
8.TX_Tello_Test_4 (With Flip Control).c
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
#include <string.h>
#define IBUS_BUFFSIZE 32 // Max iBus packet size (2 byte header, 14 channels x 2 bytes, 2 byte checksum)
#define IBUS_MAXCHANNELS 10 // My TX only has 10 channels, no point in polling the rest
int Channel_1;
int Channel_2;
int Channel_3;
int Channel_4;
int Pot_5;
int Pot_6;
int Switch_A;
int Switch_B;
int Switch_C;
int Switch_D;
bool IsFlying = false;
bool Condition = false;
static uint8_t ibusIndex = 0;
static uint8_t ibus[IBUS_BUFFSIZE] = {0};
static uint16_t rcValue[IBUS_MAXCHANNELS];
static boolean rxFrameDone;
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "TELLO-5EA8AA";
const char* password = "";
IPAddress TelloIp(192, 168, 10, 1);
const int TelloPort = 8889; // local port to listen on
WiFiUDP Udp;
const int localUdpPort = 9002; // local port to listen on
const char cmdCommandPacekt[] = "command"; // command
const char cmdTakeoffPacekt[] = "takeoff"; // takeoff
const char cmdLandPacekt[] = "land"; // land
char cmdRcPacekt[128];
const char cmdFlipForwardPacekt[] = "flip f";
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println("setup done!");
{
/////////////////////////////////////Serial.begin(115200);
//----------------------Connecting to Tello------------------
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
//----------------------Enter SDK Mode-----------------------
Udp.beginPacket(TelloIp, TelloPort);
Udp.write(cmdCommandPacekt);
Udp.endPacket();
delay(100);
Udp.begin(localUdpPort);
Serial.printf ("Now listening at IP %s, UDP port %d\n",
WiFi.localIP().toString().c_str(), localUdpPort);
//-----------------------------------------------------------
}
}
void loop()
{
{
rxFrameDone = false;
if (Serial.available())
{
uint8_t val = Serial.read();
// Look for 0x2040 as start of packet
if (ibusIndex == 0 && val != 0x20)
{
ibusIndex = 0;
return;
}
if (ibusIndex == 1 && val != 0x40)
{
ibusIndex = 0;
return;
}
if (ibusIndex == IBUS_BUFFSIZE)
{
ibusIndex = 0;
int high=3;
int low=2;
for(int i=0;i<IBUS_MAXCHANNELS; i++)
{
//left shift away the first 8 bits of the first byte and add the whole value of the previous one
rcValue[i] = (ibus[high] << 8) + ibus[low];
// Serial.print(rcValue[i]); //values between 1000 and 2000
// Serial.print(" ");
high += 2;
low += 2;
}
Switch_A = map(rcValue[6], 1000, 2000, 0, 1);
Switch_B = map(rcValue[7], 1000, 2000, 0, 1);
Switch_C = map(rcValue[8], 1000, 2000, 0, 1);
Switch_D = map(rcValue[9], 1000, 2000, 0, 1);
// Serial.print(Channel_3);
// Serial.println();
rxFrameDone = true;
return;
}
else
{
ibus[ibusIndex] = val;
ibusIndex++;
}
}
}
if (Switch_A==1 && IsFlying==false) // To _take_off
{
Serial.println("TELLO_Takeoff");
Udp.beginPacket(TelloIp, TelloPort);
Udp.write(cmdTakeoffPacekt);
Udp.endPacket();
IsFlying=true;
}
if (Switch_A==0 && IsFlying==true) // To_Land
{
Serial.println("TELLO_Landing");
Udp.beginPacket(TelloIp, TelloPort);
Udp.write(cmdLandPacekt);
Udp.endPacket();
IsFlying=false;
}
if (Switch_B==1 && Condition==false) // Fliptest
{
Serial.println("fliping_forward");
Udp.beginPacket(TelloIp, TelloPort);
Udp.write(cmdFlipForwardPacekt);
Udp.endPacket();
Condition=true;
}
if (Switch_B==0)
{
Condition=false;
}
if (Switch_D==1) // To_Control
{
{
rxFrameDone = false;
if (Serial.available())
{
uint8_t val = Serial.read();
// Look for 0x2040 as start of packet
if (ibusIndex == 0 && val != 0x20)
{
ibusIndex = 0;
return;
}
if (ibusIndex == 1 && val != 0x40)
{
ibusIndex = 0;
return;
}
if (ibusIndex == IBUS_BUFFSIZE)
{
ibusIndex = 0;
int high=3;
int low=2;
for(int i=0;i<IBUS_MAXCHANNELS; i++)
{
//left shift away the first 8 bits of the first byte and add the whole value of the previous one
rcValue[i] = (ibus[high] << 8) + ibus[low];
// Serial.print(rcValue[i]); //values between 1000 and 2000
// Serial.print(" ");
high += 2;
low += 2;
}
Channel_1 = map(rcValue[0], 1000, 2000, -100, 100);
Channel_2 = map(rcValue[1], 1000, 2000, -100, 100);
Channel_3 = map(rcValue[2], 1000, 2000, -100, 100);
Channel_4 = map(rcValue[3], 1000, 2000, -100, 100);
Pot_5 = map(rcValue[4], 1000, 2000, 0, 100);
Pot_6 = map(rcValue[5], 1000, 2000, 0, 100);
sprintf(cmdRcPacekt,"rc %d %d %d %d", Channel_1,Channel_2,Channel_3,Channel_4);
// snprintf(cmdRcPacekt, sizeof cmdRcPacekt, "rc %d %d %d %d", Channel_1,Channel_2,Channel_3,Channel_4);
Serial.println(cmdRcPacekt);
Udp.beginPacket(TelloIp, TelloPort);
Udp.write(cmdRcPacekt);
Udp.endPacket();
// Serial.print(Channel_3);
// Serial.println();
rxFrameDone = true;
return;
}
else
{
ibus[ibusIndex] = val;
ibusIndex++;
}
}
}
// Read_Stick_Pot();
}
}
//void Read_Rx_Switches()
//void Read_Stick_Pot()