99from Quadrotor import Quadrotor
1010from TrajectoryGenerator import TrajectoryGenerator
1111
12- #Simulation parameters
12+ # Simulation parameters
1313g = 9.81
1414m = 0.2
1515Ixx = 1
1616Iyy = 1
1717Izz = 1
1818T = 5
1919
20- #Proportional coefficients
20+ # Proportional coefficients
2121Kp_x = 1
2222Kp_y = 1
2323Kp_z = 1
2424Kp_roll = 25
2525Kp_pitch = 25
2626Kp_yaw = 25
2727
28- #Derivative coefficients
28+ # Derivative coefficients
2929Kd_x = 10
3030Kd_y = 10
3131Kd_z = 1
3232
33+
3334def quad_sim (x_c , y_c , z_c ):
3435 """
3536 Calculates the necessary thrust and torques for the quadrotor to
@@ -57,9 +58,12 @@ def quad_sim(x_c, y_c, z_c):
5758 dt = 0.1
5859 t = 0
5960
60- q = Quadrotor (x = x_pos , y = y_pos , z = z_pos , roll = roll , pitch = pitch , yaw = yaw , size = 1 )
61+ q = Quadrotor (x = x_pos , y = y_pos , z = z_pos , roll = roll ,
62+ pitch = pitch , yaw = yaw , size = 1 )
6163
6264 i = 0
65+ n_run = 8
66+ irun = 0
6367
6468 while True :
6569 while t <= T :
@@ -73,38 +77,48 @@ def quad_sim(x_c, y_c, z_c):
7377 des_y_acc = calculate_acceleration (y_c [i ], t )
7478 des_z_acc = calculate_acceleration (z_c [i ], t )
7579
76- thrust = m * (g + des_z_acc + Kp_z * (des_z_pos - z_pos ) + Kd_z * (des_z_vel - z_vel ))
80+ thrust = m * (g + des_z_acc + Kp_z * (des_z_pos -
81+ z_pos ) + Kd_z * (des_z_vel - z_vel ))
7782
78- roll_torque = Kp_roll * (((des_x_acc * sin (des_yaw ) - des_y_acc * cos (des_yaw ))/ g ) - roll )
79- pitch_torque = Kp_pitch * (((des_x_acc * cos (des_yaw ) - des_y_acc * sin (des_yaw ))/ g ) - pitch )
80- yaw_torque = Kp_yaw * (des_yaw - yaw )
83+ roll_torque = Kp_roll * \
84+ (((des_x_acc * sin (des_yaw ) - des_y_acc * cos (des_yaw )) / g ) - roll )
85+ pitch_torque = Kp_pitch * \
86+ (((des_x_acc * cos (des_yaw ) - des_y_acc * sin (des_yaw )) / g ) - pitch )
87+ yaw_torque = Kp_yaw * (des_yaw - yaw )
8188
82- roll_vel += roll_torque * dt / Ixx
83- pitch_vel += pitch_torque * dt / Iyy
84- yaw_vel += yaw_torque * dt / Izz
89+ roll_vel += roll_torque * dt / Ixx
90+ pitch_vel += pitch_torque * dt / Iyy
91+ yaw_vel += yaw_torque * dt / Izz
8592
86- roll += roll_vel * dt
87- pitch += pitch_vel * dt
88- yaw += yaw_vel * dt
93+ roll += roll_vel * dt
94+ pitch += pitch_vel * dt
95+ yaw += yaw_vel * dt
8996
9097 R = rotation_matrix (roll , pitch , yaw )
91- acc = (np .matmul (R , np .array ([0 , 0 , thrust ]).T ) - np .array ([0 , 0 , m * g ]).T )/ m
98+ acc = (np .matmul (R , np .array (
99+ [0 , 0 , thrust ]).T ) - np .array ([0 , 0 , m * g ]).T ) / m
92100 x_acc = acc [0 ]
93101 y_acc = acc [1 ]
94102 z_acc = acc [2 ]
95- x_vel += x_acc * dt
96- y_vel += y_acc * dt
97- z_vel += z_acc * dt
98- x_pos += x_vel * dt
99- y_pos += y_vel * dt
100- z_pos += z_vel * dt
103+ x_vel += x_acc * dt
104+ y_vel += y_acc * dt
105+ z_vel += z_acc * dt
106+ x_pos += x_vel * dt
107+ y_pos += y_vel * dt
108+ z_pos += z_vel * dt
101109
102110 q .update_pose (x_pos , y_pos , z_pos , roll , pitch , yaw )
103111
104112 t += dt
105113
106114 t = 0
107- i = (i + 1 )% 4
115+ i = (i + 1 ) % 4
116+ irun += 1
117+ if irun >= n_run :
118+ break
119+
120+ print ("Done" )
121+
108122
109123def calculate_position (c , t ):
110124 """
@@ -118,7 +132,8 @@ def calculate_position(c, t):
118132 Returns
119133 Position
120134 """
121- return c [0 ]* t ** 5 + c [1 ]* t ** 4 + c [2 ]* t ** 3 + c [3 ]* t ** 2 + c [4 ]* t + c [5 ]
135+ return c [0 ] * t ** 5 + c [1 ] * t ** 4 + c [2 ] * t ** 3 + c [3 ] * t ** 2 + c [4 ] * t + c [5 ]
136+
122137
123138def calculate_velocity (c , t ):
124139 """
@@ -132,7 +147,8 @@ def calculate_velocity(c, t):
132147 Returns
133148 Velocity
134149 """
135- return 5 * c [0 ]* t ** 4 + 4 * c [1 ]* t ** 3 + 3 * c [2 ]* t ** 2 + 2 * c [3 ]* t + c [4 ]
150+ return 5 * c [0 ] * t ** 4 + 4 * c [1 ] * t ** 3 + 3 * c [2 ] * t ** 2 + 2 * c [3 ] * t + c [4 ]
151+
136152
137153def calculate_acceleration (c , t ):
138154 """
@@ -146,7 +162,8 @@ def calculate_acceleration(c, t):
146162 Returns
147163 Acceleration
148164 """
149- return 20 * c [0 ]* t ** 3 + 12 * c [1 ]* t ** 2 + 6 * c [2 ]* t + 2 * c [3 ]
165+ return 20 * c [0 ] * t ** 3 + 12 * c [1 ] * t ** 2 + 6 * c [2 ] * t + 2 * c [3 ]
166+
150167
151168def rotation_matrix (roll , pitch , yaw ):
152169 """
@@ -161,10 +178,12 @@ def rotation_matrix(roll, pitch, yaw):
161178 3x3 rotation matrix as NumPy array
162179 """
163180 return np .array (
164- [[cos (yaw )* cos (pitch ), - sin (yaw )* cos (roll )+ cos (yaw )* sin (pitch )* sin (roll ), sin (yaw )* sin (roll )+ cos (yaw )* sin (pitch )* cos (roll )],
165- [sin (yaw )* cos (pitch ), cos (yaw )* cos (roll )+ sin (yaw )* sin (pitch )* sin (roll ), - cos (yaw )* sin (roll )+ sin (yaw )* sin (pitch )* cos (roll )],
166- [- sin (pitch ), cos (pitch )* sin (roll ), cos (pitch )* cos (yaw )]
167- ])
181+ [[cos (yaw ) * cos (pitch ), - sin (yaw ) * cos (roll ) + cos (yaw ) * sin (pitch ) * sin (roll ), sin (yaw ) * sin (roll ) + cos (yaw ) * sin (pitch ) * cos (roll )],
182+ [sin (yaw ) * cos (pitch ), cos (yaw ) * cos (roll ) + sin (yaw ) * sin (pitch ) *
183+ sin (roll ), - cos (yaw ) * sin (roll ) + sin (yaw ) * sin (pitch ) * cos (roll )],
184+ [- sin (pitch ), cos (pitch ) * sin (roll ), cos (pitch ) * cos (yaw )]
185+ ])
186+
168187
169188def main ():
170189 """
@@ -177,13 +196,14 @@ def main():
177196 waypoints = [[- 5 , - 5 , 5 ], [5 , - 5 , 5 ], [5 , 5 , 5 ], [- 5 , 5 , 5 ]]
178197
179198 for i in range (4 ):
180- traj = TrajectoryGenerator (waypoints [i ], waypoints [(i + 1 ) % 4 ], T )
199+ traj = TrajectoryGenerator (waypoints [i ], waypoints [(i + 1 ) % 4 ], T )
181200 traj .solve ()
182201 x_coeffs [i ] = traj .x_c
183202 y_coeffs [i ] = traj .y_c
184203 z_coeffs [i ] = traj .z_c
185204
186205 quad_sim (x_coeffs , y_coeffs , z_coeffs )
187206
207+
188208if __name__ == "__main__" :
189209 main ()
0 commit comments