Firmware
navigator.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Copyright (c) 2013-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  ****************************************************************************/
42 #pragma once
43 
44 #include "datalinkloss.h"
45 #include "enginefailure.h"
46 #include "follow_target.h"
47 #include "geofence.h"
48 #include "gpsfailure.h"
49 #include "land.h"
50 #include "precland.h"
51 #include "loiter.h"
52 #include "mission.h"
53 #include "navigator_mode.h"
54 #include "rcloss.h"
55 #include "rtl.h"
56 #include "takeoff.h"
57 
58 #include "navigation.h"
59 
60 #include <lib/perf/perf_counter.h>
61 #include <px4_module.h>
62 #include <px4_module_params.h>
63 #include <uORB/Subscription.hpp>
64 #include <uORB/topics/geofence_result.h>
65 #include <uORB/topics/mission.h>
66 #include <uORB/topics/mission_result.h>
67 #include <uORB/topics/parameter_update.h>
68 #include <uORB/topics/position_controller_status.h>
69 #include <uORB/topics/position_setpoint_triplet.h>
70 #include <uORB/topics/vehicle_command.h>
71 #include <uORB/topics/vehicle_global_position.h>
72 #include <uORB/topics/vehicle_gps_position.h>
73 #include <uORB/topics/vehicle_land_detected.h>
74 #include <uORB/topics/vehicle_local_position.h>
75 #include <uORB/uORB.h>
76 
80 #define NAVIGATOR_MODE_ARRAY_SIZE 11
81 
82 
83 class Navigator : public ModuleBase<Navigator>, public ModuleParams
84 {
85 public:
86  Navigator();
87  ~Navigator() override;
88 
89  Navigator(const Navigator &) = delete;
90  Navigator operator=(const Navigator &) = delete;
91 
93  static int task_spawn(int argc, char *argv[]);
94 
96  static Navigator *instantiate(int argc, char *argv[]);
97 
99  static int custom_command(int argc, char *argv[]);
100 
102  static int print_usage(const char *reason = nullptr);
103 
105  void run() override;
106 
108  int print_status() override;
109 
113  void load_fence_from_file(const char *filename);
114 
119 
120  void publish_vehicle_cmd(vehicle_command_s *vcmd);
121 
132  void fake_traffic(const char *callsign, float distance, float direction, float traffic_heading, float altitude_diff,
133  float hor_velocity, float ver_velocity);
134 
138  void check_traffic();
139 
143  void set_can_loiter_at_sp(bool can_loiter) { _can_loiter_at_sp = can_loiter; }
144  void set_position_setpoint_triplet_updated() { _pos_sp_triplet_updated = true; }
145  void set_mission_result_updated() { _mission_result_updated = true; }
146 
150  struct home_position_s *get_home_position() { return &_home_pos; }
151  struct mission_result_s *get_mission_result() { return &_mission_result; }
152  struct position_setpoint_triplet_s *get_position_setpoint_triplet() { return &_pos_sp_triplet; }
153  struct position_setpoint_triplet_s *get_reposition_triplet() { return &_reposition_triplet; }
154  struct position_setpoint_triplet_s *get_takeoff_triplet() { return &_takeoff_triplet; }
155  struct vehicle_global_position_s *get_global_position() { return &_global_pos; }
156  struct vehicle_land_detected_s *get_land_detected() { return &_land_detected; }
157  struct vehicle_local_position_s *get_local_position() { return &_local_pos; }
158  struct vehicle_status_s *get_vstatus() { return &_vstatus; }
159  PrecLand *get_precland() { return &_precland; }
161  const vehicle_roi_s &get_vroi() { return _vroi; }
162  void reset_vroi() { _vroi = {}; }
163 
164  bool home_alt_valid() { return (_home_pos.timestamp > 0 && _home_pos.valid_alt); }
165  bool home_position_valid() { return (_home_pos.timestamp > 0 && _home_pos.valid_alt && _home_pos.valid_hpos); }
166 
167  int get_mission_sub() { return _mission_sub; }
168 
169  Geofence &get_geofence() { return _geofence; }
170 
171  bool get_can_loiter_at_sp() { return _can_loiter_at_sp; }
172  float get_loiter_radius() { return _param_nav_loiter_rad.get(); }
173 
178 
184  float get_acceptance_radius();
185 
192 
199 
205  float get_cruising_speed();
206 
216  void set_cruising_speed(float speed = -1.0f);
217 
223  void reset_cruising_speed();
224 
225 
229  void reset_triplets();
230 
236  float get_cruising_throttle();
237 
241  void set_cruising_throttle(float throttle = -1.0f) { _mission_throttle = throttle; }
242 
250  float get_acceptance_radius(float mission_item_radius);
251 
260  float get_yaw_acceptance(float mission_item_yaw);
261 
262  orb_advert_t *get_mavlink_log_pub() { return &_mavlink_log_pub; }
263 
264  void increment_mission_instance_count() { _mission_result.instance_count++; }
265 
266  void set_mission_failure(const char *reason);
267 
268  // MISSION
269  bool is_planned_mission() const { return _navigation_mode == &_mission; }
270  bool on_mission_landing() { return _mission.landing(); }
271  bool start_mission_landing() { return _mission.land_start(); }
272  bool mission_start_land_available() { return _mission.get_land_start_available(); }
273 
274  // RTL
275  bool mission_landing_required() { return _rtl.rtl_type() == RTL::RTL_LAND; }
276  int rtl_type() { return _rtl.rtl_type(); }
277  bool in_rtl_state() const { return _vstatus.nav_state == vehicle_status_s::NAVIGATION_STATE_AUTO_RTL; }
278 
279  bool abort_landing();
280 
281  // Param access
282  float get_loiter_min_alt() const { return _param_mis_ltrmin_alt.get(); }
283  float get_takeoff_min_alt() const { return _param_mis_takeoff_alt.get(); }
284  bool get_takeoff_required() const { return _param_mis_takeoff_req.get(); }
285  float get_yaw_timeout() const { return _param_mis_yaw_tmt.get(); }
286  float get_yaw_threshold() const { return _param_mis_yaw_err.get(); }
287 
288  float get_vtol_back_trans_deceleration() const { return _param_back_trans_dec_mss; }
289  float get_vtol_reverse_delay() const { return _param_reverse_delay; }
290 
291  bool force_vtol();
292 
293 private:
294  DEFINE_PARAMETERS(
295  (ParamFloat<px4::params::NAV_LOITER_RAD>) _param_nav_loiter_rad,
296  (ParamFloat<px4::params::NAV_ACC_RAD>) _param_nav_acc_rad,
298  _param_nav_fw_alt_rad,
300  _param_nav_fw_altl_rad,
302  _param_nav_mc_alt_rad,
303  (ParamInt<px4::params::NAV_FORCE_VT>) _param_nav_force_vt,
304  (ParamInt<px4::params::NAV_TRAFF_AVOID>) _param_nav_traff_avoid,
306  // non-navigator parameters
307  // Mission (MIS_*)
308  (ParamFloat<px4::params::MIS_LTRMIN_ALT>) _param_mis_ltrmin_alt,
309  (ParamFloat<px4::params::MIS_TAKEOFF_ALT>) _param_mis_takeoff_alt,
310  (ParamBool<px4::params::MIS_TAKEOFF_REQ>) _param_mis_takeoff_req,
311  (ParamFloat<px4::params::MIS_YAW_TMT>) _param_mis_yaw_tmt,
312  (ParamFloat<px4::params::MIS_YAW_ERR>) _param_mis_yaw_err
313  )
314 
315  int _global_pos_sub{-1};
316  int _gps_pos_sub{-1};
317  int _home_pos_sub{-1};
318  int _land_detected_sub{-1};
319  int _local_pos_sub{-1};
320  int _mission_sub{-1};
321  int _param_update_sub{-1};
322  int _pos_ctrl_landing_status_sub{-1};
323  int _traffic_sub{-1};
324  int _vehicle_command_sub{-1};
325  int _vstatus_sub{-1};
327  orb_advert_t _geofence_result_pub{nullptr};
328  orb_advert_t _mavlink_log_pub{nullptr};
329  orb_advert_t _mission_result_pub{nullptr};
330  orb_advert_t _pos_sp_triplet_pub{nullptr};
331  orb_advert_t _vehicle_cmd_ack_pub{nullptr};
332  orb_advert_t _vehicle_cmd_pub{nullptr};
333  orb_advert_t _vehicle_roi_pub{nullptr};
334 
335  // Subscriptions
336  home_position_s _home_pos{};
337  mission_result_s _mission_result{};
338  vehicle_global_position_s _global_pos{};
339  vehicle_gps_position_s _gps_pos{};
340  vehicle_land_detected_s _land_detected{};
341  vehicle_local_position_s _local_pos{};
342  vehicle_status_s _vstatus{};
344  uORB::Subscription<position_controller_status_s> _position_controller_status_sub{ORB_ID(position_controller_status)};
345 
346  uint8_t _previous_nav_state{};
348  // Publications
349  geofence_result_s _geofence_result{};
350  position_setpoint_triplet_s _pos_sp_triplet{};
351  position_setpoint_triplet_s _reposition_triplet{};
352  position_setpoint_triplet_s _takeoff_triplet{};
353  vehicle_roi_s _vroi{};
355  perf_counter_t _loop_perf;
357  Geofence _geofence;
358  bool _geofence_violation_warning_sent{false};
360  bool _can_loiter_at_sp{false};
361  bool _pos_sp_triplet_updated{false};
362  bool _pos_sp_triplet_published_invalid_once{false};
363  bool _mission_result_updated{false};
365  NavigatorMode *_navigation_mode{nullptr};
366  Mission _mission;
367  Loiter _loiter;
368  Takeoff _takeoff;
369  Land _land;
370  PrecLand _precland;
371  RTL _rtl;
372  RCLoss _rcLoss;
373  DataLinkLoss _dataLinkLoss;
374  EngineFailure _engineFailure;
375  GpsFailure _gpsFailure;
376  FollowTarget _follow_target;
377 
378  NavigatorMode *_navigation_mode_array[NAVIGATOR_MODE_ARRAY_SIZE];
380  param_t _handle_back_trans_dec_mss{PARAM_INVALID};
381  param_t _handle_reverse_delay{PARAM_INVALID};
382  float _param_back_trans_dec_mss{0.f};
383  float _param_reverse_delay{0.f};
384 
385  float _mission_cruising_speed_mc{-1.0f};
386  float _mission_cruising_speed_fw{-1.0f};
387  float _mission_throttle{-1.0f};
388 
389  // update subscriptions
390  void global_position_update();
391  void gps_position_update();
392  void home_position_update(bool force = false);
393  void local_position_update();
394  void params_update();
395  void vehicle_land_detected_update();
396  void vehicle_status_update();
397 
401  void publish_position_setpoint_triplet();
402 
406  void publish_mission_result();
407 
408  void publish_vehicle_command_ack(const vehicle_command_s &cmd, uint8_t result);
409 };
#define PARAM_INVALID
Handle returned when a parameter cannot be found.
Definition: param.h:103
Definition: navigator_mode.h:46
Definition: rtl.h:51
Definition: px4_param.h:313
void set_cruising_speed(float speed=-1.0f)
Set the cruising speed.
Definition: navigator_main.cpp:926
float get_default_altitude_acceptance_radius()
Get the default altitude acceptance radius (i.e.
Definition: navigator_main.cpp:869
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
struct home_position_s * get_home_position()
Getters.
Definition: navigator.h:150
Helper class to do precision landing with a landing target.
Helper class to loiter.
Helper class to land at the current position.
int print_status() override
Definition: navigator_main.cpp:827
static int print_usage(const char *reason=nullptr)
Definition: navigator_main.cpp:1188
Navigator mode to access missions.
static Navigator * instantiate(int argc, char *argv[])
Definition: navigator_main.cpp:815
static int task_spawn(int argc, char *argv[])
Definition: navigator_main.cpp:798
void fake_traffic(const char *callsign, float distance, float direction, float traffic_heading, float altitude_diff, float hor_velocity, float ver_velocity)
Generate an artificial traffic indication.
Definition: navigator_main.cpp:1009
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
Definition: rcloss.h:49
Definition: datalinkloss.h:49
float get_cruising_speed()
Get the cruising speed.
Definition: navigator_main.cpp:904
Helper class for Data Link Loss Mode acording to the OBC rules.
void check_traffic()
Check nearby traffic for potential collisions.
Definition: navigator_main.cpp:1045
Definition: land.h:46
float get_default_acceptance_radius()
Returns the default acceptance radius defined by the parameter.
Definition: navigator_main.cpp:857
Provides functions for handling the geofence.
float get_altitude_acceptance_radius()
Get the altitude acceptance radius.
Definition: navigator_main.cpp:889
static int custom_command(int argc, char *argv[])
Definition: navigator_main.cpp:1219
Header common to all counters.
Definition: perf_counter.cpp:65
API for the uORB lightweight object broker.
Definition: px4_param.h:318
Helper class for RC Loss Mode acording to the OBC rules.
C++ base class for modules/classes using configuration parameters.
Definition: px4_module_params.h:46
void set_cruising_throttle(float throttle=-1.0f)
Set the target throttle.
Definition: navigator.h:241
float get_yaw_acceptance(float mission_item_yaw)
Get the yaw acceptance given the current mission item.
Definition: navigator_main.cpp:984
float get_cruising_throttle()
Get the target throttle.
Definition: navigator_main.cpp:953
Helper class for a fixedwing engine failure mode.
Definition: precland.h:66
Helper class to take off.
Definition: navigator.h:83
Definition: enginefailure.h:47
void load_fence_from_file(const char *filename)
Load fence from file.
Definition: navigator_main.cpp:1000
Helper class for RTL.
Helper class for Data Link Loss Mode according to the OBC rules.
PrecLand * get_precland()
allow others, e.g.
Definition: navigator.h:159
Definition: gpsfailure.h:48
float get_acceptance_radius()
Get the acceptance radius.
Definition: navigator_main.cpp:863
Definition: mission.h:67
void set_can_loiter_at_sp(bool can_loiter)
Setters.
Definition: navigator.h:143
Definition: takeoff.h:46
void reset_triplets()
Set triplets to invalid.
Definition: navigator_main.cpp:944
Definition: loiter.h:48
void run() override
Definition: navigator_main.cpp:208
void publish_geofence_result()
Publish the geofence result.
Definition: navigator_main.cpp:1269
void reset_cruising_speed()
Reset cruising speed to default values.
Definition: navigator_main.cpp:937
uint32_t param_t
Parameter handle.
Definition: param.h:98
Definition: follow_target.h:52
Definition: geofence.h:59
Performance measuring tools.