SMACC2
Loading...
Searching...
No Matches
pattern_generators.cpp
Go to the documentation of this file.
1// Copyright 2026 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
22
23#include <algorithm>
24#include <limits>
25
26namespace cl_px4_mr
27{
28
32
33std::vector<NedPoint> generateFlightPatternAscend(
34 const FlightPatternAscendParams & p, const NedPoint & current)
35{
36 NedPoint target;
37 target.x = pick(p.targetX, current.x);
38 target.y = pick(p.targetY, current.y);
39 target.z = -p.altitudeAgl;
40
41 const bool atXy = std::hypot(target.x - current.x, target.y - current.y) <= p.tolerance;
42 if (atXy && std::fabs(current.z - target.z) <= p.tolerance)
43 {
44 return {target};
45 }
46 NedPoint start = current;
47 start.yaw = kNaN;
48 return {start, target};
49}
50
51std::vector<NedPoint> generateFlightPatternLoiter(
52 const FlightPatternLoiterParams & p, const NedPoint & current)
53{
54 const float cx = pick(p.centerX, current.x);
55 const float cy = pick(p.centerY, current.y);
56 const float z = altitudeToZ(p.altitudeAgl, current.z);
57 const float r = std::max(p.radius, 0.5f);
58 const int n = std::max(p.pointsPerCircle, 8);
59 const float sign = turnSign(p.direction);
60
61 if (p.count <= 0)
62 {
63 // nothing to fly: hold the entry point (the follower posts success at once)
64 NedPoint here = current;
65 here.z = z;
66 here.yaw = kNaN;
67 return {here};
68 }
69 const int count = p.count;
70
71 // enter at the point of the circle nearest the vehicle; if at the centre
72 // use the entry heading to pick a start
73 const float dx = current.x - cx;
74 const float dy = current.y - cy;
75 float theta0 =
76 std::hypot(dx, dy) > 0.1f ? std::atan2(dy, dx) : (std::isnan(current.yaw) ? 0.0f : current.yaw);
77
78 std::vector<NedPoint> path;
79 path.reserve(static_cast<size_t>(count) * static_cast<size_t>(n) + 1);
80 for (int k = 0; k <= count * n; ++k)
81 {
82 const float theta = theta0 + sign * 2.0f * static_cast<float>(M_PI) * static_cast<float>(k) /
83 static_cast<float>(n);
84 NedPoint v;
85 v.x = cx + r * std::cos(theta);
86 v.y = cy + r * std::sin(theta);
87 v.z = z;
88 v.yaw = p.faceCenter ? std::atan2(cy - v.y, cx - v.x) : kNaN;
89 path.push_back(v);
90 }
91 return path;
92}
93
94namespace
95{
96
97struct Leg
98{
99 float ux = 0.0f, uy = 0.0f; // unit direction
100 float length = 0.0f;
101};
102
103Leg legFrom(const NedPoint & start, float endX, float endY)
104{
105 Leg leg;
106 const float dx = endX - start.x;
107 const float dy = endY - start.y;
108 leg.length = std::hypot(dx, dy);
109 if (leg.length > 1e-3f)
110 {
111 leg.ux = dx / leg.length;
112 leg.uy = dy / leg.length;
113 }
114 return leg;
115}
116
117} // namespace
118
120 const FlightPatternFigureEightParams & p, const NedPoint & current)
121{
122 const float cx = pick(p.centerX, current.x);
123 const float cy = pick(p.centerY, current.y);
124 const float z = altitudeToZ(p.altitudeAgl, current.z);
125 const float a = std::max(p.size, 0.5f);
126 const int n = std::max(p.pointsPerLoop, 16);
127 const float sign = turnSign(p.direction);
128
129 if (p.count <= 0)
130 {
131 NedPoint here = current;
132 here.z = z;
133 here.yaw = kNaN;
134 return {here};
135 }
136
137 // lobe axis: given, else the entry heading, else north
138 const float heading =
139 std::isnan(p.heading) ? (std::isnan(current.yaw) ? 0.0f : current.yaw) : p.heading;
140 const float ch = std::cos(heading);
141 const float sh = std::sin(heading);
142
143 // one loop of the lemniscate x = a cos t / (1 + sin^2 t), y = a sin t cos t / (1 + sin^2 t)
144 std::vector<NedPoint> loop;
145 loop.reserve(static_cast<size_t>(n));
146 for (int k = 0; k < n; ++k)
147 {
148 const float t =
149 sign * 2.0f * static_cast<float>(M_PI) * static_cast<float>(k) / static_cast<float>(n);
150 const float st = std::sin(t);
151 const float ct = std::cos(t);
152 const float denom = 1.0f + st * st;
153 const float lx = a * ct / denom;
154 const float ly = a * st * ct / denom;
155 NedPoint v;
156 v.x = cx + lx * ch - ly * sh;
157 v.y = cy + lx * sh + ly * ch;
158 v.z = z;
159 v.yaw = kNaN;
160 loop.push_back(v);
161 }
162
163 // enter at the sample nearest the vehicle
164 int start = 0;
165 float best = std::numeric_limits<float>::infinity();
166 for (int k = 0; k < n; ++k)
167 {
168 const float d = std::hypot(loop[k].x - current.x, loop[k].y - current.y);
169 if (d < best)
170 {
171 best = d;
172 start = k;
173 }
174 }
175
176 std::vector<NedPoint> path;
177 path.reserve(static_cast<size_t>(p.count) * static_cast<size_t>(n) + 1);
178 for (int k = 0; k <= p.count * n; ++k)
179 {
180 path.push_back(loop[static_cast<size_t>((start + k) % n)]);
181 }
182 return path;
183}
184
186{
187 // perimeter of the lemniscate of Bernoulli = 5.2441 a
188 return 5.2441f * std::max(p.size, 0.5f) * static_cast<float>(std::max(p.count, 0));
189}
190
192 const FlightPatternSineWaveVerticalParams & p, const NedPoint & current)
193{
194 const float endX = pick(p.endX, current.x);
195 const float endY = pick(p.endY, current.y);
196 const float zBase = altitudeToZ(p.baseAltitudeAgl, current.z);
197 const float lambda = std::max(p.wavelength, 0.1f);
198 const float spacing = std::max(p.sampleSpacing, 0.1f);
199 const Leg leg = legFrom(current, endX, endY);
200
201 std::vector<NedPoint> path;
202 if (leg.length <= 1e-3f)
203 {
204 path.push_back(NedPoint{endX, endY, zBase, kNaN});
205 return path;
206 }
207
208 const int n = static_cast<int>(std::floor(leg.length / spacing));
209 path.reserve(static_cast<size_t>(n) + 2);
210 for (int k = 0; k < n; ++k)
211 {
212 const float s = static_cast<float>(k) * spacing;
213 NedPoint v;
214 v.x = current.x + s * leg.ux;
215 v.y = current.y + s * leg.uy;
216 v.z = zBase - p.amplitude * std::sin(2.0f * static_cast<float>(M_PI) * s / lambda);
217 path.push_back(v);
218 }
219 path.push_back(NedPoint{endX, endY, zBase, kNaN});
220 return path;
221}
222
224 const FlightPatternSineWaveHorizontalParams & p, const NedPoint & current)
225{
226 const float endX = pick(p.endX, current.x);
227 const float endY = pick(p.endY, current.y);
228 const float z = altitudeToZ(p.altitudeAgl, current.z);
229 const float lambda = std::max(p.wavelength, 0.1f);
230 const float spacing = std::max(p.sampleSpacing, 0.1f);
231 const Leg leg = legFrom(current, endX, endY);
232
233 std::vector<NedPoint> path;
234 if (leg.length <= 1e-3f)
235 {
236 path.push_back(NedPoint{endX, endY, z, kNaN});
237 return path;
238 }
239
240 // left-of-track normal in NED (x north, y east): rotate u by -90 deg
241 const float nx = leg.uy;
242 const float ny = -leg.ux;
243
244 const int n = static_cast<int>(std::floor(leg.length / spacing));
245 path.reserve(static_cast<size_t>(n) + 2);
246 for (int k = 0; k < n; ++k)
247 {
248 const float s = static_cast<float>(k) * spacing;
249 const float offset = p.amplitude * std::sin(2.0f * static_cast<float>(M_PI) * s / lambda);
250 NedPoint v;
251 v.x = current.x + s * leg.ux + offset * nx;
252 v.y = current.y + s * leg.uy + offset * ny;
253 v.z = z;
254 path.push_back(v);
255 }
256 path.push_back(NedPoint{endX, endY, z, kNaN});
257 return path;
258}
259
261 const FlightPatternSquareSpiralParams & p, const NedPoint & current)
262{
263 const float ox = pick(p.originX, current.x);
264 const float oy = pick(p.originY, current.y);
265 const float z = altitudeToZ(p.altitudeAgl, current.z);
266 const float spacing = std::max(p.spacing, 0.5f);
267 const float sign = turnSign(p.direction);
268 float heading = pick(p.initialHeading, std::isnan(current.yaw) ? 0.0f : current.yaw);
269
270 std::vector<NedPoint> path;
271 path.reserve(static_cast<size_t>(std::max(p.numLegs, 0)) + 1);
272 NedPoint v;
273 v.x = ox;
274 v.y = oy;
275 v.z = z;
276 path.push_back(v);
277
278 for (int i = 0; i < p.numLegs; ++i)
279 {
280 const float len = spacing * static_cast<float>(i / 2 + 1);
281 if (p.maxLegLength > 0.0f && len > p.maxLegLength)
282 {
283 break;
284 }
285 v.x += len * std::cos(heading);
286 v.y += len * std::sin(heading);
287 path.push_back(v);
288 heading += sign * static_cast<float>(M_PI) / 2.0f;
289 }
290 return path;
291}
292
294{
295 const float spacing = std::max(p.spacing, 0.5f);
296 float total = 0.0f;
297 for (int i = 0; i < p.numLegs; ++i)
298 {
299 const float len = spacing * static_cast<float>(i / 2 + 1);
300 if (p.maxLegLength > 0.0f && len > p.maxLegLength)
301 {
302 break;
303 }
304 total += len;
305 }
306 return total;
307}
308
309std::vector<NedPoint> generateFlightPatternSpiral(
310 const FlightPatternSpiralParams & p, const NedPoint & current)
311{
312 const float cx = pick(p.centerX, current.x);
313 const float cy = pick(p.centerY, current.y);
314 const float z = altitudeToZ(p.altitudeAgl, current.z);
315 const float spacing = std::max(p.spacing, 0.5f);
316 const float a = spacing / (2.0f * static_cast<float>(M_PI)); // r = a * theta
317 const float rStart = std::max(p.startRadius, 0.0f);
318 const float rEnd = std::max(p.endRadius, rStart);
319 const float sign = turnSign(p.direction);
320 const float ds = std::max(p.sampleSpacing, 0.1f);
321
322 // angular origin: bearing of the vehicle from the centre, else entry heading
323 const float dx = current.x - cx;
324 const float dy = current.y - cy;
325 const float theta0 =
326 std::hypot(dx, dy) > 0.1f ? std::atan2(dy, dx) : (std::isnan(current.yaw) ? 0.0f : current.yaw);
327
328 const float thetaStart = rStart / a;
329 const float thetaEnd = rEnd / a;
330
331 std::vector<NedPoint> path;
332 float theta = thetaStart;
333 while (theta < thetaEnd)
334 {
335 const float r = a * theta;
336 NedPoint v;
337 v.x = cx + r * std::cos(theta0 + sign * theta);
338 v.y = cy + r * std::sin(theta0 + sign * theta);
339 v.z = z;
340 path.push_back(v);
341 theta += ds / std::max(r, a); // constant arc-length steps
342 }
343 NedPoint last;
344 last.x = cx + rEnd * std::cos(theta0 + sign * thetaEnd);
345 last.y = cy + rEnd * std::sin(theta0 + sign * thetaEnd);
346 last.z = z;
347 path.push_back(last);
348
349 if (p.inward)
350 {
351 std::reverse(path.begin(), path.end());
352 }
353 return path;
354}
355
357{
358 // area between the radii divided by the track spacing
359 const float rStart = std::max(p.startRadius, 0.0f);
360 const float rEnd = std::max(p.endRadius, rStart);
361 const float spacing = std::max(p.spacing, 0.5f);
362 return static_cast<float>(M_PI) * (rEnd * rEnd - rStart * rStart) / spacing;
363}
364
366{
367 const float spacing = std::max(p.laneSpacing, 0.5f);
368 return std::max(1, static_cast<int>(std::floor(std::max(p.width, 0.0f) / spacing + 1e-3f)) + 1);
369}
370
372{
373 const int lanes = flightPatternLawnmowerLaneCount(p);
374 return static_cast<float>(lanes) * std::max(p.laneLength, 0.0f) +
375 static_cast<float>(lanes - 1) * std::max(p.laneSpacing, 0.5f);
376}
377
378std::vector<NedPoint> generateFlightPatternLawnmower(
379 const FlightPatternLawnmowerParams & p, const NedPoint & current)
380{
381 const float z = altitudeToZ(p.altitudeAgl, current.z);
382 const float heading = pick(p.laneHeading, std::isnan(current.yaw) ? 0.0f : current.yaw);
383 const float ux = std::cos(heading);
384 const float uy = std::sin(heading);
385 // side normal: RIGHT of the heading in NED (x north, y east) is (-uy, ux)
386 const float side = turnSign(p.firstTurn);
387 const float nx = -uy * side;
388 const float ny = ux * side;
389
390 const int lanes = flightPatternLawnmowerLaneCount(p);
391 const float spacing = std::max(p.laneSpacing, 0.5f);
392 const float length = std::max(p.laneLength, 0.0f);
393 const float coveredWidth = static_cast<float>(lanes - 1) * spacing;
394
395 float ox = pick(p.originX, current.x);
396 float oy = pick(p.originY, current.y);
397 if (p.originIsCenter)
398 {
399 ox -= 0.5f * length * ux + 0.5f * coveredWidth * nx;
400 oy -= 0.5f * length * uy + 0.5f * coveredWidth * ny;
401 }
402
403 std::vector<NedPoint> path;
404 path.reserve(2 * static_cast<size_t>(lanes));
405 for (int i = 0; i < lanes; ++i)
406 {
407 NedPoint a;
408 a.x = ox + static_cast<float>(i) * spacing * nx;
409 a.y = oy + static_cast<float>(i) * spacing * ny;
410 a.z = z;
411 NedPoint b;
412 b.x = a.x + length * ux;
413 b.y = a.y + length * uy;
414 b.z = z;
415 if (i % 2 == 0)
416 {
417 path.push_back(a);
418 path.push_back(b);
419 }
420 else
421 {
422 path.push_back(b);
423 path.push_back(a);
424 }
425 }
426 return path;
427}
428
437
439 const FlightPatternGridPatternParams & p, const NedPoint & current)
440{
442 first.originIsCenter = true; // the grid is always centred on the origin
443 first.originX = pick(p.base.originX, current.x);
444 first.originY = pick(p.base.originY, current.y);
445 first.laneHeading = pick(p.base.laneHeading, std::isnan(current.yaw) ? 0.0f : current.yaw);
446
447 std::vector<NedPoint> path = generateFlightPatternLawnmower(first, current);
448 if (!p.secondPass || path.empty())
449 {
450 return path;
451 }
452
453 // second pass: rotated 90 degrees over the same rectangle; try the four
454 // start corners (heading +-90, step side left/right) and take the one that
455 // starts nearest the end of pass 1
456 const NedPoint & joint = path.back();
457 std::vector<NedPoint> best;
458 float bestDist = std::numeric_limits<float>::max();
459 for (int variant = 0; variant < 4; ++variant)
460 {
461 FlightPatternLawnmowerParams second = first;
462 second.laneLength = first.width;
463 second.width = first.laneLength;
464 second.laneHeading =
465 first.laneHeading + ((variant & 1) ? -1.0f : 1.0f) * static_cast<float>(M_PI) / 2.0f;
466 second.firstTurn = (variant & 2) ? Turn::LEFT : Turn::RIGHT;
467 std::vector<NedPoint> candidate = generateFlightPatternLawnmower(second, current);
468 if (candidate.empty())
469 {
470 continue;
471 }
472 const float d = nedDistance(joint, candidate.front());
473 if (d < bestDist)
474 {
475 bestDist = d;
476 best = std::move(candidate);
477 }
478 }
479 path.insert(path.end(), best.begin(), best.end());
480 return path;
481}
482
484{
485 return 9.0f * std::max(p.radius, 1.0f) * static_cast<float>(std::max(p.cycles, 1));
486}
487
488std::vector<NedPoint> generateFlightPatternVSSearch(
489 const FlightPatternVSSearchParams & p, const NedPoint & current)
490{
491 const float dx = pick(p.datumX, current.x);
492 const float dy = pick(p.datumY, current.y);
493 const float z = altitudeToZ(p.altitudeAgl, current.z);
494 const float r = std::max(p.radius, 1.0f);
495 const float sign = turnSign(p.direction);
496 const float h0 = pick(p.initialHeading, std::isnan(current.yaw) ? 0.0f : current.yaw);
497 const int cycles = std::max(p.cycles, 1);
498
499 // leg headings in units of 120 degrees relative to the cycle's base heading:
500 // legs 3->4 and 6->7 continue straight through the datum
501 static const int kSteps[9] = {0, 1, 2, 2, 0, 1, 1, 2, 0};
502 const float third = 2.0f * static_cast<float>(M_PI) / 3.0f;
503
504 std::vector<NedPoint> path;
505 path.reserve(9 * static_cast<size_t>(cycles) + 1);
506 NedPoint v;
507 v.x = dx;
508 v.y = dy;
509 v.z = z;
510 path.push_back(v);
511
512 for (int c = 0; c < cycles; ++c)
513 {
514 const float base = h0 + sign * p.reorientation * static_cast<float>(c);
515 for (int leg = 0; leg < 9; ++leg)
516 {
517 const float heading = base + sign * third * static_cast<float>(kSteps[leg]);
518 v.x += r * std::cos(heading);
519 v.y += r * std::sin(heading);
520 path.push_back(v);
521 }
522 // snap back onto the datum to cancel accumulated float drift
523 path.back().x = dx;
524 path.back().y = dy;
525 }
526 return path;
527}
528
529} // namespace cl_px4_mr
Leg legFrom(const NedPoint &start, float endX, float endY)
float altitudeToZ(float altitudeAgl, float currentZ)
float pick(float param, float fallback)
std::vector< NedPoint > generateFlightPatternSpiral(const FlightPatternSpiralParams &p, const NedPoint &current)
std::vector< NedPoint > generateFlightPatternFigureEight(const FlightPatternFigureEightParams &p, const NedPoint &current)
std::vector< NedPoint > generateFlightPatternSineWaveVertical(const FlightPatternSineWaveVerticalParams &p, const NedPoint &current)
std::vector< NedPoint > generateFlightPatternVSSearch(const FlightPatternVSSearchParams &p, const NedPoint &current)
std::vector< NedPoint > generateFlightPatternSquareSpiral(const FlightPatternSquareSpiralParams &p, const NedPoint &current)
std::vector< NedPoint > generateFlightPatternLoiter(const FlightPatternLoiterParams &p, const NedPoint &current)
float flightPatternSquareSpiralLength(const FlightPatternSquareSpiralParams &p)
float nedDistance(const NedPoint &a, const NedPoint &b)
std::vector< NedPoint > generateFlightPatternSineWaveHorizontal(const FlightPatternSineWaveHorizontalParams &p, const NedPoint &current)
float flightPatternGridPatternLength(const FlightPatternGridPatternParams &p)
float flightPatternFigureEightLength(const FlightPatternFigureEightParams &p)
std::vector< NedPoint > generateFlightPatternLawnmower(const FlightPatternLawnmowerParams &p, const NedPoint &current)
int flightPatternLawnmowerLaneCount(const FlightPatternLawnmowerParams &p)
float flightPatternVSSearchLength(const FlightPatternVSSearchParams &p)
std::vector< NedPoint > generateFlightPatternGridPattern(const FlightPatternGridPatternParams &p, const NedPoint &current)
float turnSign(Turn t)
float flightPatternSpiralLength(const FlightPatternSpiralParams &p)
float flightPatternLawnmowerLength(const FlightPatternLawnmowerParams &p)
std::vector< NedPoint > generateFlightPatternAscend(const FlightPatternAscendParams &p, const NedPoint &current)