sym_vehicle.py
Go to the documentation of this file.
1 # Copyright (c) 2016-2019 The UUV Simulator Authors.
2 # All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 import numpy as np
16 from .vehicle import Vehicle, cross_product_operator
17 from uuv_thrusters import ThrusterManager
18 from uuv_auv_actuator_interface import ActuatorManager
19 try:
20  import casadi
21  CASADI_IMPORTED = True
22 except ImportError:
23  CASADI_IMPORTED = False
24 
25 
26 class SymVehicle(Vehicle):
27  def __init__(self, inertial_frame_id='world'):
28  Vehicle.__init__(self, inertial_frame_id)
29 
30  if CASADI_IMPORTED:
31  # Declaring state variables
32  ## Generalized position vector
33  self.eta = casadi.SX.sym('eta', 6)
34  ## Generalized velocity vector
35  self.nu = casadi.SX.sym('nu', 6)
36 
37  # Build the Coriolis matrix
38  self.CMatrix = casadi.SX.zeros(6, 6)
39 
40  S_12 = - cross_product_operator(
41  casadi.mtimes(self._Mtotal[0:3, 0:3], self.nu[0:3]) +
42  casadi.mtimes(self._Mtotal[0:3, 3:6], self.nu[3:6]))
43  S_22 = - cross_product_operator(
44  casadi.mtimes(self._Mtotal[3:6, 0:3], self.nu[0:3]) +
45  casadi.mtimes(self._Mtotal[3:6, 3:6], self.nu[3:6]))
46 
47  self.CMatrix[0:3, 3:6] = S_12
48  self.CMatrix[3:6, 0:3] = S_12
49  self.CMatrix[3:6, 3:6] = S_22
50 
51  # Build the damping matrix (linear and nonlinear elements)
52  self.DMatrix = - casadi.diag(self._linear_damping)
53  self.DMatrix -= casadi.diag(self._linear_damping_forward_speed)
54  self.DMatrix -= casadi.diag(self._quad_damping * self.nu)
55 
56  # Build the restoring forces vectors wrt the BODY frame
57  Rx = np.array([[1, 0, 0],
58  [0, casadi.cos(self.eta[3]), -1 * casadi.sin(self.eta[3])],
59  [0, casadi.sin(self.eta[3]), casadi.cos(self.eta[3])]])
60  Ry = np.array([[casadi.cos(self.eta[4]), 0, casadi.sin(self.eta[4])],
61  [0, 1, 0],
62  [-1 * casadi.sin(self.eta[4]), 0, casadi.cos(self.eta[4])]])
63  Rz = np.array([[casadi.cos(self.eta[5]), -1 * casadi.sin(self.eta[5]), 0],
64  [casadi.sin(self.eta[5]), casadi.cos(self.eta[5]), 0],
65  [0, 0, 1]])
66 
67  R_n_to_b = casadi.transpose(casadi.mtimes(Rz, casadi.mtimes(Ry, Rx)))
68 
69  if inertial_frame_id == 'world_ned':
70  Fg = casadi.SX([0, 0, -self.mass * self.gravity])
71  Fb = casadi.SX([0, 0, self.volume * self.gravity * self.density])
72  else:
73  Fg = casadi.SX([0, 0, self.mass * self.gravity])
74  Fb = casadi.SX([0, 0, -self.volume * self.gravity * self.density])
75 
76  self.gVec = casadi.SX.zeros(6)
77 
78  self.gVec[0:3] = -1 * casadi.mtimes(R_n_to_b, Fg + Fb)
79  self.gVec[3:6] = -1 * casadi.mtimes(
80  R_n_to_b, casadi.cross(self._cog, Fg) + casadi.cross(self._cob, Fb))
81 
82  # Build Jacobian
83  T = 1 / casadi.cos(self.eta[4]) * np.array(
84  [[0, casadi.sin(self.eta[3]) * casadi.sin(self.eta[4]), casadi.cos(self.eta[3]) * casadi.sin(self.eta[4])],
85  [0, casadi.cos(self.eta[3]) * casadi.cos(self.eta[4]), -casadi.cos(self.eta[4]) * casadi.sin(self.eta[3])],
86  [0, casadi.sin(self.eta[3]), casadi.cos(self.eta[3])]])
87 
88  self.eta_dot = casadi.vertcat(
89  casadi.mtimes(casadi.transpose(R_n_to_b), self.nu[0:3]),
90  casadi.mtimes(T, self.nu[3::]))
91 
92  self.u = casadi.SX.sym('u', 6)
93 
94  self.nu_dot = casadi.solve(
95  self._Mtotal,
96  self.u - casadi.mtimes(self.CMatrix, self.nu) - casadi.mtimes(self.DMatrix, self.nu) - self.gVec)
97 
98 
eta
Generalized position vector.
Definition: sym_vehicle.py:33
def __init__(self, inertial_frame_id='world')
Definition: sym_vehicle.py:27


uuv_trajectory_control
Author(s):
autogenerated on Thu Jun 18 2020 03:28:42