-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMomentumSimulator.py
138 lines (110 loc) · 4.04 KB
/
MomentumSimulator.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
import math
import matplotlib.pyplot as plt
import argparse
angle_of_rocket = []
real_angle_of_tvc = []
angle_of_tvc = []
time = []
angle_velocity = []
angle_acceleration = []
fx = []
dt = 0.001
proportional = 0
derivative = 0
integral = 0
def setup(args):
global kp, ki, kd
global moment_of_inertia, distance_to_center_of_mass, thrust, mass
global servo_delay
kp = args.p
ki = args.i
kd = args.d
moment_of_inertia = args.moment
distance_to_center_of_mass = args.distance
thrust = args.thrust
mass = args.mass
servo_delay = args.delay
for x in range(0, int(servo_delay / dt)):
angle_of_rocket.append(args.angle)
angle_of_tvc.append(0)
angle_velocity.append(0)
angle_acceleration.append(0)
fx.append(0)
time.append(x*dt)
def getFx(tvc_angle, iteration):
if tvc_angle[iteration] > 10:
tvc_angle[iteration] = 10
elif tvc_angle[iteration] < -10:
tvc_angle[iteration] = -10
fx.append(-thrust *
math.sin(math.radians(tvc_angle[iteration-int(servo_delay/dt)])))
return -thrust*math.sin(math.radians(tvc_angle[iteration-int(servo_delay/dt)]))
def getAngle(fx, iteration):
angle_acceleration.append(math.degrees(
(fx * distance_to_center_of_mass) / moment_of_inertia))
angle_velocity.append(
angle_velocity[iteration] + angle_acceleration[iteration + 1] * dt)
angle_of_rocket.append(
angle_of_rocket[iteration] + angle_velocity[iteration + 1] * dt)
def loop(iteration):
global integral
fx = getFx(angle_of_tvc, iteration)
getAngle(fx, iteration)
proportional = kp * angle_of_rocket[iteration+1]
integral += ki * angle_of_rocket[iteration+1] * dt
derivative = kd * \
(angle_of_rocket[iteration+1] - angle_of_rocket[iteration]) / dt
angle_of_tvc.append(proportional + integral + derivative)
time.append(time[iteration] + dt)
def plot():
plt.figure('TVC Simulator')
grid = plt.GridSpec(2, 2, hspace=0.3)
plt.subplot(grid[0, 0])
plt.plot(time, angle_of_rocket, color='red')
plt.title("Rocket angle")
plt.xlabel("time (s)")
plt.ylabel("angle (deg)")
plt.subplot(grid[0, 1])
plt.plot(time, angle_of_tvc, color='red')
plt.title("TVC angle")
plt.xlabel("time (s)")
plt.ylabel("angle (deg)")
plt.subplot(grid[1, 0])
plt.plot(time, angle_velocity, color='red')
plt.title("Angle velocity")
plt.xlabel("time (s)")
plt.ylabel("angle velocity (deg/s)")
plt.subplot(grid[1, 1])
plt.plot(time, fx, color='red')
plt.title("Angle acceleration")
plt.xlabel("time (s)")
plt.ylabel("angle acceleration (deg/s^2)")
plt.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='PID Simulator for Thrust-Vector-Control rockets.')
parser.add_argument(
'-p', help='Proportional element of PID', type=float, required=True)
parser.add_argument('-i', help='Integral element of PID',
type=float, required=True)
parser.add_argument('-d', help='Derivative element of PID',
type=float, required=True)
parser.add_argument(
'-angle', help='Initial rocket angle (deg)', type=float, required=True)
parser.add_argument(
'-thrust', help='Thrust of the engine (N)', type=float, required=True)
parser.add_argument(
'-moment', help='Moment of intertia (kg*m^2)', type=float, required=True)
parser.add_argument(
'-mass', help='Mass of the rocket (kg)', type=float, required=True)
parser.add_argument(
'-distance', help='Distance from the engine to the center of mass (m)', type=float, required=True)
parser.add_argument(
'-delay', help="Delay between sending a command and actuator's movement (s)", type=float, required=True)
parser.add_argument(
'-length', help="Length of the simulation (s)", type=float, required=True)
args = parser.parse_args()
setup(args)
for x in range(int(servo_delay / dt) - 1, int(args.length / dt)):
loop(x)
plot()