Advanced: Redirecting Module Output to Stand-Alone Message

Consider a more advanced Basilisk simulation setup where you have two modules that both need to write to the same stand-alone message. The motivation here is to simultaneously run two or more flight guidance algorithm modules, but only one gets executed depending on the flight mode. Regardless of which guidance module is executed, the guidance output message must be fed to the same control module. This cannot be accomplished if the third module subscribes either to the output message of module 1 or 2. To avoid trying to re-subscribe to different module output messages when switching flight modes, we can choose to have both modules 1 and 2 write to the same stand-alone message as illustrated below.


The benefit is that the 3rd module can subscribe its input message to this one stand-alone message. To be clear, this sample application assumes either module 1 or 2 is executed, but not both. Otherwise, one would overwrite the others’ message output.

The sample simulation script creates both a C and C++ module which have their individual output messages redirected to a stand-alone message. The process is different for both programming languages.



Basilisk C modules contain C wrapped message objects and thus can only write to a stand-alone C wrapped message interface. Similarly, a C++ module contains C++ message objects and can only write to a C++ stand-alone message. You can’t have a C module write to a C++ stand-alone message.

In the following sample code, a C and C++ Basilisk module are created. To create a C wrapped stand-alone message the messaging package must be imported from Basilisk.architecture. Next, assume a message of type SomeMsg needs to be created. This is done using:

cStandAloneMsg = messaging.SomeMsg_C()

To enable a C module someCModule to redirect its output message dataOutMsg writing to this stand-alone message use:

messaging.SomeMsg_C_addAuthor(someCModule.dataOutMsg, cStandAloneMsg)

Now the module someCModule will not write to its own internal output message, but rather it will write into this stand-alone message.

 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
 8def run():
 9    """
10    Illustration of re-directing module output message to stand-alone messages
11    """
13    #  Create a sim module as an empty container
14    scSim = SimulationBaseClass.SimBaseClass()
16    #  create the simulation process
17    dynProcess = scSim.CreateNewProcess("dynamicsProcess")
19    # create the dynamics task and specify the integration update time
20    dynProcess.addTask(scSim.CreateNewTask("dynamicsTask", macros.sec2nano(1.)))
22    # create modules
23    mod1 = cModuleTemplate.cModuleTemplateConfig()
24    mod1Wrap = scSim.setModelDataWrap(mod1)
25    mod1Wrap.ModelTag = "cModule1"
26    scSim.AddModelToTask("dynamicsTask", mod1Wrap, mod1)
28    mod2 = cppModuleTemplate.CppModuleTemplate()
29    mod2.ModelTag = "cppModule2"
30    scSim.AddModelToTask("dynamicsTask", mod2)
32    # create stand-alone message with a C interface and re-direct
33    # the C module output message writing to this stand-alone message
34    cMsg = messaging.CModuleTemplateMsg_C()
35    messaging.CModuleTemplateMsg_C_addAuthor(mod1.dataOutMsg, cMsg)
37    # create stand-along message with a C++ interface and re-direct
38    # the C++ module output message writing to this stand-alone message
39    cppMsg = messaging.CModuleTemplateMsg()
40    mod2.dataOutMsg = cppMsg
42    #  initialize Simulation:
43    scSim.InitializeSimulation()
45    #   configure a simulation stop time and execute the simulation run
46    scSim.ConfigureStopTime(macros.sec2nano(1.0))
47    scSim.ExecuteSimulation()
49    # read the message values and print them to the terminal
50    print("mod1.dataOutMsg:")
51    print(
52    print("cMsg:")
53    print(
54    print("mod2.dataOutMsg:")
55    print(
56    print("cppMsg:")
57    print(
59    return
62if __name__ == "__main__":
63    run()

For the C++ Basilisk module it is simpler to re-direct the output message. The stand-alone message is created as before:

cppStandAloneMsg = messaging.SomeMsg()

To redirect the output of a C++ module someCppModule to this stand-alone message, simply set:

someCppModule.dataOutMsg = cppStandAloneMsg


If you want to record the output of someCModule be sure to record cStandAloneMsg instead of someCModule.dataOutMsg. The later is no longer being written to. In C++ we are setting cppStandAloneMsg equal to someCppModule.dataOutMsg. Here recording either will give the same result.

To see the message states of both the module internal message objects and the stand-alone messages, the sample script shows how to use .read() to read the current state of the message object. This will return a copy of the message payload structure. The same method can be used to access both C and C++ wrapped messages. After executing the script you should see the following terminal output:

source/codeSamples % python
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
[0.0, 0.0, 0.0]
[2.0, 0.0, 0.0]
[2.0, 0.0, 0.0]
[2.0, 0.0, 0.0]