Skip to content

Base Env

cbfpy.envs.base_env

Base Environment

This is a convenient structure for building demo environments to test CBFs. However, it is not necessary to use this.

For instance, going back to the CBF usage pseudocode,

while True:
    z = get_state()
    z_des = get_desired_state()
    u_nom = nominal_controller(z, z_des)
    u = cbf.safety_filter(z, u_nom)
    apply_control(u)
    step() 
We use this base environment to set up the get_state, get_desired_state, apply_control, and step methods in any derived environments.

BaseEnv

Bases: ABC

Simulation environment Abstract Base Class for testing CBFs

Any environment inheriting from this class should implement the following methods:

  • step: Run a single simulation step
  • get_state: Get the current state of the robot
  • get_desired_state: Get the desired state of the robot
  • apply_control: Apply a control input to the robot
Source code in cbfpy/envs/base_env.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class BaseEnv(ABC):
    """Simulation environment Abstract Base Class for testing CBFs

    Any environment inheriting from this class should implement the following methods:

    - `step`: Run a single simulation step
    - `get_state`: Get the current state of the robot
    - `get_desired_state`: Get the desired state of the robot
    - `apply_control`: Apply a control input to the robot
    """

    @abstractmethod
    def step(self) -> None:
        """Runs a single simulation step for the environment

        This should update any dynamics and visuals
        """
        pass

    @abstractmethod
    def get_state(self) -> ArrayLike:
        """Returns the current state of the environment

        Returns:
            ArrayLike: State, shape (n,)
        """
        pass

    @abstractmethod
    def get_desired_state(self) -> ArrayLike:
        """Returns the desired state of the environment

        Returns:
            ArrayLike: Desired state, shape (n,)
        """
        pass

    @abstractmethod
    def apply_control(self, u: ArrayLike) -> None:
        """Applies the control input to the environment

        Args:
            u (ArrayLike): Control, shape (m,)
        """
        pass

step() abstractmethod

Runs a single simulation step for the environment

This should update any dynamics and visuals

Source code in cbfpy/envs/base_env.py
36
37
38
39
40
41
42
@abstractmethod
def step(self) -> None:
    """Runs a single simulation step for the environment

    This should update any dynamics and visuals
    """
    pass

get_state() abstractmethod

Returns the current state of the environment

Returns:

Name Type Description
ArrayLike ArrayLike

State, shape (n,)

Source code in cbfpy/envs/base_env.py
44
45
46
47
48
49
50
51
@abstractmethod
def get_state(self) -> ArrayLike:
    """Returns the current state of the environment

    Returns:
        ArrayLike: State, shape (n,)
    """
    pass

get_desired_state() abstractmethod

Returns the desired state of the environment

Returns:

Name Type Description
ArrayLike ArrayLike

Desired state, shape (n,)

Source code in cbfpy/envs/base_env.py
53
54
55
56
57
58
59
60
@abstractmethod
def get_desired_state(self) -> ArrayLike:
    """Returns the desired state of the environment

    Returns:
        ArrayLike: Desired state, shape (n,)
    """
    pass

apply_control(u) abstractmethod

Applies the control input to the environment

Parameters:

Name Type Description Default
u ArrayLike

Control, shape (m,)

required
Source code in cbfpy/envs/base_env.py
62
63
64
65
66
67
68
69
@abstractmethod
def apply_control(self, u: ArrayLike) -> None:
    """Applies the control input to the environment

    Args:
        u (ArrayLike): Control, shape (m,)
    """
    pass