Skip to content

Follow and smooth the connections between swath groups - #149

Closed
u33549 wants to merge 7 commits into
open-navigation:mainfrom
u33549:smooth-connection-corners
Closed

u33549 wants to merge 7 commits into
open-navigation:mainfrom
u33549:smooth-connection-corners

Conversation

@u33549

@u33549 u33549 commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A connection between two swath groups was always driven as a single turn from the end of one group to the start of the next, ignoring the connection's own waypoints.

That is right for the u-turn between neighbouring swaths, where the track is the straight hop. It is wrong everywhere else. On a detour around a concave notch, or between decomposed cells, the route deliberately walks the border to stay inside the field, and planning a turn straight across it cuts through exactly the ground the route was avoiding.

Approach

Follow the connection's own track, rounding its corners through the turn planner and leaving a corner square when no maneuver fits within half an operation width of it.

Which of the two treatments a connection gets is decided by how far its track strays from the straight hop, not by the ratio of their lengths. A long cell's rounded corner is only about 20% longer than the diagonal across it, yet shares no ground with it, so a length ratio cannot tell them apart.

The corners at either end are planned from the headings of the swaths there, not from the track's first and last legs. The border graph splices the swath endpoint into the ring, so those legs are often a short spur that the robot is not driving along. Reading the heading off them plans the turn from a pose the robot is never in: a 90° corner comes out as an S, a reversal as an L, and the arc meets the swath at a cusp. Corners too close together to be taken one at a time are folded into a single maneuver for the same reason.

Known limitations

Two things I deliberately left out of this PR rather than hide:

  1. kCornerCutTol is a compile-time constant (0.5 × operation_width). It is really a user-facing tolerance — how far a rounded corner may leave the track — and belongs as a ROS parameter beside operation_width and min_turning_radius. Kept as a constant here to hold the PR to one concern; happy to add the parameter here instead if you would rather it landed together.
  2. The end-heading logic and the shallow-corner bound are not unit tested. The five tests above cover the rounding; the heading-aware ends, the corner folding and the jog bound were verified in RViz only.

The turn maths is heuristic and carries a number of tuning constants — the block at the top of path_generator.cpp. They are all fractions of the operation width or multiples of the turning radius, never absolute distances, but they are tuned rather than derived and are the part most worth a second opinion.

u33549 added 3 commits August 4, 2026 17:29
Following a connection's track needs a handful of plain geometry: the turn
between two headings, the distance from a point to a segment, a point a given
way along one, Douglas-Peucker thinning, and how far a planned path strays
from the polyline it stands in for. None of it knows anything about coverage,
so it sits in utils rather than in the path generator that uses it.

Also a spreadsheet-style label for a connection turn, so the same turn can be
named in the debug log and in RViz.
A connection was driven as a straight turn from the end of one swath group to
the start of the next. That holds for the u-turn between neighbours, where the
track is the hop itself, but not for a detour around a concave notch or between
decomposed cells, where it cut across ground the route had deliberately routed
around.

Follow the connection's own track instead, rounding its corners through the
turn planner and leaving them square where no maneuver fits within half an
operation width of the track. Which of the two a connection gets is decided by
how far the track strays from the straight hop, not by their length ratio:
rounding a long cell's corner is only about 20% longer than cutting its
diagonal yet shares no ground with it.

The corners at either end are planned from the headings of the swaths there,
not from the track's first and last legs. The border graph splices the swath
endpoint into the ring, so those legs are often a short spur the robot is not
driving along, and reading the heading off them plans the turn from a pose the
robot is never in - a 90 degree corner comes out as an S, a reversal as an L,
and the arc meets the swath at a cusp. Corners too close together to take one
at a time are folded into a single maneuver for the same reason.

Sharp corners are reported once per path as a warning; the per-corner detail is
at debug level.
The turns planned between swath groups are hard to pick out of the coverage
path, which is what makes a corner that came out wrong hard to see. Publish
them on their own topic as a marker array, each labelled the way the debug log
names it, so a turn on screen can be matched to the line that produced it.

