PX4 Firmware
PX4 Autopilot Software http://px4.io
FlightTasks.cpp
Go to the documentation of this file.
1 #include "FlightTasks.hpp"
2 
5 
7 {
8  // initialize all flight-tasks
9  // currently this is required to get all parameters read
10  for (int i = 0; i < static_cast<int>(FlightTaskIndex::Count); i++) {
11  _initTask(static_cast<FlightTaskIndex>(i));
12  }
13 
14  // disable all tasks
15  _initTask(FlightTaskIndex::None);
16 }
17 
19 {
21 
22  if (isAnyTaskActive()) {
24  }
25 
26  return false;
27 }
28 
30 {
31  if (isAnyTaskActive()) {
33 
34  } else {
36  }
37 }
38 
40 {
41  if (isAnyTaskActive()) {
43 
44  } else {
46  }
47 }
48 
50 {
51  if (isAnyTaskActive()) {
52  return _current_task.task->getGear();
53 
54  } else {
56  }
57 }
58 
59 FlightTaskError FlightTasks::switchTask(FlightTaskIndex new_task_index)
60 {
61  // switch to the running task, nothing to do
62  if (new_task_index == _current_task.index) {
64  }
65 
66  // Save current setpoints for the next FlightTask
68 
69  if (_initTask(new_task_index)) {
70  // invalid task
72  }
73 
74  if (!_current_task.task) {
75  // no task running
77  }
78 
79  // activation failed
80  if (!_current_task.task->updateInitialize() || !_current_task.task->activate(last_setpoint)) {
82  _current_task.task = nullptr;
83  _current_task.index = FlightTaskIndex::None;
85  }
86 
88 }
89 
91 {
92  // make sure we are in range of the enumeration before casting
93  if (static_cast<int>(FlightTaskIndex::None) <= new_task_index &&
94  static_cast<int>(FlightTaskIndex::Count) > new_task_index) {
95  return switchTask(FlightTaskIndex(new_task_index));
96  }
97 
98  switchTask(FlightTaskIndex::None);
100 }
101 
103 {
104  if (isAnyTaskActive()) {
106  }
107 }
108 
110 {
111  for (auto e : _taskError) {
112  if (static_cast<FlightTaskError>(e.error) == error) {
113  return e.msg;
114  }
115  }
116 
117  return "This error is not mapped to a string or is unknown.";
118 }
119 
121 {
122  if (_current_task.task) {
124  }
125 }
126 
128 {
129  // check if there's any new command
130  bool updated = _sub_vehicle_command.updated();
131 
132  if (!updated) {
133  return;
134  }
135 
136  // get command
139 
140  // check what command it is
141  FlightTaskIndex desired_task = switchVehicleCommand(command.command);
142 
143  if (desired_task == FlightTaskIndex::None) {
144  // ignore all unkown commands
145  return;
146  }
147 
148  // switch to the commanded task
149  FlightTaskError switch_result = switchTask(desired_task);
150  uint8_t cmd_result = vehicle_command_ack_s::VEHICLE_RESULT_FAILED;
151 
152  // if we are in/switched to the desired task
153  if (switch_result >= FlightTaskError::NoError) {
154  cmd_result = vehicle_command_ack_s::VEHICLE_RESULT_ACCEPTED;
155 
156  // if the task is running apply parameters to it and see if it rejects
158  cmd_result = vehicle_command_ack_s::VEHICLE_RESULT_DENIED;
159 
160  // if we just switched and parameters are not accepted, go to failsafe
161  if (switch_result >= FlightTaskError::NoError) {
162  switchTask(FlightTaskIndex::ManualPosition);
163  cmd_result = vehicle_command_ack_s::VEHICLE_RESULT_FAILED;
164  }
165  }
166  }
167 
168  // send back acknowledgment
169  vehicle_command_ack_s command_ack{};
170  command_ack.timestamp = hrt_absolute_time();
171  command_ack.command = command.command;
172  command_ack.result = cmd_result;
173  command_ack.result_param1 = static_cast<int>(switch_result);
174  command_ack.target_system = command.source_system;
175  command_ack.target_component = command.source_component;
176 
177  _pub_vehicle_command_ack.publish(command_ack);
178 }
static const vehicle_local_position_setpoint_s empty_setpoint
Empty setpoint.
Definition: FlightTask.hpp:139
void handleParameterUpdate()
Call this whenever a parameter update notification is received (parameter_update uORB message) ...
Definition: FlightTask.hpp:155
const landing_gear_s & getGear()
Get landing gear position.
Definition: FlightTask.hpp:127
virtual bool activate(vehicle_local_position_setpoint_s last_setpoint)
Call once on the event where you switch to the task.
Definition: FlightTask.cpp:12
const vehicle_local_position_setpoint_s getPositionSetpoint()
Get the output data from the current task.
Definition: FlightTasks.cpp:29
static const landing_gear_s empty_landing_gear_default_keep
default landing gear state
Definition: FlightTask.hpp:150
task_error_t _taskError[_numError]
Map from Error int to user friendly string.
virtual ~FlightTask()=default
const vehicle_constraints_s getConstraints()
Get task dependent constraints.
Definition: FlightTasks.cpp:39
uORB::PublicationQueued< vehicle_command_ack_s > _pub_vehicle_command_ack
bool publish(const T &data)
Publish the struct.
int _initTask(FlightTaskIndex task_index)
FlightTaskError
Definition: FlightTasks.hpp:54
bool update()
Call regularly in the control loop cycle to execute the task.
Definition: FlightTasks.cpp:18
virtual bool update()=0
To be called regularly in the control loop cycle to execute the task.
FlightTaskIndex switchVehicleCommand(const int command)
const vehicle_constraints_s & getConstraints()
Get vehicle constraints.
Definition: FlightTask.hpp:120
virtual bool updateFinalize()
Call after update() to constrain the generated setpoints in order to comply with the constraints of t...
Definition: FlightTask.hpp:107
void reActivate()
This method will re-activate current task.
virtual bool updateInitialize()
Call before activate() or update() to initialize time and input data.
Definition: FlightTask.cpp:27
bool isAnyTaskActive() const
Check if any task is active.
virtual void reActivate()
Call this to reset an active Flight Task.
Definition: FlightTask.cpp:22
void _updateCommand()
Check for vehicle commands (received via MAVLink), evaluate and acknowledge them. ...
bool updated()
Check if there is a new update.
flight_task_t _current_task
virtual bool applyCommandParameters(const vehicle_command_s &command)
To be called to adopt parameters from an arrived vehicle command.
Definition: FlightTask.hpp:86
const vehicle_local_position_setpoint_s getPositionSetpoint()
Get the output data.
Definition: FlightTask.cpp:81
void handleParameterUpdate()
Call this whenever a parameter update notification is received (parameter_update uORB message) ...
FlightTaskError switchTask()
Switch to the next task in the available list (for testing)
uORB::Subscription _sub_vehicle_command
topic handle on which commands are received
const char * errorToString(const FlightTaskError error)
Call this method to get the description of a task error.
const landing_gear_s getGear()
Get landing gear position.
Definition: FlightTasks.cpp:49
static const vehicle_constraints_s empty_constraints
Empty constraints.
Definition: FlightTask.hpp:145
bool copy(void *dst)
Copy the struct.
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).