Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/ArduPlane/mode_guided.cpp
Views: 1798
#include "mode.h"1#include "Plane.h"23bool ModeGuided::_enter()4{5plane.guided_throttle_passthru = false;6/*7when entering guided mode we set the target as the current8location. This matches the behaviour of the copter code9*/10Location loc{plane.current_loc};1112#if HAL_QUADPLANE_ENABLED13if (plane.quadplane.guided_mode_enabled()) {14/*15if using Q_GUIDED_MODE then project forward by the stopping distance16*/17loc.offset_bearing(degrees(ahrs.groundspeed_vector().angle()),18plane.quadplane.stopping_distance());19}20#endif2122// set guided radius to WP_LOITER_RAD on mode change.23active_radius_m = 0;2425plane.set_guided_WP(loc);26return true;27}2829void ModeGuided::update()30{31#if HAL_QUADPLANE_ENABLED32if (plane.auto_state.vtol_loiter && plane.quadplane.available()) {33plane.quadplane.guided_update();34return;35}36#endif3738// Received an external msg that guides roll in the last 3 seconds?39if (plane.guided_state.last_forced_rpy_ms.x > 0 &&40millis() - plane.guided_state.last_forced_rpy_ms.x < 3000) {41plane.nav_roll_cd = constrain_int32(plane.guided_state.forced_rpy_cd.x, -plane.roll_limit_cd, plane.roll_limit_cd);42plane.update_load_factor();4344#if AP_PLANE_OFFBOARD_GUIDED_SLEW_ENABLED45// guided_state.target_heading is radians at this point between -pi and pi ( defaults to -4 )46// This function is used in Guided and AvoidADSB, check for guided47} else if ((plane.control_mode == &plane.mode_guided) && (plane.guided_state.target_heading_type != GUIDED_HEADING_NONE) ) {48uint32_t tnow = AP_HAL::millis();49float delta = (tnow - plane.guided_state.target_heading_time_ms) * 1e-3f;50plane.guided_state.target_heading_time_ms = tnow;5152float error = 0.0f;53if (plane.guided_state.target_heading_type == GUIDED_HEADING_HEADING) {54error = wrap_PI(plane.guided_state.target_heading - AP::ahrs().get_yaw());55} else {56Vector2f groundspeed = AP::ahrs().groundspeed_vector();57error = wrap_PI(plane.guided_state.target_heading - atan2f(-groundspeed.y, -groundspeed.x) + M_PI);58}5960float bank_limit = degrees(atanf(plane.guided_state.target_heading_accel_limit/GRAVITY_MSS)) * 1e2f;61bank_limit = MIN(bank_limit, plane.roll_limit_cd);6263// push error into AC_PID64const float desired = plane.g2.guidedHeading.update_error(error, delta, plane.guided_state.target_heading_limit);6566// Check for output saturation67plane.guided_state.target_heading_limit = fabsf(desired) >= bank_limit;6869plane.nav_roll_cd = constrain_int32(desired, -bank_limit, bank_limit);70plane.update_load_factor();7172#endif // AP_PLANE_OFFBOARD_GUIDED_SLEW_ENABLED73} else {74plane.calc_nav_roll();75}7677if (plane.guided_state.last_forced_rpy_ms.y > 0 &&78millis() - plane.guided_state.last_forced_rpy_ms.y < 3000) {79plane.nav_pitch_cd = constrain_int32(plane.guided_state.forced_rpy_cd.y, plane.pitch_limit_min*100, plane.aparm.pitch_limit_max.get()*100);80} else {81plane.calc_nav_pitch();82}8384// Throttle output85if (plane.guided_throttle_passthru) {86// manual passthrough of throttle in fence breach87SRV_Channels::set_output_scaled(SRV_Channel::k_throttle, plane.get_throttle_input(true));8889} else if (plane.aparm.throttle_cruise > 1 &&90plane.guided_state.last_forced_throttle_ms > 0 &&91millis() - plane.guided_state.last_forced_throttle_ms < 3000) {92// Received an external msg that guides throttle in the last 3 seconds?93SRV_Channels::set_output_scaled(SRV_Channel::k_throttle, plane.guided_state.forced_throttle);9495} else {96// TECS control97plane.calc_throttle();9899}100101}102103void ModeGuided::navigate()104{105plane.update_loiter(active_radius_m);106}107108bool ModeGuided::handle_guided_request(Location target_loc)109{110// add home alt if needed111if (target_loc.relative_alt) {112target_loc.alt += plane.home.alt;113target_loc.relative_alt = 0;114}115116plane.set_guided_WP(target_loc);117118return true;119}120121void ModeGuided::set_radius_and_direction(const float radius, const bool direction_is_ccw)122{123// constrain to (uint16_t) range for update_loiter()124active_radius_m = constrain_int32(fabsf(radius), 0, UINT16_MAX);125plane.loiter.direction = direction_is_ccw ? -1 : 1;126}127128void ModeGuided::update_target_altitude()129{130#if AP_PLANE_OFFBOARD_GUIDED_SLEW_ENABLED131// target altitude can be negative (e.g. flying below home altitude from the top of a mountain)132if (((plane.guided_state.target_alt_time_ms != 0) || plane.guided_state.target_location.alt != -1 )) { // target_alt now defaults to -1, and _time_ms defaults to zero.133// offboard altitude demanded134uint32_t now = AP_HAL::millis();135float delta = 1e-3f * (now - plane.guided_state.target_alt_time_ms);136plane.guided_state.target_alt_time_ms = now;137// determine delta accurately as a float138float delta_amt_f = delta * plane.guided_state.target_alt_rate;139// then scale x100 to match last_target_alt and convert to a signed int32_t as it may be negative140int32_t delta_amt_i = (int32_t)(100.0 * delta_amt_f);141// To calculate the required velocity (up or down), we need to target and current altitudes in the target frame142const Location::AltFrame target_frame = plane.guided_state.target_location.get_alt_frame();143int32_t target_alt_previous_cm;144if (plane.current_loc.initialised() && plane.guided_state.target_location.initialised() &&145plane.current_loc.get_alt_cm(target_frame, target_alt_previous_cm)) {146// create a new interim target location that that takes current_location and moves delta_amt_i in the right direction147int32_t temp_alt_cm = constrain_int32(plane.guided_state.target_location.alt, target_alt_previous_cm - delta_amt_i, target_alt_previous_cm + delta_amt_i);148Location temp_location = plane.guided_state.target_location;149temp_location.set_alt_cm(temp_alt_cm, target_frame);150151// incrementally step the altitude towards the target152plane.set_target_altitude_location(temp_location);153}154} else155#endif // AP_PLANE_OFFBOARD_GUIDED_SLEW_ENABLED156{157Mode::update_target_altitude();158}159}160161162