OP-1156 fix path logic to not deviate from correct altitude too much
[librepilot.git] / flight / libraries / paths.c
blob3bcd0a03d93de077e9f599f727aebf9c7cabc813
1 /**
2 ******************************************************************************
4 * @file paths.c
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
6 * @brief Library path manipulation
8 * @see The GNU Public License (GPL) Version 3
10 *****************************************************************************/
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <pios.h>
28 #include <pios_math.h>
29 #include <mathmisc.h>
31 #include "uavobjectmanager.h" // <--.
32 #include "pathdesired.h" // <-- needed only for correct ENUM macro usage with path modes (PATHDESIRED_MODE_xxx,
33 #include "paths.h"
34 // no direct UAVObject usage allowed in this file
36 // private functions
37 static void path_endpoint(PathDesiredData *path, float *cur_point, struct path_status *status, bool mode);
38 static void path_vector(PathDesiredData *path, float *cur_point, struct path_status *status, bool mode);
39 static void path_circle(PathDesiredData *path, float *cur_point, struct path_status *status, bool clockwise);
41 /**
42 * @brief Compute progress along path and deviation from it
43 * @param[in] path PathDesired structure
44 * @param[in] cur_point Current location
45 * @param[out] status Structure containing progress along path and deviation
47 void path_progress(PathDesiredData *path, float *cur_point, struct path_status *status)
49 switch (path->Mode) {
50 case PATHDESIRED_MODE_FLYVECTOR:
51 return path_vector(path, cur_point, status, true);
53 break;
54 case PATHDESIRED_MODE_DRIVEVECTOR:
55 return path_vector(path, cur_point, status, false);
57 break;
58 case PATHDESIRED_MODE_FLYCIRCLERIGHT:
59 case PATHDESIRED_MODE_DRIVECIRCLERIGHT:
60 return path_circle(path, cur_point, status, 1);
62 break;
63 case PATHDESIRED_MODE_FLYCIRCLELEFT:
64 case PATHDESIRED_MODE_DRIVECIRCLELEFT:
65 return path_circle(path, cur_point, status, 0);
67 break;
68 case PATHDESIRED_MODE_FLYENDPOINT:
69 return path_endpoint(path, cur_point, status, true);
71 break;
72 case PATHDESIRED_MODE_DRIVEENDPOINT:
73 default:
74 // use the endpoint as default failsafe if called in unknown modes
75 return path_endpoint(path, cur_point, status, false);
77 break;
81 /**
82 * @brief Compute progress towards endpoint. Deviation equals distance
83 * @param[in] path PathDesired
84 * @param[in] cur_point Current location
85 * @param[out] status Structure containing progress along path and deviation
86 * @param[in] mode3D set true to include altitude in distance and progress calculation
88 static void path_endpoint(PathDesiredData *path, float *cur_point, struct path_status *status, bool mode3D)
90 float diff[3];
91 float dist_path, dist_diff;
93 // Distance to go
94 status->path_vector[0] = path->End.North - path->Start.North;
95 status->path_vector[1] = path->End.East - path->Start.East;
96 status->path_vector[2] = mode3D ? path->End.Down - path->Start.Down : 0.0f;
98 // Current progress location relative to end
99 diff[0] = path->End.North - cur_point[0];
100 diff[1] = path->End.East - cur_point[1];
101 diff[2] = mode3D ? path->End.Down - cur_point[2] : 0.0f;
103 dist_diff = vector_lengthf(diff, 3);
104 dist_path = vector_lengthf(status->path_vector, 3);
106 if (dist_diff < 1e-6f) {
107 status->fractional_progress = 1;
108 status->error = 0.0f;
109 status->correction_vector[0] = status->correction_vector[1] = status->correction_vector[2] = 0.0f;
110 // we have no base movement direction in this mode
111 status->path_vector[0] = status->path_vector[1] = status->path_vector[2] = 0.0f;
113 return;
116 if (fmaxf(dist_path, 1.0f) > dist_diff) {
117 status->fractional_progress = 1 - dist_diff / fmaxf(dist_path, 1.0f);
118 } else {
119 status->fractional_progress = 0; // we don't want fractional_progress to become negative
121 status->error = dist_diff;
123 // Compute correction vector
124 status->correction_vector[0] = diff[0];
125 status->correction_vector[1] = diff[1];
126 status->correction_vector[2] = diff[2];
128 // base movement direction in this mode is a constant velocity offset on top of correction in the same direction
129 status->path_vector[0] = path->EndingVelocity * status->correction_vector[0] / dist_diff;
130 status->path_vector[1] = path->EndingVelocity * status->correction_vector[1] / dist_diff;
131 status->path_vector[2] = path->EndingVelocity * status->correction_vector[2] / dist_diff;
135 * @brief Compute progress along path and deviation from it
136 * @param[in] path PathDesired
137 * @param[in] cur_point Current location
138 * @param[out] status Structure containing progress along path and deviation
139 * @param[in] mode3D set true to include altitude in distance and progress calculation
141 static void path_vector(PathDesiredData *path, float *cur_point, struct path_status *status, bool mode3D)
143 float diff[3];
144 float dist_path;
145 float dot;
146 float velocity;
147 float track_point[3];
149 // Distance to go
150 status->path_vector[0] = path->End.North - path->Start.North;
151 status->path_vector[1] = path->End.East - path->Start.East;
152 status->path_vector[2] = mode3D ? path->End.Down - path->Start.Down : 0.0f;
154 // Current progress location relative to start
155 diff[0] = cur_point[0] - path->Start.North;
156 diff[1] = cur_point[1] - path->Start.East;
157 diff[2] = mode3D ? cur_point[2] - path->Start.Down : 0.0f;
159 dot = status->path_vector[0] * diff[0] + status->path_vector[1] * diff[1] + status->path_vector[2] * diff[2];
160 dist_path = vector_lengthf(status->path_vector, 3);
162 if (dist_path > 1e-6f) {
163 // Compute direction to travel & progress
164 status->fractional_progress = dot / (dist_path * dist_path);
165 } else {
166 // Fly towards the endpoint to prevent flying away,
167 // but assume progress=1 either way.
168 path_endpoint(path, cur_point, status, mode3D);
169 status->fractional_progress = 1;
170 return;
172 // Compute point on track that is closest to our current position.
173 track_point[0] = status->fractional_progress * status->path_vector[0] + path->Start.North;
174 track_point[1] = status->fractional_progress * status->path_vector[1] + path->Start.East;
175 track_point[2] = status->fractional_progress * status->path_vector[2] + path->Start.Down;
177 status->correction_vector[0] = track_point[0] - cur_point[0];
178 status->correction_vector[1] = track_point[1] - cur_point[1];
179 status->correction_vector[2] = track_point[2] - cur_point[2];
181 status->error = vector_lengthf(status->correction_vector, 3);
183 // correct movement vector to current velocity
184 velocity = path->StartingVelocity + boundf(status->fractional_progress, 0.0f, 1.0f) * (path->EndingVelocity - path->StartingVelocity);
185 status->path_vector[0] = velocity * status->path_vector[0] / dist_path;
186 status->path_vector[1] = velocity * status->path_vector[1] / dist_path;
187 status->path_vector[2] = velocity * status->path_vector[2] / dist_path;
191 * @brief Compute progress along circular path and deviation from it
192 * @param[in] path PathDesired
193 * @param[in] cur_point Current location
194 * @param[out] status Structure containing progress along path and deviation
196 static void path_circle(PathDesiredData *path, float *cur_point, struct path_status *status, bool clockwise)
198 float radius_north, radius_east, diff_north, diff_east, diff_down;
199 float radius, cradius;
200 float normal[2];
201 float progress;
202 float a_diff, a_radius;
204 // Radius
205 radius_north = path->End.North - path->Start.North;
206 radius_east = path->End.East - path->Start.East;
208 // Current location relative to center
209 diff_north = cur_point[0] - path->End.North;
210 diff_east = cur_point[1] - path->End.East;
211 diff_down = cur_point[2] - path->End.Down;
213 radius = sqrtf(squaref(radius_north) + squaref(radius_east));
214 cradius = sqrtf(squaref(diff_north) + squaref(diff_east));
216 // circles are always horizontal (for now - TODO: allow 3d circles - problem: clockwise/counterclockwise does no longer apply)
217 status->path_vector[2] = 0.0f;
219 // error is current radius minus wanted radius - positive if too close
220 status->error = radius - cradius;
222 if (cradius < 1e-6f) {
223 // cradius is zero, just fly somewhere
224 status->fractional_progress = 1;
225 status->correction_vector[0] = 0;
226 status->correction_vector[1] = 0;
227 status->path_vector[0] = path->EndingVelocity;
228 status->path_vector[1] = 0;
229 } else {
230 if (clockwise) {
231 // Compute the normal to the radius clockwise
232 normal[0] = -diff_east / cradius;
233 normal[1] = diff_north / cradius;
234 } else {
235 // Compute the normal to the radius counter clockwise
236 normal[0] = diff_east / cradius;
237 normal[1] = -diff_north / cradius;
240 // normalize progress to 0..1
241 a_diff = atan2f(diff_north, diff_east);
242 a_radius = atan2f(radius_north, radius_east);
244 if (a_diff < 0) {
245 a_diff += 2.0f * M_PI_F;
247 if (a_radius < 0) {
248 a_radius += 2.0f * M_PI_F;
251 progress = (a_diff - a_radius + M_PI_F) / (2.0f * M_PI_F);
253 if (progress < 0.0f) {
254 progress += 1.0f;
255 } else if (progress >= 1.0f) {
256 progress -= 1.0f;
259 if (clockwise) {
260 progress = 1.0f - progress;
263 status->fractional_progress = progress;
265 // Compute direction to travel
266 status->path_vector[0] = normal[0] * path->EndingVelocity;
267 status->path_vector[1] = normal[1] * path->EndingVelocity;
269 // Compute direction to correct error
270 status->correction_vector[0] = status->error * diff_north / cradius;
271 status->correction_vector[1] = status->error * diff_east / cradius;
274 status->correction_vector[2] = -diff_down;
276 status->error = fabs(status->error);