gen_second_order_dynamics.py
Go to the documentation of this file.
1 from sympy import Symbol, sin, cos, tan, diff
2 # from pprint import pprint
3 from sympy.matrices import Matrix
4 import numpy as np
5 
6 x_ = Symbol('x_')
7 y_ = Symbol('y_')
8 z_ = Symbol('z_')
9 phi = Symbol('phi')
10 theta = Symbol('theta')
11 psi = Symbol('psi')
12 x_dot = Symbol('x_dot')
13 y_dot = Symbol('y_dot')
14 z_dot = Symbol('z_dot')
15 phi_dot = Symbol('phi_dot')
16 theta_dot = Symbol('theta_dot')
17 psi_dot = Symbol('psi_dot')
18 k_f_ = Symbol('k_f_')
19 k_m_ = Symbol('k_m_')
20 u0 = Symbol('u(0)')
21 u1 = Symbol('u(1)')
22 u2 = Symbol('u(2)')
23 u3 = Symbol('u(3)')
24 g_ = Symbol('g_')
25 mass_ = Symbol('mass_')
26 L_ = Symbol('L_')
27 # b_ = Symbol('b_')
28 
29 F_1 = k_f_ * u0
30 F_2 = k_f_ * u1
31 F_3 = k_f_ * u2
32 F_4 = k_f_ * u3
33 
34 M_1 = k_m_ * u0
35 M_2 = k_m_ * u1
36 M_3 = k_m_ * u2
37 M_4 = k_m_ * u3
38 
39 sin_phi = sin(phi)
40 cos_phi = cos(phi)
41 tan_phi = tan(phi)
42 sin_theta = sin(theta)
43 cos_theta = cos(theta)
44 tan_theta = tan(theta)
45 sin_psi = sin(psi)
46 cos_psi = cos(psi)
47 tan_psi = tan(psi)
48 
49 Rx = Matrix([
50  [1, 0, 0],
51  [0, cos_phi, -sin_phi],
52  [0, sin_phi, cos_phi]
53 ])
54 Ry = Matrix([
55  [cos_theta, 0, sin_theta],
56  [0, 1, 0],
57  [-sin_theta, 0, cos_theta]
58 ])
59 Rz = Matrix([
60  [cos_psi, -sin_psi, 0],
61  [sin_psi, cos_psi, 0],
62  [0, 0, 1]
63 ])
64 
65 R = Rz * Rx * Ry
66 
67 Gtot = Matrix([0, 0, - g_])
68 Ftot = Matrix([0, 0, F_1 + F_2 + F_3 + F_4])
69 
70 pos_ddot = Gtot + R * Ftot / mass_
71 # pprint(R)
72 
73 # phi, theta, psi dynamics
74 tau = Matrix([
75  L_ * (F_1 - F_2), L_ * (F_1 - F_3),
76  # b_ * (M_1 - M_2 + M_3 - M_4)
77  (M_1 - M_2 + M_3 - M_4)
78 ])
79 omega = Matrix([phi_dot, theta_dot, psi_dot])
80 
81 radius = L_ / 2.0
82 Ix = 2 * mass_ * (radius * radius) / 5.0 + 2 * radius * radius * mass_
83 Iy = 2 * mass_ * (radius * radius) / 5.0 + 2 * radius * radius * mass_
84 Iz = 2 * mass_ * (radius * radius) / 5.0 + 4 * radius * radius * mass_
85 
86 # Eigen::Matrix3d I, Iinv;
87 I = Matrix([[Ix, 0, 0], [0, Iy, 0], [0, 0, Iz]])
88 Iinv = Matrix([[1 / Ix, 0, 0], [0, 1 / Iy, 0], [0, 0, 1 / Iz]])
89 
90 omega_dot = Iinv * (tau - omega.cross(I * omega))
91 
92  # Eigen::VectorXd state_dot(12);
93 state_dot = Matrix([
94  x_dot, y_dot, z_dot,
95  phi_dot, theta_dot, psi_dot,
96  pos_ddot[0], pos_ddot[1], pos_ddot[2],
97  omega_dot[0], omega_dot[1], omega_dot[2]
98 ])
99 
100 statevars = [
101  x_, y_, z_, phi, theta, psi, x_dot, y_dot, z_dot, phi_dot, theta_dot, psi_dot
102 ]
103 fx = []
104 
105 for var in statevars:
106  fx.append(diff(state_dot, var).T)
107 
108 fx = Matrix(np.array(fx).T.tolist())
109 
110 print('fx')
111 print('=' * 40)
112 for i in range(12):
113  print(list(fx.row(i)))
114 
115 
116 print('')
117 print('fu')
118 print('=' * 40)
119 
120 fu = []
121 
122 for var in [u0, u1, u2, u3]:
123  fu.append(diff(state_dot, var).T)
124 
125 fu = Matrix(np.array(fu).T.tolist())
126 # print(fu)
127 for i in range(12):
128  print(list(fu.row(i)))


exotica_quadrotor_dynamics_solver
Author(s):
autogenerated on Sat Apr 10 2021 02:36:44