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_cruise.cpp
Views: 1798
#include "mode.h"1#include "Plane.h"23bool ModeCruise::_enter()4{5locked_heading = false;6lock_timer_ms = 0;78#if HAL_SOARING_ENABLED9// for ArduSoar soaring_controller10plane.g2.soaring_controller.init_cruising();11#endif1213plane.set_target_altitude_current();1415return true;16}1718void ModeCruise::update()19{20/*21in CRUISE mode we use the navigation code to control22roll when heading is locked. Heading becomes unlocked on23any aileron or rudder input24*/25if (plane.channel_roll->get_control_in() != 0 || plane.channel_rudder->get_control_in() != 0) {26locked_heading = false;27lock_timer_ms = 0;28}2930#if AP_SCRIPTING_ENABLED31if (plane.nav_scripting_active()) {32// while a trick is running unlock heading and zero altitude offset33locked_heading = false;34lock_timer_ms = 0;35plane.set_target_altitude_current();36}37#endif3839if (!locked_heading) {40plane.nav_roll_cd = plane.channel_roll->norm_input() * plane.roll_limit_cd;41plane.update_load_factor();42} else {43plane.calc_nav_roll();44}45plane.update_fbwb_speed_height();46}4748/*49handle CRUISE mode, locking heading to GPS course when we have50sufficient ground speed, and no aileron or rudder input51*/52void ModeCruise::navigate()53{54#if AP_SCRIPTING_ENABLED55if (plane.nav_scripting_active()) {56// don't try to navigate while running trick57return;58}59#endif6061// check if we are moving in the direction of the front of the vehicle62const int32_t ground_course_cd = plane.gps.ground_course_cd();63const bool moving_forwards = fabsf(wrap_PI(radians(ground_course_cd * 0.01) - plane.ahrs.get_yaw())) < M_PI_2;6465if (!locked_heading &&66plane.channel_roll->get_control_in() == 0 &&67plane.rudder_input() == 0 &&68plane.gps.status() >= AP_GPS::GPS_OK_FIX_2D &&69plane.gps.ground_speed() >= 3 &&70moving_forwards &&71lock_timer_ms == 0) {72// user wants to lock the heading - start the timer73lock_timer_ms = millis();74}75if (lock_timer_ms != 0 &&76(millis() - lock_timer_ms) > 500) {77// lock the heading after 0.5 seconds of zero heading input78// from user79locked_heading = true;80lock_timer_ms = 0;81locked_heading_cd = ground_course_cd;82plane.prev_WP_loc = plane.current_loc;83}84if (locked_heading) {85plane.next_WP_loc = plane.prev_WP_loc;86// always look 1km ahead87plane.next_WP_loc.offset_bearing(locked_heading_cd*0.01f, plane.prev_WP_loc.get_distance(plane.current_loc) + 1000);88plane.nav_controller->update_waypoint(plane.prev_WP_loc, plane.next_WP_loc);89}90}9192bool ModeCruise::get_target_heading_cd(int32_t &target_heading) const93{94target_heading = locked_heading_cd;95return locked_heading;96}979899