PX4 Firmware
PX4 Autopilot Software http://px4.io
loiter.cpp
Go to the documentation of this file.
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 /**
34  * @file loiter.cpp
35  *
36  * Helper class to loiter
37  *
38  * @author Julian Oes <julian@oes.ch>
39  * @author Anton Babushkin <anton.babushkin@me.com>
40  */
41 
42 #include "loiter.h"
43 #include "navigator.h"
44 
46  MissionBlock(navigator),
47  ModuleParams(navigator)
48 {
49 }
50 
51 void
53 {
54  _loiter_pos_set = false;
55 }
56 
57 void
59 {
61  reposition();
62 
63  } else {
65  }
66 }
67 
68 void
70 {
72  reposition();
73  }
74 
75  // reset the loiter position if we get disarmed
76  if (_navigator->get_vstatus()->arming_state != vehicle_status_s::ARMING_STATE_ARMED) {
77  _loiter_pos_set = false;
78  }
79 }
80 
81 void
83 {
84  if (_navigator->get_vstatus()->arming_state != vehicle_status_s::ARMING_STATE_ARMED &&
86 
87  // Not setting loiter position if disarmed and landed, instead mark the current
88  // setpoint as invalid and idle (both, just to be sure).
89 
91  _navigator->get_position_setpoint_triplet()->current.type = position_setpoint_s::SETPOINT_TYPE_IDLE;
93  _loiter_pos_set = false;
94  return;
95 
96  } else if (_loiter_pos_set) {
97  // Already set, nothing to do.
98  return;
99  }
100 
101  _loiter_pos_set = true;
102 
103  // set current mission item to loiter
105 
106  // convert mission item to current setpoint
108  pos_sp_triplet->current.velocity_valid = false;
109  pos_sp_triplet->previous.valid = false;
112  pos_sp_triplet->next.valid = false;
113 
114  _navigator->set_can_loiter_at_sp(pos_sp_triplet->current.type == position_setpoint_s::SETPOINT_TYPE_LOITER);
116 }
117 
118 void
120 {
121  // we can't reposition if we are not armed yet
122  if (_navigator->get_vstatus()->arming_state != vehicle_status_s::ARMING_STATE_ARMED) {
123  return;
124  }
125 
127 
128  if (rep->current.valid) {
129  // set loiter position based on reposition command
130 
131  // convert mission item to current setpoint
133  pos_sp_triplet->current.velocity_valid = false;
134  pos_sp_triplet->previous.yaw = _navigator->get_global_position()->yaw;
135  pos_sp_triplet->previous.lat = _navigator->get_global_position()->lat;
136  pos_sp_triplet->previous.lon = _navigator->get_global_position()->lon;
137  pos_sp_triplet->previous.alt = _navigator->get_global_position()->alt;
138  memcpy(&pos_sp_triplet->current, &rep->current, sizeof(rep->current));
139  pos_sp_triplet->next.valid = false;
140 
141  _navigator->set_can_loiter_at_sp(pos_sp_triplet->current.type == position_setpoint_s::SETPOINT_TYPE_LOITER);
143 
144  // mark this as done
145  memset(rep, 0, sizeof(*rep));
146  }
147 }
Helper class to loiter.
float get_loiter_min_alt() const
Definition: navigator.h:285
void set_loiter_item(struct mission_item_s *item, float min_clearance=-1.0f)
Set a loiter mission item, if possible reuse the position setpoint, otherwise take the current positi...
struct position_setpoint_s next
void set_loiter_position()
Set the position to hold based on the current local position.
Definition: loiter.cpp:82
struct position_setpoint_s previous
struct vehicle_land_detected_s * get_land_detected()
Definition: navigator.h:157
Navigator * _navigator
void on_activation() override
This function is called one time when mode becomes active, pos_sp_triplet must be initialized here...
Definition: loiter.cpp:58
void on_active() override
This function is called while the mode is active.
Definition: loiter.cpp:69
struct position_setpoint_s current
void on_inactive() override
This function is called while the mode is inactive.
Definition: loiter.cpp:52
bool _loiter_pos_set
Definition: loiter.h:70
struct vehicle_global_position_s * get_global_position()
Definition: navigator.h:156
mission_item_s _mission_item
void mission_apply_limitation(mission_item_s &item)
General function used to adjust the mission item based on vehicle specific limitations.
struct position_setpoint_triplet_s * get_position_setpoint_triplet()
Definition: navigator.h:153
bool mission_item_to_position_setpoint(const mission_item_s &item, position_setpoint_s *sp)
Convert a mission item to a position setpoint.
void reposition()
Use the stored reposition location of the navigator to move to a new location.
Definition: loiter.cpp:119
struct position_setpoint_triplet_s * get_reposition_triplet()
Definition: navigator.h:154
struct vehicle_status_s * get_vstatus()
Definition: navigator.h:159
void set_can_loiter_at_sp(bool can_loiter)
Setters.
Definition: navigator.h:144
void set_position_setpoint_triplet_updated()
Definition: navigator.h:145
Loiter(Navigator *navigator)
Definition: loiter.cpp:45