forked from jimmckeeth/DelphiARDrone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.parrot.ardrone.pas
452 lines (381 loc) · 9.54 KB
/
api.parrot.ardrone.pas
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
unit api.parrot.ardrone;
interface
{$SCOPEDENUMS ON}
uses
System.SysUtils, System.Classes,
IdBaseComponent, IdComponent, IdGlobal,
IdUDPBase, IdUDPClient,
IdTCPConnection, IdTCPClient;
type
TDroneMovement = (
Hover,
MoveUp,
MoveDown,
MoveLeft,
MoveRight,
MoveForward,
MoveBackward,
RotateCW,
RotateCCW
);
TLEDAnimation = (
BlinkGreenRed,
BlinkGreen,
BlinkRed,
BlinkOrange,
SnakeGreenRed,
Fire,
Standard,
Red,
Green,
RedSnake,
Blank,
RightMissile,
LeftMissile,
DoubleMissile,
FrontLeftGreenOthersRed,
FrontRightGreenOthersRed,
RearRightGreenOthersRed,
RearLeftGreenOthersRed,
LeftGreenRightRed,
LeftRedRightGreen,
BlinkStandard);
TDroneAnimation = (
PhiM30Deg,
Phi30Deg,
ThetaM30Deg,
Theta30Deg,
Theta20degYaw200deg,
Theta20degYawM200deg,
Turnaround,
TurnaroundGodown,
YawShake,
YawDance,
PhiDance,
ThetaDance,
VZDance,
Wave,
PhiThetaMixed,
DoublePhiThetaMixed,
Mayday);
TARDrone = class(TComponent)
private
udp: TIdUDPClient;
navUdp: TIdUDPClient;
seq: Integer;
FGaz: Single;
FTheta: Single;
FYaw: Single;
FPhi: Single;
FOutdoor: Boolean;
FFlightWithoutShell: Boolean;
FConnect: TNotifyEvent;
FDisconnect: TNotifyEvent;
procedure UdpConnect(sender: TObject);
procedure UdpDisconnet(sender: TObject);
procedure SetGaz(const Value: Single);
procedure SetPhi(const Value: Single);
procedure SetTheta(const Value: Single);
procedure SetYaw(const Value: Single);
procedure SetOutdoor(const Value: Boolean);
procedure SetFlightWithoutShell(const Value: Boolean);
procedure ConnectNavData;
protected
{ Protected declarations }
procedure UpdateMovement;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Connect;
procedure Disconnect;
procedure SendCommand(cmd, arg: string);
procedure RestrictAltitude(const Max: Integer); // between 500 to 5000
procedure UnlimitedAltitude;
procedure Config(const Key, Value: string);
procedure RenameSSID(const SSID: string);
procedure RenameDrone(const NewName: string);
procedure AnimateLEDs(const Animation: TLEDAnimation; const FreqHz: Single; const DurationSeconds: Integer);
procedure Takeoff;
procedure Land;
procedure Emergency;
procedure FlatTrims; // Tell drone is is horizontal
procedure Hover;
procedure AnimateDrone(const Animation: TDroneAnimation; const DurationSeconds: Integer);
procedure RotateCW(const yaw: Single = 1);
procedure RotateCCW(const yaw: Single = 1);
procedure MoveForward(const theta: Single = 1);
procedure MoveBackward(const theta: Single = 1);
procedure MoveLeft(const phi: Single = 1);
procedure MoveRight(const phi: Single = 1);
procedure MoveUp(const gaz: Single = 1);
procedure MoveDown(const gaz: Single = 1);
// Zero's the other movements
procedure AnglularSpeed(const yaw: Single);
procedure LeftRightAngle(const phi: Single);
procedure FrontBackAngle(const theta: Single);
procedure VerticalSpeed(const gaz: Single);
// Full control of the movement (-1 to 1)
procedure Move(const phi, theta, gaz, yaw: Single);
published
property Yaw: Single read FYaw write SetYaw;
property Phi: Single read FPhi write SetPhi;
property Theta: Single read FTheta write SetTheta;
property Gaz: Single read FGaz write SetGaz;
property Outdoor: Boolean read FOutdoor write SetOutdoor default False;
property FlightWithoutShell: Boolean read FFlightWithoutShell write SetFlightWithoutShell default True;
property OnConnect: TNotifyEvent read FConnect write FConnect;
property OnDisconnect: TNotifyEvent read FDisconnect write FDisconnect;
end;
function IEEEFloat(const aFloat: Single): Integer;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Gadgets', [TARDrone]);
end;
function IEEEFloat(const aFloat: Single): Integer;
type
TIEEEFloat = record
case Boolean of
True: (Float: Single);
False: (Int: Integer);
end;
var
Convert: TIEEEFloat;
begin
Convert.Float := aFloat;
Result := Convert.Int;
end;
{ TARDrone }
constructor TARDrone.Create;
begin
inherited Create(AOwner);
seq := 1;
FOutdoor := True;
FFlightWithoutShell := False;
end;
destructor TARDrone.Destroy;
begin
if Assigned(udp) then
try
udp.Free;
except
udp := nil;
end;
if Assigned(navUdp) then
try
navUdp.Free;
except
navUdp := nil;
end;
inherited Destroy;
end;
procedure TARDrone.Connect;
begin
if not Assigned(udp) then
begin
udp := TIdUDPClient.Create(nil);
udp.OnConnected := UdpConnect;
udp.OnDisconnected := UdpDisconnet;
udp.Host := '192.168.1.1';
udp.Port := 5556;
end;
udp.Connect;
RestrictAltitude(2000);
FlatTrims;
// reduce NavData
Config('general:navdata_demo','TRUE');
end;
procedure TARDrone.ConnectNavData;
var
Buf: TIdBytes;
begin
if not Assigned(NavUDP) then
begin
NavUDP := TIdUDPClient.Create(nil);
NavUDP.Host := '192.168.1.1';
NavUDP.Port := 5554;
NavUDP.Connect;
SetLength(Buf, 5);
Buf[0] := 1;
NavUDP.SendBuffer(buf);
end;
end;
procedure TARDrone.Disconnect;
begin
udp.Disconnect;
end;
procedure TARDrone.AnglularSpeed(const yaw: Single);
begin
SendCommand('AT*PCMD',Format('1,0,0,0,%d', [IEEEFloat(yaw)]));
end;
procedure TARDrone.AnimateDrone(const Animation: TDroneAnimation;
const DurationSeconds: Integer);
begin
SendCommand('AT*ANIM', Format('%d,%d',
[Ord(Animation), DurationSeconds]));
end;
procedure TARDrone.AnimateLEDs(const Animation: TLEDAnimation;
const FreqHz: Single; const DurationSeconds: Integer);
begin
SendCommand('AT*LED', Format('%d,%d,%d',
[Ord(Animation), IEEEFloat(FreqHz), DurationSeconds]));
end;
procedure TARDrone.FlatTrims;
begin
SendCommand('AT*FTROM','');
end;
procedure TARDrone.FrontBackAngle(const theta: Single);
begin
SendCommand('AT*PCMD',Format('1,0,%d,0,0', [IEEEFloat(theta * -1)]));
end;
procedure TARDrone.Hover;
begin
FGaz := 0;
FTheta := 0;
FYaw := 0;
FPhi := 0;
UpdateMovement;
end;
procedure TARDrone.LeftRightAngle(const phi: Single);
begin
SendCommand('AT*PCMD',Format('1,%d,0,0,0', [IEEEFloat(phi * -1)]));
end;
procedure TARDrone.Move(const phi, theta, gaz, yaw: Single);
begin
FGaz := gaz;
FTheta := theta;
FYaw := yaw;
FPhi := phi;
SendCommand('AT*PCMD',Format('1,%d,%d,%d,%d',
[IEEEFloat(phi), IEEEFloat(theta), IEEEFloat(gaz), IEEEFloat(yaw)]));
end;
procedure TARDrone.MoveBackward(const theta: Single);
begin
FrontBackAngle(-1 * theta);
end;
procedure TARDrone.MoveDown(const gaz: Single);
begin
VerticalSpeed(-1 * gaz);
end;
procedure TARDrone.MoveForward(const theta: Single);
begin
FrontBackAngle(theta);
end;
procedure TARDrone.MoveLeft(const phi: Single);
begin
LeftRightAngle(phi);
end;
procedure TARDrone.MoveRight(const phi: Single);
begin
LeftRightAngle(-1 * phi);
end;
procedure TARDrone.MoveUp(const gaz: Single);
begin
VerticalSpeed(gaz);
end;
procedure TARDrone.RotateCCW(const yaw: Single);
begin
AnglularSpeed(-1 * yaw);
end;
procedure TARDrone.RotateCW(const yaw: Single);
begin
AnglularSpeed(yaw);
end;
procedure TARDrone.RenameDrone(const NewName: string);
begin
Config('GENERAL:ardrone_name', NewName);
end;
procedure TARDrone.RenameSSID(const SSID: string);
begin
Config('network:ssid_single_player', SSID);
end;
procedure TARDrone.Config(const Key, Value: string);
begin
SendCommand('AT*CONFIG', Format('"%s","%s"', [key, value]));
end;
procedure TARDrone.RestrictAltitude(const Max: Integer);
var
limit: Integer;
begin
limit := Max;
if limit < 500 then limit := 500;
if limit > 5000 then limit := 5000;
Config('control:altitude_max', IntToStr(limit));
end;
procedure TARDrone.SendCommand(cmd, arg: string);
var
full: string;
begin
if csDesigning in ComponentState then exit;
if not assigned(udp) then exit;
if not udp.Active then Connect;
full := Format('%s=%d,%s' + Chr(13), [Cmd, Seq, arg]);
Seq := Seq + 1;
udp.Send(full);
end;
procedure TARDrone.SetFlightWithoutShell(const Value: Boolean);
begin
FFlightWithoutShell := Value;
Config('CONTROL:flight_without_shell', BoolToStr(Value, True));
end;
procedure TARDrone.SetGaz(const Value: Single);
begin
FGaz := Value;
UpdateMovement;
end;
procedure TARDrone.SetOutdoor(const Value: Boolean);
begin
FOutdoor := Value;
Config('CONTROL:outdoor', BoolToStr(Value, True));
end;
procedure TARDrone.SetPhi(const Value: Single);
begin
FPhi := Value;
UpdateMovement;
end;
procedure TARDrone.SetTheta(const Value: Single);
begin
FTheta := Value;
UpdateMovement;
end;
procedure TARDrone.SetYaw(const Value: Single);
begin
FYaw := Value;
UpdateMovement;
end;
procedure TARDrone.Takeoff;
begin
SendCommand('AT*REF','290718208');
end;
procedure TARDrone.Emergency;
begin
SendCommand('AT*REF','290717952');
end;
procedure TARDrone.Land;
begin
SendCommand('AT*REF','290717696');
end;
procedure TARDrone.UdpConnect(sender: TObject);
begin
if Assigned(FConnect) then
FConnect(self);
end;
procedure TARDrone.UdpDisconnet(sender: TObject);
begin
if Assigned(FDisconnect) then
FDisconnect(self);
end;
procedure TARDrone.UnlimitedAltitude;
begin
SendCommand('AT*CONFIG', '"control:altitude_max","10000"');
end;
procedure TARDrone.UpdateMovement;
begin
Move(FPhi, FTheta, FGaz, FYaw);
end;
procedure TARDrone.VerticalSpeed(const gaz: Single);
begin
SendCommand('AT*PCMD',Format('1,0,0,%d,0', [IEEEFloat(gaz)]));
end;
end.