-
Notifications
You must be signed in to change notification settings - Fork 0
/
mode_auto.cpp
1904 lines (1624 loc) · 62.6 KB
/
mode_auto.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
#include "Copter.h"
#if MODE_AUTO_ENABLED == ENABLED
/*
* Init and run calls for auto flight mode
*
* This file contains the implementation for Land, Waypoint navigation and Takeoff from Auto mode
* Command execution code (i.e. command_logic.pde) should:
* a) switch to Auto flight mode with set_mode() function. This will cause auto_init to be called
* b) call one of the three auto initialisation functions: auto_wp_start(), auto_takeoff_start(), auto_land_start()
* c) call one of the verify functions auto_wp_verify(), auto_takeoff_verify, auto_land_verify repeated to check if the command has completed
* The main loop (i.e. fast loop) will call update_flight_modes() which will in turn call auto_run() which, based upon the auto_mode variable will call
* correct auto_wp_run, auto_takeoff_run or auto_land_run to actually implement the feature
*/
/*
* While in the auto flight mode, navigation or do/now commands can be run.
* Code in this file implements the navigation commands
*/
// auto_init - initialise auto controller
bool ModeAuto::init(bool ignore_checks)
{
if (mission.num_commands() > 1 || ignore_checks) {
_mode = Auto_Loiter;
// reject switching to auto mode if landed with motors armed but first command is not a takeoff (reduce chance of flips)
if (motors->armed() && copter.ap.land_complete && !mission.starts_with_takeoff_cmd()) {
gcs().send_text(MAV_SEVERITY_CRITICAL, "Auto: Missing Takeoff Cmd");
return false;
}
// stop ROI from carrying over from previous runs of the mission
// To-Do: reset the yaw as part of auto_wp_start when the previous command was not a wp command to remove the need for this special ROI check
if (auto_yaw.mode() == AUTO_YAW_ROI) {
auto_yaw.set_mode(AUTO_YAW_HOLD);
}
// initialise waypoint and spline controller
wp_nav->wp_and_spline_init();
// clear guided limits
copter.mode_guided.limit_clear();
// don't start the mission until we have an origin
Location loc;
if (copter.ahrs.get_origin(loc)) {
// start/resume the mission (based on MIS_RESTART parameter)
mission.start_or_resume();
waiting_for_origin = false;
} else {
waiting_for_origin = true;
}
return true;
} else {
return false;
}
}
// auto_run - runs the auto controller
// should be called at 100hz or more
// relies on run_autopilot being called at 10hz which handles decision making and non-navigation related commands
void ModeAuto::run()
{
// call the correct auto controller
switch (_mode) {
case Auto_TakeOff:
takeoff_run();
break;
case Auto_WP:
case Auto_CircleMoveToEdge:
wp_run();
break;
case Auto_Land:
land_run();
break;
case Auto_RTL:
rtl_run();
break;
case Auto_Circle:
circle_run();
break;
case Auto_Spline:
spline_run();
break;
case Auto_NavGuided:
#if NAV_GUIDED == ENABLED
nav_guided_run();
#endif
break;
case Auto_Loiter:
loiter_run();
break;
case Auto_LoiterToAlt:
loiter_to_alt_run();
break;
case Auto_NavPayloadPlace:
payload_place_run();
break;
}
}
bool ModeAuto::allows_arming(bool from_gcs) const
{
return (copter.g2.auto_options & (uint32_t)Options::AllowArming) != 0;
};
// auto_loiter_start - initialises loitering in auto mode
// returns success/failure because this can be called by exit_mission
bool ModeAuto::loiter_start()
{
// return failure if GPS is bad
if (!copter.position_ok()) {
return false;
}
_mode = Auto_Loiter;
// calculate stopping point
Vector3f stopping_point;
wp_nav->get_wp_stopping_point(stopping_point);
// initialise waypoint controller target to stopping point
wp_nav->set_wp_destination(stopping_point);
// hold yaw at current heading
auto_yaw.set_mode(AUTO_YAW_HOLD);
return true;
}
// auto_rtl_start - initialises RTL in AUTO flight mode
void ModeAuto::rtl_start()
{
_mode = Auto_RTL;
// call regular rtl flight mode initialisation and ask it to ignore checks
copter.mode_rtl.init(true);
}
// auto_takeoff_start - initialises waypoint controller to implement take-off
void ModeAuto::takeoff_start(const Location& dest_loc)
{
_mode = Auto_TakeOff;
Location dest(dest_loc);
if (!copter.current_loc.initialised()) {
// vehicle doesn't know where it is ATM. We should not
// initialise our takeoff destination without knowing this!
return;
}
// set horizontal target
dest.lat = copter.current_loc.lat;
dest.lng = copter.current_loc.lng;
// get altitude target
int32_t alt_target;
if (!dest.get_alt_cm(Location::AltFrame::ABOVE_HOME, alt_target)) {
// this failure could only happen if take-off alt was specified as an alt-above terrain and we have no terrain data
AP::logger().Write_Error(LogErrorSubsystem::TERRAIN, LogErrorCode::MISSING_TERRAIN_DATA);
// fall back to altitude above current altitude
alt_target = copter.current_loc.alt + dest.alt;
}
// sanity check target
if (alt_target < copter.current_loc.alt) {
dest.set_alt_cm(copter.current_loc.alt, Location::AltFrame::ABOVE_HOME);
}
// Note: if taking off from below home this could cause a climb to an unexpectedly high altitude
if (alt_target < 100) {
dest.set_alt_cm(100, Location::AltFrame::ABOVE_HOME);
}
// set waypoint controller target
if (!wp_nav->set_wp_destination(dest)) {
// failure to set destination can only be because of missing terrain data
copter.failsafe_terrain_on_event();
return;
}
// initialise yaw
auto_yaw.set_mode(AUTO_YAW_HOLD);
// clear i term when we're taking off
set_throttle_takeoff();
// get initial alt for WP_NAVALT_MIN
auto_takeoff_set_start_alt();
}
// auto_wp_start - initialises waypoint controller to implement flying to a particular destination
void ModeAuto::wp_start(const Location& dest_loc)
{
_mode = Auto_WP;
// send target to waypoint controller
if (!wp_nav->set_wp_destination(dest_loc)) {
// failure to set destination can only be because of missing terrain data
copter.failsafe_terrain_on_event();
return;
}
// initialise yaw
// To-Do: reset the yaw only when the previous navigation command is not a WP. this would allow removing the special check for ROI
if (auto_yaw.mode() != AUTO_YAW_ROI) {
auto_yaw.set_mode_to_default(false);
}
}
// auto_land_start - initialises controller to implement a landing
void ModeAuto::land_start()
{
// set target to stopping point
Vector3f stopping_point;
loiter_nav->get_stopping_point_xy(stopping_point);
// call location specific land start function
land_start(stopping_point);
}
// auto_land_start - initialises controller to implement a landing
void ModeAuto::land_start(const Vector3f& destination)
{
_mode = Auto_Land;
// initialise loiter target destination
loiter_nav->init_target(destination);
// initialise position and desired velocity
if (!pos_control->is_active_z()) {
pos_control->set_alt_target(inertial_nav.get_altitude());
pos_control->set_desired_velocity_z(inertial_nav.get_velocity_z());
}
// initialise yaw
auto_yaw.set_mode(AUTO_YAW_HOLD);
// optionally deploy landing gear
copter.landinggear.deploy_for_landing();
}
// auto_circle_movetoedge_start - initialise waypoint controller to move to edge of a circle with it's center at the specified location
// we assume the caller has performed all required GPS_ok checks
void ModeAuto::circle_movetoedge_start(const Location &circle_center, float radius_m)
{
// set circle center
copter.circle_nav->set_center(circle_center);
// set circle radius
if (!is_zero(radius_m)) {
copter.circle_nav->set_radius(radius_m * 100.0f);
}
// check our distance from edge of circle
Vector3f circle_edge_neu;
copter.circle_nav->get_closest_point_on_circle(circle_edge_neu);
float dist_to_edge = (inertial_nav.get_position() - circle_edge_neu).length();
// if more than 3m then fly to edge
if (dist_to_edge > 300.0f) {
// set the state to move to the edge of the circle
_mode = Auto_CircleMoveToEdge;
// convert circle_edge_neu to Location
Location circle_edge(circle_edge_neu);
// convert altitude to same as command
circle_edge.set_alt_cm(circle_center.alt, circle_center.get_alt_frame());
// initialise wpnav to move to edge of circle
if (!wp_nav->set_wp_destination(circle_edge)) {
// failure to set destination can only be because of missing terrain data
copter.failsafe_terrain_on_event();
}
// if we are outside the circle, point at the edge, otherwise hold yaw
const Vector3f &circle_center_neu = copter.circle_nav->get_center();
const Vector3f &curr_pos = inertial_nav.get_position();
float dist_to_center = norm(circle_center_neu.x - curr_pos.x, circle_center_neu.y - curr_pos.y);
// initialise yaw
// To-Do: reset the yaw only when the previous navigation command is not a WP. this would allow removing the special check for ROI
if (auto_yaw.mode() != AUTO_YAW_ROI) {
if (dist_to_center > copter.circle_nav->get_radius() && dist_to_center > 500) {
auto_yaw.set_mode_to_default(false);
} else {
// vehicle is within circle so hold yaw to avoid spinning as we move to edge of circle
auto_yaw.set_mode(AUTO_YAW_HOLD);
}
}
} else {
circle_start();
}
}
// auto_circle_start - initialises controller to fly a circle in AUTO flight mode
// assumes that circle_nav object has already been initialised with circle center and radius
void ModeAuto::circle_start()
{
_mode = Auto_Circle;
// initialise circle controller
copter.circle_nav->init(copter.circle_nav->get_center(), copter.circle_nav->center_is_terrain_alt());
if (auto_yaw.mode() != AUTO_YAW_ROI) {
auto_yaw.set_mode(AUTO_YAW_CIRCLE);
}
}
// auto_spline_start - initialises waypoint controller to implement flying to a particular destination using the spline controller
// seg_end_type can be SEGMENT_END_STOP, SEGMENT_END_STRAIGHT or SEGMENT_END_SPLINE. If Straight or Spline the next_destination should be provided
void ModeAuto::spline_start(const Location& destination, bool stopped_at_start,
AC_WPNav::spline_segment_end_type seg_end_type,
const Location& next_destination)
{
_mode = Auto_Spline;
// initialise wpnav
if (!wp_nav->set_spline_destination(destination, stopped_at_start, seg_end_type, next_destination)) {
// failure to set destination can only be because of missing terrain data
copter.failsafe_terrain_on_event();
return;
}
// initialise yaw
// To-Do: reset the yaw only when the previous navigation command is not a WP. this would allow removing the special check for ROI
if (auto_yaw.mode() != AUTO_YAW_ROI) {
auto_yaw.set_mode_to_default(false);
}
}
#if NAV_GUIDED == ENABLED
// auto_nav_guided_start - hand over control to external navigation controller in AUTO mode
void ModeAuto::nav_guided_start()
{
_mode = Auto_NavGuided;
// call regular guided flight mode initialisation
copter.mode_guided.init(true);
// initialise guided start time and position as reference for limit checking
copter.mode_guided.limit_init_time_and_pos();
}
#endif //NAV_GUIDED
bool ModeAuto::is_landing() const
{
switch(_mode) {
case Auto_Land:
return true;
case Auto_RTL:
return copter.mode_rtl.is_landing();
default:
return false;
}
return false;
}
bool ModeAuto::is_taking_off() const
{
return ((_mode == Auto_TakeOff) && !wp_nav->reached_wp_destination());
}
// auto_payload_place_start - initialises controller to implement a placing
void ModeAuto::payload_place_start()
{
// set target to stopping point
Vector3f stopping_point;
loiter_nav->get_stopping_point_xy(stopping_point);
// call location specific place start function
payload_place_start(stopping_point);
}
// start_command - this function will be called when the ap_mission lib wishes to start a new command
bool ModeAuto::start_command(const AP_Mission::Mission_Command& cmd)
{
// To-Do: logging when new commands start/end
if (copter.should_log(MASK_LOG_CMD)) {
copter.logger.Write_Mission_Cmd(mission, cmd);
}
switch(cmd.id) {
///
/// navigation commands
///
case MAV_CMD_NAV_TAKEOFF: // 22
do_takeoff(cmd);
break;
case MAV_CMD_NAV_WAYPOINT: // 16 Navigate to Waypoint
do_nav_wp(cmd);
break;
case MAV_CMD_NAV_LAND: // 21 LAND to Waypoint
do_land(cmd);
break;
case MAV_CMD_NAV_LOITER_UNLIM: // 17 Loiter indefinitely
do_loiter_unlimited(cmd);
break;
case MAV_CMD_NAV_LOITER_TURNS: //18 Loiter N Times
do_circle(cmd);
break;
case MAV_CMD_NAV_LOITER_TIME: // 19
do_loiter_time(cmd);
break;
case MAV_CMD_NAV_LOITER_TO_ALT:
do_loiter_to_alt(cmd);
break;
case MAV_CMD_NAV_RETURN_TO_LAUNCH: //20
do_RTL();
break;
case MAV_CMD_NAV_SPLINE_WAYPOINT: // 82 Navigate to Waypoint using spline
do_spline_wp(cmd);
break;
#if NAV_GUIDED == ENABLED
case MAV_CMD_NAV_GUIDED_ENABLE: // 92 accept navigation commands from external nav computer
do_nav_guided_enable(cmd);
break;
#endif
case MAV_CMD_NAV_DELAY: // 93 Delay the next navigation command
do_nav_delay(cmd);
break;
case MAV_CMD_NAV_PAYLOAD_PLACE: // 94 place at Waypoint
do_payload_place(cmd);
break;
//
// conditional commands
//
case MAV_CMD_CONDITION_DELAY: // 112
do_wait_delay(cmd);
break;
case MAV_CMD_CONDITION_DISTANCE: // 114
do_within_distance(cmd);
break;
case MAV_CMD_CONDITION_YAW: // 115
do_yaw(cmd);
break;
///
/// do commands
///
case MAV_CMD_DO_CHANGE_SPEED: // 178
do_change_speed(cmd);
break;
case MAV_CMD_DO_SET_HOME: // 179
do_set_home(cmd);
break;
case MAV_CMD_DO_SET_ROI: // 201
// point the copter and camera at a region of interest (ROI)
do_roi(cmd);
break;
case MAV_CMD_DO_MOUNT_CONTROL: // 205
// point the camera to a specified angle
do_mount_control(cmd);
break;
case MAV_CMD_DO_FENCE_ENABLE:
#if AC_FENCE == ENABLED
if (cmd.p1 == 0) { //disable
copter.fence.enable(false);
gcs().send_text(MAV_SEVERITY_INFO, "Fence Disabled");
} else { //enable fence
copter.fence.enable(true);
gcs().send_text(MAV_SEVERITY_INFO, "Fence Enabled");
}
#endif //AC_FENCE == ENABLED
break;
#if NAV_GUIDED == ENABLED
case MAV_CMD_DO_GUIDED_LIMITS: // 220 accept guided mode limits
do_guided_limits(cmd);
break;
#endif
#if WINCH_ENABLED == ENABLED
case MAV_CMD_DO_WINCH: // Mission command to control winch
do_winch(cmd);
break;
#endif
default:
// unable to use the command, allow the vehicle to try the next command
return false;
}
// always return success
return true;
}
// exit_mission - function that is called once the mission completes
void ModeAuto::exit_mission()
{
// play a tone
AP_Notify::events.mission_complete = 1;
// if we are not on the ground switch to loiter or land
if (!copter.ap.land_complete) {
// try to enter loiter but if that fails land
if (!loiter_start()) {
set_mode(Mode::Number::LAND, ModeReason::MISSION_END);
}
} else {
// if we've landed it's safe to disarm
copter.arming.disarm(AP_Arming::Method::MISSIONEXIT);
}
}
// do_guided - start guided mode
bool ModeAuto::do_guided(const AP_Mission::Mission_Command& cmd)
{
// only process guided waypoint if we are in guided mode
if (copter.control_mode != Mode::Number::GUIDED && !(copter.control_mode == Mode::Number::AUTO && mode() == Auto_NavGuided)) {
return false;
}
// switch to handle different commands
switch (cmd.id) {
case MAV_CMD_NAV_WAYPOINT:
{
// set wp_nav's destination
Location dest(cmd.content.location);
return copter.mode_guided.set_destination(dest);
}
case MAV_CMD_CONDITION_YAW:
do_yaw(cmd);
return true;
default:
// reject unrecognised command
return false;
}
return true;
}
uint32_t ModeAuto::wp_distance() const
{
switch (_mode) {
case Auto_Circle:
return copter.circle_nav->get_distance_to_target();
case Auto_WP:
case Auto_CircleMoveToEdge:
default:
return wp_nav->get_wp_distance_to_destination();
}
}
int32_t ModeAuto::wp_bearing() const
{
switch (_mode) {
case Auto_Circle:
return copter.circle_nav->get_bearing_to_target();
case Auto_WP:
case Auto_CircleMoveToEdge:
default:
return wp_nav->get_wp_bearing_to_destination();
}
}
bool ModeAuto::get_wp(Location& destination)
{
switch (_mode) {
case Auto_NavGuided:
return copter.mode_guided.get_wp(destination);
case Auto_WP:
return wp_nav->get_oa_wp_destination(destination);
case Auto_RTL:
return copter.mode_rtl.get_wp(destination);
default:
return false;
}
}
// update mission
void ModeAuto::run_autopilot()
{
Location loc;
if (waiting_for_origin) {
if (copter.ahrs.get_origin(loc)) {
// start/resume the mission (based on MIS_RESTART parameter)
mission.start_or_resume();
waiting_for_origin = false;
}
} else {
mission.update();
}
}
/*******************************************************************************
Verify command Handlers
Each type of mission element has a "verify" operation. The verify
operation returns true when the mission element has completed and we
should move onto the next mission element.
Return true if we do not recognize the command so that we move on to the next command
*******************************************************************************/
// verify_command - callback function called from ap-mission at 10hz or higher when a command is being run
// we double check that the flight mode is AUTO to avoid the possibility of ap-mission triggering actions while we're not in AUTO mode
bool ModeAuto::verify_command(const AP_Mission::Mission_Command& cmd)
{
if (copter.flightmode != &copter.mode_auto) {
return false;
}
bool cmd_complete = false;
switch (cmd.id) {
//
// navigation commands
//
case MAV_CMD_NAV_TAKEOFF:
cmd_complete = verify_takeoff();
break;
case MAV_CMD_NAV_WAYPOINT:
cmd_complete = verify_nav_wp(cmd);
break;
case MAV_CMD_NAV_LAND:
cmd_complete = verify_land();
break;
case MAV_CMD_NAV_PAYLOAD_PLACE:
cmd_complete = verify_payload_place();
break;
case MAV_CMD_NAV_LOITER_UNLIM:
cmd_complete = verify_loiter_unlimited();
break;
case MAV_CMD_NAV_LOITER_TURNS:
cmd_complete = verify_circle(cmd);
break;
case MAV_CMD_NAV_LOITER_TIME:
cmd_complete = verify_loiter_time(cmd);
break;
case MAV_CMD_NAV_LOITER_TO_ALT:
return verify_loiter_to_alt();
case MAV_CMD_NAV_RETURN_TO_LAUNCH:
cmd_complete = verify_RTL();
break;
case MAV_CMD_NAV_SPLINE_WAYPOINT:
cmd_complete = verify_spline_wp(cmd);
break;
#if NAV_GUIDED == ENABLED
case MAV_CMD_NAV_GUIDED_ENABLE:
cmd_complete = verify_nav_guided_enable(cmd);
break;
#endif
case MAV_CMD_NAV_DELAY:
cmd_complete = verify_nav_delay(cmd);
break;
///
/// conditional commands
///
case MAV_CMD_CONDITION_DELAY:
cmd_complete = verify_wait_delay();
break;
case MAV_CMD_CONDITION_DISTANCE:
cmd_complete = verify_within_distance();
break;
case MAV_CMD_CONDITION_YAW:
cmd_complete = verify_yaw();
break;
// do commands (always return true)
case MAV_CMD_DO_CHANGE_SPEED:
case MAV_CMD_DO_SET_HOME:
case MAV_CMD_DO_SET_ROI:
case MAV_CMD_DO_MOUNT_CONTROL:
case MAV_CMD_DO_GUIDED_LIMITS:
case MAV_CMD_DO_FENCE_ENABLE:
case MAV_CMD_DO_WINCH:
cmd_complete = true;
break;
default:
// error message
gcs().send_text(MAV_SEVERITY_WARNING,"Skipping invalid cmd #%i",cmd.id);
// return true if we do not recognize the command so that we move on to the next command
cmd_complete = true;
break;
}
// send message to GCS
if (cmd_complete) {
gcs().send_mission_item_reached_message(cmd.index);
}
return cmd_complete;
}
// takeoff_run - takeoff in auto mode
// called by auto_run at 100hz or more
void ModeAuto::takeoff_run()
{
// if the user doesn't want to raise the throttle we can set it automatically
// note that this can defeat the disarm check on takeoff
if ((copter.g2.auto_options & (int32_t)Options::AllowTakeOffWithoutRaisingThrottle) != 0) {
copter.set_auto_armed(true);
}
auto_takeoff_run();
}
// auto_wp_run - runs the auto waypoint controller
// called by auto_run at 100hz or more
void ModeAuto::wp_run()
{
// process pilot's yaw input
float target_yaw_rate = 0;
if (!copter.failsafe.radio) {
// get pilot's desired yaw rate
target_yaw_rate = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
if (!is_zero(target_yaw_rate)) {
auto_yaw.set_mode(AUTO_YAW_HOLD);
}
}
// if not armed set throttle to zero and exit immediately
if (is_disarmed_or_landed()) {
make_safe_spool_down();
wp_nav->wp_and_spline_init();
return;
}
// set motors to full range
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
// run waypoint controller
copter.failsafe_terrain_set_status(wp_nav->update_wpnav());
// call z-axis position controller (wpnav should have already updated it's alt target)
pos_control->update_z_controller();
// call attitude controller
if (auto_yaw.mode() == AUTO_YAW_HOLD) {
// roll & pitch from waypoint controller, yaw rate from pilot
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(wp_nav->get_roll(), wp_nav->get_pitch(), target_yaw_rate);
} else {
// roll, pitch from waypoint controller, yaw heading from auto_heading()
attitude_control->input_euler_angle_roll_pitch_yaw(wp_nav->get_roll(), wp_nav->get_pitch(), auto_yaw.yaw(), true);
}
}
// auto_spline_run - runs the auto spline controller
// called by auto_run at 100hz or more
void ModeAuto::spline_run()
{
// if not armed set throttle to zero and exit immediately
if (is_disarmed_or_landed()) {
make_safe_spool_down();
wp_nav->wp_and_spline_init();
return;
}
// process pilot's yaw input
float target_yaw_rate = 0;
if (!copter.failsafe.radio) {
// get pilot's desired yaw rate
target_yaw_rate = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
if (!is_zero(target_yaw_rate)) {
auto_yaw.set_mode(AUTO_YAW_HOLD);
}
}
// set motors to full range
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
// run waypoint controller
wp_nav->update_spline();
// call z-axis position controller (wpnav should have already updated it's alt target)
pos_control->update_z_controller();
// call attitude controller
if (auto_yaw.mode() == AUTO_YAW_HOLD) {
// roll & pitch from waypoint controller, yaw rate from pilot
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(wp_nav->get_roll(), wp_nav->get_pitch(), target_yaw_rate);
} else {
// roll, pitch from waypoint controller, yaw heading from auto_heading()
attitude_control->input_euler_angle_roll_pitch_yaw(wp_nav->get_roll(), wp_nav->get_pitch(), auto_yaw.yaw(), true);
}
}
// auto_land_run - lands in auto mode
// called by auto_run at 100hz or more
void ModeAuto::land_run()
{
// if not armed set throttle to zero and exit immediately
if (is_disarmed_or_landed()) {
make_safe_spool_down();
loiter_nav->clear_pilot_desired_acceleration();
loiter_nav->init_target();
return;
}
// set motors to full range
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
land_run_horizontal_control();
land_run_vertical_control();
}
// auto_rtl_run - rtl in AUTO flight mode
// called by auto_run at 100hz or more
void ModeAuto::rtl_run()
{
// call regular rtl flight mode run function
copter.mode_rtl.run(false);
}
// auto_circle_run - circle in AUTO flight mode
// called by auto_run at 100hz or more
void ModeAuto::circle_run()
{
// process pilot's yaw input
float target_yaw_rate = 0;
if (!copter.failsafe.radio) {
// get pilot's desired yaw rate
target_yaw_rate = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
if (!is_zero(target_yaw_rate)) {
auto_yaw.set_mode(AUTO_YAW_HOLD);
}
}
// call circle controller
copter.failsafe_terrain_set_status(copter.circle_nav->update());
// call z-axis position controller
pos_control->update_z_controller();
if (auto_yaw.mode() == AUTO_YAW_HOLD) {
// roll & pitch from waypoint controller, yaw rate from pilot
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(copter.circle_nav->get_roll(), copter.circle_nav->get_pitch(), target_yaw_rate);
} else {
// roll, pitch from waypoint controller, yaw heading from auto_heading()
attitude_control->input_euler_angle_roll_pitch_yaw(copter.circle_nav->get_roll(), copter.circle_nav->get_pitch(), auto_yaw.yaw(), true);
}
}
#if NAV_GUIDED == ENABLED
// auto_nav_guided_run - allows control by external navigation controller
// called by auto_run at 100hz or more
void ModeAuto::nav_guided_run()
{
// call regular guided flight mode run function
copter.mode_guided.run();
}
#endif // NAV_GUIDED
// auto_loiter_run - loiter in AUTO flight mode
// called by auto_run at 100hz or more
void ModeAuto::loiter_run()
{
// if not armed set throttle to zero and exit immediately
if (is_disarmed_or_landed()) {
make_safe_spool_down();
wp_nav->wp_and_spline_init();
return;
}
// accept pilot input of yaw
float target_yaw_rate = 0;
if (!copter.failsafe.radio) {
target_yaw_rate = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
}
// set motors to full range
motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
// run waypoint and z-axis position controller
copter.failsafe_terrain_set_status(wp_nav->update_wpnav());
pos_control->update_z_controller();
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(wp_nav->get_roll(), wp_nav->get_pitch(), target_yaw_rate);
}
// auto_loiter_run - loiter to altitude in AUTO flight mode
// called by auto_run at 100hz or more
void ModeAuto::loiter_to_alt_run()
{
// if not auto armed or motor interlock not enabled set throttle to zero and exit immediately
if (is_disarmed_or_landed() || !motors->get_interlock()) {
zero_throttle_and_relax_ac();
return;
}
// possibly just run the waypoint controller:
if (!loiter_to_alt.reached_destination_xy) {
loiter_to_alt.reached_destination_xy = wp_nav->reached_wp_destination_xy();
if (!loiter_to_alt.reached_destination_xy) {
wp_run();
return;
}
}
if (!loiter_to_alt.loiter_start_done) {
loiter_nav->clear_pilot_desired_acceleration();
loiter_nav->init_target();
_mode = Auto_LoiterToAlt;
loiter_to_alt.loiter_start_done = true;
}
const float alt_error_cm = copter.current_loc.alt - loiter_to_alt.alt;
if (fabsf(alt_error_cm) < 5.0) { // random numbers R US
loiter_to_alt.reached_alt = true;
} else if (alt_error_cm * loiter_to_alt.alt_error_cm < 0) {
// we were above and are now below, or vice-versa
loiter_to_alt.reached_alt = true;
}
loiter_to_alt.alt_error_cm = alt_error_cm;
// loiter...
land_run_horizontal_control();
// Compute a vertical velocity demand such that the vehicle
// approaches the desired altitude.
float target_climb_rate = AC_AttitudeControl::sqrt_controller(
-alt_error_cm,
pos_control->get_pos_z_p().kP(),
pos_control->get_max_accel_z(),
G_Dt);
// get avoidance adjusted climb rate
target_climb_rate = get_avoidance_adjusted_climbrate(target_climb_rate);
pos_control->set_alt_target_from_climb_rate_ff(target_climb_rate, G_Dt, false);
pos_control->update_z_controller();
}
// auto_payload_place_start - initialises controller to implement placement of a load
void ModeAuto::payload_place_start(const Vector3f& destination)
{
_mode = Auto_NavPayloadPlace;
nav_payload_place.state = PayloadPlaceStateType_Calibrating_Hover_Start;
// initialise loiter target destination
loiter_nav->init_target(destination);
// initialise position and desired velocity
if (!pos_control->is_active_z()) {
pos_control->set_alt_target(inertial_nav.get_altitude());
pos_control->set_desired_velocity_z(inertial_nav.get_velocity_z());
}
// initialise yaw
auto_yaw.set_mode(AUTO_YAW_HOLD);
}
// auto_payload_place_run - places an object in auto mode
// called by auto_run at 100hz or more
void ModeAuto::payload_place_run()
{
if (!payload_place_run_should_run()) {
zero_throttle_and_relax_ac();
// set target to current position
loiter_nav->clear_pilot_desired_acceleration();