Drivers

These classes define agents that can drive a mobile robot around a workspace, and are useful for testing localization algorithms.

Random Path

class RandomPath(dthresh=0.05, seed=0, **kwargs)[source]

Bases: VehicleDriverBase

Driving agent for a random path

__init__(dthresh=0.05, seed=0, **kwargs)[source]

Driving agent for random path

Parameters:
  • dthresh (float) – distance threshold as a fraction of the larger workspace dimension, defaults to 0.05

  • seed (int | None) – random number seed, defaults to 0, None for a random seed

  • kwargs (Any) – arguments passed to VehicleDriverBase, in particular workspace which is required

Raises:

ValueError – no workspace specified

Returns a driver object that drives the attached vehicle to a sequence of random waypoints.

The driver is connected to the vehicle by:

veh = Bicycle(control=driver)

or:

veh = Bicycle()
veh.control = driver

The waypoints are positioned inside a rectangular region defined by the workspace, which must be given:

driver = RandomPath(workspace=10)

Note

  • It is possible in some cases for the vehicle to move outside the desired region, for instance if moving to a waypoint near the edge, the limited turning circle may cause the vehicle to temporarily move outside.

  • The vehicle chooses a new waypoint when it is closer than dthresh times the larger workspace dimension to the current waypoint.

  • Uses its own random number generator so as to not influence the performance of other randomized algorithms such as path planning. Set seed=None to have it randomly initialized from the operating system.

See also

Bicycle Unicycle plotvol2()

__str__()[source]

Convert to string

Return type:

str

Returns:

driver parameters and state in a compact human readable format

init(ax=None)[source]

Initialize random path driving agent

Parameters:

ax (Axes | None) – axes in which to draw the current goal, defaults to None

Return type:

None

Called at the start of a simulation run. Ensures that the random number generator is reseeded so that the sequence of random waypoints is repeatable.

demand()[source]

Compute speed and steering angle for the current random waypoint

Return type:

ndarray[Tuple[Literal[2]], dtype[floating]]

Returns:

speed and steering angle demand for VehicleBase

This is the speed and steering angle that drives the vehicle toward the current waypoint. When the vehicle is within dthresh times the larger workspace dimension of the waypoint, a new random waypoint is chosen.

See also

VehicleBase

__repr__()

Convert to string, same as __str__()

Return type:

str

Returns:

driver parameters and state in a compact human readable format

driveto(goal)

Speed and steering demand to drive towards a goal point

Parameters:

goal (Union[List[float], Tuple[float, float], ndarray[Tuple[Literal[2]], dtype[floating]]]) – goal point (x, y)

Return type:

ndarray[Tuple[Literal[2]], dtype[floating]]

Returns:

speed and steering angle demand

The speed demand is the constant speed. The steering angle demand is the heading error to the goal, \(\Delta\theta\), scaled by headinggain.

If the goal marker is displayed, it is moved to the goal.

property vehicle: VehicleBase | None

Set/get the vehicle under control

Getter:

return the VehicleBase instance, or None

Setter:

set the VehicleBase instance

Note

The setter is invoked by vehicle.control = driver

Setter accepts:

VehicleBase

property workspace: ndarray | None

Size of robot driving workspace

Returns:

workspace bounds [xmin, xmax, ymin, ymax], or None

Return type:

ndarray(4) or None

Returns the bounds of the workspace as specified by constructor option workspace or, if that was not given, the workspace of the vehicle that the driver is attached to. None if neither has one.

Pure Pursuit

class PurePursuit(path, lookahead=1, path_style=None, **kwargs)[source]

Bases: VehicleDriverBase

Driving agent for pure pursuit

__init__(path, lookahead=1, path_style=None, **kwargs)[source]

Driving agent for pure pursuit

Parameters:
  • path (NDArray) – path as a set of waypoints, one column per point

  • lookahead (float) – lookahead distance, defaults to 1

  • path_style (dict[str, Any] | None) – Matplotlib line style used to display the path, merged over the default PATH_STYLE (a wide, translucent red line)

  • kwargs (Any) – arguments passed to VehicleDriverBase

Raises:

ValueError – path is not a 2xN array

Returns a driver object that drives the attached vehicle along the path using the pure pursuit algorithm: the vehicle steers toward the first point on the path that is at least lookahead away from the vehicle, beyond the closest point.

The driver is connected to the vehicle by:

veh = Bicycle(control=driver)

or:

veh = Bicycle()
veh.control = driver

If the simulation is animated, the path is plotted using path_style and the current goal point, which changes at every time step, is shown using goal_style.

See also

RandomPath VehicleBase

__str__()[source]

Convert to string

Return type:

str

Returns:

driver parameters and state in a compact human readable format

init(ax=None)[source]

Initialize pure pursuit driving agent

Parameters:

ax (Axes | None) – axes in which to draw the path and waypoints, defaults to None

