-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
176 lines (151 loc) · 6.95 KB
/
model.py
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
import re
import numpy as np
from copy import deepcopy
from urdf_parser_py.urdf import URDF
import adam
from adam.casadi import KinDynComputations
from casadi import MX, vertcat, norm_2, Function, cos, sin
from acados_template import AcadosModel, AcadosOcp, AcadosOcpSolver
import torch
import scipy.linalg as lin
import torch.nn as nn
import l4casadi as l4c
class NeuralNetwork(nn.Module):
def __init__(self, input_size, hidden_size, output_size, activation=nn.ReLU(), ub=None):
super().__init__()
self.linear_stack = nn.Sequential(
nn.Linear(input_size, hidden_size),
activation,
nn.Linear(hidden_size, hidden_size),
activation,
nn.Linear(hidden_size, hidden_size),
activation,
nn.Linear(hidden_size, output_size),
activation,
)
self.ub = ub if ub is not None else 1.0
def forward(self, x):
out = self.linear_stack(x) * self.ub
return out
class OldNeuralNetwork(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super().__init__()
self.linear_relu_stack = nn.Sequential(
nn.Linear(input_size, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, output_size),
nn.ReLU(),
)
def forward(self, x):
out = self.linear_relu_stack(x)
return out
class AdamModel:
def __init__(self, params, n_dofs=False):
self.params = params
self.amodel = AcadosModel()
# Robot dynamics with Adam (IIT)
robot = URDF.from_xml_file(params.robot_urdf)
try:
n_dofs = n_dofs if n_dofs else len(robot.joints)
if n_dofs > len(robot.joints) or n_dofs < 1:
raise ValueError
except ValueError:
print(f'\nInvalid number of degrees of freedom! Must be > 1 and <= {len(robot.joints)}\n')
exit()
robot_joints = robot.joints[1:n_dofs+1] if params.urdf_name == 'z1' else robot.joints[:n_dofs]
joint_names = [joint.name for joint in robot_joints]
kin_dyn = KinDynComputations(params.robot_urdf, joint_names, robot.get_root())
kin_dyn.set_frame_velocity_representation(adam.Representations.BODY_FIXED_REPRESENTATION)
self.mass = kin_dyn.mass_matrix_fun() # Mass matrix
self.bias = kin_dyn.bias_force_fun() # Nonlinear effects
self.gravity = kin_dyn.gravity_term_fun() # Gravity vector
self.fk = kin_dyn.forward_kinematics_fun(params.frame_name) # Forward kinematics
nq = len(joint_names)
self.amodel.name = params.urdf_name
self.x = MX.sym("x", nq * 2)
self.x_dot = MX.sym("x_dot", nq * 2)
self.u = MX.sym("u", nq)
self.p = MX.sym("p", 1) # Safety margin for the NN model
# Double integrator
self.f_disc = vertcat(
self.x[:nq] + params.dt * self.x[nq:] + 0.5 * params.dt**2 * self.u,
self.x[nq:] + params.dt * self.u
)
self.f_fun = Function('f', [self.x, self.u], [self.f_disc])
self.amodel.x = self.x
self.amodel.u = self.u
self.amodel.disc_dyn_expr = self.f_disc
self.amodel.p = self.p
self.nx = self.amodel.x.size()[0]
self.nu = self.amodel.u.size()[0]
self.ny = self.nx + self.nu
self.nq = nq
self.nv = nq
self.np = self.amodel.p.size()[0]
# Real dynamics
H_b = np.eye(4)
self.tau = self.mass(H_b, self.x[:nq])[6:, 6:] @ self.u + \
self.bias(H_b, self.x[:nq], np.zeros(6), self.x[nq:])[6:]
self.tau_fun = Function('tau', [self.x, self.u], [self.tau])
# EE position (global frame)
T_ee = self.fk(np.eye(4), self.x[:nq])
self.t_loc = np.array([0.035, 0., 0.])
self.t_glob = T_ee[:3, 3] + T_ee[:3, :3] @ self.t_loc
self.ee_fun = Function('ee_fun', [self.x], [self.t_glob])
# Joint limits
joint_lower = np.array([joint.limit.lower for joint in robot_joints])
joint_upper = np.array([joint.limit.upper for joint in robot_joints])
joint_velocity = np.array([joint.limit.velocity for joint in robot_joints])
# joint_effort = np.array([joint.limit.effort for joint in robot_joints])
joint_effort = np.array([2., 23., 10., 4.])
self.tau_min = - joint_effort
self.tau_max = joint_effort
self.x_min = np.hstack([joint_lower, - joint_velocity])
self.x_max = np.hstack([joint_upper, joint_velocity])
# EE target
self.ee_ref = self.jointToEE(np.zeros(self.nx))
# NN model (viability constraint)
self.l4c_model = None
self.nn_model = None
self.nn_func = None
# Cartesian constraints
self.obs_string = '_obs' if params.obs_flag else ''
def jointToEE(self, x):
return np.array(self.ee_fun(x))
def checkStateConstraints(self, x):
return np.all(np.logical_and(x >= self.x_min - self.params.tol_x,
x <= self.x_max + self.params.tol_x))
def checkTorqueConstraints(self, tau):
# for i in range(len(tau)):
# print(f' Iter {i} : {self.tau_max - np.abs(tau[i].flatten())}')
return np.all(np.logical_and(tau >= self.tau_min - self.params.tol_tau,
tau <= self.tau_max + self.params.tol_tau))
def checkRunningConstraints(self, x, u):
tau = np.array([self.tau_fun(x[i], u[i]).T for i in range(len(u))])
return self.checkStateConstraints(x) and self.checkTorqueConstraints(tau)
def checkSafeConstraints(self, x):
return self.nn_func(x, self.params.alpha) >= - self.params.tol_nn
def integrate(self, x, u):
x_next = np.zeros(self.nx)
tau = np.array(self.tau_fun(x, u).T)
if not self.checkTorqueConstraints(tau):
# Cannot exceed the torque limits --> sat and compute forward dynamics on real system
H_b = np.eye(4)
tau_sat = np.clip(tau, self.tau_min, self.tau_max)
M = np.array(self.mass(H_b, x[:self.nq])[6:, 6:])
h = np.array(self.bias(H_b, x[:self.nq], np.zeros(6), x[self.nq:])[6:])
u = np.linalg.solve(M, (tau_sat.T - h)).T
x_next[:self.nq] = x[:self.nq] + self.params.dt * x[self.nq:] + 0.5 * self.params.dt**2 * u
x_next[self.nq:] = x[self.nq:] + self.params.dt * u
return x_next, u
def checkDynamicsConstraints(self, x, u):
# Rollout the control sequence
n = np.shape(u)[0]
x_sim = np.zeros((n + 1, self.nx))
x_sim[0] = np.copy(x[0])
for i in range(n):
x_sim[i + 1], _ = self.integrate(x_sim[i], u[i])
# Check if the rollout state trajectory is almost equal to the optimal one
return np.linalg.norm(x - x_sim) < self.params.tol_dyn * np.sqrt(n+1)