Cleared per request, so one field's turns are not drawn over the next.
Copilot AI lite review requested due to automatic review settings August 4, 2026 15:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@u33549 u33549 closed this Aug 4, 2026
u33549 added 4 commits August 4, 2026 19:09
A candidate maneuver is rejected when it strays further from the track than
the corridor allows, except for a u-turn, which was exempt: a u-turn has to
swing past its vertices to come round and would fail the check by
construction.

That left it unchecked rather than checked loosely. Whether a connection is a
u-turn is a guess, made from the reversal angle and a hop that scales with the
operation width, and it is the one classification reachable in error - when it
is wrong, nothing bounds where the maneuver goes. The length budget does not
cover it either: a curve can be the right length and still bulge the wrong way.

Allow it the corridor plus the diameter it turns on, which is what a u-turn
legitimately needs, and hold it to that. The allowance is in turning radii, so
it does not inflate with the implement.
How far rounding a connection's corner may leave the track it follows was a
compile-time fraction of the operation width. It is a tolerance the operator
sets, not a property of the algorithm: how much ground a machine may cut
across depends on the field and on what the implement is doing, and the same
figure decides whether a corner is rounded at all or left square.

Declare it as `corner_cut_tolerance` in meters, defaulting to half the
operation width so existing behaviour is unchanged, and reconfigurable at
runtime on both servers. The sharp-corner warning now names it as the thing to
raise.
Cover the junction with a swath, where the connection's own track says nothing
about the heading the robot leaves on; two corners too close to round one at a
time, which have to be folded into a single maneuver; the open ends of a route,
which hold no heading and so are no corner; and the floor under a shallow
corner's approach, without which the planner is handed two poses centimetres
apart.

The first two fail without the change they cover. The other two pin behaviour
that was previously untested.
The corner code reads active_continuity_type_ to decide whether a turn needs
the wider radius a continuous curvature curve does, but nothing ever set it.
Left at UNKNOWN it read as discontinuous, so turnRadius() never grew the radius
and linear_curv_change had no bearing on a connection's turns.

Assign it where the mode is resolved. Fields using a high curvature rate are
unaffected - the clothoid ramp never binds there - but at the default rate a
90 degree corner now plans on a 0.56m radius rather than the 0.4m floor.
@u33549 u33549 reopened this Aug 4, 2026
@u33549

u33549 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author
image image image image image image image

Here are some tests. I literally cooked something here.

@u33549

u33549 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

I know it's a really long pr, but since it forms a meaningful whole, I couldn't split it up.

@SteveMacenski SteveMacenski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand this PR.

Why is this happening in the first place and isn't that a bug that should be resolved within F2C APIs instead of creating our own thing here? This is no longer simply a wrapper on F2C for ROS/Nav2 but definiting its own capabilities which is not the intent of the project. It seems to me that there's either a bug in F2C or we're not using it correctly causing an issue (or is this issue not really an issue at all but a misuse of APIs?)

Comment on lines +49 to +50
label.insert(label.begin(), static_cast<char>('A' + idx % 26));
idx = idx / 26;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit unusual. Why not simply number them like everything else?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Rviz, the numbers sometimes appear side by side; to separate them, I used letters instead of numbers for debugging purposes, but I can remove this feature.

