Skip to content

Commit e23df30

Browse files
author
Bourmindge Liey
committed
prepare to change to attitude interface.
1 parent e5664c4 commit e23df30

File tree

7 files changed

+57
-38
lines changed

7 files changed

+57
-38
lines changed

.devcontainer/devcontainer.json

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
"ms-toolsai.jupyter-keymap"
2020
],
2121
"settings": {
22+
"terminal.integrated.defaultProfile.linux": "bash",
23+
"terminal.integrated.profiles.linux": {
24+
"bash": {
25+
"path": "/bin/bash"
26+
}
27+
},
2228
"python.defaultInterpreterPath": "/home/vscode/venv/bin/python",
2329
"python.testing.pytestEnabled": true, // test framework
2430
"python.testing.pytestArgs": [

tutorials/LQR_ILQR.ipynb

+2-2
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@
916916
],
917917
"metadata": {
918918
"kernelspec": {
919-
"display_name": "control_course_venv",
919+
"display_name": "venv",
920920
"language": "python",
921921
"name": "python3"
922922
},
@@ -930,7 +930,7 @@
930930
"name": "python",
931931
"nbconvert_exporter": "python",
932932
"pygments_lexer": "ipython3",
933-
"version": "3.11.10"
933+
"version": "3.11.0"
934934
}
935935
},
936936
"nbformat": 4,

tutorials/MPC.ipynb

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
"%reload_ext autoreload\n",
1010
"%autoreload 2\n",
1111
"\n",
12-
"\n",
13-
"\n",
1412
"from pathlib import Path\n",
15-
"import os, sys \n",
13+
"import os, sys\n",
1614
"parent_dir = str(Path().resolve().parents[0])\n",
1715
"sys.path.insert(-1, parent_dir)\n",
1816
"\n",
@@ -1051,7 +1049,6 @@
10511049
"y_ref = np.concatenate([goal, goal_u])\n",
10521050
"y_ref_e = goal\n",
10531051
"\n",
1054-
"counter = 3\n",
10551052
"for i in range(2500):\n",
10561053
" counter += 1\n",
10571054
" if counter == 4:\n",

tutorials/MPC/mpc.py

+22-14
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
1+
"""This module contains the implementation of a Model Predictive Controller (MPC)."""
2+
13
import numpy as np
4+
25
from crazyflow.sim.symbolic import SymbolicModel
36
from tutorials.MPC.BaseClass import BaseController
47
from tutorials.MPC.ocp_setup import create_ocp
58

9+
610
class ModelPredictiveController(BaseController):
11+
"""Model Predictive Controller (MPC) for controlling dynamic systems."""
712

8-
def __init__(self, symbolic_model: SymbolicModel, options: dict):
13+
def __init__(self, symbolic_model: SymbolicModel, options: dict):
14+
"""Initialize the Model Predictive Controller.
915
16+
Args:
17+
symbolic_model (SymbolicModel): The symbolic model of the system.
18+
options (dict): Configuration options for the MPC.
19+
"""
1020
self.options = options
1121

1222
## parameters ##
13-
self.N = options['solver']['n_pred']
14-
self.u_pred = np.ones((options['solver']['n_pred'], symbolic_model.u_sym.size()[0])) * 0.0662175
15-
self.x_pred = np.zeros((options['solver']['n_pred']+1, symbolic_model.x_sym.size()[0]))
23+
self.N = options["solver"]["n_pred"]
24+
self.u_pred = (
25+
np.ones((options["solver"]["n_pred"], symbolic_model.u_sym.size()[0])) * 0.0662175
26+
)
27+
self.x_pred = np.zeros((options["solver"]["n_pred"] + 1, symbolic_model.x_sym.size()[0]))
1628

17-
## create acados ocp solver
29+
## create acados ocp solver
1830
self.ocp, self.ocp_solver = create_ocp(symbolic_model, options)
19-
20-
2131

2232
def control(self, x: np.ndarray, y_ref: np.ndarray, y_ref_e: np.ndarray) -> np.ndarray:
23-
'''
24-
Compute the control input for the cartpole system.
25-
'''
33+
"""Compute the control input for the cartpole system."""
2634
## set initial state
2735
self.ocp_solver.set(0, "lbx", x)
2836
self.ocp_solver.set(0, "ubx", x)
@@ -36,22 +44,22 @@ def control(self, x: np.ndarray, y_ref: np.ndarray, y_ref_e: np.ndarray) -> np.n
3644
for i in range(self.N):
3745
self.ocp_solver.set(i, "u", self.u_pred[i, :])
3846

39-
## solve ocp
47+
## solve ocp
4048
status = self.ocp_solver.solve()
4149
if status != 0:
4250
print(f"Solver failed with status {status}")
4351
# Return safe control input (e.g., zero or previous solution)
4452
return np.zeros_like(self.ocp_solver.get(0, "u"))
4553

46-
## get control input
54+
## get control input
4755
u = self.ocp_solver.get(0, "u")
4856

4957
## extract predicted control inputs
5058
for i in range(self.N):
5159
self.u_pred[i, :] = self.ocp_solver.get(i, "u")
5260

5361
## extract predicted states
54-
for i in range(self.N+1):
62+
for i in range(self.N + 1):
5563
self.x_pred[i, :] = self.ocp_solver.get(i, "x")
5664

57-
return u
65+
return u

tutorials/MPC/ocp_setup.py

+22-18
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,42 @@
11
from acados_template import AcadosOcp, AcadosOcpSolver
22

33
from crazyflow.sim.symbolic import SymbolicModel
4-
from tutorials.MPC.utils import create_prediction_model, create_ocp_constraints, create_ocp_costs, create_ocp_solver
4+
from tutorials.MPC.utils import (
5+
create_ocp_constraints,
6+
create_ocp_costs,
7+
create_ocp_solver,
8+
create_prediction_model,
9+
)
510

611

712
def create_ocp(symbolic_model: SymbolicModel, options: dict):
8-
'''
9-
The Acados OCP is composed of the following key components. Please pay attention to the options that are used to configure the OCP.
10-
These are also frequently used options for other OCP problems.
11-
'''
13+
"""The Acados OCP is composed of the following key components. Please pay attention to the options that are used to configure the OCP.
14+
These are also frequently used options for other OCP problems.
15+
"""
1216
ocp = AcadosOcp()
1317
############### 1. acados ocp model #############
14-
'''
18+
"""
1519
The first key component of an Acados OCP is the model.
16-
'''
20+
"""
1721
ocp.model = create_prediction_model(symbolic_model)
1822

1923
############### 2. acados ocp constraints #############
20-
'''
24+
"""
2125
The second key component of an Acados OCP are the constraints.
22-
'''
23-
ocp = create_ocp_constraints(ocp, options['constraint'])
26+
"""
27+
ocp = create_ocp_constraints(ocp, options["constraint"])
2428

2529
############### 3. acados ocp costs #############
26-
'''
30+
"""
2731
Then cost is added to the ocp.
28-
'''
29-
ocp = create_ocp_costs(ocp, options['cost'])
32+
"""
33+
ocp = create_ocp_costs(ocp, options["cost"])
3034

3135
############### 4. acados ocp solver #############
32-
'''
36+
"""
3337
Finally, the ocp solver is created.
34-
'''
35-
ocp = create_ocp_solver(ocp, symbolic_model, options['solver'])
36-
ocp_solver = AcadosOcpSolver(ocp, json_file = 'acados_ocp_' + ocp.model.name + '.json')
38+
"""
39+
ocp = create_ocp_solver(ocp, symbolic_model, options["solver"])
40+
ocp_solver = AcadosOcpSolver(ocp, json_file="acados_ocp_" + ocp.model.name + ".json")
3741

38-
return ocp, ocp_solver
42+
return ocp, ocp_solver

tutorials/MPC/plot_utils.py

Whitespace-only changes.

tutorials/MPC/utils.py

+4
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ def create_ocp_solver(ocp: AcadosOcp, symbolic_model: SymbolicModel, options: di
195195
ocp.solver_options.nlp_solver_tol_comp = 1e-4
196196
ocp.solver_options.nlp_solver_step_length = 1.0
197197

198+
ocp.solver_options.
199+
198200
return ocp
199201

200202

@@ -233,3 +235,5 @@ def solve_eqn(symbolic_model: SymbolicModel, goal: np.ndarray):
233235
u_solution = sol["x"].full().flatten()
234236

235237
return u_solution
238+
239+

0 commit comments

Comments
 (0)