AR_WPNav.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #pragma once
  2. #include <AP_Common/AP_Common.h>
  3. #include <APM_Control/AR_AttitudeControl.h>
  4. #include <AP_Navigation/AP_Navigation.h>
  5. #include <AC_Avoidance/AP_OAPathPlanner.h>
  6. const float AR_WPNAV_HEADING_UNKNOWN = 99999.0f; // used to indicate to set_desired_location method that next leg's heading is unknown
  7. class AR_WPNav {
  8. public:
  9. // constructor
  10. AR_WPNav(AR_AttitudeControl& atc, AP_Navigation& nav_controller);
  11. // update navigation
  12. void update(float dt);
  13. // return desired speed
  14. float get_desired_speed() const { return _desired_speed; }
  15. // set desired speed in m/s
  16. void set_desired_speed(float speed) { _desired_speed = MAX(speed, 0.0f); }
  17. // restore desired speed to default from parameter value
  18. void set_desired_speed_to_default() { _desired_speed = _speed_max; }
  19. // execute the mission in reverse (i.e. drive backwards to destination)
  20. bool get_reversed() const { return _reversed; }
  21. void set_reversed(bool reversed) { _reversed = reversed; }
  22. // get navigation outputs for speed (in m/s) and turn rate (in rad/sec)
  23. float get_speed() const { return _desired_speed_limited; }
  24. float get_turn_rate_rads() const { return _desired_turn_rate_rads; }
  25. // get desired lateral acceleration (for reporting purposes only because will be zero during pivot turns)
  26. float get_lat_accel() const { return _desired_lat_accel; }
  27. // set desired location
  28. // next_leg_bearing_cd should be heading to the following waypoint (used to slow the vehicle in order to make the turn)
  29. bool set_desired_location(const struct Location& destination, float next_leg_bearing_cd = AR_WPNAV_HEADING_UNKNOWN) WARN_IF_UNUSED;
  30. // set desired location to a reasonable stopping point, return true on success
  31. bool set_desired_location_to_stopping_location() WARN_IF_UNUSED;
  32. // set desired location as offset from the EKF origin, return true on success
  33. bool set_desired_location_NED(const Vector3f& destination, float next_leg_bearing_cd = AR_WPNAV_HEADING_UNKNOWN) WARN_IF_UNUSED;
  34. // true if vehicle has reached desired location. defaults to true because this is normally used by missions and we do not want the mission to become stuck
  35. bool reached_destination() const { return _reached_destination; }
  36. // return distance (in meters) to destination
  37. float get_distance_to_destination() const { return _distance_to_destination; }
  38. // return true if destination is valid
  39. bool is_destination_valid() const { return _orig_and_dest_valid; }
  40. // get current destination. Note: this is not guaranteed to be valid (i.e. _orig_and_dest_valid is not checked)
  41. const Location &get_destination() { return _destination; }
  42. // get object avoidance adjusted destination. Note: this is not guaranteed to be valid (i.e. _orig_and_dest_valid is not checked)
  43. const Location &get_oa_destination() { return _oa_destination; }
  44. // return heading (in degrees) and cross track error (in meters) for reporting to ground station (NAV_CONTROLLER_OUTPUT message)
  45. float wp_bearing_cd() const { return _wp_bearing_cd; }
  46. float nav_bearing_cd() const { return _desired_heading_cd; }
  47. float crosstrack_error() const { return _cross_track_error; }
  48. // return the heading (in centi-degrees) to the next waypoint accounting for OA, (used by sailboats)
  49. float oa_wp_bearing_cd() const { return _oa_wp_bearing_cd; }
  50. // settor to allow vehicle code to provide turn related param values to this library (should be updated regularly)
  51. void set_turn_params(float turn_max_g, float turn_radius, bool pivot_possible);
  52. // set default overshoot (used for sailboats)
  53. void set_default_overshoot(float overshoot);
  54. // accessors for parameter values
  55. float get_default_speed() const { return _speed_max; }
  56. float get_radius() const { return _radius; }
  57. float get_overshoot() const { return _overshoot; }
  58. float get_pivot_rate() const { return _pivot_rate; }
  59. // parameter var table
  60. static const struct AP_Param::GroupInfo var_info[];
  61. private:
  62. // true if update has been called recently
  63. bool is_active() const;
  64. // update distance and bearing from vehicle's current position to destination
  65. void update_distance_and_bearing_to_destination();
  66. // calculate steering output to drive along line from origin to destination waypoint
  67. // relies on update_distance_and_bearing_to_destination being called first
  68. void update_steering(const Location& current_loc, float current_speed);
  69. // calculated desired speed(in m/s) based on yaw error and lateral acceleration and/or distance to a waypoint
  70. // relies on update_distance_and_bearing_to_destination and update_steering being run so these internal members
  71. // have been updated: _wp_bearing_cd, _cross_track_error, _distance_to_destination
  72. void update_desired_speed(float dt);
  73. // calculate stopping location using current position and attitude controller provided maximum deceleration
  74. // returns true on success, false on failure
  75. bool get_stopping_location(Location& stopping_loc) WARN_IF_UNUSED;
  76. // returns true if vehicle should pivot turn at next waypoint
  77. bool use_pivot_steering_at_next_WP(float yaw_error_cd) const;
  78. // returns true if vehicle should pivot immediately (because heading error is too large)
  79. bool use_pivot_steering(float yaw_error_cd);
  80. // adjust speed to ensure it does not fall below value held in SPEED_MIN
  81. void apply_speed_min(float &desired_speed);
  82. private:
  83. // parameters
  84. AP_Float _speed_max; // target speed between waypoints in m/s
  85. AP_Float _speed_min; // target speed minimum in m/s. Vehicle will not slow below this speed for corners
  86. AP_Float _radius; // distance in meters from a waypoint when we consider the waypoint has been reached
  87. AP_Float _overshoot; // maximum horizontal overshoot in meters
  88. AP_Int16 _pivot_angle; // angle error that leads to pivot turn
  89. AP_Int16 _pivot_rate; // desired turn rate during pivot turns in deg/sec
  90. // references
  91. AR_AttitudeControl& _atc; // rover attitude control library
  92. AP_Navigation& _nav_controller; // navigation controller (aka L1 controller)
  93. // variables held in vehicle code (for now)
  94. float _turn_max_mss; // lateral acceleration maximum in m/s/s
  95. float _turn_radius; // vehicle turn radius in meters
  96. bool _pivot_possible; // true if vehicle can pivot
  97. bool _pivot_active; // true if vehicle is currently pivoting
  98. // variables for navigation
  99. uint32_t _last_update_ms; // system time of last call to update
  100. Location _origin; // origin Location (vehicle will travel from the origin to the destination)
  101. Location _destination; // destination Location when in Guided_WP
  102. bool _orig_and_dest_valid; // true if the origin and destination have been set
  103. bool _reversed; // execute the mission by backing up
  104. float _desired_speed_final; // desired speed in m/s when we reach the destination
  105. // main outputs from navigation library
  106. float _desired_speed; // desired speed in m/s
  107. float _desired_speed_limited; // desired speed (above) but accel/decel limited and reduced to keep vehicle within _overshoot of line
  108. float _desired_turn_rate_rads; // desired turn-rate in rad/sec (negative is counter clockwise, positive is clockwise)
  109. float _desired_lat_accel; // desired lateral acceleration (for reporting only)
  110. float _desired_heading_cd; // desired heading (back towards line between origin and destination)
  111. float _wp_bearing_cd; // heading to waypoint in centi-degrees
  112. float _cross_track_error; // cross track error (in meters). distance from current position to closest point on line between origin and destination
  113. // variables for reporting
  114. float _distance_to_destination; // distance from vehicle to final destination in meters
  115. bool _reached_destination; // true once the vehicle has reached the destination
  116. // object avoidance variables
  117. bool _oa_active; // true if we should use alternative destination to avoid obstacles
  118. Location _oa_origin; // intermediate origin during avoidance
  119. Location _oa_destination; // intermediate destination during avoidance
  120. float _oa_distance_to_destination; // OA (object avoidance) distance from vehicle to _oa_destination in meters
  121. float _oa_wp_bearing_cd; // OA adjusted heading to _oa_destination in centi-degrees
  122. };