Simulation

Simulate the rocket spinning with a PID loop tuning


In [1]:
%matplotlib inline
import numpy as np
import utils
from matplotlib import rcParams
rcParams.update({'font.size': 14})

import simulation.lv2 as model
import simulation.simulate as sim
import simulation.PIDcontroller as PIDcontroller


# Read in from open rocket
columns = np.loadtxt('./simulation/openrocket/launch-12.csv', delimiter=',', unpack=True)

time     = columns[0]
altitude = columns[1]*1000  # km to meters
velocity = columns[4]

# only care about launch to apogee
begin = 500
apogee = 25000
time = time[begin:apogee]
altitude = altitude[begin:apogee]
velocity = velocity[begin:apogee]


# Set up PID
pid = PIDcontroller.PIDController(p=10.0, i=0.01, d=0.1)
pid.setTarget(0.0)

In [2]:
def step(t, aa):
    """step disturbance, at some time `t` we push the rocket"""

    if t > 7 and t < 7.1:
        aa = 200
    
    return aa

acc, rate, angle, fin, corr = sim.simulate(time, altitude, velocity, 820, pid, step)

In [3]:
fig = utils.Plot(r"Roll Rate", "Time [$s$]", "Rate [${}^0/s$]")
fig.plot(time, rate[1:])
fig.ylim([-400, 400])
fig.show()



In [4]:
fig = utils.Plot(r"Fin Angle", "Time [$s$]", r"$\alpha$ [${}^0$]")
fig.plot(time, fin)
fig.ylim([-15, 15])
fig.show()