Setting and Recording Module VariablesΒΆ
Sometimes it is convenient to record a Basilisk module variable, not just the input or output message. This can be done via a python command as shown below. However, note that such module variable recording will slow down the simulation as this is done in the python layer.
The simulation setup is shown in the figure below. Both a C and C++ module are created and added to the single task. However, no messages are connected here. Rather, this sample code illustrates how to record module internal variables. The variables are either public C++ class variables, or they are variables with the C module configuration structure. Both Module: cModuleTemplate and Module: cppModuleTemplate have the exact same public variables for easy comparison.
The sample code is shown below. The C and C++ modules are set up as before. The variable someVariable
of module someModule
is set in python using someModule.someVariable = ...
.
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
7
8
9def run():
10 """
11 Illustration of setting and recording module variables
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
29 mod2 = cppModuleTemplate.CppModuleTemplate()
30 mod2.ModelTag = "cppModule2"
31 scSim.AddModelToTask("dynamicsTask", mod2)
32
33 # set module variables
34 mod1.dummy = 1
35 mod1.dumVector = [1., 2., 3.]
36 mod2.dummy = 1
37 mod2.dumVector = [1., 2., 3.]
38
39 # request these module variables to be recorded
40 scSim.AddVariableForLogging(mod1Wrap.ModelTag + ".dummy", macros.sec2nano(1.))
41 scSim.AddVariableForLogging(mod1Wrap.ModelTag + ".dumVector", macros.sec2nano(1.), 0, 2)
42 scSim.AddVariableForLogging(mod2.ModelTag + ".dummy", macros.sec2nano(1.))
43 scSim.AddVariableForLogging(mod2.ModelTag + ".dumVector", macros.sec2nano(1.), 0, 2)
44
45 # initialize Simulation:
46 scSim.InitializeSimulation()
47
48 # configure a simulation stop time time and execute the simulation run
49 scSim.ConfigureStopTime(macros.sec2nano(1.0))
50 scSim.ExecuteSimulation()
51
52 print("mod1.dummy:")
53 print(scSim.GetLogVariableData(mod1Wrap.ModelTag + ".dummy"))
54 print("mod1.dumVector:")
55 print(scSim.GetLogVariableData(mod1Wrap.ModelTag + ".dumVector"))
56 print("mod2.dummy:")
57 print(scSim.GetLogVariableData(mod2.ModelTag + ".dummy"))
58 print("mod2.dumVector:")
59 print(scSim.GetLogVariableData(mod2.ModelTag + ".dumVector"))
60
61 return
62
63
64if __name__ == "__main__":
65 run()
The SimulationBaseClass method to record a module variable is:
scSim.AddVariableForLogging( variableString, recordingTime, indexStart==0, indexStop==0)
Here variableString
must be composed of the module tag string, a period and the variable name. The examples above illustrate how to apply this method for a C or C++ module variable. Remember that with a C module the ModelTag
variable is defined with the module wrapper, while the C++ module has this ModelTag
variable defined within its class.
The recordingTime
variable is the minimum time that must pass, in nano-seconds again, before the module variable is recorded.
The optional integer arguments indexStart
and indexStop
are defaulted to zero, resulting in a single value being recorded. As this example is also recording a 3-dimensional array dumVector
, it is recorded by setting the start and end index to 0 and 2 respectively.
After executing the script, the recorded variables are retrieved in general using the SimulationBaseClass method:
scSim.GetLogVariableData(variableString)
Here variableString
is again composed of the ModelTag
and variable name as before. Note that the returned array has a first column that represents the time where the variable is recorded in nano-seconds. Executing the script you should thus see the following output:
source/codeSamples % python bsk-6.py
BSK_INFORMATION: Variable dummy set to 0.000000 in reset.
BSK_INFORMATION: Variable dummy set to 0.000000 in reset.
BSK_INFORMATION: C Module ID 1 ran Update at 0.000000s
BSK_INFORMATION: C++ Module ID 2 ran Update at 0.000000s
BSK_INFORMATION: C Module ID 1 ran Update at 1.000000s
BSK_INFORMATION: C++ Module ID 2 ran Update at 1.000000s
mod1.dummy:
[[0.e+00 1.e+00]
[1.e+09 2.e+00]]
mod1.dumVector:
[[0.e+00 1.e+00 2.e+00 3.e+00]
[1.e+09 1.e+00 2.e+00 3.e+00]]
mod2.dummy:
[[0.e+00 1.e+00]
[1.e+09 2.e+00]]
mod2.dumVector:
[[0.e+00 1.e+00 2.e+00 3.e+00]
[1.e+09 1.e+00 2.e+00 3.e+00]]
Note that both the C and C++ module variables are correctly being recorded.