inline double sweepBetween(double from, double to)
{
const double d = to - from;
return std::fabs(std::atan2(std::sin(d), std::cos(d)));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think angles lib does this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will check

@u33549

u33549 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

There is a genuine F2C bug, and it's still open upstream

f2c::pp::PathPlanning already has exactly this capability — planPathForConnection, backed by simplifyConnection, with a signature that matches our appendConnection almost parameter-for-parameter: (robot, s1, mp, s2, turn).

The reason we stopped calling it, back in open-navigation/opennav_coverage#137 ("Follow route connections in the path when they detour"), is this line in simplifyConnection:

if (p1.distance(p2) < 6.0 * safe_dist || mp.size() < 2) {
  path.emplace_back(p2, ang2);   // the polyline is discarded here
  return path;
}

It decides whether to follow the connection's polyline or cut straight across it by the straight-line distance between its endpoints alone — never by how far the polyline in between actually strays from that line. A detour around a concave notch or between decomposed cells routinely has its endpoints within 6 * safe_dist of each other while the path between them runs for tens of meters around real geometry. When that happens, simplifyConnection throws the polyline away and plans a straight hop through the void.

I checked this isn't fixed in the meantime: it's identical in the pinned v2.0.0 and in the current upstream tip (the in-progress v3 rework touches everything around it but leaves this exact condition untouched).

I'd also tried F2C directly before working around it, not just read the source — the results were poor on exactly the fields this matters for, concave and decomposed ones, which is what sent me looking for why.

The heading bugs were ours, not F2C's — but going back isn't the fix either

The bugs this review caught — the S-shape, the teardrop, the wrong L — all trace back to one thing: our version read the heading at each end of a connection off the polyline's own first and last leg. simplifyConnection never does that; it takes ang1/ang2 as parameters from the calling swaths and never infers them from the track. That part didn't need reinventing, and reinventing it is what let the bug in.

That doesn't make simplifyConnection the fix, though. It has real gaps of its own — at vp.size() == 2 its fallback guard misses by one and drops both corners to a straight line, and its fillet loop skips the first and last corner it detects entirely, at any size. It also never checks what createTurn hands back for a loop or excess deviation.

So the corner logic here does more than simplifyConnection ever did, not less. The heading bug was ours, and is fixed; the rest was never F2C's to have covered.

Why this isn't an isolated call

The corridor routing from an earlier PR diverges from F2C's own approach for the same kind of reason, and the per-cell TSP routing before that replaced F2C's global genRoute because the all-pairs route matrix it builds runs out of memory on a multi-cell field. Neither is a matter of using the API wrong — they're scaling and geometry limits hit on real fields, worked around one at a time. Both were built the same way: use F2C where it holds, and patch the specific place it doesn't, rather than stand up a parallel implementation next to it. This PR is the one where the patch grew past that.

Where these patches should live

If it keeps this package from turning into a shadow of F2C, I'd rather fixes like this one not live inside opennav_coverage at all. A separate, general-purpose repo for F2C patches and extensions — this connection issue, the per-cell TSP work, whatever comes next — would keep this package thin and let each patch stand on its own rather than sit embedded in a robotics stack.

@SteveMacenski

Copy link
Copy Markdown
Member

What is the reason a PR can't be opened against F2C to resolve this issue and we target that newer verssion once merged?

@u33549

u33549 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

I open the upstream issue (and a PR if they want it), and this workaround stays here until that fix is released and we've migrated to the version that has it, then we drop this branch.

@SteveMacenski

Copy link
Copy Markdown
Member

Can you link the ticket/PR here so this is all cross referenced?

I'd prefer to hold off until we hear back on that (or if we don't hear for a very long time)

@u33549

u33549 commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

It's done. I'd appreciate it if you could take a look when you have a chance.

@u33549

u33549 commented Aug 22, 2026

Copy link
Copy Markdown
Contributor Author

Hi @SteveMacenski,
The part needed for this PR is almost complete, but I’ll also move the changes we made here earlier—which actually belong in f2c—over there and open a cleanup PR for the codebase.

@u33549

u33549 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Closing this in favour of #162. Following the connections between swath groups now lives in Fields2Cover (Fields2Cover#234, released in v2.1.0), and since #162 main uses F2C v2.1.1 instead of the downstream copy. The remaining smoothing work for connection paths will go into Fields2Cover first and come back here as a thin wrapper PR once it is released. Thanks for the reviews!

@u33549 u33549 closed this Nov 26, 2026
@u33549
u33549 deleted the smooth-connection-corners branch November 30, 2026 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants