CBF
Control Barrier Functions (CBFs)
CBFs serve as safety filters on top of a nominal controller. Given a nominal control input, the CBF will compute a safe control input to keep the system within a safe set.
For a relative-degree-1 system, this optimizes the standard min-norm objective with the constraint
h_dot >= -alpha(h(z))
minimize ||u - u_des||_{2}^{2} # CBF Objective (Example)
subject to Lfh(z) + Lgh(z)u >= -alpha(h(z)) # RD1 CBF Constraint
In the case of a relative-degree-2 system, this differs slightly to enforce the RD2 constraint
h_2_dot >= -alpha_2(h_2(z))
minimize ||u - u_des||_{2}^{2} # CBF Objective (Example)
subject to Lfh_2(z) + Lgh_2(z)u >= -alpha_2(h_2(z)) # RD2 CBF Constraint
If there are constraints on the control input, we also enforce another constraint:
u_min <= u <= u_max # Control constraint
CBF
Control Barrier Function (CBF) class.
The main constructor for this class is via the from_config
method, which constructs a CBF instance
based on the provided CBFConfig configuration object.
You can then use the CBF's safety_filter
method to compute the control input that satisfies the CBF
Examples:
# Construct a CBFConfig for your problem
config = DroneConfig()
# Construct a CBF instance based on the config
cbf = CBF.from_config(config)
# Compute the safe control input
safe_control = cbf.safety_filter(current_state, nominal_control)
Source code in cbfpy/cbfs/cbf.py
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
|
from_config(config)
classmethod
Construct a CBF based on the provided configuration
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config |
CBFConfig
|
Config object for the CBF. Contains info on the system dynamics, barrier function, etc. |
required |
Returns:
Name | Type | Description |
---|---|---|
CBF |
CBF
|
Control Barrier Function instance |
Source code in cbfpy/cbfs/cbf.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
|
safety_filter(z, u_des, *h_args)
Apply the CBF safety filter to a nominal control
Parameters:
Name | Type | Description | Default |
---|---|---|---|
z |
Array
|
State, shape (n,) |
required |
u_des |
Array
|
Desired control input, shape (m,) |
required |
*h_args |
Optional additional arguments for the barrier function. |
()
|
Returns:
Name | Type | Description |
---|---|---|
Array |
Array
|
Safe control input, shape (m,) |
Source code in cbfpy/cbfs/cbf.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
h(z, *h_args)
Barrier function(s)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
z |
ArrayLike
|
State, shape (n,) |
required |
*h_args |
Optional additional arguments for the barrier function. |
()
|
Returns:
Name | Type | Description |
---|---|---|
Array |
Array
|
Barrier function evaluation, shape (num_barr,) |
Source code in cbfpy/cbfs/cbf.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
h_and_Lfh(z, *h_args)
Lie derivative of the barrier function(s) wrt the autonomous dynamics f(z)
The evaluation of the barrier function is also returned "for free", a byproduct of the jacobian-vector-product
Parameters:
Name | Type | Description | Default |
---|---|---|---|
z |
ArrayLike
|
State, shape (n,) |
required |
*h_args |
Optional additional arguments for the barrier function. |
()
|
Returns:
Name | Type | Description |
---|---|---|
h |
Array
|
Barrier function evaluation, shape (num_barr,) |
Lfh |
Array
|
Lie derivative of |
Source code in cbfpy/cbfs/cbf.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
|
Lgh(z, *h_args)
Lie derivative of the barrier function(s) wrt the control dynamics g(z)u
Parameters:
Name | Type | Description | Default |
---|---|---|---|
z |
ArrayLike
|
State, shape (n,) |
required |
*h_args |
Optional additional arguments for the barrier function. |
()
|
Returns:
Name | Type | Description |
---|---|---|
Array |
Array
|
Lgh, shape (num_barr, m) |
Source code in cbfpy/cbfs/cbf.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
|
P_qp(z, u_des, *h_args)
Quadratic term in the QP objective (minimize 0.5 * x^T P x + q^T x
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
z |
Array
|
State, shape (n,) |
required |
u_des |
Array
|
Desired control input, shape (m,) |
required |
*h_args |
Optional additional arguments for the barrier function. |
()
|
Returns:
Name | Type | Description |
---|---|---|
Array |
Array
|
P matrix, shape (m, m) |
Source code in cbfpy/cbfs/cbf.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
|
q_qp(z, u_des, *h_args)
Linear term in the QP objective (minimize 0.5 * x^T P x + q^T x
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
z |
Array
|
State, shape (n,) |
required |
u_des |
Array
|
Desired control input, shape (m,) |
required |
*h_args |
Optional additional arguments for the barrier function. |
()
|
Returns:
Name | Type | Description |
---|---|---|
Array |
Array
|
q vector, shape (m,) |
Source code in cbfpy/cbfs/cbf.py
281 282 283 284 285 286 287 288 289 290 291 292 293 |
|
G_qp(z, u_des, *h_args)
Inequality constraint matrix for the QP (Gx <= h
)
Note
The number of constraints depends on if we have control constraints or not.
Without control constraints, num_constraints == num_barriers
.
With control constraints, num_constraints == num_barriers + 2*m
Parameters:
Name | Type | Description | Default |
---|---|---|---|
z |
Array
|
State, shape (n,) |
required |
u_des |
Array
|
Desired control input, shape (m,) |
required |
*h_args |
Optional additional arguments for the barrier function. |
()
|
Returns:
Name | Type | Description |
---|---|---|
Array |
Array
|
G matrix, shape (num_constraints, m) |
Source code in cbfpy/cbfs/cbf.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
|
h_qp(z, u_des, *h_args)
Upper bound on constraints for the QP (Gx <= h
)
Note
The number of constraints depends on if we have control constraints or not.
Without control constraints, num_constraints == num_barriers
.
With control constraints, num_constraints == num_barriers + 2*m
Parameters:
Name | Type | Description | Default |
---|---|---|---|
z |
Array
|
State, shape (n,) |
required |
u_des |
Array
|
Desired control input, shape (m,) |
required |
*h_args |
Optional additional arguments for the barrier function. |
()
|
Returns:
Name | Type | Description |
---|---|---|
Array |
Array
|
h vector, shape (num_constraints,) |
Source code in cbfpy/cbfs/cbf.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
|
qp_data(z, u_des, *h_args)
Constructs the QP matrices based on the current state and desired control
i.e. the matrices/vectors (P, q, A, b, G, h) for the optimization problem:
minimize 0.5 * x^T P x + q^T x
subject to A x == b
G x <= h
Note
- CBFs do not rely on equality constraints, so
A
andb
are empty. - The number of constraints depends on if we have control constraints or not.
Without control constraints,
num_constraints == num_barriers
. With control constraints,num_constraints == num_barriers + 2*m
Parameters:
Name | Type | Description | Default |
---|---|---|---|
z |
Array
|
State, shape (n,) |
required |
u_des |
Array
|
Desired control input, shape (m,) |
required |
*h_args |
Optional additional arguments for the barrier function. |
()
|
Returns:
Name | Type | Description |
---|---|---|
P |
Array
|
Quadratic term in the QP objective, shape (m, m) |
q |
Array
|
Linear term in the QP objective, shape (m,) |
A |
Array
|
Equality constraint matrix, shape (0, m) |
b |
Array
|
Equality constraint vector, shape (0,) |
G |
Array
|
Inequality constraint matrix, shape (num_constraints, m) |
h |
Array
|
Upper bound on constraints, shape (num_constraints,) |
Source code in cbfpy/cbfs/cbf.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
|