Return type:

None

Called at the start of a simulation run. If ax is given, plot the path and create the goal marker.

demand()[source]

Compute speed and steering angle to follow the path

Return type:

ndarray[Tuple[Literal[2]], dtype[floating]]

Returns:

speed and steering angle demand for VehicleBase

The goal is the first point on the path, beyond the point closest to the vehicle, that is at least lookahead away from the vehicle. If there is no such point the goal is the end of the path.

__repr__()

Convert to string, same as __str__()

Return type:

str

Returns:

driver parameters and state in a compact human readable format

driveto(goal)

Speed and steering demand to drive towards a goal point

Parameters:

goal (Union[List[float], Tuple[float, float], ndarray[Tuple[Literal[2]], dtype[floating]]]) – goal point (x, y)

Return type:

ndarray[Tuple[Literal[2]], dtype[floating]]

Returns:

speed and steering angle demand

The speed demand is the constant speed. The steering angle demand is the heading error to the goal, \(\Delta\theta\), scaled by headinggain.

If the goal marker is displayed, it is moved to the goal.

property vehicle: VehicleBase | None

Set/get the vehicle under control

Getter:

return the VehicleBase instance, or None

Setter:

set the VehicleBase instance

Note

The setter is invoked by vehicle.control = driver

Setter accepts:

VehicleBase

property workspace: ndarray | None

Size of robot driving workspace

Returns:

workspace bounds [xmin, xmax, ymin, ymax], or None

Return type:

ndarray(4) or None

Returns the bounds of the workspace as specified by constructor option workspace or, if that was not given, the workspace of the vehicle that the driver is attached to. None if neither has one.

Superclass

class VehicleDriverBase(workspace=None, speed=1, headinggain=0.3, goal_style=None, verbose=False, goalmarkerstyle=None)[source]

Bases: ABC

Abstract vehicle driver class

Abstract class for an agent that can drive a mobile robot.

__init__(workspace=None, speed=1, headinggain=0.3, goal_style=None, verbose=False, goalmarkerstyle=None)[source]

Abstract vehicle driver

Parameters:
  • workspace (Union[float, ndarray, Sequence[float], HasWorkspace, None]) – dimension of workspace, see spatialmath.base.graphics.expand_dims(), defaults to None

  • speed (float) – forward speed, defaults to 1

  • headinggain (float) – gain of the proportional controller that steers the vehicle towards the current goal, defaults to 0.3

  • goal_style (dict[str, Any] | None) – Matplotlib line style of the marker used to display the current goal, merged over the default GOAL_STYLE (a red diamond)

  • verbose (bool) – display debug information, defaults to False

  • goalmarkerstyle (dict[str, Any] | None) – deprecated, use goal_style

Deprecated since version 1.5.0: goalmarkerstyle is deprecated, use goal_style instead.

The workspace of the robot is a rectangular region defined by workspace that is specified by (see plotvol2()):

workspace

x-range

y-range

A (scalar)

-A:A

-A:A

[A, B]

A:B

A:B

[A, B, C, D]

A:B

C:D

abstractmethod demand()[source]

Compute speed and steering angle

Return type:

ndarray[Tuple[Literal[2]], dtype[floating]]

Returns:

speed and steering angle demand for VehicleBase

When an instance of a VehicleDriverBase class is attached as the control for an instance of a VehicleBase class, this method is called at each time step to provide the control input.

Has access to the vehicle and its state through the vehicle() property.

abstractmethod init(ax=None)[source]

Initialize driving agent

Parameters:

ax (Axes | None) – axes in which to draw markers, defaults to None

Return type:

None

Called at the start of a simulation run. Used to initialize state including random number generator state.

property vehicle: VehicleBase | None

Set/get the vehicle under control

Getter:

return the VehicleBase instance, or None

Setter:

set the VehicleBase instance

Note

The setter is invoked by vehicle.control = driver

Setter accepts:

VehicleBase

__repr__()[source]

Convert to string, same as __str__()

Return type:

str

Returns:

driver parameters and state in a compact human readable format

property workspace: ndarray | None

Size of robot driving workspace

Returns:

workspace bounds [xmin, xmax, ymin, ymax], or None

Return type:

ndarray(4) or None

Returns the bounds of the workspace as specified by constructor option workspace or, if that was not given, the workspace of the vehicle that the driver is attached to. None if neither has one.

driveto(goal)[source]

Speed and steering demand to drive towards a goal point

Parameters:

goal (Union[List[float], Tuple[float, float], ndarray[Tuple[Literal[2]], dtype[floating]]]) – goal point (x, y)

Return type:

ndarray[Tuple[Literal[2]], dtype[floating]]

Returns:

speed and steering angle demand

The speed demand is the constant speed. The steering angle demand is the heading error to the goal, \(\Delta\theta\), scaled by headinggain.

If the goal marker is displayed, it is moved to the goal.