Repository navigation
Follow connection polylines instead of cutting across them - #234
Gonzalo-Mier merged 11 commits into
Conversation
discretizeSwath() only densified SWATH states; HL_SWATH stayed a single waypoint, so headland/corridor legs stored with that type couldn't be subdivided. Neither type has ever been produced by F2C itself (HL_SWATH has been dead since the v2.0.0 initial commit), so no existing call path changes behavior.
The only public way to produce an HL_SWATH state was building a PathState by hand and calling addState. appendSwath always hardcoded SWATH. New overload takes the type explicitly; the 2-argument version now delegates to it with SWATH, so the existing signature and ABI are unchanged.
planPathForConnection routed every consecutive waypoint pair through the turn planner, including straight legs, and simplifyConnection's angle-wrap bug (fabs on a raw angle difference) mis-simplified sharply-turning tracks. Replace both with a corner-by-corner planner: straight runs become HL_SWATH legs, only genuine corners go through createTurn, and corners too tight or too close together are folded or left sharp rather than plotting a shortcut across ground the route routed around. Four details the geometry needs: - The approach to a corner is sized from the radius the turn actually sweeps, which is the wider of two effects: the clothoid ramps spend part of the deflection (getSmoothTurningRadius, public since Fields2Cover#233), and a shallow corner never reaches full curvature at all, peaking at sqrt(rate * sweep). Take only the first and a shallow corner is planned from a radius it never drives, which starts the turn a metre early and cuts ground the legs either side of it are covering. - A candidate turn is judged by how far it turns, not how long it is. A loop costs an extra 2*pi*R, which at a small turning radius is a couple of metres and hides inside any length budget, but it cannot hide in the turning itself. - cut_tol is floored in turning radii and capped at the operation width, so a compact implement still gets room for a fillet while a wide-turning one keeps the corners it should leave square. - The tangent margin is room for a clothoid lead-in, so it applies only to a continuous-curvature turn; a discontinuous one needs the plain tangent length and cutting more would only cut corner. At a pinned route end taking a single corner there is no outgoing leg -- the reference point is the corner itself -- so the lateral-offset bound collapses to zero and would refuse a corner the tangent bound allows. Skip it there. Enables the fields2cover_pp_pp.planPathForConnection test, dead since v2.0.0's first commit.
Nine tests over the corner-rounding path, which had no coverage before: detour corners, tight jogs folded into one S, neighbour u-turns, curved tracks that must not be flattened, corners left sharp because no turn fits, corners taken from the swath's heading rather than the track's, open-ended route connections, shallow-corner approach floors, and corners too close to round one at a time. Each bound here was checked by reverting the code it covers and confirming the test goes red, so none of them is decorative. Two are worth naming: the shallow-corner test bounds the approach on both sides, because starting a turn too early cuts ground the covering legs either side of it need; and maxTurnSweep is what catches a turn planner answering with a loop, which is smooth, barely longer, and invisible to every other measure here. distanceToPath measures to the polyline through the states, not to the states themselves: a straight run is one state, so measuring to states alone would miss the track between them.
|
It looks super nice the changes! Thank you really much for all your efforts. I asked to move a bit some functions, but the logic seems solid. Thank you again @u33549 |
|
@Gonzalo-Mier thank you, glad the approach reads well! I can't see the comments about moving the functions on my side the diff shows none. Happy to do the moves as soon as I can read them. |
Sorry, I forgot to send them |
|
Two structural questions before I do, since they change the shape of the diff: 1. 2. Making the constants settable. My plan otherwise: the corner-cut tolerance onto |
|
Hi @u33549, Answering to your message:
|
Following a connection's track needs a handful of plain geometry that has nothing to do with path planning: a point a given way along a ray, the points of a geometry as a vector, appending a straight leg to a path, and how far a path strays from a line. Point::getPointAlong, MultiPoint::toVectorPoint, LineString::toVectorPoint, Path::appendStraight and Path::maxDistanceTo. appendStraight takes a speed rather than a Robot, matching appendSwath -- Robot.h already includes Path.h, so the dependency cannot run the other way.
getSmoothTurningRadius computes a property of the robot, and the corner planner needs two more of the same kind: the radius a turn of a given deflection is driven at, and the tolerances and thresholds that decide whether a corner is rounded at all. Robot::getSmoothTurningRadius and Robot::getTurnRadius, plus settable parameters for the corner planner. The four that scale with the machine are stored in real units and derived from the robot when unset -- an operator sets "at most 1.25 m of corner may be cut", not a fraction -- following the turn_vel_ precedent already in the class.
The anonymous namespace was carrying general-purpose geometry and a block of tuning constants that callers had no way to reach. Both now live on the types they describe, so this file keeps only the cornering logic. getSmoothTurningRadius stays on PathPlanning as a forwarder: it was made public one release ago, and moving it outright would break callers.
At 185 lines the reasoning was hard to follow. Same logic, in the four steps it was already doing: the deflection at each corner, the fold of corners too close to take one at a time, the room a span leaves for a maneuver, and the search for a turn that fits it. No behaviour change.
|
Hi @Gonzalo-Mier, Three places where I did not follow the letter of the review, each for a reason:
The parameters that scale with the machine are stored in real units, not as the fractions they were.
On the |
The wrapper still took F2CPath& and mutated it, the exact pattern the review asked to move off of -- moving the logic onto Path only helped if callers actually called the member function.
|
One more: I'd resolved the thread about not mutating the passed-in |
|
One last thing before accepting it. You use comments with |
|
I hope I've changed the comments correctly |
|
Awesome changes and awesome PR! Thank you a lot again @u33549 |
|
@Gonzalo-Mier You're welcome, it was a pleasure for me. I have a few more changes planned here, and I'm happy to work together on v3 once they're done. |
|
I’d also like to share what we’ve done on LinkedIn—if you’re okay with that, of course. If you approve, should I post about this change now, or wait until everything is finished and post everything at once? |
|
Feel free to post everywhere what you do for open-source! no need to ask for permission or approval. It's your own work, so be proud wherever you want 😄 |












