-
Notifications
You must be signed in to change notification settings - Fork 21
/
mavlink_control.cpp
1668 lines (1398 loc) · 58.4 KB
/
mavlink_control.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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* @file mavlink_control.cpp
* @author The GremsyCo
* @version V2.3.0
* @date August-21-2018
* @brief This file contains a example for showing how to control gimbal in
* some cases
*
* @Copyright (c) 2018 Gremsy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "mavlink_control.h"
/* Private define-------------------------------------------------------------*/
/* Uncomment line below to use MAVLink Gimbal Protocol V1 */
// #define _USE_MAVLINK_GIMBAL_V1
#define _TIMEOUT 30
#define _TIMEOUT_RETURN_HOME 3
/* Private Typedef------------------------------------------------------------*/
enum sdk_process_state_t {
STATE_IDLE,
STATE_CONNECTED,
STATE_PROCESS,
STATE_DONE
};
enum gimbal_axis{
AXIS_PITCH = 0x01,
AXIS_ROLL,
AXIS_PAN,
};
enum type_of_gimbal
{
TWO_AXIS = 0x01,
THREE_AXIS,
};
enum mount_mode
{
TWO_AXIS_GIMBAL_MOUNT_MODE_ROLL_TILT = 0x01,
TWO_AXIS_GIMBAL_MOUNT_MODE_PAN_TILT,
THREE_AXIS_GIMBAL_MOUNT_NORMAL_MODE = 0x06,
THREE_AXIS_GIMBAL_MOUNT_INVERTED_MODE,
OFFSET = 5
};
struct sdk_process_t {
sdk_process_state_t state = STATE_IDLE;
uint64_t timeout_ms;
};
struct gimbal_mode_t{
type_of_gimbal gimbal_type;
uint8_t mnt_mode; /*Reference by 'mount_mode'*/
};
/* Private variable ----------------------------------------------------------*/
static sdk_process_t sdk;
static Serial_Port *serial_port_quit;
static Gimbal_Interface *gimbal_interface_quit;
static gimbal_mode_t gimbal_mode;
static bool is_boot_mode = false;
#ifdef _USE_MAVLINK_GIMBAL_V1
static Gimbal_Interface::MAVLINK_PROTO mav_gimbal_proto = Gimbal_Interface::MAVLINK_GIMBAL_V1;
#else
static Gimbal_Interface::MAVLINK_PROTO mav_gimbal_proto = Gimbal_Interface::MAVLINK_GIMBAL_V2;
#endif
/* Private prototype ---------------------------------------------------------*/
int gGimbal_sample(int argc, char **argv);
void gGimbal_displays(Gimbal_Interface &onboard);
void parse_commandline(int argc, char **argv, char *&uart_name, int &baudrate);
void quit_handler(int sig);
//Function
static void setting_sample_gimbal_setup_param_startup(Gimbal_Interface &onboard);
static void setting_sample_gimbal_setup_message_rate(Gimbal_Interface &onboard);
static void setting_sample_gimbal_set_follow_param(Gimbal_Interface &onboard);
static void setting_sample_gimbal_set_stiffness_param(Gimbal_Interface &onboard);
static void control_sample_gimbal_process(Gimbal_Interface &onboard, Serial_Port &serial_port);
static void control_sample_gimbal_off(Gimbal_Interface &onboard);
static void control_sample_gimbal_on(Gimbal_Interface &onboard);
static void control_sample_gimbal_change_mount_mode(Gimbal_Interface &onboard);
static void control_sample_gimbal_config_follow_para(Gimbal_Interface &onboard);
static void control_sample_gimbal_get_gimbal_information(Gimbal_Interface &onboard);
static bool control_sample_gimbal_set_lock_mode(Gimbal_Interface &onboard);
static bool control_sample_gimbal_set_follow_mode(Gimbal_Interface &onboard);
static void control_sample_gimbal_set_mapping_mode(Gimbal_Interface &onboard);
static void control_sample_gimbal_set_move_angle(Gimbal_Interface &onboard, float pitch_angle, float roll_angle, float yaw_angle);
static void control_sample_gimbal_set_move_rate(Gimbal_Interface &onboard, float pitch_rate, float roll_rate, float yaw_rate, uint8_t duration);
static void control_sample_gimbal_return_home(Gimbal_Interface &onboard);
static void control_sample_gimbal_reboot(Gimbal_Interface &onboard);
static bool upgrade_firmware(Gimbal_Interface &onboard, Serial_Port &serial_port);
static void monitor_attitude_imu_encoder(Gimbal_Interface &onboard, uint8_t duration);
// ------------------------------------------------------------------------------
// Gimbal sample control and get data
// ------------------------------------------------------------------------------
int gGimbal_sample(int argc, char **argv)
{
// --------------------------------------------------------------------------
// PARSE THE COMMANDS
// --------------------------------------------------------------------------
// Default input arguments
#ifdef __APPLE__
char *uart_name = (char *)"/dev/tty.usbmodem1";
#else
char *uart_name = (char *)"/dev/ttyUSB0";
#endif
int baudrate = 115200;
// do the parse, will throw an int if it fails
parse_commandline(argc, argv, uart_name, baudrate);
// --------------------------------------------------------------------------
// PORT and THREAD STARTUP
// --------------------------------------------------------------------------
/*
* Instantiate a serial port object
*
* This object handles the opening and closing of the offboard computer's
* serial port over which it will communicate to an autopilot. It has
* methods to read and write a mavlink_message_t object. To help with read
* and write in the context of pthreading, it gaurds port operations with a
* pthread mutex lock.
*
*/
Serial_Port serial_port(uart_name, baudrate);
/*
* Instantiate an autopilot interface object
*
* This starts two threads for read and write over MAVlink. The read thread
* listens for any MAVlink message and pushes it to the current_messages
* attribute. The write thread at the moment only streams a heartbeat 1hz It's
* important that one way or another this program signals offboard mode exit,
* otherwise the vehicle will go into failsafe.
*
*/
Gimbal_Interface gimbal_interface(&serial_port, 1, MAV_COMP_ID_ONBOARD_COMPUTER, mav_gimbal_proto, MAVLINK_COMM_0);
/*
* Setup interrupt signal handler
*
* Responds to early exits signaled with Ctrl-C. The handler will command
* to exit offboard mode if required, and close threads and the port.
* The handler in this example needs references to the above objects.
*
*/
serial_port_quit = &serial_port;
gimbal_interface_quit = &gimbal_interface;
signal(SIGINT, quit_handler);
/*
* Start the port and Gimbal_interface
* This is where the port is opened, and read and write threads are started.
*/
serial_port.start();
gimbal_interface.start();
/// Process data
while (!gimbal_interface.get_flag_exit()) {
if(sdk.state == STATE_IDLE){
if (gimbal_interface.present()) {
sdk.state = STATE_CONNECTED;
}
} else if(sdk.state == STATE_CONNECTED) {
setting_sample_gimbal_setup_param_startup(gimbal_interface);
sdk.state = STATE_PROCESS;
} else {
control_sample_gimbal_process(gimbal_interface,serial_port);
}
usleep(1000); // Run at 1kHz
}
// --------------------------------------------------------------------------
// THREAD and PORT SHUTDOWN
// --------------------------------------------------------------------------
/*
* Now that we are done we can stop the threads and close the port
*/
gimbal_interface.stop();
serial_port.stop();
// --------------------------------------------------------------------------
// DONE
// --------------------------------------------------------------------------
// woot!
return 0;
}
void gGimbal_displays(Gimbal_Interface &onboard){
Gimbal_Interface::fw_version_t fw = onboard.get_gimbal_version();
GSDK_DebugInfo("Gimbal Firmware version is %d.%d.%d.%s\n", fw.x, fw.y, fw.z, fw.type);
GSDK_DebugInfo("You selected gimbal mount mode ");
std :: string mode ;
switch (gimbal_mode.mnt_mode)
{
case TWO_AXIS_GIMBAL_MOUNT_MODE_ROLL_TILT:
GSDK_DebugInfo("Two axis Roll-Tilt mount\n");
break;
case TWO_AXIS_GIMBAL_MOUNT_MODE_PAN_TILT:
GSDK_DebugInfo("Two axis Pan-Tilt Mount\n");
break;
case THREE_AXIS_GIMBAL_MOUNT_INVERTED_MODE:
GSDK_DebugInfo("Three axis Inverted Mount\n");
break;
case THREE_AXIS_GIMBAL_MOUNT_NORMAL_MODE:
GSDK_DebugInfo("Three axis Normal Mount\n");
break;
default:
break;
}
uint8_t gimbal_mode = onboard.get_gimbal_mode();
GSDK_DebugInfo("Gimbal mode: %d\n",gimbal_mode);
uint8_t gimbal_state = onboard.get_gimbal_status().state;
GSDK_DebugInfo("Gimbal state: %d\n",gimbal_state);
Gimbal_Interface::imu_t my_imu = onboard.get_gimbal_raw_imu();
GSDK_DebugInfo("Raw imu: xacc:%d, yacc:%d, zacc:%d, xgyro:%d, xgyro:%d, xgyro:%d(raw)\n",
my_imu.accel.x,
my_imu.accel.y,
my_imu.accel.z,
my_imu.gyro.x,
my_imu.gyro.y,
my_imu.gyro.z);
attitude<float> myattitude;
myattitude= onboard.get_gimbal_attitude();
GSDK_DebugInfo("Gimbal attitude Pitch - Roll - Yaw: (%.2f) - (%.2f) - (%.2f)\n" ,myattitude.pitch, myattitude.roll, myattitude.yaw);
attitude<int16_t> myencoder;
myencoder = onboard.get_gimbal_encoder();
GSDK_DebugInfo("Gimbal encoder Pitch - Roll - Yaw: (%d) - (%d) - (%d)\n" ,myencoder.pitch, myencoder.roll, myencoder.yaw);
Gimbal_Interface::gimbal_config_axis_t new_config = onboard.get_gimbal_config_tilt_axis();
GSDK_DebugInfo("Config follow TILT: Dir: %d -- Speed control: %d -- Smooth control: %d -- Smooth follow: %d -- Window follow: %d\n"
, new_config.dir , new_config.speed_control ,new_config.smooth_control, new_config.smooth_follow, new_config.window_follow);
new_config = onboard.get_gimbal_config_roll_axis();
GSDK_DebugInfo("Config follow ROLL: Dir: %d -- Speed control: %d -- Smooth control: %d -- Smooth follow: %d -- Window follow: %d\n"
, new_config.dir , new_config.speed_control ,new_config.smooth_control, new_config.smooth_follow, new_config.window_follow);
new_config = onboard.get_gimbal_config_pan_axis();
GSDK_DebugInfo("Config follow PAN: Dir: %d -- Speed control: %d -- Smooth control: %d -- Smooth follow: %d -- Window follow: %d\n"
, new_config.dir , new_config.speed_control ,new_config.smooth_control, new_config.smooth_follow, new_config.window_follow);
Gimbal_Interface :: limit_angle_t limit = onboard.get_limit_angle_pitch();
GSDK_DebugInfo("Pitch max: %d -- Pitch min: %d\n" , limit.angle_max, limit.angle_min);
limit = onboard.get_limit_angle_roll();
GSDK_DebugInfo("Roll max: %d -- Roll min: %d\n" , limit.angle_max, limit.angle_min);
limit = onboard.get_limit_angle_yaw();
GSDK_DebugInfo("Yaw max: %d -- Yaw min: %d\n" , limit.angle_max, limit.angle_min);
Gimbal_Interface:: gimbal_motor_control_t tilt;
Gimbal_Interface:: gimbal_motor_control_t roll;
Gimbal_Interface:: gimbal_motor_control_t pan;
uint8_t gyro_filter ;
uint8_t output_filter;
onboard.get_gimbal_motor_control(tilt,roll,pan,gyro_filter,output_filter);
GSDK_DebugInfo("Tilt hold strength: %d -- Tilt stiffness: %d\n" , tilt.holdstrength, tilt.stiffness);
GSDK_DebugInfo("Roll hold strength: %d -- Roll stiffness: %d\n" , roll.holdstrength, roll.stiffness);
GSDK_DebugInfo("Pan hold strength: %d -- Pan stiffness: %d\n" , pan.holdstrength, pan.stiffness);
GSDK_DebugInfo("Gyro filter: %d\n",gyro_filter );
GSDK_DebugInfo("Output filter: %d\n" , output_filter);
}
// ------------------------------------------------------------------------------
// Parse Command Line
// ------------------------------------------------------------------------------
// throws EXIT_FAILURE if could not open the port
void parse_commandline(int argc, char **argv, char *&uart_name, int &baudrate)
{
// string for command line usage
const char *commandline_usage = "usage: mavlink_serial -d <devicename> -b <baudrate>";
// Read input arguments
for (int i = 1; i < argc; i++) { // argv[0] is "mavlink"
// Help
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
printf("%s\n", commandline_usage);
throw EXIT_FAILURE;
}
// UART device ID
if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--device") == 0) {
if (argc > i + 1) {
uart_name = argv[i + 1];
} else {
printf("%s\n", commandline_usage);
throw EXIT_FAILURE;
}
}
// Baud rate
if (strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "--baud") == 0) {
if (argc > i + 1) {
baudrate = atoi(argv[i + 1]);
} else {
printf("%s\n", commandline_usage);
throw EXIT_FAILURE;
}
}
}
// end: for each input argument
// Done!
}
// ------------------------------------------------------------------------------
// Quit Signal Handler
// ------------------------------------------------------------------------------
// this function is called when you press Ctrl-C
void quit_handler(int sig)
{
printf("\n");
printf("TERMINATING AT USER REQUEST\n");
printf("\n");
// autopilot interface
try {
gimbal_interface_quit->handle_quit(sig);
} catch (int error) {}
// serial port
try {
serial_port_quit->handle_quit(sig);
} catch (int error) {}
// end program here
exit(0);
}
// ------------------------------------------------------------------------------
// Main
// ------------------------------------------------------------------------------
int main(int argc, char **argv)
{
// This program uses throw, wrap one big try/catch here
try {
int result = gGimbal_sample(argc, argv);
return result;
} catch ( int error ) {
fprintf(stderr, "mavlink_control threw exception %i \n", error);
return error;
}
}
// ------------------------------------------------------------------------------
// Helper function
// ------------------------------------------------------------------------------
static void setting_sample_set_limit_angle(Gimbal_Interface &onboard){
Gimbal_Interface :: limit_angle_t limit ;
//setitng limit for pan axis
limit.angle_max = 320;
limit.angle_min = -320;
while (onboard.set_limit_angle_yaw(limit) != Gimbal_Protocol::SUCCESS)
{
printf("Try to set yaw limit again!\r\n");
usleep(1000);
}
printf("Set yaw max: %d -- Set yaw min: %d\n" , limit.angle_max, limit.angle_min);
// setting limit for titl axis
limit.angle_max = 45;
limit.angle_min = -45;
while (onboard.set_limit_angle_pitch(limit) != Gimbal_Protocol::SUCCESS)
{
printf("Try to set titl limit again!\r\n");
usleep(1000);
}
printf("Set yaw max: %d -- Set yaw min: %d\n" , limit.angle_max, limit.angle_min);
// setting limit for roll axis
limit.angle_max = 40;
limit.angle_min = -40;
while (onboard.set_limit_angle_roll(limit) != Gimbal_Protocol::SUCCESS)
{
printf("Try to set roll limit again!\r\n");
usleep(1000);
}
printf("Set roll max: %d -- Set roll min: %d\n" , limit.angle_max, limit.angle_min);
}
static void get_input_angle(float &pitch,float &roll, float &yaw);
static void get_input_rate(float &pitch,float &roll, float &yaw, uint8_t &duration);
static void get_input_duration(uint8_t &duration);
static void get_encoder_mode(uint8_t &mode);
static void control_sample_gimbal_process(Gimbal_Interface &onboard, Serial_Port &serial_port){
if (onboard.present() == false) {
return;
}
int number = 0;
printf("\33[39m\n\r Please Enter number [0-15] to seclect Gimbal control mode\n\r");
printf("\t 0. OFF Gimbal\n\r");
printf("\t 1. ON Gimbal\n\r");
printf("\t 2. Change mount mode\n\r");
printf("\t 3. Gimbal information\n\r");
printf("\t 4. Config gimbal follow parameter\n\r");
printf("\t 5. Set Gimbal to LOCK mode\n\r");
printf("\t 6. Set Gimbal to move angle in LOCK mode\n\r");
printf("\t 7. Set Gimbal to move rate in LOCK mode\n\r");
printf("\t 8. Set Gimbal to FOLLOW mode\n\r");
printf("\t 9. Set Gimbal to move angle in FOLLOW mode\n\r");
printf("\t 10. Set Gimbal to move rate in FOLLOW mode\n\r");
printf("\t 11. Set Gimbal to MAPPING mode\n\r");
printf("\t 12. Set Gimbal to Return Home\n\r");\
printf("\t 13. Set Gimbal Reboot\n\r");
printf("\t 14. Upgrade Firmware\n\r");
printf("\t 15. Monitoring Encoder - Attitude - IMU\n\r");
scanf("%d", &number);
switch (number)
{
case 0: // OFF Gimbal
{
control_sample_gimbal_off(onboard);
}
break;
case 1: // ON Gimbal
{
control_sample_gimbal_on(onboard);
}
break;
case 2: //Change mount mode
{
control_sample_gimbal_change_mount_mode(onboard);
}
break;
case 3: // Get gimbal information
{
control_sample_gimbal_get_gimbal_information(onboard);
}
break;
case 4: // Config gimbal follow parameter
{
do
{
char input = 0;
control_sample_gimbal_config_follow_para(onboard);
printf("\t\n\rContinue (Y/N)?\n\r");
scanf(" %c", &input);
if(input != 'Y' && input != 'y'){
break;
}
} while (true);
}
break;
case 5: // Set Gimbal to LOCK mode
{
control_sample_gimbal_set_lock_mode(onboard);
}
break;
case 6: // Set Gimbal to move angle in LOCK mode
{
/// set gimbal to LOCK mode
if(control_sample_gimbal_set_lock_mode(onboard) == true){
float yaw;
float pitch;
float roll;
get_input_angle(pitch,roll,yaw);
control_sample_gimbal_set_move_angle(onboard,pitch,roll,yaw);
}
}
break;
case 7: // Set Gimbal to move rate in LOCK mode
{
// set gimbal to LOCK mode
if(control_sample_gimbal_set_lock_mode(onboard) == true){
// control gimbal moving
float yaw_rate;
float pitch_rate;
float roll_rate;
uint8_t duration;
get_input_rate(pitch_rate,roll_rate,yaw_rate,duration);
control_sample_gimbal_set_move_rate(onboard,pitch_rate,roll_rate,yaw_rate,duration);
}
}
break;
case 8: // Set Gimbal to FOLLOW mode
{
control_sample_gimbal_set_follow_mode(onboard);
}
break;
case 9: // Set Gimbal to move angle in FOLLOW mode
{
/// set gimbal to FOLLOW mode
if(control_sample_gimbal_set_follow_mode(onboard) == true){
float yaw;
float pitch;
float roll;
get_input_angle(pitch,roll,yaw);
control_sample_gimbal_set_move_angle(onboard,pitch,roll,yaw);
}
}
break;
case 10: // Set Gimbal to move rate in FOLLOW mode
{
/// set gimbal to FOLLOW mode
if(control_sample_gimbal_set_follow_mode(onboard) == true){
/// control gimbal moving
float yaw_rate;
float pitch_rate;
float roll_rate;
uint8_t duration;
get_input_rate(pitch_rate,roll_rate,yaw_rate,duration);
control_sample_gimbal_set_move_rate(onboard,pitch_rate,roll_rate,yaw_rate,duration);
}
}
break;
case 11: // Set Gimbal to MAPPING mode
{
control_sample_gimbal_set_mapping_mode(onboard);
}
break;
case 12: // Set Gimbal to Return Home
{
control_sample_gimbal_return_home(onboard);
}
break;
case 13: // Set Gimbal Reboot
/* code */
{
control_sample_gimbal_reboot(onboard);
}
break;
case 14: // upgrade firmware
/* code */
{
upgrade_firmware(onboard,serial_port);
}
break;
case 15: // Monitoring
{
uint8_t duration = 0;
get_input_duration(duration);
monitor_attitude_imu_encoder(onboard,duration);
}
break;
default:
{
}
break;
}
}
static void setting_sample_gimbal_setup_param_startup(Gimbal_Interface &onboard){
Gimbal_Protocol::result_t res = Gimbal_Protocol::UNKNOWN;
if(mav_gimbal_proto == Gimbal_Interface::MAVLINK_GIMBAL_V1){
GSDK_DebugInfo("Use mavlink gimbal V1");
}else{
GSDK_DebugInfo("Use mavlink gimbal V2");
}
Gimbal_Interface::fw_version_t fw = onboard.get_gimbal_version();
GSDK_DebugInfo("Gimbal Firmware version is %d.%d.%d.%s", fw.x, fw.y, fw.z, fw.type);
if(fw.x * 100 + fw.y * 10 + fw.z <= 773){
while(1){
GSDK_DebugInfo("\n\rCurrent gSDK not support for this gimbal firmware version !!!\n\r"
"Please contact to GREMSY support team for get correct version !!!\n\r"
"Thank you !!!\n\r");
usleep(1000000);
}
}
onboard.set_gimbal_encoder_type_send(true);
GSDK_DebugSuccess("Set gimbal send raw encoder value.\n");
onboard.request_gimbal_device_info();
GSDK_DebugSuccess("Request gimbal device information.\n");
usleep(300000);
{
char gimbal_name[32] {0};
onboard.get_gimbal_name(gimbal_name);
std::string gimbal_str(gimbal_name);
std::string search_str = "AEVO";
if (gimbal_str.find(search_str) != std::string::npos) {
GSDK_DebugWarning("The AEVO gimbal needs to have the message rate set to function properly!\n\r");
setting_sample_gimbal_setup_message_rate(onboard);
}
else{
char number = 0;
GSDK_DebugMsg("Please Enter y/n(yes or no) to setting gimbal mavlink message rate\n\r");
scanf("%c", &number);
getchar();
if(number == 'Y'|| number == 'y')
{
setting_sample_gimbal_setup_message_rate(onboard);
}
}
}
{
char number = 0;
GSDK_DebugMsg("Please Enter y/n(yes or no) to setting gimbal stiffness - follow param\n\r");
scanf("%c", &number);
getchar();
if(number == 'Y'|| number == 'y')
{
GSDK_DebugInfo("Setting gimbal stiffness - follow parametter");
//Uncomment this if you want to set the stiffness param and follow param.
// setting_sample_gimbal_set_stiffness_param(onboard);
// setting_sample_gimbal_set_follow_param(onboard);
}
}
/// Please Uncomment to enable return home when change gimbal mode
// do{
// /* code */
// res = onboard.set_gimbal_return_home_when_change_mode(true);
// } while (res != Gimbal_Protocol::SUCCESS);
int temp_gimbal_type;
do
{
GSDK_DebugMsg("\t\n\r Please Enter number to select type of gimbal\n\r");
GSDK_DebugMsg("\t 1. Two axis\n\r");
GSDK_DebugMsg("\t 2. Three axis\n\r");
scanf("%d", &temp_gimbal_type);
getchar();
} while (!(temp_gimbal_type == (int)TWO_AXIS || temp_gimbal_type == (int)THREE_AXIS));
gimbal_mode.gimbal_type = (enum type_of_gimbal) temp_gimbal_type;
if(gimbal_mode.gimbal_type == TWO_AXIS){
do
{
GSDK_DebugMsg("Please Enter number to select two axis gimbal mount mode \n\r");
GSDK_DebugMsg("\t 1. Roll-Tilt Mount\n\r");
GSDK_DebugMsg("\t 2. Pan-Tilt Mount\n\r");
scanf("%hhu", &gimbal_mode.mnt_mode);
getchar();
} while (!(gimbal_mode.mnt_mode == TWO_AXIS_GIMBAL_MOUNT_MODE_PAN_TILT || gimbal_mode.mnt_mode == TWO_AXIS_GIMBAL_MOUNT_MODE_ROLL_TILT));
}else if (gimbal_mode.gimbal_type == THREE_AXIS)
{
do
{
GSDK_DebugMsg("Please Enter number to select three axis gimbal mode \n\r");
GSDK_DebugMsg("\t 1. Normal mode\n\r");
GSDK_DebugMsg("\t 2. Inverted mode\n\r");
scanf("%hhu", &gimbal_mode.mnt_mode);
gimbal_mode.mnt_mode = gimbal_mode.mnt_mode + OFFSET;
} while (!(gimbal_mode.mnt_mode == THREE_AXIS_GIMBAL_MOUNT_NORMAL_MODE || gimbal_mode.mnt_mode == THREE_AXIS_GIMBAL_MOUNT_INVERTED_MODE));
}
GSDK_DebugInfo("You selected gimbal mount mode ");
std :: string mode ;
switch (gimbal_mode.mnt_mode)
{
case TWO_AXIS_GIMBAL_MOUNT_MODE_ROLL_TILT:
GSDK_DebugInfo("Two axis Roll-Tilt mount\n");
break;
case TWO_AXIS_GIMBAL_MOUNT_MODE_PAN_TILT:
GSDK_DebugInfo("Two axis Pan-Tilt Mount\n");
break;
case THREE_AXIS_GIMBAL_MOUNT_INVERTED_MODE:
GSDK_DebugInfo("Three axis Inverted Mount\n");
break;
case THREE_AXIS_GIMBAL_MOUNT_NORMAL_MODE:
GSDK_DebugInfo("Three axis Normal Mount\n");
break;
default:
break;
}
}
static void setting_sample_gimbal_setup_message_rate(Gimbal_Interface &onboard){
char gimbal_name[32] {0};
onboard.get_gimbal_name(gimbal_name);
std::string gimbal_str(gimbal_name);
std::string search_str = "AEVO";
GSDK_DebugInfo("Setting gimbal mavlink message\n");
int enc_value_rate;
int orien_rate;
int imu_rate;
set_encoder:
GSDK_DebugMsg("Please Enter encoder messages rate (-1 for skip)\n");
scanf("%d", &enc_value_rate);
getchar();
uint8_t timeout = 0;
if (enc_value_rate > MAX_ENCODER_RATE)
{
GSDK_DebugWarning("The encoder messages rate exceeds the limit!");
}
else if(enc_value_rate >= 0)
{
int result = -1;
do
{
usleep(10);
result = onboard.set_msg_encoder_rate(enc_value_rate) ;
GSDK_DebugInfo("Try to set encoder messages rate: %dHz\r\n",enc_value_rate);
if(timeout++ > _TIMEOUT){
GSDK_DebugWarning("Cannot set up the encoder messages rate due to timeout!");
break;
}
} while (result != 0);
GSDK_DebugSuccess("Encoder message rate set successfully.\n");
}else if(gimbal_str.find(search_str) != std::string::npos && enc_value_rate <= 0) {
GSDK_DebugWarning("Please set message rate!");
goto set_encoder;
}
set_orient:
GSDK_DebugMsg("Please Enter mount orientation messages and attitude status messages rate (-1 for skip).\n\r");
scanf("%d", &orien_rate);
getchar();
timeout = 0;
if(orien_rate > MAX_ORIENTATION_RATE){
GSDK_DebugWarning("The mount orientation messages rate exceeds the limit!");
}
else if(orien_rate >= 0)
{
int result = -1;
do
{
usleep(10);
result = onboard.set_msg_mnt_orient_rate(orien_rate);
GSDK_DebugInfo("Try to set mount orientation messages rate: %dHz\r\n",orien_rate);
if(timeout++ > _TIMEOUT){
GSDK_DebugWarning("Cannot set up the mount orientation messages rate due to timeout!");
break;
}
} while (result != 0);
GSDK_DebugSuccess("Mount orientation messages rate set successfully.\n");
result = -1;
timeout = 0;
do
{
usleep(10);
result = onboard.set_msg_attitude_status_rate(orien_rate);
GSDK_DebugInfo("Try to set gimbal device attitude status messages rate: %dHz\r\n",orien_rate);
if(timeout++ > _TIMEOUT){
GSDK_DebugWarning("Cannot set up the attitude status messages rate due to timeout!");
break;
}
} while (result != 0);
GSDK_DebugSuccess("Gimbal device attitude status messages rate set successfully.\n");
}else if(gimbal_str.find(search_str) != std::string::npos && orien_rate <= 0) {
GSDK_DebugWarning("Please set message rate!");
goto set_orient;
}
set_imu:
GSDK_DebugMsg("Please Enter raw imu messages rate (-1 for skip)\n\r");
scanf("%d", &imu_rate);
getchar();
timeout = 0;
if (imu_rate > MAX_IMU_RAW_RATE)
{
GSDK_DebugWarning("The raw IMU rate exceeds the limit!");
}
else if(imu_rate >= 0)
{
int result = -1;
do
{
usleep(10);
result = onboard.set_msg_raw_imu_rate(imu_rate);
GSDK_DebugInfo("Try to set raw imu messages rate: %d\r\n",imu_rate);
if(timeout++ > _TIMEOUT){
GSDK_DebugWarning("Cannot set up the raw imu messages rate due to timeout!");
break;
}
} while (result != 0);
GSDK_DebugSuccess("Raw imu messages rate set successfully.");
}else if(gimbal_str.find(search_str) != std::string::npos && imu_rate <= 0) {
GSDK_DebugWarning("Please set message rate!");
goto set_imu;
}
}
static void setting_sample_gimbal_set_follow_param(Gimbal_Interface &onboard){
const int8_t tilt_dir = Gimbal_Interface::DIR_CW;
const uint8_t tilt_speed_control = 30;
const uint8_t tilt_smooth_control = 80;
const uint8_t tilt_smooth_follow = 20;
const uint8_t tilt_window_follow = 5;
const int8_t roll_dir = Gimbal_Interface::DIR_CW;
const uint8_t roll_speed_control = 10;
const uint8_t roll_smooth_control = 80;
const uint8_t roll_smooth_follow = 0;
const uint8_t roll_window_follow = 0;
const int8_t pan_dir = Gimbal_Interface::DIR_CW;
const uint8_t pan_speed_control = 30;
const uint8_t pan_smooth_control = 80;
const uint8_t pan_smooth_follow = 20;
const uint8_t pan_window_follow = 10;
Gimbal_Interface::gimbal_config_axis_t config = { 0 };
config = { tilt_dir, tilt_speed_control, tilt_smooth_control, tilt_smooth_follow, tilt_window_follow }; // Tilt
uint8_t timeout = 0;
onboard.set_gimbal_config_tilt_axis(config);
do
{
usleep(500000);
Gimbal_Interface::gimbal_config_axis_t new_config = onboard.get_gimbal_config_tilt_axis();
GSDK_DebugInfo("Config follow TILT: Dir: %u -- Speed control: %u -- Smooth control: %u -- Smooth follow: %u -- Window follow: %u\n"
, new_config.dir , new_config.speed_control ,new_config.smooth_control, new_config.smooth_follow, new_config.window_follow);
if( new_config.dir == config.dir &&
new_config.smooth_control == config.smooth_control &&
new_config.smooth_follow == config.smooth_follow &&
new_config.speed_control == config.speed_control &&
new_config.window_follow == config.window_follow )
{
GSDK_DebugSuccess("Set config for tilt successfully!");
break;
}
if(timeout++ > _TIMEOUT){
GSDK_DebugError("Cannot set up the tilt axis due to timeout!");
break;
}
onboard.set_gimbal_config_tilt_axis(config);
GSDK_DebugWarning("Try setting it again!!!");
} while (1);
config = { roll_dir, roll_speed_control, roll_smooth_control, roll_smooth_follow, roll_window_follow }; // Roll
timeout = 0;
onboard.set_gimbal_config_roll_axis(config);
do
{
usleep(500000);
Gimbal_Interface::gimbal_config_axis_t new_config = onboard.get_gimbal_config_roll_axis();
GSDK_DebugInfo("Config follow ROLL: Dir: %u -- Speed control: %u -- Smooth control: %u -- Smooth follow: %u -- Window follow: %u\n"
, new_config.dir , new_config.speed_control ,new_config.smooth_control, new_config.smooth_follow, new_config.window_follow);
if( new_config.dir == config.dir &&
new_config.smooth_control == config.smooth_control &&
new_config.speed_control == config.speed_control )
{
GSDK_DebugSuccess("Set config for roll successfully!");
break;
}
if(timeout++ > _TIMEOUT){
GSDK_DebugError("Cannot set up the roll axis due to timeout!");
break;
}
onboard.set_gimbal_config_roll_axis(config);
GSDK_DebugWarning("Try setting it again!!!");
} while (1);
config = { pan_dir, pan_speed_control, pan_smooth_control, pan_smooth_follow, pan_window_follow }; // Yaw
timeout = 0;
onboard.set_gimbal_config_pan_axis(config);
do
{
onboard.set_gimbal_config_pan_axis(config);
usleep(500000);
Gimbal_Interface::gimbal_config_axis_t new_config = onboard.get_gimbal_config_pan_axis();
GSDK_DebugInfo("Config follow PAN: Dir: %u -- Speed control: %u -- Smooth control: %u -- Smooth follow: %u -- Window follow: %u\n"
, new_config.dir , new_config.speed_control ,new_config.smooth_control, new_config.smooth_follow, new_config.window_follow);
if( new_config.dir == config.dir &&
new_config.smooth_control == config.smooth_control &&
new_config.smooth_follow == config.smooth_follow &&
new_config.speed_control == config.speed_control &&
new_config.window_follow == config.window_follow )
{
GSDK_DebugSuccess("Set config for pan successfully!");
break;
}
if(timeout++ > _TIMEOUT){
GSDK_DebugError("Cannot set up the pan axis due to timeout!");
break;
}
onboard.set_gimbal_config_pan_axis(config);
GSDK_DebugWarning("Try setting it again!!!");
} while (1);
}
static void setting_sample_gimbal_set_stiffness_param(Gimbal_Interface &onboard){
const uint8_t tilt_stiffness = 20;
const uint8_t tilt_holdstrength = 20;
const uint8_t roll_stiffness = 70;
const uint8_t roll_holdstrength = 70;
const uint8_t pan_stiffness = 60;
const uint8_t pan_holdstrength = 60;
const uint8_t gyro_filter = 4;
const uint8_t output_filter = 1;
// Motor control likes: Stiffness, holdstrength, gyro filter, output filter and gain
// Uncomment block below to configure gimbal motor
Gimbal_Interface::gimbal_motor_control_t tilt = { tilt_stiffness, tilt_holdstrength };
Gimbal_Interface::gimbal_motor_control_t roll = { roll_stiffness, roll_holdstrength };
Gimbal_Interface::gimbal_motor_control_t pan = { pan_stiffness, pan_holdstrength };
onboard.set_gimbal_motor_control( tilt, roll, pan, gyro_filter, output_filter );
uint8_t timeout = 0;
do
{
usleep(500000);
Gimbal_Interface:: gimbal_motor_control_t n_tilt;
Gimbal_Interface:: gimbal_motor_control_t n_roll;
Gimbal_Interface:: gimbal_motor_control_t n_pan;