This Python program demonstrates the use of control barrier functions (CBFs) for safe robot control in a simple 2D environment. The program simulates a controllable green robot navigating through a scenario with stationary and patrolling robots. CBFs are employed to avoid collisions between the green robot and other robots in the environment. When safety filter is activated, it modified the user command to prevent collision while keeping the modification minimal.
(Left: w/o safety filter; Right: w/ safety filter)
The program was developed and tested in the following environment.
- Python 3.9+
torch==1.8.1+cpu
(optional)osqp==0.6.7
pygame==2.6.1
numpy==1.26.4
scipy==1.13.1
First, install Docker and Docker Compose using the official install guide.
Then, clone this repository and go into the project folder
git clone https://github.com/shaoanlu/CBF_QP_safety_filter.git
cd CBF_QP_safety_filter
To run the application, build the docker image and launch the container
docker compose up --build
To stop the container, execute the following command
docker compose down
Run demo.py
to start the simulation. A simple version w/ code generation is provided as demo_codegen_osqp.py
.
python demo.py # a PyGame window will be spawn
Unit test can be executed through the following command.
python -m unittest
# ----------------------------------------------------------------------
# Ran 11 tests in 0.000s
# OK
Arrow keys
: Control the green robot's movement.X
: Toggle CBF ON/OFF.Z
: Cycle through different CBF alpha values.C
: Change the settings of constraints on filtered control.V
: Enable/Disable safety filter of blue robots.R
: Reset robot positions.
The program consists of the following classes and files:
controllers/robot_cbf.py
: Implements theRobotCBF
class representing a robot in the environment. It includes methods for controlling the robot and detecting collisions.models/robot_dynamics.py
: Implements theSimpleRobotDynamics
class defining the dynamics of the robot. It includes methods for calculating state transition, control barrier functions, and the derivatives of control barrier funcitons.demo.py
: The entry point of the program that handles user input, robot movement, collision detection, and rendering.utils.py
: This file contains a helper function to draw the robots on the screen.
The safety filter based on CBF-QP aims to modify a user's command (desired control input) to satisfy system constraints while keeping the modification minimal. The optimization problem of the safety filter is given below.
where
The dynamic of the robot is as follow.
- 制御バリア関数(CBF)を使えるようになろう
- EECS C106B/206B Robotic Manipulation and Interaction, Spring 2023 (Week 10 - Finishing Grasping, CBFs)
- Ames, Aaron D., et al. "Control barrier functions: Theory and applications." 2019 18th European control conference (ECC). IEEE, 2019. (arxiv)