Firmware
vtol_att_control_main.h
1 /****************************************************************************
2  *
3  * Copyright (c) 2013, 2014 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 
49 #ifndef VTOL_ATT_CONTROL_MAIN_H
50 #define VTOL_ATT_CONTROL_MAIN_H
51 
52 #include <px4_config.h>
53 #include <px4_defines.h>
54 #include <px4_tasks.h>
55 #include <px4_posix.h>
56 
57 #include <arch/board/board.h>
58 #include <drivers/drv_hrt.h>
59 #include <drivers/drv_pwm_output.h>
60 #include <lib/ecl/geo/geo.h>
61 #include <mathlib/mathlib.h>
62 #include <matrix/math.hpp>
63 #include <parameters/param.h>
64 
65 #include <uORB/topics/actuator_controls.h>
66 #include <uORB/topics/airspeed.h>
67 #include <uORB/topics/manual_control_setpoint.h>
68 #include <uORB/topics/parameter_update.h>
69 #include <uORB/topics/tecs_status.h>
70 #include <uORB/topics/vehicle_attitude.h>
71 #include <uORB/topics/vehicle_attitude_setpoint.h>
72 #include <uORB/topics/vehicle_command.h>
73 #include <uORB/topics/vehicle_command_ack.h>
74 #include <uORB/topics/vehicle_control_mode.h>
75 #include <uORB/topics/vehicle_land_detected.h>
76 #include <uORB/topics/vehicle_local_position.h>
77 #include <uORB/topics/vehicle_local_position_setpoint.h>
78 #include <uORB/topics/position_setpoint_triplet.h>
79 #include <uORB/topics/vtol_vehicle_status.h>
80 
81 #include "tiltrotor.h"
82 #include "tailsitter.h"
83 #include "standard.h"
84 
85 
86 extern "C" __EXPORT int vtol_att_control_main(int argc, char *argv[]);
87 
88 
90 {
91 public:
92 
95 
96  int start(); /* start the task and return OK on success */
97  bool is_fixed_wing_requested();
98  void abort_front_transition(const char *reason);
99 
100  struct actuator_controls_s *get_actuators_fw_in() {return &_actuators_fw_in;}
101  struct actuator_controls_s *get_actuators_mc_in() {return &_actuators_mc_in;}
102  struct actuator_controls_s *get_actuators_out0() {return &_actuators_out_0;}
103  struct actuator_controls_s *get_actuators_out1() {return &_actuators_out_1;}
104  struct airspeed_s *get_airspeed() {return &_airspeed;}
105  struct position_setpoint_triplet_s *get_pos_sp_triplet() {return &_pos_sp_triplet;}
106  struct tecs_status_s *get_tecs_status() {return &_tecs_status;}
107  struct vehicle_attitude_s *get_att() {return &_v_att;}
108  struct vehicle_attitude_setpoint_s *get_att_sp() {return &_v_att_sp;}
109  struct vehicle_attitude_setpoint_s *get_fw_virtual_att_sp() {return &_fw_virtual_att_sp;}
110  struct vehicle_attitude_setpoint_s *get_mc_virtual_att_sp() {return &_mc_virtual_att_sp;}
111  struct vehicle_control_mode_s *get_control_mode() {return &_v_control_mode;}
112  struct vehicle_land_detected_s *get_land_detected() {return &_land_detected;}
113  struct vehicle_local_position_s *get_local_pos() {return &_local_pos;}
114  struct vehicle_local_position_setpoint_s *get_local_pos_sp() {return &_local_pos_sp;}
115  struct vtol_vehicle_status_s *get_vtol_vehicle_status() {return &_vtol_vehicle_status;}
116 
117  struct Params *get_params() {return &_params;}
118 
119 
120 private:
121 //******************flags & handlers******************************************************
122  bool _task_should_exit{false};
123  int _control_task{-1}; //task handle for VTOL attitude controller
124 
125  /* handlers for subscriptions */
126  int _actuator_inputs_fw{-1}; //topic on which the fw_att_controller publishes actuator inputs
127  int _actuator_inputs_mc{-1}; //topic on which the mc_att_controller publishes actuator inputs
128  int _airspeed_sub{-1}; // airspeed subscription
129  int _fw_virtual_att_sp_sub{-1};
130  int _land_detected_sub{-1};
131  int _local_pos_sp_sub{-1}; // setpoint subscription
132  int _local_pos_sub{-1}; // sensor subscription
133  int _manual_control_sp_sub{-1}; //manual control setpoint subscription
134  int _mc_virtual_att_sp_sub{-1};
135  int _params_sub{-1}; //parameter updates subscription
136  int _pos_sp_triplet_sub{-1}; // local position setpoint subscription
137  int _tecs_status_sub{-1};
138  int _v_att_sub{-1}; //vehicle attitude subscription
139  int _v_control_mode_sub{-1}; //vehicle control mode subscription
140  int _vehicle_cmd_sub{-1};
141 
142  //handlers for publishers
143  orb_advert_t _actuators_0_pub{nullptr}; //input for the mixer (roll,pitch,yaw,thrust)
144  orb_advert_t _mavlink_log_pub{nullptr}; // mavlink log uORB handle
145  orb_advert_t _v_att_sp_pub{nullptr};
146  orb_advert_t _v_cmd_ack_pub{nullptr};
147  orb_advert_t _vtol_vehicle_status_pub{nullptr};
148  orb_advert_t _actuators_1_pub{nullptr};
149 
150 //*******************data containers***********************************************************
151 
152  vehicle_attitude_setpoint_s _v_att_sp{}; //vehicle attitude setpoint
153  vehicle_attitude_setpoint_s _fw_virtual_att_sp{}; // virtual fw attitude setpoint
154  vehicle_attitude_setpoint_s _mc_virtual_att_sp{}; // virtual mc attitude setpoint
155 
156  actuator_controls_s _actuators_fw_in{}; //actuator controls from fw_att_control
157  actuator_controls_s _actuators_mc_in{}; //actuator controls from mc_att_control
158  actuator_controls_s _actuators_out_0{}; //actuator controls going to the mc mixer
159  actuator_controls_s _actuators_out_1{}; //actuator controls going to the fw mixer (used for elevons)
160 
161  airspeed_s _airspeed{}; // airspeed
162  manual_control_setpoint_s _manual_control_sp{}; //manual control setpoint
163  position_setpoint_triplet_s _pos_sp_triplet{};
164  tecs_status_s _tecs_status{};
165  vehicle_attitude_s _v_att{}; //vehicle attitude
166  vehicle_command_s _vehicle_cmd{};
167  vehicle_control_mode_s _v_control_mode{}; //vehicle control mode
168  vehicle_land_detected_s _land_detected{};
169  vehicle_local_position_s _local_pos{};
170  vehicle_local_position_setpoint_s _local_pos_sp{};
171  vtol_vehicle_status_s _vtol_vehicle_status{};
172 
173  Params _params{}; // struct holding the parameters
174 
175  struct {
176  param_t idle_pwm_mc;
177  param_t vtol_motor_count;
178  param_t vtol_fw_permanent_stab;
179  param_t vtol_type;
180  param_t elevons_mc_lock;
181  param_t fw_min_alt;
182  param_t fw_alt_err;
183  param_t fw_qc_max_pitch;
184  param_t fw_qc_max_roll;
185  param_t front_trans_time_openloop;
186  param_t front_trans_time_min;
187  param_t front_trans_duration;
188  param_t back_trans_duration;
189  param_t transition_airspeed;
190  param_t front_trans_throttle;
191  param_t back_trans_throttle;
192  param_t airspeed_blend;
193  param_t airspeed_mode;
194  param_t front_trans_timeout;
195  param_t mpc_xy_cruise;
196  param_t fw_motors_off;
197  param_t diff_thrust;
198  param_t diff_thrust_scale;
199  param_t v19_vt_rolldir;
200  } _params_handles{};
201 
202  /* for multicopters it is usual to have a non-zero idle speed of the engines
203  * for fixed wings we want to have an idle speed of zero since we do not want
204  * to waste energy when gliding. */
205  int _transition_command{vtol_vehicle_status_s::VEHICLE_VTOL_STATE_MC};
206  bool _abort_front_transition{false};
207 
208  VtolType *_vtol_type{nullptr}; // base class for different vtol types
209 
210 //*****************Member functions***********************************************************************
211 
212  void task_main(); //main task
213  static int task_main_trampoline(int argc, char *argv[]); //Shim for calling task_main from task_create.
214 
215  void land_detected_poll();
216  void tecs_status_poll();
217  void vehicle_attitude_poll(); //Check for attitude updates.
218  void vehicle_cmd_poll();
219  void vehicle_control_mode_poll(); //Check for changes in vehicle control mode.
220  void vehicle_manual_poll(); //Check for changes in manual inputs.
221  void actuator_controls_fw_poll(); //Check for changes in fw_attitude_control output
222  void actuator_controls_mc_poll(); //Check for changes in mc_attitude_control output
223  void fw_virtual_att_sp_poll();
224  void mc_virtual_att_sp_poll();
225  void pos_sp_triplet_poll(); // Check for changes in position setpoint values
226  void vehicle_airspeed_poll(); // Check for changes in airspeed
227  void vehicle_local_pos_poll(); // Check for changes in sensor values
228  void vehicle_local_pos_sp_poll(); // Check for changes in setpoint values
229 
230  int parameters_update(); //Update local parameter cache
231 
232  void handle_command();
233 };
234 
235 #endif
~VtolAttitudeControl()
Destructor.
Definition: vtol_att_control_main.cpp:114
Configuration flags used in code.
Includes POSIX-like functions for virtual character devices.
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
High-resolution timer with callouts and timekeeping.
Definition: vtol_type.h:50
Generally used magic defines.
Global flash based parameter store.
Definition: vtol_type.h:111
VtolAttitudeControl()
Constructor.
Definition: vtol_att_control_main.cpp:60
Definition: I2C.hpp:51
Common header for mathlib exports.
Definition: vtol_att_control_main.h:89
VTOL with fixed multirotor motor configurations (such as quad) and a pusher (or puller aka tractor) m...
uint32_t param_t
Parameter handle.
Definition: param.h:98