Skip to content

Add always_turn_between_swaths parameter - #168

Open
alireza-moayyedi wants to merge 1 commit into
open-navigation:mainfrom
nobleo:add-always-turn-between-swaths-param
Open

alireza-moayyedi wants to merge 1 commit into
open-navigation:mainfrom
nobleo:add-always-turn-between-swaths-param

Conversation

@alireza-moayyedi

Copy link
Copy Markdown
Contributor

Hi,

For my specific controller, I need paths with no sharp turns. Right now, that is not guaranteed. So I thought having a simple parameter enforcing that would be useful in such situations. Default is false so should not break any existing behavior.

For the test added below this would be the difference:
image

@agentscanapp

agentscanapp Bot commented Nov 27, 2026

Copy link
Copy Markdown

Insufficient data

Not enough activity yet to make a reliable assessment.

View full analysis →

This is an automated analysis by AgentScan

@alireza-moayyedi
alireza-moayyedi force-pushed the add-always-turn-between-swaths-param branch from 3d3ba55 to 7e1eb14 Compare November 27, 2026 10:49
@u33549

u33549 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the PR. This one is on our side: Fields2Cover v2.1.1 already plans these connections properly, following the detour and rounding its corners, but this package still uses its own older connection code, and that is what produces the sharp corners. When I closed #149 I thought #162 had already switched it over, but it had not.

I am preparing a PR that does, together with a few similar leftovers from #162. On your test route it gives the same turn as your flag, and on a real detour it keeps to the route instead of cutting across.
pr168_paths

I will link it here once it has been through the same scenario checks as #162. If you want to try it on your fields before that, the change is in assemblePath: call generator_->planPathForConnection(...) instead of appendConnection(...).

@SteveMacenski

Copy link
Copy Markdown
Member

@alireza-moayyedi is this still needed from #171?

@alireza-moayyedi

Copy link
Copy Markdown
Contributor Author

@SteveMacenski @u33549 I did some tests and here for instance you can see the different cases that I have run on Jazzy v2:

before updating to fields2cover v2.1.1 and without enforcing turns using the parameter introduced in this PR:
Screenshot from 2026-12-01 17-44-37

Before updating to fields2cover v2.1.1 but with enforcing turns using the parameter:
Screenshot from 2026-12-01 17-43-57

Using fields2cover v2.1.1 which is now on Jazzy v2 branch:
Screenshot from 2026-12-01 17-42-47

Although v2.1.1 it fixes the oiriginal problem, one thing that I find odd is that now on the lower left corner, the path is planned too close to the field boundary.

@u33549

u33549 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

I'm aware of the issue you mentioned; it's essentially an upstream bug in Fields2Cover. I worked around it using the route-fix branch from this fork, as it hasn't been upstreamed to the main package yet.

@SteveMacenski

Copy link
Copy Markdown
Member

@u33549 should we do this here or fix it in F2C?

@u33549

u33549 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

This should be fixed in F2C. I hope it will be included in V2.2.

@u33549

u33549 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

@alireza-moayyedi could you share your field and the parameters you used, so I can look into this properly? I'd rather check your exact case than reason about it in theory. The field polygon and the goal/server parameters (headland width, robot_width, operation_width, min_turning_radius, path mode) would be enough.

@alireza-moayyedi

Copy link
Copy Markdown
Contributor Author

@u33549 Just to be clear, I do not have one specific field. My robot is supposed to work in different shapes. And I see this happening often with trapezoidal fields in the corners. But for the sake of traceability, I vibecoded this python script which shows the exact field and settings I was using to report the abovementioned tests. You can just run it with python3. Additionally, I have also added a debug publisher for the exclusion zone polygon because that I think also should happen differently. Currently the turns are too close to the exclusion zone:

Screenshot from 2026-12-02 11-22-40
#!/usr/bin/env python3

import argparse
import subprocess
import time

import rclpy
from geometry_msgs.msg import Point32, PolygonStamped
from rclpy.action import ActionClient
from rclpy.node import Node
from rclpy.qos import DurabilityPolicy, QoSProfile

from opennav_coverage_msgs.action import ComputeCoveragePath
from opennav_coverage_msgs.msg import Coordinate, Coordinates


