fence.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "Sub.h"
  2. // Code to integrate AC_Fence library with main ArduSub code
  3. #if AC_FENCE == ENABLED
  4. // fence_check - ask fence library to check for breaches and initiate the response
  5. // called at 1hz
  6. void Sub::fence_check()
  7. {
  8. // ignore any fence activity when not armed
  9. if (!motors.armed()) {
  10. return;
  11. }
  12. const uint8_t orig_breaches = fence.get_breaches();
  13. // check for new breaches; new_breaches is bitmask of fence types breached
  14. const uint8_t new_breaches = fence.check();
  15. // if there is a new breach take action
  16. if (new_breaches) {
  17. // if the user wants some kind of response and motors are armed
  18. if (fence.get_action() != AC_FENCE_ACTION_REPORT_ONLY) {
  19. //
  20. // // disarm immediately if we think we are on the ground or in a manual flight mode with zero throttle
  21. // // don't disarm if the high-altitude fence has been broken because it's likely the user has pulled their throttle to zero to bring it down
  22. // if (ap.land_complete || (mode_has_manual_throttle(control_mode) && ap.throttle_zero && !failsafe.manual_control && ((fence.get_breaches() & AC_FENCE_TYPE_ALT_MAX)== 0))){
  23. // init_disarm_motors();
  24. // }else{
  25. // // if we are within 100m of the fence, RTL
  26. // if (fence.get_breach_distance(new_breaches) <= AC_FENCE_GIVE_UP_DISTANCE) {
  27. // if (!set_mode(RTL, MODE_REASON_FENCE_BREACH)) {
  28. // set_mode(LAND, MODE_REASON_FENCE_BREACH);
  29. // }
  30. // }else{
  31. // // if more than 100m outside the fence just force a land
  32. // set_mode(LAND, MODE_REASON_FENCE_BREACH);
  33. // }
  34. // }
  35. }
  36. AP::logger().Write_Error(LogErrorSubsystem::FAILSAFE_FENCE, LogErrorCode(new_breaches));
  37. } else if (orig_breaches) {
  38. // record clearing of breach
  39. AP::logger().Write_Error(LogErrorSubsystem::FAILSAFE_FENCE, LogErrorCode::ERROR_RESOLVED);
  40. }
  41. }
  42. #endif