PX4 Firmware
PX4 Autopilot Software http://px4.io
mission.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2013-2018 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  * @file mission.h
35  *
36  * Navigator mode to access missions
37  *
38  * @author Julian Oes <julian@oes.ch>
39  * @author Thomas Gubler <thomasgubler@gmail.com>
40  * @author Anton Babushkin <anton.babushkin@me.com>
41  * @author Ban Siesta <bansiesta@gmail.com>
42  * @author Lorenz Meier <lorenz@px4.io>
43  */
44 
45 #pragma once
46 
47 #include "mission_block.h"
49 #include "navigator_mode.h"
50 
51 #include <float.h>
52 
53 #include <dataman/dataman.h>
54 #include <drivers/drv_hrt.h>
55 #include <px4_platform_common/module_params.h>
56 #include <uORB/Subscription.hpp>
58 #include <uORB/topics/mission.h>
64 #include <uORB/uORB.h>
65 
66 class Navigator;
67 
68 class Mission : public MissionBlock, public ModuleParams
69 {
70 public:
72  ~Mission() override = default;
73 
74  void on_inactive() override;
75  void on_inactivation() override;
76  void on_activation() override;
77  void on_active() override;
78 
82  };
83 
84  bool set_current_mission_index(uint16_t index);
85 
86  bool land_start();
87  bool landing();
88 
89  uint16_t get_land_start_index() const { return _land_start_index; }
91  bool get_mission_finished() const { return _mission_type == MISSION_TYPE_NONE; }
92  bool get_mission_changed() const { return _mission_changed ; }
94  double get_landing_lat() { return _landing_lat; }
95  double get_landing_lon() { return _landing_lon; }
96  float get_landing_alt() { return _landing_alt; }
97 
99 
100  /**
101  * Set a new mission mode and handle the switching between the different modes
102  *
103  * For a list of the different modes refer to mission_result.msg
104  */
105  void set_execution_mode(const uint8_t mode);
106 private:
107 
108  /**
109  * Update mission topic
110  */
111  void update_mission();
112 
113  /**
114  * Move on to next mission item or switch to loiter
115  */
116  void advance_mission();
117 
118  /**
119  * Set new mission items
120  */
121  void set_mission_items();
122 
123  /**
124  * Returns true if we need to do a takeoff at the current state
125  */
127 
128  /**
129  * Returns true if we need to move to waypoint location before starting descent
130  */
131  bool do_need_move_to_land();
132 
133  /**
134  * Returns true if we need to move to waypoint location after vtol takeoff
135  */
137 
138  /**
139  * Copies position from setpoint if valid, otherwise copies current position
140  */
141  void copy_position_if_valid(struct mission_item_s *mission_item, struct position_setpoint_s *setpoint);
142 
143  /**
144  * Create mission item to align towards next waypoint
145  */
146  void set_align_mission_item(struct mission_item_s *mission_item, struct mission_item_s *mission_item_next);
147 
148  /**
149  * Calculate takeoff height for mission item considering ground clearance
150  */
151  float calculate_takeoff_altitude(struct mission_item_s *mission_item);
152 
153  /**
154  * Updates the heading of the vehicle. Rotary wings only.
155  */
156  void heading_sp_update();
157 
158  /**
159  * Updates the altitude sp to follow a foh
160  */
161  void altitude_sp_foh_update();
162 
163  /**
164  * Update the cruising speed setpoint.
165  */
167 
168  /**
169  * Abort landing
170  */
171  void do_abort_landing();
172 
173  /**
174  * Read the current and the next mission item. The next mission item read is the
175  * next mission item that contains a position.
176  *
177  * @return true if current mission item available
178  */
179  bool prepare_mission_items(mission_item_s *mission_item,
180  mission_item_s *next_position_mission_item, bool *has_next_position_item);
181 
182  /**
183  * Read current (offset == 0) or a specific (offset > 0) mission item
184  * from the dataman and watch out for DO_JUMPS
185  *
186  * @return true if successful
187  */
188  bool read_mission_item(int offset, struct mission_item_s *mission_item);
189 
190  /**
191  * Save current mission state to dataman
192  */
193  void save_mission_state();
194 
195  /**
196  * Inform about a changed mission item after a DO_JUMP
197  */
198  void report_do_jump_mission_changed(int index, int do_jumps_remaining);
199 
200  /**
201  * Set a mission item as reached
202  */
204 
205  /**
206  * Set the current mission item
207  */
209 
210  /**
211  * Check whether a mission is ready to go
212  */
213  void check_mission_valid(bool force);
214 
215  /**
216  * Reset mission
217  */
218  void reset_mission(struct mission_s &mission);
219 
220  /**
221  * Returns true if we need to reset the mission
222  */
223  bool need_to_reset_mission(bool active);
224 
225  /**
226  * Project current location with heading to far away location and fill setpoint.
227  */
228  void generate_waypoint_from_heading(struct position_setpoint_s *setpoint, float yaw);
229 
230  /**
231  * Find and store the index of the landing sequence (DO_LAND_START)
232  */
234 
235  /**
236  * Return the index of the closest mission item to the current global position.
237  */
238  int32_t index_closest_mission_item() const;
239 
240  bool position_setpoint_equal(const position_setpoint_s *p1, const position_setpoint_s *p2) const;
241 
242  DEFINE_PARAMETERS(
243  (ParamFloat<px4::params::MIS_DIST_1WP>) _param_mis_dist_1wp,
244  (ParamFloat<px4::params::MIS_DIST_WPS>) _param_mis_dist_wps,
245  (ParamInt<px4::params::MIS_ALTMODE>) _param_mis_altmode,
246  (ParamInt<px4::params::MIS_MNT_YAW_CTL>) _param_mis_mnt_yaw_ctl
247  )
248 
249  uORB::Subscription _mission_sub{ORB_ID(mission)}; /**< mission subscription */
251 
253 
254  // track location of planned mission landing
256  uint16_t _land_start_index{UINT16_MAX}; /**< index of DO_LAND_START, INVALID_DO_LAND_START if no planned landing */
257  double _landing_lat{0.0};
258  double _landing_lon{0.0};
259  float _landing_alt{0.0f};
260 
261  bool _need_takeoff{true}; /**< if true, then takeoff must be performed before going to the first waypoint (if needed) */
262 
263  enum {
266  } _mission_type{MISSION_TYPE_NONE};
267 
268  bool _inited{false};
269  bool _home_inited{false};
270  bool _need_mission_reset{false};
272  bool _mission_changed{false}; /** < true if the mission changed since the mission mode was active */
273 
274  float _min_current_sp_distance_xy{FLT_MAX}; /**< minimum distance which was achieved to the current waypoint */
275 
276  float _distance_current_previous{0.0f}; /**< distance from previous to current sp in pos_sp_triplet,
277  only use if current and previous are valid */
278 
280  WORK_ITEM_TYPE_DEFAULT, /**< default mission item */
281  WORK_ITEM_TYPE_TAKEOFF, /**< takeoff before moving to waypoint */
282  WORK_ITEM_TYPE_MOVE_TO_LAND, /**< move to land waypoint before descent */
283  WORK_ITEM_TYPE_ALIGN, /**< align for next waypoint */
288  } _work_item_type{WORK_ITEM_TYPE_DEFAULT}; /**< current type of work to do (sub mission item) */
289 
290  uint8_t _mission_execution_mode{mission_result_s::MISSION_EXECUTION_MODE_NORMAL}; /**< the current mode of how the mission is executed,look at mission_result.msg for the definition */
292 };
bool land_start()
Definition: mission.cpp:429
Global position setpoint in WGS84 coordinates.
Definition: navigation.h:145
int32_t index_closest_mission_item() const
Return the index of the closest mission item to the current global position.
Definition: mission.cpp:1751
enum Mission::@122 MISSION_TYPE_NONE
void copy_position_if_valid(struct mission_item_s *mission_item, struct position_setpoint_s *setpoint)
Copies position from setpoint if valid, otherwise copies current position.
Definition: mission.cpp:1175
align for next waypoint
Definition: mission.h:283
void advance_mission()
Move on to next mission item or switch to loiter.
Definition: mission.cpp:533
void generate_waypoint_from_heading(struct position_setpoint_s *setpoint, float yaw)
Project current location with heading to far away location and fill setpoint.
Definition: mission.cpp:1740
void on_inactive() override
This function is called while the mode is inactive.
Definition: mission.cpp:69
API for the uORB lightweight object broker.
uint8_t _mission_execution_mode
the current mode of how the mission is executed,look at mission_result.msg for the definition ...
Definition: mission.h:290
Mission(Navigator *navigator)
Definition: mission.cpp:62
bool _mission_waypoints_changed
Definition: mission.h:271
float _landing_alt
Definition: mission.h:259
enum Mission::work_item_type WORK_ITEM_TYPE_DEFAULT
current type of work to do (sub mission item)
Helper class to use mission items.
void altitude_sp_foh_update()
Updates the altitude sp to follow a foh.
Definition: mission.cpp:1290
takeoff before moving to waypoint
Definition: mission.h:281
void set_current_mission_item()
Set the current mission item.
Definition: mission.cpp:1640
DEFINE_PARAMETERS((ParamFloat< px4::params::MIS_DIST_1WP >) _param_mis_dist_1wp,(ParamFloat< px4::params::MIS_DIST_WPS >) _param_mis_dist_wps,(ParamInt< px4::params::MIS_ALTMODE >) _param_mis_altmode,(ParamInt< px4::params::MIS_MNT_YAW_CTL >) _param_mis_mnt_yaw_ctl) uORB mission_s _mission
< mission subscription
Definition: mission.h:250
void on_inactivation() override
This function is called one time when mode becomes inactive.
Definition: mission.cpp:137
default mission item
Definition: mission.h:280
int32_t _current_mission_index
Definition: mission.h:252
bool need_to_reset_mission(bool active)
Returns true if we need to reset the mission.
Definition: mission.cpp:1724
float _distance_current_previous
distance from previous to current sp in pos_sp_triplet, only use if current and previous are valid ...
Definition: mission.h:276
void report_do_jump_mission_changed(int index, int do_jumps_remaining)
Inform about a changed mission item after a DO_JUMP.
Definition: mission.cpp:1620
void check_mission_valid(bool force)
Check whether a mission is ready to go.
Definition: mission.cpp:1651
High-resolution timer with callouts and timekeeping.
bool _mission_changed
Definition: mission.h:272
Provides checks if mission is feasible given the navigation capabilities.
bool do_need_move_to_takeoff()
Returns true if we need to move to waypoint location after vtol takeoff.
Definition: mission.cpp:1160
double get_landing_lat()
Definition: mission.h:94
void do_abort_landing()
Abort landing.
Definition: mission.cpp:1383
~Mission() override=default
bool _need_takeoff
if true, then takeoff must be performed before going to the first waypoint (if needed) ...
Definition: mission.h:261
bool _home_inited
Definition: mission.h:269
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
uint16_t _land_start_index
index of DO_LAND_START, INVALID_DO_LAND_START if no planned landing
Definition: mission.h:256
move to land waypoint before descent
Definition: mission.h:282
uint16_t get_land_start_index() const
Definition: mission.h:89
bool get_mission_changed() const
Definition: mission.h:92
float get_landing_alt()
Definition: mission.h:96
bool get_mission_finished() const
Definition: mission.h:91
bool set_current_mission_index(uint16_t index)
Definition: mission.cpp:282
void reset_mission(struct mission_s &mission)
Reset mission.
Definition: mission.cpp:1674
void on_activation() override
This function is called one time when mode becomes active, pos_sp_triplet must be initialized here...
Definition: mission.cpp:149
void cruising_speed_sp_update()
Update the cruising speed setpoint.
Definition: mission.cpp:1365
DATAMANAGER driver.
double get_landing_lon()
Definition: mission.h:95
bool _execution_mode_changed
Definition: mission.h:291
void heading_sp_update()
Updates the heading of the vehicle.
Definition: mission.cpp:1223
void set_closest_item_as_current()
Definition: mission.cpp:309
void set_execution_mode(const uint8_t mode)
Set a new mission mode and handle the switching between the different modes.
Definition: mission.cpp:315
bool _land_start_available
Definition: mission.h:255
bool position_setpoint_equal(const position_setpoint_s *p1, const position_setpoint_s *p2) const
Definition: mission.cpp:1808
void update_mission()
Update mission topic.
Definition: mission.cpp:458
bool get_land_start_available() const
Definition: mission.h:90
bool find_mission_land_start()
Find and store the index of the landing sequence (DO_LAND_START)
Definition: mission.cpp:379
bool do_need_move_to_land()
Returns true if we need to move to waypoint location before starting descent.
Definition: mission.cpp:1145
void save_mission_state()
Save current mission state to dataman.
Definition: mission.cpp:1566
bool read_mission_item(int offset, struct mission_item_s *mission_item)
Read current (offset == 0) or a specific (offset > 0) mission item from the dataman and watch out for...
Definition: mission.cpp:1474
bool prepare_mission_items(mission_item_s *mission_item, mission_item_s *next_position_mission_item, bool *has_next_position_item)
Read the current and the next mission item.
Definition: mission.cpp:1437
bool do_need_vertical_takeoff()
Returns true if we need to do a takeoff at the current state.
Definition: mission.cpp:1107
float calculate_takeoff_altitude(struct mission_item_s *mission_item)
Calculate takeoff height for mission item considering ground clearance.
Definition: mission.cpp:1206
void set_mission_item_reached()
Set a mission item as reached.
Definition: mission.cpp:1631
mission_altitude_mode
Definition: mission.h:79
bool _inited
Definition: mission.h:268
void on_active() override
This function is called while the mode is active.
Definition: mission.cpp:175
bool get_mission_waypoints_changed() const
Definition: mission.h:93
bool landing()
Definition: mission.cpp:446
work_item_type
Definition: mission.h:279
double _landing_lon
Definition: mission.h:258
mode
Definition: vtol_type.h:76
bool _need_mission_reset
Definition: mission.h:270
void set_align_mission_item(struct mission_item_s *mission_item, struct mission_item_s *mission_item_next)
Create mission item to align towards next waypoint.
Definition: mission.cpp:1192
double _landing_lat
Definition: mission.h:257
float _min_current_sp_distance_xy
< true if the mission changed since the mission mode was active
Definition: mission.h:274
void set_mission_items()
Set new mission items.
Definition: mission.cpp:587