cbfpy 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cbfpy-0.0.1/LICENSE +21 -0
- cbfpy-0.0.1/PKG-INFO +226 -0
- cbfpy-0.0.1/README.md +191 -0
- cbfpy-0.0.1/cbfpy/__init__.py +11 -0
- cbfpy-0.0.1/cbfpy/cbfs/__init__.py +0 -0
- cbfpy-0.0.1/cbfpy/cbfs/cbf.py +384 -0
- cbfpy-0.0.1/cbfpy/cbfs/clf_cbf.py +490 -0
- cbfpy-0.0.1/cbfpy/config/__init__.py +0 -0
- cbfpy-0.0.1/cbfpy/config/cbf_config.py +401 -0
- cbfpy-0.0.1/cbfpy/config/clf_cbf_config.py +251 -0
- cbfpy-0.0.1/cbfpy/envs/__init__.py +0 -0
- cbfpy-0.0.1/cbfpy/envs/arm_envs.py +84 -0
- cbfpy-0.0.1/cbfpy/envs/base_env.py +69 -0
- cbfpy-0.0.1/cbfpy/envs/car_env.py +332 -0
- cbfpy-0.0.1/cbfpy/envs/drone_env.py +153 -0
- cbfpy-0.0.1/cbfpy/envs/point_robot_envs.py +209 -0
- cbfpy-0.0.1/cbfpy/examples/__init__.py +0 -0
- cbfpy-0.0.1/cbfpy/examples/adaptive_cruise_control_demo.py +117 -0
- cbfpy-0.0.1/cbfpy/examples/drone_demo.py +109 -0
- cbfpy-0.0.1/cbfpy/examples/joint_limits_demo.py +150 -0
- cbfpy-0.0.1/cbfpy/examples/point_robot_demo.py +91 -0
- cbfpy-0.0.1/cbfpy/examples/point_robot_obstacle_demo.py +118 -0
- cbfpy-0.0.1/cbfpy/temp/test_import.py +3 -0
- cbfpy-0.0.1/cbfpy/utils/__init__.py +0 -0
- cbfpy-0.0.1/cbfpy/utils/general_utils.py +131 -0
- cbfpy-0.0.1/cbfpy/utils/jax_utils.py +26 -0
- cbfpy-0.0.1/cbfpy/utils/math_utils.py +21 -0
- cbfpy-0.0.1/cbfpy/utils/visualization.py +93 -0
- cbfpy-0.0.1/cbfpy.egg-info/PKG-INFO +226 -0
- cbfpy-0.0.1/cbfpy.egg-info/SOURCES.txt +36 -0
- cbfpy-0.0.1/cbfpy.egg-info/dependency_links.txt +1 -0
- cbfpy-0.0.1/cbfpy.egg-info/requires.txt +24 -0
- cbfpy-0.0.1/cbfpy.egg-info/top_level.txt +5 -0
- cbfpy-0.0.1/pyproject.toml +48 -0
- cbfpy-0.0.1/setup.cfg +4 -0
- cbfpy-0.0.1/test/__init__.py +0 -0
- cbfpy-0.0.1/test/test_speed.py +191 -0
- cbfpy-0.0.1/test/test_utils.py +34 -0
cbfpy-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Daniel Morton
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
cbfpy-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: cbfpy
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Control Barrier Functions in Python
|
|
5
|
+
Author-email: Daniel Morton <danielpmorton@gmail.com>
|
|
6
|
+
Project-URL: Documentation, https://danielpmorton.github.io/cbfpy/
|
|
7
|
+
Project-URL: Repository, https://github.com/danielpmorton/cbfpy/
|
|
8
|
+
Keywords: control,barrier,function,CBF,Jax
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: numpy<2
|
|
15
|
+
Requires-Dist: jax
|
|
16
|
+
Requires-Dist: jaxlib
|
|
17
|
+
Requires-Dist: qpax
|
|
18
|
+
Provides-Extra: examples
|
|
19
|
+
Requires-Dist: pybullet; extra == "examples"
|
|
20
|
+
Requires-Dist: pygame; extra == "examples"
|
|
21
|
+
Requires-Dist: wheel; extra == "examples"
|
|
22
|
+
Requires-Dist: matplotlib; extra == "examples"
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: mkdocs-material; extra == "dev"
|
|
25
|
+
Requires-Dist: mkdocstrings[python]; extra == "dev"
|
|
26
|
+
Requires-Dist: pylint; extra == "dev"
|
|
27
|
+
Requires-Dist: black; extra == "dev"
|
|
28
|
+
Provides-Extra: all
|
|
29
|
+
Requires-Dist: pylint; extra == "all"
|
|
30
|
+
Requires-Dist: black; extra == "all"
|
|
31
|
+
Requires-Dist: pybullet; extra == "all"
|
|
32
|
+
Requires-Dist: pygame; extra == "all"
|
|
33
|
+
Requires-Dist: mkdocs-material; extra == "all"
|
|
34
|
+
Requires-Dist: mkdocstrings[python]; extra == "all"
|
|
35
|
+
|
|
36
|
+
# CBFpy: Control Barrier Functions in Python and Jax
|
|
37
|
+
|
|
38
|
+
CBFpy is an easy-to-use and high-performance framework for constructing and solving Control Barrier Functions (CBFs) and Control Lyapunov Functions (CLFs), using [Jax](https://github.com/google/jax) for:
|
|
39
|
+
|
|
40
|
+
- Just-in-time compilation
|
|
41
|
+
- Accelerated linear algebra operations with [XLA](https://openxla.org/xla)
|
|
42
|
+
- Automatic differentiation
|
|
43
|
+
|
|
44
|
+
For API reference, see the following [documentation](https://danielpmorton.github.io/cbfpy)
|
|
45
|
+
|
|
46
|
+
If you use CBFpy in your research, please use the following citation:
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
@software{Morton_CBFpy_2024,
|
|
50
|
+
author = {Morton, Daniel},
|
|
51
|
+
license = {MIT},
|
|
52
|
+
title = {{CBFpy: Control Barrier Functions in Python and Jax}},
|
|
53
|
+
url = {https://github.com/danielpmorton/cbfpy},
|
|
54
|
+
version = {0.0.1},
|
|
55
|
+
month = Dec,
|
|
56
|
+
year = {2024}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Installation
|
|
61
|
+
|
|
62
|
+
### From PyPI
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
pip install cbfpy
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### From source
|
|
69
|
+
|
|
70
|
+
A virtual environment is optional, but highly recommended. For `pyenv` installation instructions, see [here](https://danielpmorton.github.io/cbfpy/pyenv).
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
git clone https://github.com/danielpmorton/cbfpy
|
|
74
|
+
cd cbfpy
|
|
75
|
+
pip install -e ".[examples]"
|
|
76
|
+
```
|
|
77
|
+
The `[examples]` tag installs all of the required packages for development and running the examples. The pure `cbfpy` functionality does not require these extra packages though. If you want to contribute to the repo, you can also include the `[dev]` dependencies.
|
|
78
|
+
|
|
79
|
+
If you are working on Apple silicon and have issues installing Jax, the following threads may be useful: [[1]](https://stackoverflow.com/questions/68327863/importing-jax-fails-on-mac-with-m1-chip), [[2]](https://github.com/jax-ml/jax/issues/5501#issuecomment-955590288)
|
|
80
|
+
|
|
81
|
+
## Usage:
|
|
82
|
+
|
|
83
|
+
#### Example: A point-mass robot in 1D with an applied force and a positional barrier
|
|
84
|
+
|
|
85
|
+
For this problem, the state $z$ is defined as the position and velocity of the robot,
|
|
86
|
+
|
|
87
|
+
$$z = [x, \dot{x}]$$
|
|
88
|
+
|
|
89
|
+
So, the state derivative $\dot{z}$ is therefore
|
|
90
|
+
|
|
91
|
+
$$\dot{z} = [\dot{x}, \ddot{x}]$$
|
|
92
|
+
|
|
93
|
+
And the control input is the applied force in the $x$ direction:
|
|
94
|
+
|
|
95
|
+
$$u = F_{x}$$
|
|
96
|
+
|
|
97
|
+
The dynamics can be expressed as follows (with $m$ denoting the robot's mass):
|
|
98
|
+
|
|
99
|
+
$$\dot{z} = \begin{bmatrix}0 & 1 \\
|
|
100
|
+
0 & 0
|
|
101
|
+
\end{bmatrix}z +
|
|
102
|
+
\begin{bmatrix}0 \\
|
|
103
|
+
1/m
|
|
104
|
+
\end{bmatrix} u$$
|
|
105
|
+
|
|
106
|
+
This is a control affine system, since the dynamics can be expressed as
|
|
107
|
+
|
|
108
|
+
$$\dot{z} = f(z) + g(z) u$$
|
|
109
|
+
|
|
110
|
+
If the robot is controlled by some nominal (unsafe) controller, we may want to guarantee that it remains in some safe region. If we define $X_{safe} \in [x_{min}, \infty]$, we can construct a (relative-degree-2, zeroing) barrier $h$ where $h(z) \geq 0$ for any $z$ in the safe set:
|
|
111
|
+
|
|
112
|
+
$$h(z) = x - x_{min}$$
|
|
113
|
+
|
|
114
|
+
### In Code
|
|
115
|
+
|
|
116
|
+
We'll first define our problem (dynamics, barrier, and any additional parameters) in a `CBFConfig`-derived class.
|
|
117
|
+
|
|
118
|
+
We use [Jax](https://github.com/google/jax) for fast compilation of the problem. Jax can be tricky to learn at first, but luckily `cbfpy` just requires formulating your functions in `jax.numpy` which has the same familiar interface as `numpy`. These should be pure functions without side effects (for instance, modifying a class variable in `self`).
|
|
119
|
+
|
|
120
|
+
Additional tuning parameters/functions can be found in the `CBFConfig` documentation.
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
import jax.numpy as jnp
|
|
124
|
+
from cbfpy import CBF, CBFConfig
|
|
125
|
+
|
|
126
|
+
# Create a config class for your problem inheriting from the CBFConfig class
|
|
127
|
+
class MyCBFConfig(CBFConfig):
|
|
128
|
+
def __init__(self):
|
|
129
|
+
super().__init__(
|
|
130
|
+
# Define the state and control dimensions
|
|
131
|
+
n = 2, # [x, x_dot]
|
|
132
|
+
m = 1, # [F_x]
|
|
133
|
+
# Define control limits (if desired)
|
|
134
|
+
u_min = None,
|
|
135
|
+
u_max = None,
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
# Define the control-affine dynamics functions `f` and `g` for your system
|
|
139
|
+
def f(self, z):
|
|
140
|
+
A = jnp.array([[0.0, 1.0], [0.0, 0.0]])
|
|
141
|
+
return A @ z
|
|
142
|
+
|
|
143
|
+
def g(self, z):
|
|
144
|
+
mass = 1.0
|
|
145
|
+
B = jnp.array([[0.0], [1.0 / mass]])
|
|
146
|
+
return B
|
|
147
|
+
|
|
148
|
+
# Define the barrier function `h`
|
|
149
|
+
# The *relative degree* of this system is 2, so, we'll use the h_2 method
|
|
150
|
+
def h_2(self, z):
|
|
151
|
+
x_min = 1.0
|
|
152
|
+
x = z[0]
|
|
153
|
+
return jnp.array([x - x_min])
|
|
154
|
+
```
|
|
155
|
+
We can then construct the CBF from our config and use it in our control loop as follows.
|
|
156
|
+
```python
|
|
157
|
+
config = MyCBFConfig()
|
|
158
|
+
cbf = CBF.from_config(config)
|
|
159
|
+
|
|
160
|
+
# Pseudocode
|
|
161
|
+
while True:
|
|
162
|
+
z = get_state()
|
|
163
|
+
z_des = get_desired_state()
|
|
164
|
+
u_nom = nominal_controller(z, z_des)
|
|
165
|
+
u = cbf.safety_filter(z, u_nom)
|
|
166
|
+
apply_control(u)
|
|
167
|
+
step()
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Examples
|
|
171
|
+
|
|
172
|
+
These can be found in the `examples` folder [here](https://github.com/danielpmorton/cbfpy/tree/main/cbfpy/examples)
|
|
173
|
+
|
|
174
|
+
### [Adaptive Cruise Control](https://github.com/danielpmorton/cbfpy/blob/main/cbfpy/examples/adaptive_cruise_control_demo.py)
|
|
175
|
+
|
|
176
|
+
Use a CLF-CBF to maintain a safe follow distance to the vehicle in front, while tracking a desired velocity
|
|
177
|
+
|
|
178
|
+
- State: z = [Follower velocity, Leader velocity, Follow distance] (n = 3)
|
|
179
|
+
- Control: u = [Follower wheel force] (m = 1)
|
|
180
|
+
- Relative degree: 1
|
|
181
|
+
|
|
182
|
+

|
|
183
|
+
|
|
184
|
+
### [Point Robot Safe-Set Containment](https://github.com/danielpmorton/cbfpy/blob/main/cbfpy/examples/point_robot_demo.py)
|
|
185
|
+
|
|
186
|
+
Use a CBF to enforce that a point robot stays within a safe box, while a PD controller attempts to reduce the distance to a target position
|
|
187
|
+
|
|
188
|
+
- State: z = [Position, Velocity] (n = 6)
|
|
189
|
+
- Control: u = [Force] (m = 3)
|
|
190
|
+
- Relative degree: 2
|
|
191
|
+
|
|
192
|
+

|
|
193
|
+
|
|
194
|
+
### [Point Robot Obstacle Avoidance](https://github.com/danielpmorton/cbfpy/blob/main/cbfpy/examples/point_robot_obstacle_demo.py)
|
|
195
|
+
|
|
196
|
+
Use a CBF to keep a point robot inside a safe box, while avoiding a moving obstacle. The nominal PD controller attempts to keep the robot at the origin.
|
|
197
|
+
|
|
198
|
+
- State: z = [Position, Velocity] (n = 6)
|
|
199
|
+
- Control: u = [Force] (m = 3)
|
|
200
|
+
- Relative degree: 1 + 2 (1 for obstacle avoidance, 2 for safe set containment)
|
|
201
|
+
- Additional data: The state of the obstacle (position and velocity)
|
|
202
|
+
|
|
203
|
+

|
|
204
|
+
|
|
205
|
+
### [Manipulator Joint Limit Avoidance](https://github.com/danielpmorton/cbfpy/blob/main/cbfpy/examples/joint_limits_demo.py)
|
|
206
|
+
|
|
207
|
+
Use a CBF to keep a manipulator operating within its joint limits, even if a nominal joint trajectory is unsafe.
|
|
208
|
+
|
|
209
|
+
- State: z = [Joint angles] (n = 3)
|
|
210
|
+
- Control: u = [Joint velocities] (m = 3)
|
|
211
|
+
- Relative degree: 1
|
|
212
|
+
|
|
213
|
+

|
|
214
|
+
|
|
215
|
+
### [Drone Obstacle Avoidance](https://github.com/danielpmorton/cbfpy/blob/main/cbfpy/examples/drone_demo.py)
|
|
216
|
+
|
|
217
|
+
Use a CBF to keep a drone inside a safe box, while avoiding a moving obstacle. This is similar to the "point robot obstacle avoidance" demo, but with slightly different dynamics.
|
|
218
|
+
|
|
219
|
+
- State: z = [Position, Velocity] (n = 6)
|
|
220
|
+
- Control: u = [Velocity] (m = 3)
|
|
221
|
+
- Relative degree: 1
|
|
222
|
+
- Additional data: The state of the obstacle (position and velocity)
|
|
223
|
+
|
|
224
|
+
This is the same CBF which was used in the ["Drone Fencing" demo](https://danielpmorton.github.io/drone_fencing/) at the Stanford Robotics center.
|
|
225
|
+
|
|
226
|
+

|
cbfpy-0.0.1/README.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# CBFpy: Control Barrier Functions in Python and Jax
|
|
2
|
+
|
|
3
|
+
CBFpy is an easy-to-use and high-performance framework for constructing and solving Control Barrier Functions (CBFs) and Control Lyapunov Functions (CLFs), using [Jax](https://github.com/google/jax) for:
|
|
4
|
+
|
|
5
|
+
- Just-in-time compilation
|
|
6
|
+
- Accelerated linear algebra operations with [XLA](https://openxla.org/xla)
|
|
7
|
+
- Automatic differentiation
|
|
8
|
+
|
|
9
|
+
For API reference, see the following [documentation](https://danielpmorton.github.io/cbfpy)
|
|
10
|
+
|
|
11
|
+
If you use CBFpy in your research, please use the following citation:
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
@software{Morton_CBFpy_2024,
|
|
15
|
+
author = {Morton, Daniel},
|
|
16
|
+
license = {MIT},
|
|
17
|
+
title = {{CBFpy: Control Barrier Functions in Python and Jax}},
|
|
18
|
+
url = {https://github.com/danielpmorton/cbfpy},
|
|
19
|
+
version = {0.0.1},
|
|
20
|
+
month = Dec,
|
|
21
|
+
year = {2024}
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
### From PyPI
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
pip install cbfpy
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### From source
|
|
34
|
+
|
|
35
|
+
A virtual environment is optional, but highly recommended. For `pyenv` installation instructions, see [here](https://danielpmorton.github.io/cbfpy/pyenv).
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
git clone https://github.com/danielpmorton/cbfpy
|
|
39
|
+
cd cbfpy
|
|
40
|
+
pip install -e ".[examples]"
|
|
41
|
+
```
|
|
42
|
+
The `[examples]` tag installs all of the required packages for development and running the examples. The pure `cbfpy` functionality does not require these extra packages though. If you want to contribute to the repo, you can also include the `[dev]` dependencies.
|
|
43
|
+
|
|
44
|
+
If you are working on Apple silicon and have issues installing Jax, the following threads may be useful: [[1]](https://stackoverflow.com/questions/68327863/importing-jax-fails-on-mac-with-m1-chip), [[2]](https://github.com/jax-ml/jax/issues/5501#issuecomment-955590288)
|
|
45
|
+
|
|
46
|
+
## Usage:
|
|
47
|
+
|
|
48
|
+
#### Example: A point-mass robot in 1D with an applied force and a positional barrier
|
|
49
|
+
|
|
50
|
+
For this problem, the state $z$ is defined as the position and velocity of the robot,
|
|
51
|
+
|
|
52
|
+
$$z = [x, \dot{x}]$$
|
|
53
|
+
|
|
54
|
+
So, the state derivative $\dot{z}$ is therefore
|
|
55
|
+
|
|
56
|
+
$$\dot{z} = [\dot{x}, \ddot{x}]$$
|
|
57
|
+
|
|
58
|
+
And the control input is the applied force in the $x$ direction:
|
|
59
|
+
|
|
60
|
+
$$u = F_{x}$$
|
|
61
|
+
|
|
62
|
+
The dynamics can be expressed as follows (with $m$ denoting the robot's mass):
|
|
63
|
+
|
|
64
|
+
$$\dot{z} = \begin{bmatrix}0 & 1 \\
|
|
65
|
+
0 & 0
|
|
66
|
+
\end{bmatrix}z +
|
|
67
|
+
\begin{bmatrix}0 \\
|
|
68
|
+
1/m
|
|
69
|
+
\end{bmatrix} u$$
|
|
70
|
+
|
|
71
|
+
This is a control affine system, since the dynamics can be expressed as
|
|
72
|
+
|
|
73
|
+
$$\dot{z} = f(z) + g(z) u$$
|
|
74
|
+
|
|
75
|
+
If the robot is controlled by some nominal (unsafe) controller, we may want to guarantee that it remains in some safe region. If we define $X_{safe} \in [x_{min}, \infty]$, we can construct a (relative-degree-2, zeroing) barrier $h$ where $h(z) \geq 0$ for any $z$ in the safe set:
|
|
76
|
+
|
|
77
|
+
$$h(z) = x - x_{min}$$
|
|
78
|
+
|
|
79
|
+
### In Code
|
|
80
|
+
|
|
81
|
+
We'll first define our problem (dynamics, barrier, and any additional parameters) in a `CBFConfig`-derived class.
|
|
82
|
+
|
|
83
|
+
We use [Jax](https://github.com/google/jax) for fast compilation of the problem. Jax can be tricky to learn at first, but luckily `cbfpy` just requires formulating your functions in `jax.numpy` which has the same familiar interface as `numpy`. These should be pure functions without side effects (for instance, modifying a class variable in `self`).
|
|
84
|
+
|
|
85
|
+
Additional tuning parameters/functions can be found in the `CBFConfig` documentation.
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
import jax.numpy as jnp
|
|
89
|
+
from cbfpy import CBF, CBFConfig
|
|
90
|
+
|
|
91
|
+
# Create a config class for your problem inheriting from the CBFConfig class
|
|
92
|
+
class MyCBFConfig(CBFConfig):
|
|
93
|
+
def __init__(self):
|
|
94
|
+
super().__init__(
|
|
95
|
+
# Define the state and control dimensions
|
|
96
|
+
n = 2, # [x, x_dot]
|
|
97
|
+
m = 1, # [F_x]
|
|
98
|
+
# Define control limits (if desired)
|
|
99
|
+
u_min = None,
|
|
100
|
+
u_max = None,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
# Define the control-affine dynamics functions `f` and `g` for your system
|
|
104
|
+
def f(self, z):
|
|
105
|
+
A = jnp.array([[0.0, 1.0], [0.0, 0.0]])
|
|
106
|
+
return A @ z
|
|
107
|
+
|
|
108
|
+
def g(self, z):
|
|
109
|
+
mass = 1.0
|
|
110
|
+
B = jnp.array([[0.0], [1.0 / mass]])
|
|
111
|
+
return B
|
|
112
|
+
|
|
113
|
+
# Define the barrier function `h`
|
|
114
|
+
# The *relative degree* of this system is 2, so, we'll use the h_2 method
|
|
115
|
+
def h_2(self, z):
|
|
116
|
+
x_min = 1.0
|
|
117
|
+
x = z[0]
|
|
118
|
+
return jnp.array([x - x_min])
|
|
119
|
+
```
|
|
120
|
+
We can then construct the CBF from our config and use it in our control loop as follows.
|
|
121
|
+
```python
|
|
122
|
+
config = MyCBFConfig()
|
|
123
|
+
cbf = CBF.from_config(config)
|
|
124
|
+
|
|
125
|
+
# Pseudocode
|
|
126
|
+
while True:
|
|
127
|
+
z = get_state()
|
|
128
|
+
z_des = get_desired_state()
|
|
129
|
+
u_nom = nominal_controller(z, z_des)
|
|
130
|
+
u = cbf.safety_filter(z, u_nom)
|
|
131
|
+
apply_control(u)
|
|
132
|
+
step()
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Examples
|
|
136
|
+
|
|
137
|
+
These can be found in the `examples` folder [here](https://github.com/danielpmorton/cbfpy/tree/main/cbfpy/examples)
|
|
138
|
+
|
|
139
|
+
### [Adaptive Cruise Control](https://github.com/danielpmorton/cbfpy/blob/main/cbfpy/examples/adaptive_cruise_control_demo.py)
|
|
140
|
+
|
|
141
|
+
Use a CLF-CBF to maintain a safe follow distance to the vehicle in front, while tracking a desired velocity
|
|
142
|
+
|
|
143
|
+
- State: z = [Follower velocity, Leader velocity, Follow distance] (n = 3)
|
|
144
|
+
- Control: u = [Follower wheel force] (m = 1)
|
|
145
|
+
- Relative degree: 1
|
|
146
|
+
|
|
147
|
+

|
|
148
|
+
|
|
149
|
+
### [Point Robot Safe-Set Containment](https://github.com/danielpmorton/cbfpy/blob/main/cbfpy/examples/point_robot_demo.py)
|
|
150
|
+
|
|
151
|
+
Use a CBF to enforce that a point robot stays within a safe box, while a PD controller attempts to reduce the distance to a target position
|
|
152
|
+
|
|
153
|
+
- State: z = [Position, Velocity] (n = 6)
|
|
154
|
+
- Control: u = [Force] (m = 3)
|
|
155
|
+
- Relative degree: 2
|
|
156
|
+
|
|
157
|
+

|
|
158
|
+
|
|
159
|
+
### [Point Robot Obstacle Avoidance](https://github.com/danielpmorton/cbfpy/blob/main/cbfpy/examples/point_robot_obstacle_demo.py)
|
|
160
|
+
|
|
161
|
+
Use a CBF to keep a point robot inside a safe box, while avoiding a moving obstacle. The nominal PD controller attempts to keep the robot at the origin.
|
|
162
|
+
|
|
163
|
+
- State: z = [Position, Velocity] (n = 6)
|
|
164
|
+
- Control: u = [Force] (m = 3)
|
|
165
|
+
- Relative degree: 1 + 2 (1 for obstacle avoidance, 2 for safe set containment)
|
|
166
|
+
- Additional data: The state of the obstacle (position and velocity)
|
|
167
|
+
|
|
168
|
+

|
|
169
|
+
|
|
170
|
+
### [Manipulator Joint Limit Avoidance](https://github.com/danielpmorton/cbfpy/blob/main/cbfpy/examples/joint_limits_demo.py)
|
|
171
|
+
|
|
172
|
+
Use a CBF to keep a manipulator operating within its joint limits, even if a nominal joint trajectory is unsafe.
|
|
173
|
+
|
|
174
|
+
- State: z = [Joint angles] (n = 3)
|
|
175
|
+
- Control: u = [Joint velocities] (m = 3)
|
|
176
|
+
- Relative degree: 1
|
|
177
|
+
|
|
178
|
+

|
|
179
|
+
|
|
180
|
+
### [Drone Obstacle Avoidance](https://github.com/danielpmorton/cbfpy/blob/main/cbfpy/examples/drone_demo.py)
|
|
181
|
+
|
|
182
|
+
Use a CBF to keep a drone inside a safe box, while avoiding a moving obstacle. This is similar to the "point robot obstacle avoidance" demo, but with slightly different dynamics.
|
|
183
|
+
|
|
184
|
+
- State: z = [Position, Velocity] (n = 6)
|
|
185
|
+
- Control: u = [Velocity] (m = 3)
|
|
186
|
+
- Relative degree: 1
|
|
187
|
+
- Additional data: The state of the obstacle (position and velocity)
|
|
188
|
+
|
|
189
|
+
This is the same CBF which was used in the ["Drone Fencing" demo](https://danielpmorton.github.io/drone_fencing/) at the Stanford Robotics center.
|
|
190
|
+
|
|
191
|
+

|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""CBFpy: Control Barrier Functions in Python and Jax"""
|
|
2
|
+
|
|
3
|
+
import jax as _jax
|
|
4
|
+
|
|
5
|
+
# 64 bit precision is generally necessary for these problems to be feasible
|
|
6
|
+
_jax.config.update("jax_enable_x64", True)
|
|
7
|
+
|
|
8
|
+
from cbfpy.cbfs.cbf import CBF
|
|
9
|
+
from cbfpy.cbfs.clf_cbf import CLFCBF
|
|
10
|
+
from cbfpy.config.cbf_config import CBFConfig
|
|
11
|
+
from cbfpy.config.clf_cbf_config import CLFCBFConfig
|
|
File without changes
|