flight_mode.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "Sub.h"
  2. // change flight mode and perform any necessary initialisation
  3. // returns true if mode was successfully set
  4. bool Sub::set_mode(control_mode_t mode, mode_reason_t reason)
  5. {
  6. // boolean to record if flight mode could be set
  7. bool success = false;
  8. // return immediately if we are already in the desired mode
  9. if (mode == control_mode) {
  10. prev_control_mode = control_mode;
  11. prev_control_mode_reason = control_mode_reason;
  12. control_mode_reason = reason;
  13. return true;
  14. }
  15. switch (mode) {
  16. case ACRO:
  17. success = acro_init();
  18. break;
  19. case STABILIZE:
  20. success = stabilize_init();
  21. break;
  22. case ALT_HOLD:
  23. success = althold_init();
  24. break;
  25. case AUTO:
  26. success = auto_init();
  27. break;
  28. case CIRCLE:
  29. success = circle_init();
  30. break;
  31. case GUIDED:
  32. success = guided_init();
  33. break;
  34. case SURFACE:
  35. success = surface_init();
  36. break;
  37. #if POSHOLD_ENABLED == ENABLED
  38. case POSHOLD:
  39. success = poshold_init();
  40. break;
  41. #endif
  42. case MANUAL:
  43. success = manual_init();
  44. break;
  45. case MOTOR_DETECT:
  46. success = motordetect_init();
  47. break;
  48. case CLEAN:
  49. success = clean_init();
  50. break;
  51. case SPORT:
  52. success = sport_init();
  53. break;
  54. default:
  55. success = false;
  56. break;
  57. }
  58. // update flight mode
  59. if (success) {
  60. // perform any cleanup required by previous flight mode
  61. exit_mode(control_mode, mode);
  62. prev_control_mode = control_mode;
  63. prev_control_mode_reason = control_mode_reason;
  64. control_mode = mode;
  65. control_mode_reason = reason;
  66. logger.Write_Mode(control_mode, control_mode_reason);
  67. gcs().send_message(MSG_HEARTBEAT);
  68. // update notify object
  69. notify_flight_mode(control_mode);
  70. #if CAMERA == ENABLED
  71. camera.set_is_auto_mode(control_mode == AUTO);
  72. #endif
  73. #if AC_FENCE == ENABLED
  74. // pilot requested flight mode change during a fence breach indicates pilot is attempting to manually recover
  75. // this flight mode change could be automatic (i.e. fence, battery, GPS or GCS failsafe)
  76. // but it should be harmless to disable the fence temporarily in these situations as well
  77. fence.manual_recovery_start();
  78. #endif
  79. } else {
  80. // Log error that we failed to enter desired flight mode
  81. AP::logger().Write_Error(LogErrorSubsystem::FLIGHT_MODE, LogErrorCode(mode));
  82. }
  83. // return success or failure
  84. return success;
  85. }
  86. // update_flight_mode - calls the appropriate attitude controllers based on flight mode
  87. // called at 100hz or more
  88. void Sub::update_flight_mode()
  89. {
  90. switch (control_mode) {
  91. case ACRO:
  92. acro_run();
  93. break;
  94. case STABILIZE:
  95. stabilize_run();
  96. break;
  97. case ALT_HOLD:
  98. althold_run();
  99. break;
  100. case AUTO:
  101. auto_run();
  102. break;
  103. case CIRCLE:
  104. circle_run();
  105. break;
  106. case GUIDED:
  107. guided_run();
  108. break;
  109. case SURFACE:
  110. surface_run();
  111. break;
  112. #if POSHOLD_ENABLED == ENABLED
  113. case POSHOLD:
  114. poshold_run();
  115. break;
  116. #endif
  117. case MANUAL:
  118. manual_run();
  119. break;
  120. case MOTOR_DETECT:
  121. motordetect_run();
  122. break;
  123. case CLEAN:
  124. clean_run();
  125. break;
  126. case SPORT:
  127. //sport_run();
  128. sport_run_alt();
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. // exit_mode - high level call to organise cleanup as a flight mode is exited
  135. void Sub::exit_mode(control_mode_t old_control_mode, control_mode_t new_control_mode)
  136. {
  137. // stop mission when we leave auto mode
  138. if (old_control_mode == AUTO) {
  139. if (mission.state() == AP_Mission::MISSION_RUNNING) {
  140. mission.stop();
  141. }
  142. #if MOUNT == ENABLED
  143. camera_mount.set_mode_to_default();
  144. #endif // MOUNT == ENABLED
  145. }
  146. }
  147. // returns true or false whether mode requires GPS
  148. bool Sub::mode_requires_GPS(control_mode_t mode)
  149. {
  150. switch (mode) {
  151. case AUTO:
  152. case GUIDED:
  153. case CIRCLE:
  154. case POSHOLD:
  155. return true;
  156. default:
  157. return false;
  158. }
  159. return false;
  160. }
  161. // mode_has_manual_throttle - returns true if the flight mode has a manual throttle (i.e. pilot directly controls throttle)
  162. bool Sub::mode_has_manual_throttle(control_mode_t mode)
  163. {
  164. switch (mode) {
  165. case ACRO:
  166. case STABILIZE:
  167. case MANUAL:
  168. return true;
  169. default:
  170. return false;
  171. }
  172. return false;
  173. }
  174. // mode_allows_arming - returns true if vehicle can be armed in the specified mode
  175. // arming_from_gcs should be set to true if the arming request comes from the ground station
  176. bool Sub::mode_allows_arming(control_mode_t mode, bool arming_from_gcs)
  177. {
  178. return (mode_has_manual_throttle(mode)
  179. || mode == ALT_HOLD
  180. || mode == POSHOLD
  181. || (arming_from_gcs&& mode == GUIDED)
  182. );
  183. }
  184. // notify_flight_mode - sets notify object based on flight mode. Only used for OreoLED notify device
  185. void Sub::notify_flight_mode(control_mode_t mode)
  186. {
  187. switch (mode) {
  188. case AUTO:
  189. case GUIDED:
  190. case CIRCLE:
  191. case SURFACE:
  192. // autopilot modes
  193. AP_Notify::flags.autopilot_mode = true;
  194. break;
  195. default:
  196. // all other are manual flight modes
  197. AP_Notify::flags.autopilot_mode = false;
  198. break;
  199. }
  200. }