Execution of Basilisk Modules¶
This guide discusses the main functions that a Basilisk module must perform during setup and when running the simulation. Each Basilisk module has 3 key methods that it must be able to perform:
SelfInit()
: With the C-modules this method acts as the constructor that connects the output messages to write to their own payload (i.e. message data). This step is not required with C++ modules.
Reset()
: This method should reset the module variables to desired default states. For example, this is where the integral feedback gain might be reset to 0, where module parameters like the spacecraft, reaction wheel or thruster configuration messages are read it, etc. This method typically also does some sanity checks that the module is configured properly, and that required input messages are connected, etc.
Update()
: This is the primary module routine that is called every time the simulation advanced one time step. This routine shoudl controll all the functions that this module is to perform.
The function scSim.InitializeSimulation()
calls SelfInit()
and Reset()
for each module. The Update()
mehtod is called each task time step when the simulation is executed.
The sample script below creates a single Basilisk module as illustrated above. The module variable dummy
is set to a non-zero value after the module is created. The InitializeSimulation()
method calls Reset()
which sets this dummy
variable equal to zero.
1
2from Basilisk.utilities import SimulationBaseClass
3from Basilisk.utilities import macros
4from Basilisk.moduleTemplates import cModuleTemplate
5from Basilisk.moduleTemplates import cppModuleTemplate
6from Basilisk.architecture import messaging
7from Basilisk.architecture import cMsgCInterfacePy as cMsgPy
8
9def run():
10 """
11 Controlling the simulation time
12 """
13
14 # Create a sim module as an empty container
15 scSim = SimulationBaseClass.SimBaseClass()
16
17 # create the simulation process
18 dynProcess = scSim.CreateNewProcess("dynamicsProcess")
19
20 # create the dynamics task and specify the integration update time
21 dynProcess.addTask(scSim.CreateNewTask("dynamicsTask", macros.sec2nano(1.)))
22
23 # create modules
24 mod1 = cModuleTemplate.cModuleTemplateConfig()
25 mod1Wrap = scSim.setModelDataWrap(mod1)
26 mod1Wrap.ModelTag = "cModule1"
27 scSim.AddModelToTask("dynamicsTask", mod1Wrap, mod1)
28 mod1.dummy = -10
29 print(mod1.dummy)
30
31 # initialize Simulation:
32 scSim.InitializeSimulation()
33 print(mod1.dummy)
34
35 # perform a single Update on all modules
36 scSim.TotalSim.SingleStepProcesses()
37 print(mod1.dummy)
38
39 return
40
41
42if __name__ == "__main__":
43 run()
To execute the code, this script doesn’t run the simulation for a period of time. Rather, the simulation is executed for a single time step. This is convenient in particular when testing the module input-output behavior. The command to execute Basilisk for one time step is:
scSim.TotalSim.SingleStepProcesses()
After the single process step execution the module dummy
variable is printed out again to illustrate that an Update()
call has occured. Looking at the module source code you see that this variable is zero’d on reset and incremented by +1 on each Update()
call.
If you execute this python code you should see the following terminal output:
$ python bsk-2a.py
-10.0
BSK_INFORMATION: Variable dummy set to 0.000000 in reset.
0.0
BSK_INFORMATION: C Module ID 1 ran Update at 0.000000s
1.0