# Outer boundary first, followed by the exclusion zone (inner cutout), in the "map" frame
BOUNDARY = [
    (3.368756055831909, 6.888899326324463),
    (3.922778844833374, 11.768609046936035),
    (-3.2223546504974365, 11.186932563781738),
    (-3.5314040184020996, 3.1656458377838135),
]
EXCLUSION_ZONE = [
    (-0.27028051018714905, 8.343915939331055),
    (1.5234079360961914, 8.317665100097656),
    (1.336050033569336, 7.566812038421631),
    (-0.061599697917699814, 7.153715133666992),
]


ACTION = "compute_coverage_path"
SERVER_PARAMS = {
    "default_headland_width": 0.32,
    "robot_width": 0.63,
    "operation_width": 0.63,
    "min_turning_radius": 0.3,
    "linear_curv_change": 1000.0,
    "default_turn_point_distance": 0.01,
    "default_route_type": "TSP",
    "default_swath_type": "NUMBER",
}


def start_server():
    cmd = ["ros2", "run", "opennav_coverage", "opennav_coverage", "--ros-args"]
    for k, v in SERVER_PARAMS.items():
        cmd += ["-p", f"{k}:={v}"]
    proc = subprocess.Popen(cmd)
    # coverage_server is a lifecycle node
    for transition in ("configure", "activate"):
        for _ in range(20):
            if subprocess.run(["ros2", "lifecycle", "set", "/coverage_server", transition],
                              capture_output=True).returncode == 0:
                break
            time.sleep(0.5)
        else:
            proc.terminate()
            raise SystemExit(f"Could not {transition} /coverage_server")
    return proc


def ring(points):
    coords = Coordinates()
    # The server requires closed rings (first == last)
    for x, y in [*points, points[0]]:
        coords.coordinates.append(Coordinate(axis1=x, axis2=y))
    return coords


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--headland", type=float, default=0.63, help="headland width (0 = none)")
    args = ap.parse_args()

    goal = ComputeCoveragePath.Goal()
    goal.frame_id = "map"
    goal.polygons = [ring(BOUNDARY), ring(EXCLUSION_ZONE)]
    goal.generate_headland = args.headland > 0
    if goal.generate_headland:
        goal.headland_mode.mode = "CONSTANT"
        goal.headland_mode.width = args.headland
    # Swath/route/path modes are left unset, so the server's default_* parameters apply

    server = start_server()
    try:
        rclpy.init()
        node = Node("coverage_repro")
        # Latched, so RViz can pick it up whenever it subscribes
        zone_pub = node.create_publisher(
            PolygonStamped, "exclusion_zone",
            QoSProfile(depth=1, durability=DurabilityPolicy.TRANSIENT_LOCAL))
        zone = PolygonStamped()
        zone.header.frame_id = "map"
        zone.polygon.points = [Point32(x=x, y=y) for x, y in EXCLUSION_ZONE]
        zone_pub.publish(zone)
        client = ActionClient(node, ComputeCoveragePath, ACTION)
        if not client.wait_for_server(timeout_sec=10.0):
            raise SystemExit(f"Action server '{ACTION}' not available")

        print(f"Sending {len(goal.polygons)} polygon(s): "
              f"{[len(p.coordinates) for p in goal.polygons]} points each (incl. closing point)")
        send = client.send_goal_async(goal)
        rclpy.spin_until_future_complete(node, send)
        handle = send.result()
        if not handle.accepted:
            raise SystemExit("Goal rejected")
        res = handle.get_result_async()
        rclpy.spin_until_future_complete(node, res)
        result = res.result().result

        print(f"error_code: {result.error_code}")
        print(f"planning_time: {result.planning_time.sec}.{result.planning_time.nanosec:09d}s")
        print(f"nav_path poses: {len(result.nav_path.poses)}")
        for pose in result.nav_path.poses:
            print(f"{pose.pose.position.x:.4f} {pose.pose.position.y:.4f}")

        print("Publishing /exclusion_zone (PolygonStamped, frame 'map'); Ctrl+C to quit")
        try:
            rclpy.spin(node)
        except KeyboardInterrupt:
            pass
        node.destroy_node()
        rclpy.shutdown()

    finally:
        server.terminate()
        server.wait()

if __name__ == "__main__":
    main()

@u33549

u33549 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Thank you for the information; I will try to conduct some tests as soon as possible and get feedback.

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