GCS_Fence.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "GCS.h"
  2. #include <AC_Fence/AC_Fence.h>
  3. MAV_RESULT GCS_MAVLINK::handle_command_do_fence_enable(const mavlink_command_long_t &packet)
  4. {
  5. AC_Fence *fence = AP::fence();
  6. if (fence == nullptr) {
  7. return MAV_RESULT_UNSUPPORTED;
  8. }
  9. switch ((uint16_t)packet.param1) {
  10. case 0:
  11. fence->enable(false);
  12. return MAV_RESULT_ACCEPTED;
  13. case 1:
  14. fence->enable(true);
  15. return MAV_RESULT_ACCEPTED;
  16. default:
  17. return MAV_RESULT_FAILED;
  18. }
  19. }
  20. void GCS_MAVLINK::handle_fence_message(const mavlink_message_t &msg)
  21. {
  22. AC_Fence *fence = AP::fence();
  23. if (fence == nullptr) {
  24. return;
  25. }
  26. // send or receive fence points with GCS
  27. switch (msg.msgid) {
  28. case MAVLINK_MSG_ID_FENCE_POINT:
  29. case MAVLINK_MSG_ID_FENCE_FETCH_POINT:
  30. fence->handle_msg(*this, msg);
  31. break;
  32. default:
  33. #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
  34. AP_HAL::panic("Unhandled common fence message");
  35. #endif
  36. break;
  37. }
  38. }
  39. // fence_send_mavlink_status - send fence status to ground station
  40. void GCS_MAVLINK::send_fence_status() const
  41. {
  42. const AC_Fence *fence = AP::fence();
  43. if (fence == nullptr) {
  44. return;
  45. }
  46. if (!fence->enabled()) {
  47. return;
  48. }
  49. // traslate fence library breach types to mavlink breach types
  50. uint8_t mavlink_breach_type = FENCE_BREACH_NONE;
  51. const uint8_t breaches = fence->get_breaches();
  52. if ((breaches & AC_FENCE_TYPE_ALT_MAX) != 0) {
  53. mavlink_breach_type = FENCE_BREACH_MAXALT;
  54. }
  55. if ((breaches & (AC_FENCE_TYPE_CIRCLE | AC_FENCE_TYPE_POLYGON)) != 0) {
  56. mavlink_breach_type = FENCE_BREACH_BOUNDARY;
  57. }
  58. // send status
  59. mavlink_msg_fence_status_send(chan,
  60. static_cast<int8_t>(fence->get_breaches() != 0),
  61. fence->get_breach_count(),
  62. mavlink_breach_type,
  63. fence->get_breach_time());
  64. }