Firmware
Commander.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 
34 #ifndef COMMANDER_HPP_
35 #define COMMANDER_HPP_
36 
37 #include "state_machine_helper.h"
39 
40 #include <lib/controllib/blocks.hpp>
41 #include <lib/mathlib/mathlib.h>
42 #include <px4_module.h>
43 #include <px4_module_params.h>
45 
46 // publications
47 #include <uORB/Publication.hpp>
48 #include <uORB/topics/actuator_armed.h>
49 #include <uORB/topics/home_position.h>
50 #include <uORB/topics/vehicle_status.h>
51 
52 // subscriptions
53 #include <uORB/Subscription.hpp>
54 #include <uORB/topics/airspeed.h>
55 #include <uORB/topics/estimator_status.h>
56 #include <uORB/topics/iridiumsbd_status.h>
57 #include <uORB/topics/mission_result.h>
58 #include <uORB/topics/sensor_bias.h>
59 #include <uORB/topics/telemetry_status.h>
60 #include <uORB/topics/vehicle_command.h>
61 #include <uORB/topics/vehicle_global_position.h>
62 #include <uORB/topics/vehicle_local_position.h>
63 
64 using math::constrain;
65 using uORB::Publication;
66 using uORB::Subscription;
67 
68 using namespace time_literals;
69 
70 class Commander : public ModuleBase<Commander>, public ModuleParams
71 {
72 public:
73  Commander();
74  ~Commander();
75 
77  static int task_spawn(int argc, char *argv[]);
78 
80  static Commander *instantiate(int argc, char *argv[]);
81 
83  static int custom_command(int argc, char *argv[]);
84 
86  static int print_usage(const char *reason = nullptr);
87 
89  void run() override;
90 
91  void enable_hil();
92 
93  // TODO: only temporarily static until low priority thread is removed
94  static bool preflight_check(bool report);
95 
96 private:
97 
98  DEFINE_PARAMETERS(
99 
100  (ParamInt<px4::params::NAV_DLL_ACT>) _param_nav_dll_act,
101  (ParamInt<px4::params::COM_DL_LOSS_T>) _param_com_dl_loss_t,
102 
103  (ParamInt<px4::params::COM_HLDL_LOSS_T>) _param_com_hldl_loss_t,
104  (ParamInt<px4::params::COM_HLDL_REG_T>) _param_com_hldl_reg_t,
105 
106  (ParamInt<px4::params::NAV_RCL_ACT>) _param_nav_rcl_act,
107  (ParamFloat<px4::params::COM_RC_LOSS_T>) _param_com_rc_loss_t,
108 
109  (ParamFloat<px4::params::COM_HOME_H_T>) _param_com_home_h_t,
110  (ParamFloat<px4::params::COM_HOME_V_T>) _param_com_home_v_t,
111 
112  (ParamFloat<px4::params::COM_POS_FS_EPH>) _param_com_pos_fs_eph,
113  (ParamFloat<px4::params::COM_POS_FS_EPV>) _param_com_pos_fs_epv,
114  (ParamFloat<px4::params::COM_VEL_FS_EVH>) _param_com_vel_fs_evh,
115 
116  (ParamInt<px4::params::COM_POS_FS_DELAY>) _param_com_pos_fs_delay,
117  (ParamInt<px4::params::COM_POS_FS_PROB>) _param_com_pos_fs_prob,
118  (ParamInt<px4::params::COM_POS_FS_GAIN>) _param_com_pos_fs_gain,
119 
120  (ParamInt<px4::params::COM_LOW_BAT_ACT>) _param_com_low_bat_act,
121  (ParamFloat<px4::params::COM_DISARM_LAND>) _param_com_disarm_land,
122 
123  (ParamInt<px4::params::COM_OBS_AVOID>) _param_com_obs_avoid,
124  (ParamInt<px4::params::COM_OA_BOOT_T>) _param_com_oa_boot_t,
125 
126  (ParamFloat<px4::params::COM_TAS_FS_INNOV>) _tas_innov_threshold,
127  (ParamFloat<px4::params::COM_TAS_FS_INTEG>) _tas_innov_integ_threshold,
128  (ParamInt<px4::params::COM_TAS_FS_T1>) _tas_use_stop_delay,
129  (ParamInt<px4::params::COM_TAS_FS_T2>) _tas_use_start_delay,
130  (ParamInt<px4::params::COM_ASPD_FS_ACT>) _airspeed_fail_action,
131  (ParamFloat<px4::params::COM_ASPD_STALL>) _airspeed_stall,
132  (ParamInt<px4::params::COM_ASPD_FS_DLY>) _airspeed_rtl_delay
133 
134  )
135 
136  const int64_t POSVEL_PROBATION_MIN = 1_s;
137  const int64_t POSVEL_PROBATION_MAX = 100_s;
139  hrt_abstime _last_gpos_fail_time_us{0};
140  hrt_abstime _last_lpos_fail_time_us{0};
141  hrt_abstime _last_lvel_fail_time_us{0};
143  // Probation times for position and velocity validity checks to pass if failed
144  hrt_abstime _gpos_probation_time_us = POSVEL_PROBATION_MIN;
145  hrt_abstime _lpos_probation_time_us = POSVEL_PROBATION_MIN;
146  hrt_abstime _lvel_probation_time_us = POSVEL_PROBATION_MIN;
147 
148  /* class variables used to check for navigation failure after takeoff */
149  hrt_abstime _time_at_takeoff{0};
150  hrt_abstime _time_last_innov_pass{0};
151  bool _nav_test_passed{false};
152  bool _nav_test_failed{false};
154  /* class variables used to check for airspeed sensor failure */
155  bool _tas_check_fail{false};
156  hrt_abstime _time_last_tas_pass{0};
157  hrt_abstime _time_last_tas_fail{0};
158  static constexpr hrt_abstime TAS_INNOV_FAIL_DELAY{1_s};
159  bool _tas_use_inhibit{false};
160  hrt_abstime _time_tas_good_declared{0};
161  hrt_abstime _time_tas_bad_declared{0};
162  hrt_abstime _time_last_airspeed{0};
163  hrt_abstime _time_last_aspd_innov_check{0};
164  char *_airspeed_fault_type = new char[7];
165  float _load_factor_ratio{0.5f};
166  float _apsd_innov_integ_state{0.0f};
168  FailureDetector _failure_detector;
169  bool _failure_detector_termination_printed{false};
170 
171  bool handle_command(vehicle_status_s *status, const vehicle_command_s &cmd, actuator_armed_s *armed,
172  orb_advert_t *command_ack_pub, bool *changed);
173 
174  bool set_home_position();
175  bool set_home_position_alt_only();
176 
177  // Set the main system state based on RC and override device inputs
178  transition_result_t set_main_state(const vehicle_status_s &status, bool *changed);
179 
180  // Enable override (manual reversion mode) on the system
181  transition_result_t set_main_state_override_on(const vehicle_status_s &status, bool *changed);
182 
183  // Set the system main state based on the current RC inputs
184  transition_result_t set_main_state_rc(const vehicle_status_s &status, bool *changed);
185 
186  void check_valid(const hrt_abstime &timestamp, const hrt_abstime &timeout, const bool valid_in, bool *valid_out,
187  bool *changed);
188 
189  bool check_posvel_validity(const bool data_valid, const float data_accuracy, const float required_accuracy,
190  const hrt_abstime &data_timestamp_us, hrt_abstime *last_fail_time_us, hrt_abstime *probation_time_us, bool *valid_state,
191  bool *validity_changed);
192 
193  void reset_posvel_validity(bool *changed);
194 
195  void mission_init();
196 
197  void estimator_check(bool *status_changed);
198 
199  void airspeed_use_check();
200 
201  void battery_status_check();
202 
206  void data_link_check(bool &status_changed);
207 
208  int _telemetry_status_sub{-1};
209 
210  hrt_abstime _datalink_last_heartbeat_gcs{0};
211 
212  hrt_abstime _datalink_last_heartbeat_onboard_controller{0};
213  bool _onboard_controller_lost{false};
214 
215  hrt_abstime _datalink_last_heartbeat_avoidance_system{0};
216  bool _avoidance_system_lost{false};
217 
218  bool _avoidance_system_status_change{false};
219  uint8_t _datalink_last_status_avoidance_system{telemetry_status_s::MAV_STATE_UNINIT};
220 
221  int _iridiumsbd_status_sub{-1};
222 
223  hrt_abstime _high_latency_datalink_heartbeat{0};
224  hrt_abstime _high_latency_datalink_lost{0};
225 
226  int _battery_sub{-1};
227  uint8_t _battery_warning{battery_status_s::BATTERY_WARNING_NONE};
228  float _battery_current{0.0f};
229 
230  systemlib::Hysteresis _auto_disarm_landed{false};
231  systemlib::Hysteresis _auto_disarm_killed{false};
232 
233  bool _print_avoidance_msg_once{false};
234 
235  // Subscriptions
236  Subscription<airspeed_s> _airspeed_sub{ORB_ID(airspeed)};
237  Subscription<estimator_status_s> _estimator_status_sub{ORB_ID(estimator_status)};
238  Subscription<mission_result_s> _mission_result_sub{ORB_ID(mission_result)};
239  Subscription<sensor_bias_s> _sensor_bias_sub{ORB_ID(sensor_bias)};
240  Subscription<vehicle_global_position_s> _global_position_sub{ORB_ID(vehicle_global_position)};
241  Subscription<vehicle_local_position_s> _local_position_sub{ORB_ID(vehicle_local_position)};
242 
243  Publication<home_position_s> _home_pub{ORB_ID(home_position)};
244 
245  orb_advert_t _status_pub{nullptr};
246 };
247 
248 #endif /* COMMANDER_HPP_ */
Definition: px4_param.h:313
State machine helper functions definitions.
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
Base class for failure detection logic based on vehicle states for failsafe triggering.
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
Definition: px4_param.h:318
C++ base class for modules/classes using configuration parameters.
Definition: px4_module_params.h:46
Definition: Commander.hpp:70
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
Hysteresis of a boolean value.
Common header for mathlib exports.
Definition: FailureDetector.hpp:65
Subscription wrapper class.
Definition: Subscription.hpp:143
Publication wrapper class.
Definition: Publication.hpp:122
Definition: hysteresis.h:49