Rocket Flight Sim

2D Rocket Flight Sim (Python)
Goals
The main goal of this project was to develop and tune a PID controller for thrust-vector control on my model rocket without having to physically launch it a dozen times. Launching repeatedly would be expensive and wasteful, so I built a simulation environment where I could safely test control gains, visualize responses, and refine the system before committing to real hardware.
This simulation modeled one dimension of the rocket’s control system (pitch or yaw). In reality, the rocket requires two PID controllers running in parallel—one for each axis. That meant I had to run this simulation twice (once per axis) to tune gains for both pitch and yaw.
Objectives included:
-
Build a flight dynamics model that captures rocket torque, angular acceleration, and attitude changes.
-
Incorporate actuator limits such as servo speed (60° in 0.08 s) and gimbal range (±5°).
-
Model sensor imperfections by injecting gyroscope noise into the measured angle.
-
Implement and test a flexible PID controller with tunable Kp, Ki, and Kd.
-
Generate real-time visualization and post-run plots to evaluate controller behavior.

Physics Model
-
Modeled the rocket’s rotational dynamics using its measured moment of inertia and thrust lever arms.
-
Calculated torque based on gimbal deflection and thrust, converting torque to angular acceleration and updating angular velocity and angle at each time step.
-
Kept all math in radians internally, converting to degrees only for visualization.
Control System
-
Wrote a PID loop to command gimbal angle corrections and stabilize the rocket.
-
Incorporated rate limiting so the gimbal angle could only change at realistic servo speeds.
-
Added hard stops at ±5° gimbal deflection to prevent unrealistic actuator commands.
-
Tuned the PID by running repeated simulations, testing gain sets for stability and response speed.
Sensor Model
-
Simulated noisy data by adding random error to the measured angle.
-
Fed the noisy measurement into the PID controller rather than the true state to better match real hardware performance.
Visualization & Logging
-
Used pygame to render a simple rocket, engine, and plume that rotated according to simulated attitude.
-
Logged rocket angle, gimbal input, and torque at every time step.
-
At the end of each run, used matplotlib to plot results for performance evaluation.
Results
The simulation demonstrated how different PID values influenced stability, correction speed, and overshoot. By running tests in software, I identified gain ranges that produced stable flight within seconds of launch while respecting servo speed and noise sensitivity. This gave me confidence that the controller would work in real launches without wasting hardware or propellant.