Skip to content

Commit a251cdc

Browse files
committed
precommit fix. WIP: odl implementation
1 parent 33602f4 commit a251cdc

5 files changed

Lines changed: 217 additions & 91 deletions

File tree

docking_control/src/mission_control.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import sys
2020

2121
sys.path.insert(0, "/home/ros/ws_dock/src/underwater_docking/docking_control/src")
22-
from auto_dock import MPControl # noqa: E402
2322
from odl_controller import ODL # noqa: E402
2423

2524

@@ -319,7 +318,7 @@ def rov_pose_cb(self, pose):
319318
y = pose.pose.orientation.y
320319
z = pose.pose.orientation.z
321320
w = pose.pose.orientation.w
322-
euler = R.from_quat([x, y, z, w]).as_euler("xyz")
321+
R.from_quat([x, y, z, w]).as_euler("xyz")
323322
# roll = euler[0]
324323
# pitch = euler[1]
325324
# yaw = euler[2]
@@ -546,7 +545,9 @@ def auto_control(self, joy):
546545
else:
547546
x0 = self.rov_odom
548547

549-
xr = np.array([[-1.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]).T
548+
xr = np.array(
549+
[[-1.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
550+
).T
550551

551552
try:
552553
if self.mpc.gp_enabled and self.mpc.sogp_models:
@@ -558,7 +559,9 @@ def auto_control(self, joy):
558559
self.timestamp,
559560
)
560561

561-
forces, self.wrench, converge_flag = self.mpc.run_mpc(x0, xr, self.gp_residual_pred)
562+
forces, self.wrench, converge_flag = self.mpc.run_mpc(
563+
x0, xr, self.gp_residual_pred
564+
)
562565
if converge_flag:
563566
msg = """
564567
[BlueROV2][auto_control] ROV reached dock successfully!

docking_control/src/mpc_sogp_cbf.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
import numpy as np
22
import yaml
33
from acados_template import AcadosOcp, AcadosOcpSolver, AcadosModel
4-
from casadi import evalf, SX, mtimes, pinv, vertcat, hessian, Function, jacobian, dot, fmin, fmax, sumsqr, cos, sin, pi
4+
from casadi import (
5+
evalf,
6+
SX,
7+
mtimes,
8+
pinv,
9+
vertcat,
10+
Function,
11+
jacobian,
12+
dot,
13+
fmin,
14+
fmax,
15+
sumsqr,
16+
cos,
17+
sin,
18+
pi,
19+
)
520
from scipy.linalg import block_diag
621
import sys
722

@@ -178,13 +193,17 @@ def frustum_barrier(
178193
179194
Args:
180195
x (ca.MX): The AUV's current state vector (12x1).
181-
xr (ca.MX): The AUV's reference state vector (12x1), including target position and orientation.
196+
xr (ca.MX): The AUV's reference state vector (12x1), including
197+
target position and orientation.
198+
182199
p_start (ca.MX): The start point of the frustum's axis (3x1).
183200
start_radius (float): The radius of the corridor at the start point.
184201
end_radius (float): The radius of the corridor at the end point.
185-
orientation_weight (float): Tuning parameter to scale the orientation constraint's importance.
186-
max_align_angle_rad (float): The maximum allowed angle (in radians) between the AUV's
187-
forward vector and the target's forward vector.
202+
orientation_weight (float): Tuning parameter to scale the orientation
203+
constraint's importance.
204+
205+
max_align_angle_rad (float): The maximum allowed angle (in radians)
206+
between the AUV's forward vector and the target's forward vector.
188207
189208
Returns:
190209
ca.MX: The value of the combined barrier function.
@@ -203,7 +222,7 @@ def frustum_barrier(
203222

204223
# --- 2. Orientation Barrier (Aligns AUV frame with Target frame) ---
205224
# AUV's current orientation
206-
phi_auv, theta_auv, psi_auv = x[3], x[4], x[5]
225+
_, theta_auv, psi_auv = x[3], x[4], x[5]
207226

208227
# AUV's forward vector (body x-axis) in the world frame
209228
v_auv_x = cos(psi_auv) * cos(theta_auv)
@@ -212,7 +231,7 @@ def frustum_barrier(
212231
v_auv = vertcat(v_auv_x, v_auv_y, v_auv_z)
213232

214233
# Target's orientation from the reference state
215-
phi_target, theta_target, psi_target = xr[3], xr[4], xr[5]
234+
_, theta_target, psi_target = xr[3], xr[4], xr[5]
216235

217236
# Target's forward vector (body x-axis) in the world frame
218237
v_target_orient_x = cos(psi_target) * cos(theta_target)
@@ -228,8 +247,9 @@ def frustum_barrier(
228247
# The cosine of the maximum allowed alignment angle
229248
cos_max_angle = cos(max_align_angle_rad)
230249

231-
# The orientation barrier is positive when the frames are aligned within the allowed cone
232-
b_orientation = cos_angle_error - cos_max_angle
250+
# The orientation barrier is positive when the frames are aligned within the
251+
# allowed cone
252+
cos_angle_error - cos_max_angle
233253

234254
# --- 3. Combined Barrier ---
235255
b_combined = b_position # + orientation_weight * b_orientation
@@ -259,7 +279,8 @@ def tube_barrier(x, p_start, p_end, tube_radius):
259279

260280
# Calculate the projection parameter 't'.
261281
# This determines the closest point on the infinite line.
262-
# Add a small epsilon to prevent division by zero if start and end points are the same.
282+
# Add a small epsilon to prevent division by zero if start and end
283+
# points are the same.
263284
t = dot(w, v) / (dot(v, v) + 1e-9)
264285

265286
# Clamp 't' to the range [0, 1] to stay on the line *segment*.
@@ -363,8 +384,8 @@ def create_ocp_solver_description(self):
363384
self.acados_ocp.dims.N = self.horizon
364385
self.acados_ocp.parameter_values = np.zeros(2 * self.vehicle_state_dim)
365386

366-
xr = self.acados_model.p[0:self.vehicle_state_dim]
367-
residual = self.acados_model.p[self.vehicle_state_dim:]
387+
xr = self.acados_model.p[0 : self.vehicle_state_dim]
388+
residual = self.acados_model.p[self.vehicle_state_dim :]
368389

369390
self.acados_ocp.cost.cost_type = "NONLINEAR_LS"
370391
self.acados_ocp.cost.cost_type_e = "NONLINEAR_LS"
@@ -433,7 +454,7 @@ def optimize(self, x, x_ref, residual):
433454
x0 = x[0:12, :]
434455
N = self.horizon
435456

436-
nx = self.acados_ocp.model.x.shape[0]
457+
self.acados_ocp.model.x.shape[0]
437458
nu = self.acados_ocp.model.u.shape[0]
438459

439460
# set initial state constraint

docking_control/src/odl_controller.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
import time
33
import numpy as np
44
import sys
5-
from casadi import evalf
65
import yaml
7-
from typing import Any, Dict, List, Tuple, Optional
6+
from typing import Dict, List, Tuple, Optional
87
import logging
98

109
sys.path.insert(0, "/home/ros/ws_dock/src/underwater_docking/docking_control/src")
1110

1211
from auv_hinsdale import AUV # noqa: E402
13-
from sogp import SOGP
12+
from sogp import SOGP # noqa: E402
1413
from mpc_sogp_cbf import MPC # noqa: E402
1514

1615

@@ -100,7 +99,8 @@ def _initialize_sogp_models(self) -> None:
10099

101100
if not isinstance(loaded_params, dict):
102101
logging.warning(
103-
f"YAML 'hyperparameters' format is invalid (type: {type(loaded_params)}). "
102+
"YAML 'hyperparameters' format is invalid "
103+
f"(type: {type(loaded_params)}). "
104104
"Using all default values."
105105
)
106106
loaded_params = {}
@@ -111,7 +111,8 @@ def _initialize_sogp_models(self) -> None:
111111
)
112112
else:
113113
logging.info(
114-
"Loading SOGP hyperparameters from YAML, using defaults for any missing keys."
114+
"Loading SOGP hyperparameters from YAML, using "
115+
"defaults for any missing keys."
115116
)
116117

117118
length_scale = loaded_params.get(
@@ -221,7 +222,6 @@ def load_yaml_params(self, filename: str):
221222
params = yaml.load(f.read(), Loader=yaml.SafeLoader)
222223
return params
223224

224-
225225
def wrap_pi2negpi(self, angle):
226226
"""This function wraps the angle to the range -pi to pi.
227227
@@ -259,14 +259,12 @@ def run_mpc(self, x0, xr, residual):
259259
"""
260260

261261
if self.gp_enabled and np.any(np.abs(residual) > 1e2):
262-
logging.warning(
263-
f"Large GP residual {residual.T}, resetting to zero."
264-
)
262+
logging.warning(f"Large GP residual {residual.T}, resetting to zero.")
265263
residual = np.zeros_like(residual)
266264

267265
current_gp_res_for_mpc = (
268-
residual if self.gp_enabled else np.zeros_like(residual)
269-
)
266+
residual if self.gp_enabled else np.zeros_like(residual)
267+
)
270268

271269
process_t0 = time.perf_counter()
272270
self.distance = np.linalg.norm(x0[0:3, :] - xr[0:3, :])

0 commit comments

Comments
 (0)