Skip to content

Commit

Permalink
Add AUTO_TRIM flight mode
Browse files Browse the repository at this point in the history
  • Loading branch information
shellixyz committed Dec 9, 2023
1 parent 5aaed60 commit b53ab9f
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ArduPlane/Attitude.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ void Plane::calc_nav_yaw_coordinated(float speed_scaler)
bool disable_integrator = false;
int16_t rudder_in = rudder_input();

if ((control_mode == &mode_course_hold || control_mode == &mode_cruise) && g2.flight_options & FlightOptions::COURSE_HOLD_HEADING_CONTROL_WITH_YAW_STICK) {
if ((control_mode == &mode_course_hold || control_mode == &mode_auto_trim || control_mode == &mode_cruise) && g2.flight_options & FlightOptions::COURSE_HOLD_HEADING_CONTROL_WITH_YAW_STICK) {
rudder_in = 0;
}

Expand Down
1 change: 1 addition & 0 deletions ArduPlane/GCS_Mavlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ MAV_MODE GCS_MAVLINK_Plane::base_mode() const
#endif
#endif // HAL_QUADPLANE_ENABLED
case Mode::Number::COURSE_HOLD:
case Mode::Number::MODE_AUTO_TRIM:
case Mode::Number::CRUISE:
_base_mode = MAV_MODE_FLAG_STABILIZE_ENABLED;
break;
Expand Down
1 change: 1 addition & 0 deletions ArduPlane/GCS_Plane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void GCS_Plane::update_vehicle_sensor_status_flags(void)
#endif // HAL_QUADPLANE_ENABLED
case Mode::Number::FLY_BY_WIRE_B:
case Mode::Number::COURSE_HOLD:
case Mode::Number::MODE_AUTO_TRIM:
case Mode::Number::CRUISE:
rate_controlled = true;
attitude_stabilized = true;
Expand Down
3 changes: 2 additions & 1 deletion ArduPlane/Plane.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ class Plane : public AP_Vehicle {
ModeFBWA mode_fbwa;
ModeFBWB mode_fbwb;
ModeCourseHold mode_course_hold;
ModeAutoTrim mode_auto_trim;
ModeCruise mode_cruise;
ModeAutoTune mode_autotune;
ModeAuto mode_auto;
Expand Down Expand Up @@ -1326,7 +1327,7 @@ class Plane : public AP_Vehicle {
bool is_auto_throttle_gliding(void) const override { return auto_thr_gliding_state == ATGS_GLIDING; }

bool get_course_hold_heading(uint16_t &locked_heading) const override {
if (control_mode != &mode_cruise && control_mode != &mode_course_hold) {
if (control_mode != &mode_cruise && control_mode != &mode_course_hold && control_mode != &mode_auto_trim) {
return false;
}

Expand Down
1 change: 1 addition & 0 deletions ArduPlane/commands_logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,7 @@ bool Plane::nav_scripting_enable(uint8_t mode)
case Mode::Number::FLY_BY_WIRE_A:
case Mode::Number::FLY_BY_WIRE_B:
case Mode::Number::COURSE_HOLD:
case Mode::Number::MODE_AUTO_TRIM:
case Mode::Number::CRUISE:
case Mode::Number::LOITER:
nav_scripting.enabled = true;
Expand Down
3 changes: 3 additions & 0 deletions ArduPlane/control_modes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Mode *Plane::mode_from_mode_num(const enum Mode::Number num)
case Mode::Number::COURSE_HOLD:
ret = &mode_course_hold;
break;
case Mode::Number::MODE_AUTO_TRIM:
ret = &mode_auto_trim;
break;
case Mode::Number::CRUISE:
ret = &mode_cruise;
break;
Expand Down
2 changes: 2 additions & 0 deletions ArduPlane/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void Plane::failsafe_short_on_event(enum failsafe_state fstype, ModeReason reaso
case Mode::Number::AUTOTUNE:
case Mode::Number::FLY_BY_WIRE_B:
case Mode::Number::COURSE_HOLD:
case Mode::Number::MODE_AUTO_TRIM:
case Mode::Number::CRUISE:
case Mode::Number::TRAINING:
if(plane.emergency_landing) {
Expand Down Expand Up @@ -126,6 +127,7 @@ void Plane::failsafe_long_on_event(enum failsafe_state fstype, ModeReason reason
case Mode::Number::AUTOTUNE:
case Mode::Number::FLY_BY_WIRE_B:
case Mode::Number::COURSE_HOLD:
case Mode::Number::MODE_AUTO_TRIM:
case Mode::Number::CRUISE:
case Mode::Number::TRAINING:
case Mode::Number::CIRCLE:
Expand Down
15 changes: 15 additions & 0 deletions ArduPlane/mode.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Mode
LOITER_ALT_QLAND = 25,
#endif
COURSE_HOLD = 26,
MODE_AUTO_TRIM = 27,
};

// Constructor
Expand Down Expand Up @@ -435,6 +436,20 @@ class ModeCourseHold : public Mode
bool _enter() override;
};

class ModeAutoTrim : public ModeCourseHold
{
public:

Number mode_number() const override { return Number::MODE_AUTO_TRIM; }
const char *name() const override { return "AUTO TRIM"; }
const char *name4() const override { return "ATRM"; }

protected:

bool _enter() override;
void _exit() override;
};

class ModeCruise : public ModeCourseHold
{
public:
Expand Down
18 changes: 18 additions & 0 deletions ArduPlane/mode_auto_trim.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "mode.h"
#include "Plane.h"

bool ModeAutoTrim::_enter()
{
if (!ModeCourseHold::_enter()) {
return false;
}

plane.servos_auto_trim_start();

return true;
}

void ModeAutoTrim::_exit() {
plane.servos_auto_trim_stop();
ModeCourseHold::_enter();
}
4 changes: 4 additions & 0 deletions ArduPlane/mode_thermal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ bool ModeThermal::exit_heading_aligned() const
int32_t target_heading_cd;
return (!plane.mode_course_hold.get_target_heading_cd(target_heading_cd) || plane.mode_loiter.isHeadingLinedUp_cd(target_heading_cd));
}
case Mode::Number::MODE_AUTO_TRIM: {
int32_t target_heading_cd;
return (!plane.mode_auto_trim.get_target_heading_cd(target_heading_cd) || plane.mode_loiter.isHeadingLinedUp_cd(target_heading_cd));
}
case Mode::Number::CRUISE: {
int32_t target_heading_cd;
return (!plane.mode_cruise.get_target_heading_cd(target_heading_cd) || plane.mode_loiter.isHeadingLinedUp_cd(target_heading_cd));
Expand Down
2 changes: 1 addition & 1 deletion ArduPlane/radio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ int16_t Plane::rudder_input(void)
return 0;
}

if (control_mode->does_auto_navigation() || ((control_mode == &mode_cruise || control_mode == &mode_course_hold) && (g2.flight_options & FlightOptions::COURSE_HOLD_HEADING_CONTROL_WITH_YAW_STICK))) {
if (control_mode->does_auto_navigation() || ((control_mode == &mode_cruise || control_mode == &mode_course_hold || control_mode == &mode_auto_trim) && (g2.flight_options & FlightOptions::COURSE_HOLD_HEADING_CONTROL_WITH_YAW_STICK))) {
return 0;
}

Expand Down
1 change: 1 addition & 0 deletions ArduPlane/reverse_thrust.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ bool Plane::allow_reverse_thrust(void) const
allow |= (g.use_reverse_thrust & USE_REVERSE_THRUST_CIRCLE);
break;
case Mode::Number::COURSE_HOLD:
case Mode::Number::MODE_AUTO_TRIM:
allow |= (g.use_reverse_thrust & USE_REVERSE_THRUST_COURSE_HOLD);
break;
case Mode::Number::CRUISE:
Expand Down
3 changes: 2 additions & 1 deletion ArduPlane/servos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ void Plane::set_servos_controlled(void)
control_mode == &mode_acro ||
control_mode == &mode_fbwa ||
control_mode == &mode_course_hold ||
control_mode == &mode_auto_trim ||
control_mode == &mode_autotune) {
// a manual throttle mode
if (!rc().has_valid_input()) {
Expand Down Expand Up @@ -1336,7 +1337,7 @@ bool Plane::servos_auto_trim_set(const Plane::ServoTrimSetEntry *const trim_set,
void Plane::servos_auto_trim(void)
{
// only in auto modes and FBWA
if ((!control_mode->does_auto_throttle() || control_mode == &mode_takeoff || control_mode == &mode_auto) && control_mode != &mode_course_hold && control_mode != &mode_fbwa) {
if ((!control_mode->does_auto_throttle() || control_mode == &mode_takeoff || control_mode == &mode_auto) && control_mode != &mode_course_hold && control_mode != &mode_auto_trim && control_mode != &mode_fbwa) {
return;
}
if (!hal.util->get_soft_armed()) {
Expand Down
2 changes: 1 addition & 1 deletion modules/mavlink
Submodule mavlink updated 1 files
+1 −1 pymavlink

0 comments on commit b53ab9f

Please sign in to comment.