AP_AdvancedFailsafe.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #pragma once
  2. /*
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /*
  15. Outback Challenge Failsafe module
  16. Andrew Tridgell and CanberraUAV, August 2012
  17. */
  18. #include <AP_Common/AP_Common.h>
  19. #include <AP_Param/AP_Param.h>
  20. #include <AP_Mission/AP_Mission.h>
  21. #include <AP_RCMapper/AP_RCMapper.h>
  22. #include <inttypes.h>
  23. class AP_AdvancedFailsafe
  24. {
  25. public:
  26. enum control_mode {
  27. AFS_MANUAL = 0,
  28. AFS_STABILIZED = 1,
  29. AFS_AUTO = 2
  30. };
  31. enum state {
  32. STATE_PREFLIGHT = 0,
  33. STATE_AUTO = 1,
  34. STATE_DATA_LINK_LOSS = 2,
  35. STATE_GPS_LOSS = 3
  36. };
  37. enum terminate_action {
  38. TERMINATE_ACTION_TERMINATE = 42,
  39. TERMINATE_ACTION_LAND = 43
  40. };
  41. // Constructor
  42. AP_AdvancedFailsafe(AP_Mission &_mission) :
  43. mission(_mission)
  44. {
  45. AP_Param::setup_object_defaults(this, var_info);
  46. _state = STATE_PREFLIGHT;
  47. _terminate.set(0);
  48. _saved_wp = 0;
  49. }
  50. bool enabled() { return _enable; }
  51. // check that everything is OK
  52. void check(uint32_t last_heartbeat_ms, bool geofence_breached, uint32_t last_valid_rc_ms);
  53. // generate heartbeat msgs, so external failsafe boards are happy
  54. // during sensor calibration
  55. void heartbeat(void);
  56. // return true if we are terminating (deliberately crashing the vehicle)
  57. bool should_crash_vehicle(void);
  58. // enables or disables a GCS based termination, returns true if AFS is in the desired termination state
  59. bool gcs_terminate(bool should_terminate, const char *reason);
  60. // called to set all outputs to termination state
  61. virtual void terminate_vehicle(void) = 0;
  62. // for holding parameters
  63. static const struct AP_Param::GroupInfo var_info[];
  64. bool terminating_vehicle_via_landing() const {
  65. return _terminate_action == TERMINATE_ACTION_LAND;
  66. };
  67. protected:
  68. // setup failsafe values for if FMU firmware stops running
  69. virtual void setup_IO_failsafe(void) = 0;
  70. // return the AFS mapped control mode
  71. virtual enum control_mode afs_mode(void) = 0;
  72. enum state _state;
  73. AP_Mission &mission;
  74. AP_Int8 _enable;
  75. // digital output pins for communicating with the failsafe board
  76. AP_Int8 _heartbeat_pin;
  77. AP_Int8 _manual_pin;
  78. AP_Int8 _terminate_pin;
  79. AP_Int8 _terminate;
  80. AP_Int8 _terminate_action;
  81. // waypoint numbers to jump to on failsafe conditions
  82. AP_Int8 _wp_comms_hold;
  83. AP_Int8 _wp_gps_loss;
  84. AP_Float _qnh_pressure;
  85. AP_Int32 _amsl_limit;
  86. AP_Int32 _amsl_margin_gps;
  87. AP_Float _rc_fail_time_seconds;
  88. AP_Int8 _max_gps_loss;
  89. AP_Int8 _max_comms_loss;
  90. AP_Int8 _enable_geofence_fs;
  91. AP_Int8 _enable_RC_fs;
  92. AP_Int8 _rc_term_manual_only;
  93. AP_Int8 _enable_dual_loss;
  94. AP_Int16 _max_range_km;
  95. bool _heartbeat_pin_value;
  96. // saved waypoint for resuming mission
  97. uint8_t _saved_wp;
  98. // number of times we've lost GPS
  99. uint8_t _gps_loss_count;
  100. // number of times we've lost data link
  101. uint8_t _comms_loss_count;
  102. // last comms loss time
  103. uint32_t _last_comms_loss_ms;
  104. // last GPS loss time
  105. uint32_t _last_gps_loss_ms;
  106. // have the failsafe values been setup?
  107. bool _failsafe_setup:1;
  108. Location _first_location;
  109. bool _have_first_location;
  110. uint32_t _term_range_notice_ms;
  111. bool check_altlimit(void);
  112. private:
  113. // update maximum range check
  114. void max_range_update();
  115. };