AtsushiSakai/PythonRobotics · error · ValueError

self.moving direction is invalid

Error message

self.moving direction is invalid 

What it means

Raised by search_start_grid when moving_direction is neither MovingDirection.RIGHT nor MovingDirection.LEFT. The sweep planner needs a lateral direction to pick the starting grid cell and treats any other value as a configuration error.

Source

Thrown at PathPlanning/GridBasedSweepCPP/grid_based_sweep_coverage_path_planner.py:117

        self.moving_direction *= -1
        self.update_turning_window()

    def search_start_grid(self, grid_map):
        x_inds = []
        y_ind = 0
        if self.sweep_direction == self.SweepDirection.DOWN:
            x_inds, y_ind = search_free_grid_index_at_edge_y(
                grid_map, from_upper=True)
        elif self.sweep_direction == self.SweepDirection.UP:
            x_inds, y_ind = search_free_grid_index_at_edge_y(
                grid_map, from_upper=False)

        if self.moving_direction == self.MovingDirection.RIGHT:
            return min(x_inds), y_ind
        elif self.moving_direction == self.MovingDirection.LEFT:
            return max(x_inds), y_ind

        raise ValueError("self.moving direction is invalid ")


def find_sweep_direction_and_start_position(ox, oy):
    # find sweep_direction
    max_dist = 0.0
    vec = [0.0, 0.0]
    sweep_start_pos = [0.0, 0.0]
    for i in range(len(ox) - 1):
        dx = ox[i + 1] - ox[i]
        dy = oy[i + 1] - oy[i]
        d = np.hypot(dx, dy)

        if d > max_dist:
            max_dist = d
            vec = [dx, dy]
            sweep_start_pos = [ox[i], oy[i]]

    return vec, sweep_start_pos

View on GitHub (pinned to 1fe4fb980f)

Solutions

  1. Set moving_direction to MovingDirection.RIGHT or MovingDirection.LEFT explicitly.
  2. If reading config from file, map strings to the enum: MovingDirection[value.upper()].
  3. Double-check you are not passing the sweep direction (an angle/vector) into the moving_direction parameter.

Example fix

# before
planner = GridBasedSweepCPP(..., moving_direction='right')

# after
planner = GridBasedSweepCPP(..., moving_direction=MovingDirection.RIGHT)
Defensive patterns

Strategy: type-guard

Validate before calling

assert planner.moving_direction in (MovingDirection.RIGHT, MovingDirection.LEFT)

Type guard

from PathPlanning.GridBasedSweepCPP.grid_based_sweep_coverage_path_planner import MovingDirection

def to_moving_direction(v):
    if isinstance(v, MovingDirection):
        return v
    try:
        return MovingDirection[v.upper()]
    except KeyError:
        raise ValueError(f'invalid moving direction: {v}')

Prevention

When it happens

Trigger: Constructing the coverage planner with moving_direction set to a string, int, or an enum member of a different type (e.g. sweeping direction enum), then calling sweep_path_search.

Common situations: Confusing moving_direction with sweep_direction, or assigning a raw string 'right' instead of MovingDirection.RIGHT when building the planner.

Related errors


AI-assisted analysis of AtsushiSakai/PythonRobotics@1fe4fb980f (2026-08-28). Data as JSON: /api/errors/16f3691dc4fb9c24. Report an issue: GitHub.