Created
April 8, 2025 01:49
-
-
Save trongan93/157127cad287aaf09e34956b30a7322c to your computer and use it in GitHub Desktop.
Attitude Control Lab 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
ntut-lab1.py | |
Sets initial conditions for a spacecraft in Basilisk 2.4.0. | |
""" | |
from Basilisk.simulation import spacecraft | |
from Basilisk.utilities import SimulationBaseClass, macros | |
import matplotlib.pyplot as plt | |
# Create simulation base | |
sim = SimulationBaseClass.SimBaseClass() | |
simProcessName = "process1" | |
simTaskName = "task1" | |
dynamicsProcess = sim.CreateNewProcess(simProcessName) | |
dynamicsProcess.addTask(sim.CreateNewTask(simTaskName, macros.sec2nano(0.1))) | |
# Create spacecraft object | |
scObject = spacecraft.Spacecraft() | |
scObject.ModelTag = "spacecraftBody" | |
# Set initial conditions directly | |
scObject.hub.r_CN_NInit = [0.0, 0.0, 0.0] # Initial position vector [m] | |
scObject.hub.v_CN_NInit = [0.0, 0.0, 0.0] # Initial velocity vector [m/s] | |
scObject.hub.sigma_BNInit = [0.0, 0.0, 0.0] # Initial MRP attitude | |
scObject.hub.omega_BN_BInit = [0.1, 0.1, 0.0] # Initial body rates [rad/s] | |
# Add spacecraft object to the simulation | |
sim.AddModelToTask(simTaskName, scObject) | |
# Log spacecraft state | |
scLog = scObject.scStateOutMsg.recorder() | |
sim.AddModelToTask(simTaskName, scLog) | |
# Run simulation | |
sim.InitializeSimulation() | |
sim.ConfigureStopTime(macros.sec2nano(600)) # Simulate 600 seconds | |
sim.ExecuteSimulation() | |
# Retrieve logged data | |
time = scLog.times() * macros.NANO2SEC | |
omega = scLog.omega_BN_B | |
# Plot angular velocity | |
plt.figure() | |
plt.plot(time, omega[:, 0], label='ω_x') | |
plt.plot(time, omega[:, 1], label='ω_y') | |
plt.plot(time, omega[:, 2], label='ω_z') | |
plt.xlabel("Time [s]") | |
plt.ylabel("Angular Velocity [rad/s]") | |
plt.title("Spacecraft Angular Velocity") | |
plt.legend() | |
plt.grid() | |
plt.tight_layout() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment