Skip to content

Commit

Permalink
FlightTask: properly initialize EKF reset counters
Browse files Browse the repository at this point in the history
This fixes a race condition when switching from a flight mode that is
not a flight task (e.g.: stabilized). In this case, the reset counters
were initialized to 0 and deltas were applied to the first setpoints if
the EKF had any of its reset counters different from 0.
  • Loading branch information
bresch authored and dagar committed Feb 5, 2025
1 parent de650ca commit 8d296a5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
3 changes: 0 additions & 3 deletions src/modules/flight_mode_manager/FlightModeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,9 @@ FlightTaskError FlightModeManager::switchTask(FlightTaskIndex new_task_index)

// Save current setpoints for the next FlightTask
trajectory_setpoint_s last_setpoint = FlightTask::empty_trajectory_setpoint;
ekf_reset_counters_s last_reset_counters{};

if (isAnyTaskActive()) {
last_setpoint = _current_task.task->getTrajectorySetpoint();
last_reset_counters = _current_task.task->getResetCounters();
}

if (_initTask(new_task_index)) {
Expand All @@ -399,7 +397,6 @@ FlightTaskError FlightModeManager::switchTask(FlightTaskIndex new_task_index)
return FlightTaskError::ActivationFailed;
}

_current_task.task->setResetCounters(last_reset_counters);
_command_failed = false;

return FlightTaskError::NoError;
Expand Down
11 changes: 11 additions & 0 deletions src/modules/flight_mode_manager/tasks/FlightTask/FlightTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ bool FlightTask::activate(const trajectory_setpoint_s &last_setpoint)
{
_resetSetpoints();
_setDefaultConstraints();
initEkfResetCounters();
_time_stamp_activate = hrt_absolute_time();
_gear = empty_landing_gear_default_keep;
return true;
Expand All @@ -24,6 +25,16 @@ void FlightTask::reActivate()
activate(setpoint_preserve_vertical);
}

void FlightTask::initEkfResetCounters()
{
_reset_counters.xy = _sub_vehicle_local_position.get().xy_reset_counter;
_reset_counters.vxy = _sub_vehicle_local_position.get().vxy_reset_counter;
_reset_counters.z = _sub_vehicle_local_position.get().z_reset_counter;
_reset_counters.hagl = _sub_vehicle_local_position.get().dist_bottom_reset_counter;
_reset_counters.vz = _sub_vehicle_local_position.get().vz_reset_counter;
_reset_counters.heading = _sub_vehicle_local_position.get().heading_reset_counter;
}

bool FlightTask::updateInitialize()
{
_time_stamp_current = hrt_absolute_time();
Expand Down
23 changes: 10 additions & 13 deletions src/modules/flight_mode_manager/tasks/FlightTask/FlightTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@
#include <uORB/topics/home_position.h>
#include <lib/geo/geo.h>

struct ekf_reset_counters_s {
uint8_t xy;
uint8_t vxy;
uint8_t z;
uint8_t vz;
uint8_t heading;
uint8_t hagl;
};

class FlightTask : public ModuleParams
{
public:
Expand All @@ -88,6 +79,8 @@ class FlightTask : public ModuleParams
*/
virtual void reActivate();

virtual void initEkfResetCounters();

/**
* To be called to adopt parameters from an arrived vehicle command
* @param command received command message containing the parameters
Expand Down Expand Up @@ -115,9 +108,6 @@ class FlightTask : public ModuleParams
*/
const trajectory_setpoint_s getTrajectorySetpoint();

const ekf_reset_counters_s getResetCounters() const { return _reset_counters; }
void setResetCounters(const ekf_reset_counters_s &counters) { _reset_counters = counters; }

/**
* Get vehicle constraints.
* The constraints can vary with task.
Expand Down Expand Up @@ -236,7 +226,14 @@ class FlightTask : public ModuleParams
float _yaw_setpoint{};
float _yawspeed_setpoint{};

ekf_reset_counters_s _reset_counters{}; ///< Counters for estimator local position resets
struct ekf_reset_counters_s {
uint8_t xy;
uint8_t vxy;
uint8_t z;
uint8_t vz;
uint8_t heading;
uint8_t hagl;
} _reset_counters{}; ///< Counters for estimator local position resets

/**
* Vehicle constraints.
Expand Down

0 comments on commit 8d296a5

Please sign in to comment.