-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmd.c
More file actions
executable file
·556 lines (484 loc) · 17 KB
/
cmd.c
File metadata and controls
executable file
·556 lines (484 loc) · 17 KB
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "crc.h"
#include "cmd.h"
#include "process.h"
extern WINDOW *console;
void flying_status_parse(uint8 *data,flying_status_s *flying_status)
{
flying_status->roll = *(float*)data;
flying_status->pitch = *(float*)(data +4);
flying_status->yaw = *(float*)(data +8);
flying_status->gx = *(float*)(data +12);
flying_status->gy = *(float*)(data +16);
flying_status->gz = *(float*)(data +20);
flying_status->ax = *(float*)(data +24);
flying_status->ay = *(float*)(data +28);
flying_status->az = *(float*)(data +32);
flying_status->g_time = *(uint32*)(data +36);
flying_status->vn = *(int*)(data +40);
flying_status->ve = *(int*)(data +44);
flying_status->vd = *(int*)(data +48);
flying_status->heading = *(int*)(data +52);
flying_status->b_h = *(int*)(data +56);
flying_status->lat = *(double*)(data +60);
flying_status->Long = *(double*)(data +68);
flying_status->g_h = *(double*)(data +76);
flying_status->vx = *(float*)(data +84);
flying_status->vy = *(float*)(data +88);
flying_status->vz = *(float*)(data +92);
flying_status->sonar_h = *(uint32*)(data +96);
flying_status->waypoint_dest = *(uint32*)(data +100);
flying_status->pwm[0] = *(uint16*)(data+102);
flying_status->pwm[1] = *(uint16*)(data+104);
flying_status->pwm[2] = *(uint16*)(data+106);
flying_status->pwm[3] = *(uint16*)(data+108);
flying_status->pwm[4] = *(uint16*)(data+110);
flying_status->pwm[5] = *(uint16*)(data+112);
flying_status->pwm[6] = *(uint16*)(data+114);
flying_status->pwm[7] = *(uint16*)(data+116);
flying_status->pwm[8] = *(uint16*)(data+118);
flying_status->pwm[9] = *(uint16*)(data+120);
flying_status->status = *(uint16*)(data+122);
flying_status->gps_status = *(uint8*)(data+124);
flying_status->imu_status = data[125];
flying_status->cpu_s = data[126];
flying_status->voltage = data[127]*3.3*11/256;
flying_status->engine = data[128];
flying_status->cpu_temp = *(int8*)(data+129);
flying_status->rsv = data[130];
}
void send_cmd_confirm(uint8 data)
{
uint16 crc_value;
uint8 buf[16];
buf[0] = CTRL_FRAME_START1;
buf[1] = CTRL_FRAME_START2;
buf[2] = plane_id&0xFF;
buf[3] = plane_id>>8;
*(uint16*)(buf+4) = 0x10;
buf[6] = 1;
buf[7] = 0;
buf[8] = 1;
buf[9] = 0;
buf[10] = CTRL_FRAME_TYPE_CMD_CONFIRM ;
buf[11] = data;
buf[12] = 0;
crc_value = crc_checksum16(buf, 13);
buf[13] = crc_value&0xFF;
buf[14] = crc_value>>8;
buf[15] = CTRL_FRAME_END;
control_cmd_send(buf, 16);
printx(console,"----|confirm cmd sent,type=%x ,waiting for execution answer.....................\n",data);
wrefresh(console);
}
void send_control_cmd(uint8 data)
{
uint16 crc_value;
uint8 buf[15];
int i=0;
buf[0] = CTRL_FRAME_START1;
buf[1] = CTRL_FRAME_START2;
buf[2] = plane_id&0xFF;
buf[3] = plane_id>>8;
*(uint16*)(buf+4) = 0x0F;
buf[6] = 1;
buf[7] = 0;
buf[8] = 1;
buf[9] = 0;
buf[10] = data ;
buf[11] = 0;
crc_value = crc_checksum16(buf, 12);
buf[12] = crc_value&0xFF;
buf[13] = crc_value>>8;
buf[14] = CTRL_FRAME_END;
memcpy(frame_wait_answer,buf,15);
printx(console,"----|");
for( i=0;i<15;i++)
printx(console,"%2x ",frame_wait_answer[i]);
printx(console,"\n");
control_cmd_send(frame_wait_answer, 15);
printx(console,"----|waiting for answer.....................\n");
wrefresh(console);
}
void send_way_point()
{
uint16 crc_value;
uint8 buf[4096];
char file_head[128];
int i=0;
FILE *fp;
uint32 id;
uint32 task;
uint32 task_para;
float v;
double Long;
double lat;
float h;
uint8 data[4096];
uint32 frame_size;
uint32 total_wp_num;
uint32 wp_num_of_this_frame;
fp=fopen("wp_init.csv","r");
if(fp==NULL){
printx(console,"----|can not open file:wp_init.csv\n");
printx(console,"----|sending waypoint failed\n");
}
//read out file information
fscanf(fp,"%s,%s",file_head+60,file_head+70);
fscanf(fp,"%d,%d",&total_wp_num,&wp_num_of_this_frame);
fscanf(fp,"%s,%s,%s,%s,%s,%s,%s",file_head,file_head+10,file_head+20,file_head+30,file_head+40,file_head+50,file_head+60);
//read data
while(fscanf(fp,"%d,%x,%x,%f,%lf,%lf,%f",&id,&task,&task_para,&v,&Long,&lat,&h)==7){
*(uint16*)(data+i*28) = (uint16)id;
*(uint8*)(data+2+i*28) = (uint8)task;
*(uint8*)(data+3+i*28) = (uint8)task_para;
*(float*)(data+4+i*28) = v;
*(double*)(data+8+i*28) = Long;
*(double*)(data+16+i*28) = lat;
*(float*)(data+24+i*28) = h;
i++;
printx(console,"----|%2d,%2x,%2x,%8f,%lf,%lf,%8f\n",id,task,task_para,v,Long,lat,h);
}
frame_size = i*28+18;
buf[0] = CTRL_FRAME_START1;
buf[1] = CTRL_FRAME_START2;
buf[2] = plane_id&0xFF;
buf[3] = plane_id>>8;
*(uint16*)(buf+4) = frame_size;
buf[6] = 1;
buf[7] = 0;
buf[8] = 1;
buf[9] = 0;
buf[10] = CTRL_FRAME_TYPE_WAYPOINT_INIT;
*(uint16*)(buf+11) = (uint16)total_wp_num;
buf[13] = (uint8)wp_num_of_this_frame;
buf[14] = 0x0;
memcpy(buf+15,data,i*28);
crc_value = crc_checksum16(buf, frame_size-3);
buf[frame_size-3] = crc_value&0xFF;
buf[frame_size-2] = crc_value>>8;
buf[frame_size-1] = CTRL_FRAME_END;
memcpy(frame_wait_answer,buf,frame_size);
printx(console,"----|");
for( i=0;i<frame_size;i++)
printx(console,"%2x ",frame_wait_answer[i]);
printx(console,"\n");
control_cmd_send(frame_wait_answer, frame_size);
printx(console,"----|waiting for answer.....................\n");
wrefresh(console);
}
void way_point_modify()
{
uint16 crc_value;
uint8 buf[128];
char file_head[128];
FILE *fp;
uint32 m_type;
uint32 id;
uint32 task;
uint32 task_para;
float v;
double Long;
double lat;
float h;
uint8 data[36];
uint32 frame_size;
int i=0;
fp=fopen("wp_modify.csv","r");
if(fp==NULL){
printx(console,"----|can not open file:wp_modify.csv\n");
printx(console,"----|sending waypoint modify command failed\n");
}
//read out file information
fscanf(fp,"%s,%s,%s,%s,%s,%s,%s,%s",file_head,file_head+10,file_head+20,file_head+30,file_head+40,file_head+50,file_head+60,file_head+70);
// read data
while(fscanf(fp,"%d,%d,%d,%d,%f,%lf,%lf,%f",&m_type,&id,&task,&task_para,&v,&Long,&lat,&h)==8){
*(uint32*)(data) = (uint32)m_type;
*(uint16*)(data+4) = id;
*(uint8*)(data+6) = (uint8)task;
*(uint8*)(data+7) = (uint8)task_para;
*(float*)(data+8) = v;
*(double*)(data+12) = Long;
*(double*)(data+20) = lat;
*(float*)(data+28) = h;
*(uint32*)(data+32) = 0;
printx(console,"----|%2x,%2d,%2x,%2x,%8f,%lf,%lf,%8f\n",m_type,id,task,task_para,v,Long,lat,h);
}
frame_size = 50;
buf[0] = CTRL_FRAME_START1;
buf[1] = CTRL_FRAME_START2;
buf[2] = plane_id&0xFF;
buf[3] = plane_id>>8;
*(uint16*)(buf+4) = frame_size;
buf[6] = 1;
buf[7] = 0;
buf[8] = 1;
buf[9] = 0;
buf[10] = CTRL_FRAME_TYPE_WAYPOINT_MODIFY;
memcpy(buf+11,data,36);
crc_value = crc_checksum16(buf, frame_size-3);
buf[frame_size-3] = crc_value&0xFF;
buf[frame_size-2] = crc_value>>8;
buf[frame_size-1] = CTRL_FRAME_END;
memcpy(frame_wait_answer,buf,frame_size);
printx(console,"----|");
for( i=0;i<frame_size;i++)
printx(console,"%2x ",frame_wait_answer[i]);
printx(console,"\n");
control_cmd_send(frame_wait_answer, frame_size);
printx(console,"----|waiting for answer.....................\n");
wrefresh(console);
}
void send_joystick_data()
{
uint16 crc_value;
uint8 buf[30];
buf[0] = CTRL_FRAME_START1;
buf[1] = CTRL_FRAME_START2;
buf[2] = plane_id&0xFF;
buf[3] = plane_id>>8;
*(uint16*)(buf+4) = 0x1e;
buf[6] = 1;
buf[7] = 0;
buf[8] = 1;
buf[9] = 0;
buf[10] = CTRL_FRAME_TYPE_STICK_DATA ;
*(uint16*)(buf+11) = 0;
*(uint16*)(buf+13) = 1;
*(uint16*)(buf+15) = 2;
*(uint16*)(buf+17) = 3;
*(uint16*)(buf+19) = 4;
*(uint16*)(buf+21) = 5;
*(uint16*)(buf+23) = 6;
*(uint16*)(buf+25) = 7;
crc_value = crc_checksum16(buf, 27);
buf[27] = crc_value&0xFF;
buf[28] = crc_value>>8;
buf[29] = CTRL_FRAME_END;
control_cmd_send(buf, 30);
}
void link_test()
{
uint16 crc_value;
uint8 buf[18];
int i=0;
buf[0] = CTRL_FRAME_START1;
buf[1] = CTRL_FRAME_START2;
buf[2] = plane_id&0xFF;
buf[3] = plane_id>>8;
*(uint16*)(buf+4) = 0x12;
buf[6] = 1;
buf[7] = 0;
buf[8] = 1;
buf[9] = 0;
buf[10] = CTRL_FRAME_TYPE_LINK_TEST ;
*(uint32*)(buf+11) = 1;
crc_value = crc_checksum16(buf, 15);
buf[15] = crc_value&0xFF;
buf[16] = crc_value>>8;
buf[17] = CTRL_FRAME_END;
printx(console,"----|");
for( i=0;i<15;i++)
printx(console,"%2x ",buf[i]);
printx(console,"\n");
wrefresh(console);
control_cmd_send(buf, 18);
}
void heli_config()
{
uint16 crc_value;
uint8 buf[25];
char file_head[254];
FILE *fp;
uint32 heli_type;
uint32 fuel_vol;
uint32 fuel_consum;
uint32 swash_typ;
uint32 servo_freq;
uint32 tail_gyro;
uint32 max_speed;
uint32 gps_typ;
uint32 radar;
uint32 rsv1;
uint32 rsv2;
uint8 data[11];
uint32 frame_size;
int i=0;
fp=fopen("heli_config.csv","r");
if(fp==NULL){
printx(console,"----|can not open file:heli_config.csv\n");
printx(console,"----|sending heli config command failed\n");
}
//read out file information
fscanf(fp,"%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s",file_head,file_head+10,
file_head+20,file_head+30,file_head+40,file_head+50,
file_head+60,file_head+70,file_head+80,file_head+90,file_head+100);
// read data
while(fscanf(fp,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", &heli_type,&fuel_vol,
&fuel_consum,&swash_typ,&servo_freq,&tail_gyro,&max_speed,&gps_typ,&radar,&rsv1,&rsv2)==11){
data[0]=(uint8)heli_type;
data[1]=(uint8)fuel_vol;
data[2]=(uint8)fuel_consum;
data[3]=(uint8)swash_typ;
data[4]=(uint8)servo_freq;
data[5]=(uint8)tail_gyro;
data[6]=(uint8)max_speed;
data[7]=(uint8)gps_typ;
data[8]=(uint8)radar;
data[9]=(uint8)rsv1;
data[10]=(uint8)rsv2;
printx(console,"----|%2x,%2x,%2x,%2x,%2x,%2x,%2x,%2x,%2x,%2x,%2x\n",heli_type,fuel_vol,
fuel_consum,swash_typ,servo_freq,tail_gyro,max_speed,gps_typ,radar,rsv1,rsv2);
}
frame_size = 25;
buf[0] = CTRL_FRAME_START1;
buf[1] = CTRL_FRAME_START2;
buf[2] = plane_id&0xFF;
buf[3] = plane_id>>8;
*(uint16*)(buf+4) = frame_size;
buf[6] = 1;
buf[7] = 0;
buf[8] = 1;
buf[9] = 0;
buf[10] = CTRL_FRAME_TYPE_HELI_CONFIG;
memcpy(buf+11,data,frame_size-14);
crc_value = crc_checksum16(buf, frame_size-3);
buf[frame_size-3] = crc_value&0xFF;
buf[frame_size-2] = crc_value>>8;
buf[frame_size-1] = CTRL_FRAME_END;
memcpy(frame_wait_answer,buf,frame_size);
printx(console,"----|");
for( i=0;i<frame_size;i++)
printx(console,"%2x ",frame_wait_answer[i]);
printx(console,"\n");
control_cmd_send(frame_wait_answer, frame_size);
printx(console,"----|waiting for answer.....................\n");
wrefresh(console);
}
void send_fly_para1()
{
uint16 crc_value;
uint8 buf[46];
FILE *fp;
uint32 data[16];
uint32 frame_size;
int i=0;
fp=fopen("fly_para1.csv","r");
if(fp==NULL){
wattron(console,COLOR_PAIR(2));
printx(console,"----|can not open file:fly_para1.csv\n");
printx(console,"----|sending fly_para1 command failed\n");
wattroff(console,COLOR_PAIR(2));
}
// read data
while(fscanf(fp,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", data,data+1,data+2,data+3,data+4,data+5,
data+6,data+7,data+8,data+9,data+10,data+11,data+12,data+13,data+14,data+15)==16){
printx(console,"----|%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",data[0],data[1],
data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9],data[10],
data[11],data[12],data[13],data[14],data[15]);
}
frame_size = 46;
buf[0] = CTRL_FRAME_START1;
buf[1] = CTRL_FRAME_START2;
buf[2] = plane_id&0xFF;
buf[3] = plane_id>>8;
*(uint16*)(buf+4) = frame_size;
buf[6] = 1;
buf[7] = 0;
buf[8] = 1;
buf[9] = 0;
buf[10] = CTRL_FRAME_TYPE_FLY_PARA1;
*(uint16*)(buf+11) = (uint16)data[0];
*(uint16*)(buf+13) = (uint16)data[1];
*(uint16*)(buf+15) = (uint16)data[2];
*(uint16*)(buf+17) = (uint16)data[3];
*(uint16*)(buf+19) = (uint16)data[4];
*(uint16*)(buf+21) = (uint16)data[5];
*(uint16*)(buf+23) = (uint16)data[6];
*(uint16*)(buf+25) = (uint16)data[7];
*(uint16*)(buf+27) = (uint16)data[8];
*(uint16*)(buf+29) = (uint16)data[9];
*(uint16*)(buf+31) = (uint16)data[10];
*(uint16*)(buf+33) = (uint16)data[11];
*(uint16*)(buf+35) = (uint16)data[12];
*(uint16*)(buf+37) = (uint16)data[13];
*(uint16*)(buf+39) = (uint16)data[14];
*(uint16*)(buf+41) = (uint16)data[15];
crc_value = crc_checksum16(buf, frame_size-3);
buf[frame_size-3] = crc_value&0xFF;
buf[frame_size-2] = crc_value>>8;
buf[frame_size-1] = CTRL_FRAME_END;
memcpy(frame_wait_answer,buf,frame_size);
printx(console,"----|");
for( i=0;i<frame_size;i++)
printx(console,"%2x ",frame_wait_answer[i]);
printx(console,"\n");
control_cmd_send(frame_wait_answer, frame_size);
printx(console,"----|waiting for answer.....................\n");
wrefresh(console);
}
void send_fly_para2()
{
uint16 crc_value;
uint8 buf[46];
FILE *fp;
uint32 data[16];
uint32 frame_size;
int i=0;
fp=fopen("fly_para2.csv","r");
if(fp==NULL){
wattron(console,COLOR_PAIR(2));
printx(console,"----|can not open file:fly_para2.csv\n");
printx(console,"----|sending fly_para2 command failed\n");
wattroff(console,COLOR_PAIR(2));
}
// read data
while(fscanf(fp,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", data,data+1,data+2,data+3,data+4,data+5,
data+6,data+7,data+8,data+9,data+10,data+11,data+12,data+13,data+14,data+15)==16){
printx(console,"----|%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",data[0],data[1],
data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9],data[10],
data[11],data[12],data[13],data[14],data[15]);
}
frame_size = 46;
buf[0] = CTRL_FRAME_START1;
buf[1] = CTRL_FRAME_START2;
buf[2] = plane_id&0xFF;
buf[3] = plane_id>>8;
*(uint16*)(buf+4) = frame_size;
buf[6] = 1;
buf[7] = 0;
buf[8] = 1;
buf[9] = 0;
buf[10] = CTRL_FRAME_TYPE_FLY_PARA2;
*(uint16*)(buf+11) = (uint16)data[0];
*(uint16*)(buf+13) = (uint16)data[1];
*(uint16*)(buf+15) = (uint16)data[2];
*(uint16*)(buf+17) = (uint16)data[3];
*(uint16*)(buf+19) = (uint16)data[4];
*(uint16*)(buf+21) = (uint16)data[5];
*(uint16*)(buf+23) = (uint16)data[6];
*(uint16*)(buf+25) = (uint16)data[7];
*(uint16*)(buf+27) = (uint16)data[8];
*(uint16*)(buf+29) = (uint16)data[9];
*(uint16*)(buf+31) = (uint16)data[10];
*(uint16*)(buf+33) = (uint16)data[11];
*(uint16*)(buf+35) = (uint16)data[12];
*(uint16*)(buf+37) = (uint16)data[13];
*(uint16*)(buf+39) = (uint16)data[14];
*(uint16*)(buf+41) = (uint16)data[15];
crc_value = crc_checksum16(buf, frame_size-3);
buf[frame_size-3] = crc_value&0xFF;
buf[frame_size-2] = crc_value>>8;
buf[frame_size-1] = CTRL_FRAME_END;
memcpy(frame_wait_answer,buf,frame_size);
printx(console,"----|");
for( i=0;i<frame_size;i++)
printx(console,"%2x ",frame_wait_answer[i]);
printx(console,"\n");
control_cmd_send(frame_wait_answer, frame_size);
printx(console,"----|waiting for answer.....................\n");
wrefresh(console);
}