commands.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "Sub.h"
  2. // checks if we should update ahrs/RTL home position from the EKF
  3. void Sub::update_home_from_EKF()
  4. {
  5. // exit immediately if home already set
  6. if (ahrs.home_is_set()) {
  7. return;
  8. }
  9. // special logic if home is set in-flight
  10. if (motors.armed()) {
  11. set_home_to_current_location_inflight();
  12. } else {
  13. // move home to current ekf location (this will set home_state to HOME_SET)
  14. if (!set_home_to_current_location(false)) {
  15. // ignore this failure
  16. }
  17. }
  18. }
  19. // set_home_to_current_location_inflight - set home to current GPS location (horizontally) and EKF origin vertically
  20. void Sub::set_home_to_current_location_inflight()
  21. {
  22. // get current location from EKF
  23. Location temp_loc;
  24. Location ekf_origin;
  25. if (ahrs.get_location(temp_loc) && ahrs.get_origin(ekf_origin)) {
  26. temp_loc.alt = ekf_origin.alt;
  27. if (!set_home(temp_loc, false)) {
  28. // ignore this failure
  29. }
  30. }
  31. }
  32. // set_home_to_current_location - set home to current GPS location
  33. bool Sub::set_home_to_current_location(bool lock)
  34. {
  35. // get current location from EKF
  36. Location temp_loc;
  37. if (ahrs.get_location(temp_loc)) {
  38. // Make home always at the water's surface.
  39. // This allows disarming and arming again at depth.
  40. // This also ensures that mission items with relative altitude frame, are always
  41. // relative to the water's surface, whether in a high elevation lake, or at sea level.
  42. temp_loc.alt -= barometer.get_altitude() * 100.0f;
  43. return set_home(temp_loc, lock);
  44. }
  45. return false;
  46. }
  47. // set_home - sets ahrs home (used for RTL) to specified location
  48. // initialises inertial nav and compass on first call
  49. // returns true if home location set successfully
  50. bool Sub::set_home(const Location& loc, bool lock)
  51. {
  52. // check if EKF origin has been set
  53. Location ekf_origin;
  54. if (!ahrs.get_origin(ekf_origin)) {
  55. return false;
  56. }
  57. const bool home_was_set = ahrs.home_is_set();
  58. // set ahrs home (used for RTL)
  59. if (!ahrs.set_home(loc)) {
  60. return false;
  61. }
  62. // init inav and compass declination
  63. if (!home_was_set) {
  64. // update navigation scalers. used to offset the shrinking longitude as we go towards the poles
  65. scaleLongDown = loc.longitude_scale();
  66. // record home is set
  67. Log_Write_Event(DATA_SET_HOME);
  68. // log new home position which mission library will pull from ahrs
  69. if (should_log(MASK_LOG_CMD)) {
  70. AP_Mission::Mission_Command temp_cmd;
  71. if (mission.read_cmd_from_storage(0, temp_cmd)) {
  72. logger.Write_Mission_Cmd(mission, temp_cmd);
  73. }
  74. }
  75. }
  76. // lock home position
  77. if (lock) {
  78. ahrs.lock_home();
  79. }
  80. // return success
  81. return true;
  82. }
  83. // far_from_EKF_origin - checks if a location is too far from the EKF origin
  84. // returns true if too far
  85. bool Sub::far_from_EKF_origin(const Location& loc)
  86. {
  87. // check distance to EKF origin
  88. Location ekf_origin;
  89. return ahrs.get_origin(ekf_origin) && (ekf_origin.get_distance(loc) > EKF_ORIGIN_MAX_DIST_M);
  90. }