|
| 1 | +# |
| 2 | +# ISC License |
| 3 | +# |
| 4 | +# Copyright (c) 2021, Autonomous Vehicle Systems Lab, University of Colorado at Boulder |
| 5 | +# |
| 6 | +# Permission to use, copy, modify, and/or distribute this software for any |
| 7 | +# purpose with or without fee is hereby granted, provided that the above |
| 8 | +# copyright notice and this permission notice appear in all copies. |
| 9 | +# |
| 10 | +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | +# |
| 18 | + |
| 19 | +r""" |
| 20 | +Overview |
| 21 | +-------- |
| 22 | +
|
| 23 | +This script demonstrates how to use the Basilisk v2.1 multi-threading to simulate a constellation |
| 24 | +of 16 spacecraft using 4 threads. The simulation scenario setup is very similar to |
| 25 | +:ref:`scenario_BasicOrbitMultiSat`. To setup the unique satellite orbits, the script here loops |
| 26 | +over all satellites an incrementally changes the orbit elements. The following Vizard screen capture |
| 27 | +illustrate the family of orbits being simulated. |
| 28 | +
|
| 29 | +.. image:: /_images/static/scenario_BasicOrbitMultiSat_MT.jpg |
| 30 | + :align: center |
| 31 | +
|
| 32 | +Inside the ``run()`` command the number of threads is specified using:: |
| 33 | +
|
| 34 | + TheScenario.TotalSim.resetThreads(numThreads) |
| 35 | +
|
| 36 | +With this basic setup it is assumed that each BSK process can run independently in a thread. Note that this script |
| 37 | +does not use any spacecraft flight software which is running in a separate process. The Basilisk v2.1 |
| 38 | +multi-threading only functions for simulations where each Basilisk process can be evaluated independently. |
| 39 | +
|
| 40 | +.. warning:: |
| 41 | +
|
| 42 | + The Basilisk v2.1 multi-threading does not have a thread-safe messaging system. This will be |
| 43 | + added in a later release. |
| 44 | +
|
| 45 | +Illustration of Simulation Results |
| 46 | +---------------------------------- |
| 47 | +
|
| 48 | +:: |
| 49 | +
|
| 50 | + showPlots = True, numberSpacecraft=16, environment = 'Earth', numThreads = 4 |
| 51 | +
|
| 52 | +.. image:: /_images/Scenarios/scenario_BasicOrbitMultiSat_MT_spacecraft_orbits.svg |
| 53 | + :align: center |
| 54 | +
|
| 55 | +
|
| 56 | +""" |
| 57 | + |
| 58 | +# Get current file path |
| 59 | +import inspect |
| 60 | +import os |
| 61 | +import sys |
| 62 | + |
| 63 | +from Basilisk.architecture import messaging |
| 64 | +# Import utilities |
| 65 | +from Basilisk.utilities import orbitalMotion, macros, vizSupport, tleHandling |
| 66 | + |
| 67 | +filename = inspect.getframeinfo(inspect.currentframe()).filename |
| 68 | +path = os.path.dirname(os.path.abspath(filename)) |
| 69 | + |
| 70 | +# Import master classes: simulation base class and scenario base class |
| 71 | +sys.path.append(path + '/../') |
| 72 | +sys.path.append(path + '/../modelsMultiSat') |
| 73 | +sys.path.append(path + '/../plottingMultiSat') |
| 74 | +from BSK_MultiSatMasters import BSKSim, BSKScenario |
| 75 | +import BSK_EnvironmentEarth, BSK_MultiSatDynamics |
| 76 | + |
| 77 | +# Import plotting files for your scenario |
| 78 | +import BSK_MultiSatPlotting as plt |
| 79 | + |
| 80 | +# Create your own scenario child class |
| 81 | +class scenario_BasicOrbitFormationFlying(BSKSim, BSKScenario): |
| 82 | + def __init__(self, tleData): |
| 83 | + super(scenario_BasicOrbitFormationFlying, self).__init__(len(tleData), fswRate=10, dynRate=10, envRate=10) |
| 84 | + self.name = 'scenario_constellationFromTle' |
| 85 | + |
| 86 | + self.set_EnvModel(BSK_EnvironmentEarth) |
| 87 | + |
| 88 | + # Here we are setting the list of spacecraft dynamics to be a homogenous formation. |
| 89 | + # To use a heterogeneous spacecraft formation, this list can contain different classes |
| 90 | + # of type BSKDynamicModels |
| 91 | + self.set_DynModel([BSK_MultiSatDynamics] * self.numberSpacecraft) |
| 92 | + |
| 93 | + # declare empty class variables |
| 94 | + self.samplingTime = [] |
| 95 | + self.snTransLog = [] |
| 96 | + self.snAttLog = [] |
| 97 | + self.rwSpeedLog = [] |
| 98 | + self.rwLogs = [[] for _ in range(self.numberSpacecraft)] |
| 99 | + |
| 100 | + # declare empty containers for orbital elements |
| 101 | + self.oe = [] |
| 102 | + |
| 103 | + self.configure_initial_conditions(tleData) |
| 104 | + self.log_outputs() |
| 105 | + |
| 106 | + if vizSupport.vizFound: |
| 107 | + # if this scenario is to interface with the BSK Viz, uncomment the saveFile line |
| 108 | + DynModelsList = [] |
| 109 | + rwStateEffectorList = [] |
| 110 | + for i in range(self.numberSpacecraft): |
| 111 | + DynModelsList.append(self.DynModels[i].scObject) |
| 112 | + rwStateEffectorList.append(self.DynModels[i].rwStateEffector) |
| 113 | + |
| 114 | + batteryPanel = vizSupport.vizInterface.GenericStorage() |
| 115 | + batteryPanel.label = "Battery" |
| 116 | + batteryPanel.units = "Ws" |
| 117 | + batteryPanel.color = vizSupport.vizInterface.IntVector( |
| 118 | + vizSupport.toRGBA255("red") + vizSupport.toRGBA255("green")) |
| 119 | + batteryPanel.thresholds = vizSupport.vizInterface.IntVector([20]) |
| 120 | + batteryInMsg = messaging.PowerStorageStatusMsgReader() |
| 121 | + batteryInMsg.subscribeTo(self.DynModels[0].powerMonitor.batPowerOutMsg) |
| 122 | + batteryPanel.batteryStateInMsg = batteryInMsg |
| 123 | + |
| 124 | + tankPanel = vizSupport.vizInterface.GenericStorage() |
| 125 | + tankPanel.label = "Tank" |
| 126 | + tankPanel.units = "kg" |
| 127 | + tankPanel.color = vizSupport.vizInterface.IntVector(vizSupport.toRGBA255("cyan")) |
| 128 | + tankInMsg = messaging.FuelTankMsgReader() |
| 129 | + tankInMsg.subscribeTo(self.DynModels[0].fuelTankStateEffector.fuelTankOutMsg) |
| 130 | + tankPanel.fuelTankStateInMsg = tankInMsg |
| 131 | + |
| 132 | + # Add this line to maintain Python references |
| 133 | + self.vizPanels = [batteryPanel, tankPanel] |
| 134 | + |
| 135 | + storageList = [None] * self.numberSpacecraft |
| 136 | + storageList[0] = [batteryPanel, tankPanel] |
| 137 | + |
| 138 | + viz = vizSupport.enableUnityVisualization(self, self.DynModels[0].taskName, DynModelsList |
| 139 | + , saveFile=__file__ |
| 140 | + , rwEffectorList=rwStateEffectorList |
| 141 | + , genericStorageList=storageList |
| 142 | + ) |
| 143 | + vizSupport.setInstrumentGuiSetting(viz, showGenericStoragePanel=True) |
| 144 | + |
| 145 | + def configure_initial_conditions(self, tleData): |
| 146 | + EnvModel = self.get_EnvModel() |
| 147 | + DynModels = self.get_DynModel() |
| 148 | + |
| 149 | + # Configure Dynamics initial conditions |
| 150 | + for i in range(self.numberSpacecraft): |
| 151 | + self.oe.append(orbitalMotion.ClassicElements()) |
| 152 | + self.oe[i].a = tleData[i].oe.a |
| 153 | + #self.oe[i].a = 1.1 * EnvModel.planetRadius + 1E5 * (i + 1) # meters |
| 154 | + self.oe[i].e = tleData[i].oe.e |
| 155 | + #self.oe[i].e = 0.01 + 0.001 * (i) |
| 156 | + self.oe[i].i = tleData[i].oe.i |
| 157 | + #self.oe[i].i = 45.0 * macros.D2R |
| 158 | + self.oe[i].Omega = tleData[i].oe.Omega |
| 159 | + #self.oe[i].Omega = (48.2 + 5.0 * i) * macros.D2R |
| 160 | + self.oe[i].omega = tleData[i].oe.omega |
| 161 | + #self.oe[i].omega = 347.8 * macros.D2R |
| 162 | + self.oe[i].f = tleData[i].oe.f |
| 163 | + #self.oe[i].f = 85.3 * macros.D2R |
| 164 | + rN, vN = orbitalMotion.elem2rv(EnvModel.mu, self.oe[i]) |
| 165 | + orbitalMotion.rv2elem(EnvModel.mu, rN, vN) |
| 166 | + DynModels[i].scObject.hub.r_CN_NInit = rN # m - r_CN_N |
| 167 | + DynModels[i].scObject.hub.v_CN_NInit = vN # m/s - v_CN_N |
| 168 | + DynModels[i].scObject.hub.sigma_BNInit = [[0.1], [0.2], [-0.3]] # sigma_BN_B |
| 169 | + DynModels[0].scObject.hub.omega_BN_BInit = [[0.0], [0.0], [0.0]] # rad/s - omega_BN_B |
| 170 | + |
| 171 | + def log_outputs(self): |
| 172 | + # Process outputs |
| 173 | + DynModels = self.get_DynModel() |
| 174 | + |
| 175 | + # Set the sampling time |
| 176 | + self.samplingTime = macros.sec2nano(1) |
| 177 | + |
| 178 | + # Loop through every spacecraft |
| 179 | + for spacecraft in range(self.numberSpacecraft): |
| 180 | + |
| 181 | + # log the navigation messages |
| 182 | + self.snTransLog.append(DynModels[spacecraft].simpleNavObject.transOutMsg.recorder(self.samplingTime)) |
| 183 | + self.snAttLog.append(DynModels[spacecraft].simpleNavObject.attOutMsg.recorder(self.samplingTime)) |
| 184 | + self.AddModelToTask(DynModels[spacecraft].taskName, self.snTransLog[spacecraft]) |
| 185 | + self.AddModelToTask(DynModels[spacecraft].taskName, self.snAttLog[spacecraft]) |
| 186 | + |
| 187 | + # log the RW wheel speed information |
| 188 | + self.rwSpeedLog.append(DynModels[spacecraft].rwStateEffector.rwSpeedOutMsg.recorder(self.samplingTime)) |
| 189 | + self.AddModelToTask(DynModels[spacecraft].taskName, self.rwSpeedLog[spacecraft]) |
| 190 | + |
| 191 | + # log addition RW information (power, etc) |
| 192 | + for item in range(DynModels[spacecraft].numRW): |
| 193 | + self.rwLogs[spacecraft].append( |
| 194 | + DynModels[spacecraft].rwStateEffector.rwOutMsgs[item].recorder(self.samplingTime)) |
| 195 | + self.AddModelToTask(DynModels[spacecraft].taskName, self.rwLogs[spacecraft][item]) |
| 196 | + |
| 197 | + def pull_outputs(self, show_plots): |
| 198 | + # |
| 199 | + # Retrieve the logged data |
| 200 | + # |
| 201 | + |
| 202 | + r_BN_N = [] |
| 203 | + for i in range(self.numberSpacecraft): |
| 204 | + r_BN_N.append(self.snTransLog[i].r_BN_N) |
| 205 | + |
| 206 | + # |
| 207 | + # Plot results |
| 208 | + # |
| 209 | + |
| 210 | + plt.clear_all_plots() |
| 211 | + |
| 212 | + plt.plot_orbits(r_BN_N, self.numberSpacecraft, 1) |
| 213 | + |
| 214 | + figureList = {} |
| 215 | + if show_plots: |
| 216 | + plt.show_all_plots() |
| 217 | + else: |
| 218 | + fileName = os.path.basename(os.path.splitext(__file__)[0]) |
| 219 | + figureNames = ["spacecraft_orbits"] |
| 220 | + figureList = plt.save_all_plots(fileName, figureNames) |
| 221 | + |
| 222 | + # close the plots being saved off to avoid over-writing old and new figures |
| 223 | + plt.clear_all_plots() |
| 224 | + |
| 225 | + return figureList |
| 226 | + |
| 227 | + |
| 228 | +def runScenario(scenario): |
| 229 | + # Initialize simulation |
| 230 | + scenario.InitializeSimulation() |
| 231 | + |
| 232 | + # Configure run time and execute simulation |
| 233 | + simulationTime = macros.hour2nano(2) |
| 234 | + scenario.ConfigureStopTime(simulationTime) |
| 235 | + |
| 236 | + scenario.ExecuteSimulation() |
| 237 | + |
| 238 | + |
| 239 | +def run(show_plots, tleData, numThreads): |
| 240 | + """ |
| 241 | + The scenarios can be run with the followings setups parameters: |
| 242 | +
|
| 243 | + Args: |
| 244 | + show_plots (bool): Determines if the script should display plots |
| 245 | + orbElem (list): list of orbital elements for each satellite (list of ClasssicElements) |
| 246 | + metaData (list): list of metadata for each satellite |
| 247 | + numThreads (int): number of threads |
| 248 | + """ |
| 249 | + |
| 250 | + # Configure a scenario in the base simulation |
| 251 | + TheScenario = scenario_BasicOrbitFormationFlying(tleData) |
| 252 | + # This call allocates out the requested number of threads into the simulation |
| 253 | + # Note that unless the user does something further, processes will be assigned |
| 254 | + # in a round-robin fashion to the allocated threads. If you desire to specify |
| 255 | + # which processes execute on a given thread, then the addProcessToThread in |
| 256 | + # the SimModel (TotalSim in SimulationBaseClass) should be used. |
| 257 | + TheScenario.TotalSim.resetThreads(numThreads) |
| 258 | + runScenario(TheScenario) |
| 259 | + figureList = TheScenario.pull_outputs(show_plots) |
| 260 | + |
| 261 | + return figureList |
| 262 | + |
| 263 | + |
| 264 | +if __name__ == "__main__": |
| 265 | + # show current path |
| 266 | + tleFile = "oneWeb.tle" # Options are "ISS_ZARYA.tle", "oneWeb.tle", "spacestations.2le" |
| 267 | + tleDataClass = tleHandling.satTle2elem(os.path.join(path, "TLE", tleFile)) |
| 268 | + |
| 269 | + run(show_plots=True, |
| 270 | + tleData=tleDataClass, |
| 271 | + numThreads=4 |
| 272 | + ) |
0 commit comments