123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- #include "Sub.h"
- bool Sub::set_mode(control_mode_t mode, mode_reason_t reason)
- {
-
- bool success = false;
-
- if (mode == control_mode) {
- prev_control_mode = control_mode;
- prev_control_mode_reason = control_mode_reason;
- control_mode_reason = reason;
- return true;
- }
- switch (mode) {
- case ACRO:
- success = acro_init();
- break;
- case STABILIZE:
- success = stabilize_init();
- break;
- case ALT_HOLD:
- success = althold_init();
- break;
- case AUTO:
- success = auto_init();
- break;
- case CIRCLE:
- success = circle_init();
- break;
- case GUIDED:
- success = guided_init();
- break;
- case SURFACE:
- success = surface_init();
- break;
- #if POSHOLD_ENABLED == ENABLED
- case POSHOLD:
- success = poshold_init();
- break;
- #endif
- case MANUAL:
- success = manual_init();
- break;
- case MOTOR_DETECT:
- success = motordetect_init();
- break;
- case CLEAN:
- success = clean_init();
- break;
- case SPORT:
- success = sport_init();
- break;
- default:
- success = false;
- break;
- }
-
- if (success) {
-
- exit_mode(control_mode, mode);
- prev_control_mode = control_mode;
- prev_control_mode_reason = control_mode_reason;
- control_mode = mode;
- control_mode_reason = reason;
- logger.Write_Mode(control_mode, control_mode_reason);
- gcs().send_message(MSG_HEARTBEAT);
-
- notify_flight_mode(control_mode);
- #if CAMERA == ENABLED
- camera.set_is_auto_mode(control_mode == AUTO);
- #endif
- #if AC_FENCE == ENABLED
-
-
-
- fence.manual_recovery_start();
- #endif
- } else {
-
- AP::logger().Write_Error(LogErrorSubsystem::FLIGHT_MODE, LogErrorCode(mode));
- }
-
- return success;
- }
- void Sub::update_flight_mode()
- {
- switch (control_mode) {
- case ACRO:
- acro_run();
- break;
- case STABILIZE:
- stabilize_run();
- break;
- case ALT_HOLD:
- althold_run();
- break;
- case AUTO:
- auto_run();
- break;
- case CIRCLE:
- circle_run();
- break;
- case GUIDED:
- guided_run();
- break;
- case SURFACE:
- surface_run();
- break;
- #if POSHOLD_ENABLED == ENABLED
- case POSHOLD:
- poshold_run();
- break;
- #endif
- case MANUAL:
- manual_run();
- break;
- case MOTOR_DETECT:
- motordetect_run();
- break;
- case CLEAN:
- clean_run();
- break;
- case SPORT:
-
- sport_run_alt();
- break;
- default:
- break;
- }
- }
- void Sub::exit_mode(control_mode_t old_control_mode, control_mode_t new_control_mode)
- {
-
- if (old_control_mode == AUTO) {
- if (mission.state() == AP_Mission::MISSION_RUNNING) {
- mission.stop();
- }
- #if MOUNT == ENABLED
- camera_mount.set_mode_to_default();
- #endif
- }
- }
- bool Sub::mode_requires_GPS(control_mode_t mode)
- {
- switch (mode) {
- case AUTO:
- case GUIDED:
- case CIRCLE:
- case POSHOLD:
- return true;
- default:
- return false;
- }
- return false;
- }
- bool Sub::mode_has_manual_throttle(control_mode_t mode)
- {
- switch (mode) {
- case ACRO:
- case STABILIZE:
- case MANUAL:
- return true;
- default:
- return false;
- }
- return false;
- }
- bool Sub::mode_allows_arming(control_mode_t mode, bool arming_from_gcs)
- {
- return (mode_has_manual_throttle(mode)
- || mode == ALT_HOLD
- || mode == POSHOLD
- || (arming_from_gcs&& mode == GUIDED)
- );
- }
- void Sub::notify_flight_mode(control_mode_t mode)
- {
- switch (mode) {
- case AUTO:
- case GUIDED:
- case CIRCLE:
- case SURFACE:
-
- AP_Notify::flags.autopilot_mode = true;
- break;
- default:
-
- AP_Notify::flags.autopilot_mode = false;
- break;
- }
- }
|