Firmware
FlightTasks.hpp
1 /****************************************************************************
2  *
3  * Copyright (c) 2017 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
42 #pragma once
43 
44 #include "FlightTask.hpp"
45 #include "SubscriptionArray.hpp"
46 #include "FlightTasks_generated.hpp"
48 
49 #include <new>
50 
52 {
53 public:
54  FlightTasks();
55 
56  ~FlightTasks()
57  {
58  if (_current_task.task) {
59  _current_task.task->~FlightTask();
60  }
61  }
62 
67  bool update();
68 
73  const vehicle_local_position_setpoint_s getPositionSetpoint();
74 
79  const vehicle_constraints_s getConstraints();
80 
85  const landing_gear_s getGear();
86 
91  const vehicle_trajectory_waypoint_s getAvoidanceWaypoint();
92 
97  const vehicle_trajectory_waypoint_s &getEmptyAvoidanceWaypoint();
98 
103  int switchTask() { return switchTask(static_cast<int>(_current_task.index) + 1); }
104 
110  int switchTask(FlightTaskIndex new_task_index);
111  int switchTask(int new_task_index);
112 
117  int getActiveTask() const { return static_cast<int>(_current_task.index); }
118 
123  bool isAnyTaskActive() const { return _current_task.task; }
124 
128  void handleParameterUpdate();
129 
133  const char *errorToString(const int error);
134 
138  void setYawHandler(WeatherVane *ext_yaw_handler) {_current_task.task->setYawHandler(ext_yaw_handler);}
139 
143  void reActivate();
144 
145  void updateVelocityControllerIO(const matrix::Vector3f &vel_sp, const matrix::Vector3f &thrust_sp) {_current_task.task->updateVelocityControllerIO(vel_sp, thrust_sp); }
146 
147 private:
148 
153  TaskUnion _task_union;
155  struct flight_task_t {
156  FlightTask *task;
157  FlightTaskIndex index;
158  };
159  flight_task_t _current_task = {nullptr, FlightTaskIndex::None};
160 
161  SubscriptionArray _subscription_array;
162 
163  struct task_error_t {
164  int error;
165  const char *msg;
166  };
167 
168  static const int _numError = 4;
172  task_error_t _taskError[_numError] = {
173  {0, "No Error"},
174  {-1, "Invalid Task "},
175  {-2, "Subscription Failed"},
176  {-3, "Activation Failed"}
177  };
181  void _updateCommand();
182  FlightTaskIndex switchVehicleCommand(const int command);
183  int _sub_vehicle_command = -1;
184  orb_advert_t _pub_vehicle_command_ack = nullptr;
186  int _initTask(FlightTaskIndex task_index);
187 };
Definition: WeatherVane.hpp:48
const landing_gear_s getGear()
Get landing gear position.
Definition: FlightTasks.cpp:50
const vehicle_local_position_setpoint_s getPositionSetpoint()
Get the output data from the current task.
Definition: FlightTasks.cpp:30
const vehicle_constraints_s getConstraints()
Get task dependent constraints.
Definition: FlightTasks.cpp:40
void reActivate()
This method will re-activate current task.
Definition: FlightTasks.cpp:128
bool update()
Call regularly in the control loop cycle to execute the task.
Definition: FlightTasks.cpp:18
Definition: FlightTask.hpp:58
const char * errorToString(const int error)
Call this method to get the description of a task error.
Definition: FlightTasks.cpp:117
int getActiveTask() const
Get the number of the active task.
Definition: FlightTasks.hpp:117
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
Simple array that contains a dynamic amount of Subscription<T> instances.
bool isAnyTaskActive() const
Check if any task is active.
Definition: FlightTasks.hpp:123
const vehicle_trajectory_waypoint_s getAvoidanceWaypoint()
Get task avoidance desired waypoints.
Abstract base class for different advanced flight tasks like orbit, follow me, ...
const vehicle_trajectory_waypoint_s & getEmptyAvoidanceWaypoint()
Get empty avoidance desired waypoints.
void setYawHandler(WeatherVane *ext_yaw_handler)
Sets an external yaw handler.
Definition: FlightTasks.hpp:138
Definition: SubscriptionArray.hpp:46
Definition: FlightTasks.hpp:51
void handleParameterUpdate()
Call this whenever a parameter update notification is received (parameter_update uORB message) ...
Definition: FlightTasks.cpp:110
int switchTask()
Switch to the next task in the available list (for testing)
Definition: FlightTasks.hpp:103