Repository navigation
Expand file tree
/
Copy pathfunctionality_controller_l1.py
More file actions
215 lines (170 loc) · 6.86 KB
/
Copy pathfunctionality_controller_l1.py
File metadata and controls
215 lines (170 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
# Written or Adapted by the Imperial College London Team for the FLAIR project, 2023
# Authors for this file:
# Maxime Allard
# Manon Flageat
# Bryan Lim
# Antoine Cully
from functools import partial
from typing import Any, Tuple
from jax.config import config
config.update("jax_enable_x64", True)
import jax
import jax.numpy as jnp
import numpy as np
from gpjax.kernels import RBF
from gpjax.kernels.stationary.utils import squared_distance
import numpy as np
from scipy.linalg import solve_continuous_are
class FunctionalityControllerL1:
"""Main FunctionalityController class, call at each lopp."""
def __init__(
self,
min_command: int,
max_command: int,
) -> None:
"""
Args:
grid_resolution: resolution of the command grid, used by all processes across FC.
min_command: min value for the command grid, used by all processes across FC.
max_command: max value for the command grid, used by all processes across FC.
state_dim: which dimension to use for the state.
state_min_opt_clip: min to clip the state before prior computation.
state_max_opt_clip: max to clip the state before prior computation.
robot_width: parameter of the robot used for prior computation.
"""
# Store the attributes
self.min_command = min_command
self.max_command = max_command
self.delta_t = 0.1
# Robot physical parameters
self.mass = 10.0 # kg
self.inertia = 1.0 # kg*m^2
self.wheel_radius = 0.1 # m
self.wheel_distance = 0.5 # m
# Define the system matrices for velocity control
# Here, A includes a small damping term (e.g., friction)
# self.A = np.array([[-0.1, 0.0],
# [ 0.0, -0.1]])
self.A = np.array([[-0.1, 0],
[0, -0.1]])
self.B = np.array([[1, 0],
[0, 1]])
# L1 adaptive control parameters
self.Gamma = 1.0 # Adaptation gain (higher means faster adaptation)
self.P = np.array([
[1.0, 0],
[0, 1.0]
]) # Lyapunov equation solution
# Low pass filter coefficients
self.omega_c = 1.0 # Filter bandwidth
# We'll compute alpha dynamically: alpha = 1 - exp(-omega_c*delta_t)
##Pre-Commputed Version
self.C = self.omega_c / (self.omega_c + 1)
# Saturation bound for uncertainty estimate for numerical stability
self.max_sigma = 100.0
# State variables
self.state = np.zeros(2) # Current state [v, ω]
self.x_ref = np.zeros(2) # Reference state
self.x_hat = np.zeros(2) # State estimate
self.sigma_hat = np.zeros(2) # Uncertainty estimate
self.u = np.zeros(2) # Control input [linear_cmd, angular_cmd]
self.u_ad = np.zeros(2) # Adaptive control input
# Robot pose [x, y, θ]
self.pose = np.zeros(3)
# Create all adaptation objects
self.reset()
def reset(self):
"""Reset the controller states"""
self.state = np.zeros(2)
self.x_hat = np.zeros(2)
self.sigma_hat = np.zeros(2)
self.u = np.zeros(2)
self.u_ad = np.zeros(2)
self.pose = np.zeros(3)
def update_reference(self, v_ref, omega_ref):
"""Update reference velocities"""
self.x_ref = np.array([v_ref, omega_ref])
def update_state(self,v,omega):
"""Update current state velocities"""
self.state = np.array([v,omega])
def state_predictor(self):
"""
Update the state predictor (state estimator)
"""
# Nominal dynamics
x_dot_nominal = np.dot(self.A, self.x_hat) + np.dot(self.B, self.u)
# Add estimated uncertainties
x_dot_pred = x_dot_nominal + np.dot(self.B, self.sigma_hat)
# Euler integration
# self.x_hat += x_dot_pred * self.delta_t
self.x_hat = x_dot_pred
def adaptation_law(self):
"""
Update the uncertainty estimate using adaptive law
"""
# Error between predicted and actual state
error = self.state - self.x_hat
# Update sigma_hat based on adaptation law
sigma_dot = self.Gamma * np.dot(self.P, error)
# self.sigma_hat += sigma_dot * self.delta_t
self.sigma_hat = sigma_dot
self.sigma_hat = np.clip(self.sigma_hat, -self.max_sigma, self.max_sigma)
def control_law(self):
"""
Compute the L1 adaptive control input
"""
# Compute reference control input
u_ref = np.dot(np.linalg.inv(self.B), self.x_ref - np.dot(self.A, (self.x_hat)))
# u_ref = self.x_ref
# Compute adaptive control to cancel uncertainties
self.u_ad = -self.C * self.sigma_hat
## Adaptive Filtering
# Compute the filter coefficient dynamically
# alpha = 1 - np.exp(-self.omega_c * self.delta_t)
# # Exponential smoothing filter on adaptive control input:
# self.u_ad = (1 - alpha) * self.u_ad - alpha * self.sigma_hat
# Total control input
self.u = u_ref + self.u_ad
## Clipping
self.u = np.clip(self.u, self.min_command, self.max_command)
return self.u
def get_command(
self,
joystick_linear_x_human_command: float,
joystick_angular_z_human_command: float,
flipper_angular_x_human_command: float,
state: np.array,
sensor_x: float,
sensor_y: float,
) -> Tuple[float, float, float, float, float, float, float]:
"""Main function called by the pipeline to get the command to execute.
Args:
joystick_linear_x_human_command
joystick_angular_z_human_command
flipper_angular_x_human_command
use_state: indicate if state-dependent prior only
use_state_gp: indicate if state-dependent prior with GP
Returns:
the new command to forward to the safety controller in form of a msg.
"""
# Scale joystick signal
x = joystick_linear_x_human_command
y = joystick_angular_z_human_command
# Define the L1 control law for velocity tracking
if state is None or sensor_x is None or sensor_y is None:
return (
joystick_linear_x_human_command,
joystick_angular_z_human_command,
flipper_angular_x_human_command,
)
self.state_predictor()
self.update_state(sensor_x,sensor_y)
self.adaptation_law()
self.update_reference(x,y)
self.control_law()
linear_velocity, angular_velocity = self.u
return (
linear_velocity,
angular_velocity,
flipper_angular_x_human_command,
)