flight_mode.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. break;
  129. default:
  130. break;
  131. }
  132. }
  133. // exit_mode - high level call to organise cleanup as a flight mode is exited
  134. void Sub::exit_mode(control_mode_t old_control_mode, control_mode_t new_control_mode)
  135. {
  136. // stop mission when we leave auto mode
  137. if (old_control_mode == AUTO) {
  138. if (mission.state() == AP_Mission::MISSION_RUNNING) {
  139. mission.stop();
  140. }
  141. #if MOUNT == ENABLED
  142. camera_mount.set_mode_to_default();
  143. #endif // MOUNT == ENABLED
  144. }
  145. }
  146. // returns true or false whether mode requires GPS
  147. bool Sub::mode_requires_GPS(control_mode_t mode)
  148. {
  149. switch (mode) {
  150. case AUTO:
  151. case GUIDED:
  152. case CIRCLE:
  153. case POSHOLD:
  154. return true;
  155. default:
  156. return false;
  157. }
  158. return false;
  159. }
  160. // mode_has_manual_throttle - returns true if the flight mode has a manual throttle (i.e. pilot directly controls throttle)
  161. bool Sub::mode_has_manual_throttle(control_mode_t mode)
  162. {
  163. switch (mode) {
  164. case ACRO:
  165. case STABILIZE:
  166. case MANUAL:
  167. return true;
  168. default:
  169. return false;
  170. }
  171. return false;
  172. }
  173. // mode_allows_arming - returns true if vehicle can be armed in the specified mode
  174. // arming_from_gcs should be set to true if the arming request comes from the ground station
  175. bool Sub::mode_allows_arming(control_mode_t mode, bool arming_from_gcs)
  176. {
  177. return (mode_has_manual_throttle(mode)
  178. || mode == ALT_HOLD
  179. || mode == POSHOLD
  180. || (arming_from_gcs&& mode == GUIDED)
  181. );
  182. }
  183. // notify_flight_mode - sets notify object based on flight mode. Only used for OreoLED notify device
  184. void Sub::notify_flight_mode(control_mode_t mode)
  185. {
  186. switch (mode) {
  187. case AUTO:
  188. case GUIDED:
  189. case CIRCLE:
  190. case SURFACE:
  191. // autopilot modes
  192. AP_Notify::flags.autopilot_mode = true;
  193. break;
  194. default:
  195. // all other are manual flight modes
  196. AP_Notify::flags.autopilot_mode = false;
  197. break;
  198. }
  199. }