Flight tasks are the mechanism PX4 uses to generate setpoints for the position/velocity controller within each flight mode. Each task is a class derived from FlightTask that overrides activate() and update() to produce the desired vehicle behavior.
The flight_mode_manager module selects which task is active based on the current flight mode and parameter settings. When switching modes, activate() is called to initialize the task and smoothly take over from the previous setpoints. update() is called every loop iteration to produce new setpoints.
text
Flight task class hierarchy ───────────────────────────────────────────────────────────────── FlightTask (base) ├── FlightTaskManualAltitude │ └── FlightTaskManualAltitudeSmoothVel │ └── FlightTaskManualPosition │ └── FlightTaskManualPositionSmoothVel ├── FlightTaskAuto │ ├── FlightTaskAutoFollowTarget │ └── (mission, RTL, takeoff, land setpoints) ├── FlightTaskOrbit ├── FlightTaskDescend ├── FlightTaskTransition └── FlightTaskMyTask (your custom task)
Creating a custom flight task
Create a directory under src/modules/flight_mode_manager/tasks/MyTask/ with FlightTaskMyTask.hpp, FlightTaskMyTask.cpp, and CMakeLists.txt. Add the task to the flight_mode_manager CMakeLists.txt. Wire it to a flight mode via a parameter switch (e.g. MPC_POS_MODE). Test in SITL before hardware.
cpp
// Minimal custom flight task skeleton
#pragma once
#include "FlightTask.hpp"
class FlightTaskMyTask : public FlightTask {
public:
FlightTaskMyTask() = default;
~FlightTaskMyTask() override = default;
bool activate(const trajectory_setpoint_s &last_setpoint) override {
bool ret = FlightTask::activate(last_setpoint);
// Initialize your task state here
return ret;
}
bool update() override {
// Generate setpoints every loop iteration
// Write to _position_setpoint, _velocity_setpoint, etc.
return true;
}
};Tasks live in src/modules/flight_mode_manager/tasks/. The naming convention is the directory matches the task name, and source files use the FlightTask prefix. For flash-constrained boards (like Pixhawk FMUv2), add your task inside the conditional block in CMakeLists.txt.