Skip to content

Controllers

BaseController

BaseController()

Bases: ABC

Interface of a controller.

Initialize the controller.

Source code in pypolo/controllers/base_controller.py
def __init__(self):
    r"""Initialize the controller."""
    self.goals = np.array([])

has_goals property

has_goals

Check if the controller has goals.

Returns:

Name Type Description
bool bool

True if the controller has goals, False otherwise.

control abstractmethod

control(state)

Compute the control action.

Parameters:

Name Type Description Default
state ndarray

The current state of the system.

required

Returns:

Type Description
ndarray

np.ndarray: The control action.

Source code in pypolo/controllers/base_controller.py
@abstractmethod
def control(self, state: np.ndarray) -> np.ndarray:
    r"""Compute the control action.

    Args:
        state (np.ndarray): The current state of the system.

    Returns:
        np.ndarray: The control action.

    """
    raise NotImplementedError

set_goals

set_goals(goals)

Set the goals of the controller.

Parameters:

Name Type Description Default
goals ndarray

The goals of the controller.

required
Source code in pypolo/controllers/base_controller.py
def set_goals(self, goals: np.ndarray) -> None:
    r"""Set the goals of the controller.

    Args:
        goals (np.ndarray): The goals of the controller.

    """
    self.goals = goals

PIDController

PIDController(error_fn, dt, kp, ki, kd, error_threshold)

Bases: BaseController

A Proportional–Integral–Derivative controller.

Initialize the PID controller.

Parameters:

Name Type Description Default
error_fn Callable

The robot's error function. It should take the goal [x, y] as arguments and return the error of shape (num_actions, ).

required
dt float

The time step.

required
kp float

The proportional gain.

required
ki float

The integral gain.

required
kd float

The derivative gain.

required
error_threshold float

If the error is less than this threshold, the goal is considered reached.

required
Source code in pypolo/controllers/pid_controller.py
def __init__(
    self,
    error_fn: Callable,
    dt: float,
    kp: float,
    ki: float,
    kd: float,
    error_threshold: float,
):
    r"""Initialize the PID controller.

    Args:
        error_fn (Callable): The robot's error function. It should take
            the goal [x, y] as arguments and return the error of
            shape (num_actions, ).
        dt (float): The time step.
        kp (float): The proportional gain.
        ki (float): The integral gain.
        kd (float): The derivative gain.
        error_threshold (float): If the error is less than this threshold,
            the goal is considered reached.

    """
    super().__init__()
    self.error_fn = error_fn
    self.dt = dt
    self.kp = kp
    self.ki = ki
    self.kd = kd
    self.error_threshold = error_threshold
    self.integral = 0.0
    self.previous_error = 0.0

control

control()

Compute the control action using the PID algorithm.

Returns:

Name Type Description
float ndarray

The control action.

Source code in pypolo/controllers/pid_controller.py
def control(self) -> np.ndarray:
    r"""Compute the control action using the PID algorithm.

    Returns:
        float: The control action.

    """
    goal = self.goals[0]
    error = self.error_fn(x=goal[0], y=goal[1])
    self.integral += error * self.dt
    derivative = (error - self.previous_error) / self.dt
    self.previous_error = error
    action = (self.kp * error + self.ki * self.integral +
              self.kd * derivative)
    if np.linalg.norm(error) < self.error_threshold:
        self.goals = self.goals[1:]
    return action