Motivation
planPathForConnection()ignores the polyline it is given. It hands every consecutive posepair to the turn planner, so a connection detouring around a concave notch or between
decomposed cells is driven straight from one end to the other, cutting across ground the
route deliberately routed around.
simplifyConnection()also compares headings withfabs(ang_in - ang_out)on raw angles, which wraps wrong.The library's own test says as much:
planPathForConnectionhas beenDISABLED_sincev2.0.0's first commit, and forcing it to run shows the path coming out shorter than the
track it is meant to follow.
PathSectionType::HL_SWATHis equally unfinished — never produced or consumed anywhere,skipped by
discretizeSwath(), unreachable through any public API. It is in this PR becausethe fix produces it: the straight legs between rounded corners are headland passes, not
turns, and without the
HL_SWATHhalf each would be a typediscretizeSwath()refuses tosubdivide, leaving it a single waypoint.
1. Discretize
HL_SWATHsections (83dc019)discretizeSwath()only densifiedSWATHstates. One condition, plus the doc comment.No call path in the tree changes behaviour, because nothing produced the type.
2. Let
appendSwathwrite the section type (e9a18f1)The two-argument form delegates with
SWATH. An overload rather than a default argument,so the ABI is untouched.
setSwathType()/setTurnType()already exist and have no callers in the library — theyare there for the outside, and the third type had no counterpart.
If the preferred answer is to drop the enumerator instead: it is serialized as
3byserializePath, so removing it changes the file format, and telling a headland pass froma swath is a real need —
generateHeadlandSwathsreturns rings, and turning one into adrivable path is the next thing we need from the library.
3. Follow and round the connection track (
9c3a06e)planPathForConnectionnow walks the polyline: straight runs becomeHL_SWATHlegs,and only genuine corners go to
createTurn. Corners too close together to take one at atime are folded into a single maneuver; a corner a turn could only round by cutting past
cut_tolis left square instead. A connection whose track stays within a quarter of the operation widthof the straight hop, or one that reverses over a short hop, is still driven direct — the
u-turn between neighbouring swaths is the hop itself, and an arc through a 180-degree
vertex has no tangent length to round with.
Four details decide whether that produces corners or loops. Each was checked by reverting it
and confirming a test goes red:
getSmoothTurningRadius(public since Small API additions: Route::reverse, TurningBase::hasContinuousCurvature, public getSmoothTurningRadius #233; the clothoid ramps spend part of thedeflection) and
sqrt(rate * sweep)(a shallow corner never reaches full curvature). Theycross around 50 degrees. Take only the first and a shallow corner starts its turn a metre
early, cutting ground the covering legs either side of it need.
2*pi*Ris acouple of metres at a small radius — inside any sane length budget, and smooth enough that
no heading-step check sees it. Turning does not hide it, and it catches doubling-back S
shapes too.
cut_tolis floored in turning radii, capped at the operation width. Tied to thewidth alone, an implement whose turning circle is close to its own width gets every corner
refused; the cap stops a wide-turning one cutting corners it should leave square.
clothoid lead-in. A discontinuous turn has nothing to ramp.
simplifyConnectionis removed, body and private declaration. This closes the second halfof #231 by construction — the angle-wrap bug lived there, and the new code uses
Point::getAngleDiffAbsthroughout.One more fix along the way: at a pinned route end taking a single corner the outgoing
reference point is the corner, so the lateral-offset bound collapsed to zero and refused a
corner the tangent bound allowed.
4. Tests (
7f7b322)fields2cover_pp_pp.planPathForConnectionis enabled with its expectations untouched.Nine new tests over behaviour that had no coverage: detour corners, tight jogs folded into
one S, neighbour u-turns, curved tracks that must not be flattened, corners left sharp
because rounding would cut too much, corners taken from the swath's heading not the track's,
open-ended route connections, shallow-corner approach floors, and corners too close to
round one at a time.
distanceToPath,maxHeadingStep,turnStartsandmaxTurnSweepgo into the checker.distanceToPathmeasures to the polyline through the states rather than to the statesthemselves — a straight run is a single state, so measuring to states alone would miss the
track between them.
maxTurnSweepis the loop fence: it takes the widest excursion of aturn's running signed heading change, so a corner or an S reads under half a revolution
while a loop runs past a full one.
The shallow-corner test bounds the approach on both sides: a turn that starts at the
corner has no tangent to turn in, and one that starts too far back leaves the leg early and
cuts ground the covering legs either side of it need.
One candidate detail was dropped after the same check — adding the clothoid ramp length to
the approach distance. With the turning fence in place it changed no output at all, and at a
swath junction it made the turn slightly wider.
Known limitations
The tuning constants (
cut_tol's fractions, the u-turn thresholds, the span limit) areconstexprin an anonymous namespace rather than parameters.f2c::pp::PathPlanningisentirely static, so exposing them raises a static-vs-instance question that is wider than
this change. Making
cut_tola defaulted argument would be a sensible follow-up: how muchground a machine may cut across depends on the field and on what the implement is doing, so
it is the one an operator would plausibly want to set.
Where a connection meets a swath, the entry pose is the swath's own end pose and cannot
slide back along it, so a continuous-curvature turn there has no approach room and swings
slightly wide before settling. How wide depends on the curvature rate: it is small at rates
a field machine uses and grows as the rate falls. Bounded and drivable, but not as tight as
a corner with room on both sides.