Post · 3 min read· pp. 180–182

Connecting Messages 

▶ YouTube video: Basilisk: Connecting Messages

Basilisk: Connecting Messages

Connecting Messages

Source Code

The python code shown below can be downloaded here.

So far we have learned how to add C or C++ Basilisk modules to a task, setting priorities to execute those modules, and specifying update rates; next we will look at how to connect module messages to one another. Messages are anything that a module outputs after it is executed, such as spacecraft state information, battery power, etc. Again we use both C Module: cModuleTemplate and C++ Module: cppModuleTemplate as the stand-in modules to illustrate setting message connections. Note that the input and output message connections of these modules are of the same type. The following simulation script again uses a single process and task. The modules are created and their input and output messages are connected as illustrated below.

../../_images/qs-bsk-3.svg

The source code is shown below. As we are going to be using the Basilisk messaging system now, it is important to import the messaging package from Basilisk.architecture. Without this the python code will not know how to subscribe to any message type, or how to create a stand-alone message.

python
from Basilisk.moduleTemplates import cModuleTemplate
from Basilisk.moduleTemplates import cppModuleTemplate
from Basilisk.utilities import SimulationBaseClass
from Basilisk.utilities import macros
from Basilisk.architecture import messaging


def run():
    """
    Illustration of connecting module messages
    """

    #  Create a sim module as an empty container
    scSim = SimulationBaseClass.SimBaseClass()

    #  create the simulation process
    dynProcess = scSim.CreateNewProcess("dynamicsProcess")

    # create the dynamics task and specify the integration update time
    dynProcess.addTask(scSim.CreateNewTask("dynamicsTask", macros.sec2nano(5.)))

    # create modules
    mod1 = cModuleTemplate.cModuleTemplate()
    mod1.ModelTag = "cModule1"

    mod2 = cppModuleTemplate.CppModuleTemplate()
    mod2.ModelTag = "cppModule2"

    # add modules to task list
    scSim.AddModelToTask("dynamicsTask", mod1)
    scSim.AddModelToTask("dynamicsTask", mod2)

    # connect messages
    mod2.dataInMsg.subscribeTo(mod1.dataOutMsg)
    mod1.dataInMsg.subscribeTo(mod2.dataOutMsg)

    #  initialize Simulation:
    scSim.InitializeSimulation()

    #   configure a simulation stop time and execute the simulation run
    scSim.ConfigureStopTime(macros.sec2nano(5.0))
    scSim.ExecuteSimulation()

    return


if __name__ == "__main__":
    run()

The method .subscribeTo() connects an output message (variable name ending with OutMsg) to an input message (variable name ending with InMsg), as shown in lines 36-37 above. C modules contain message objects with a C interface, C++ modules contain C++ message objects, and Rust modules use the C-compatible message interface. The .subscribeTo() method is set up such that the user doesn’t have to worry about this distinction. Rather, this method connects C to C, C to C++, C++ to C++ and C++ to C message connections.

Thus, a module output message anotherModule.xxxOutMsg is connected to a module input message someModule.xxxInMsg using the .subscribeTo() method as follows:

default
someModule.xxxInMsg.subscribeTo(anotherModule.xxxOutMsg)

The input and output message names are arbitrary. However, the messages being connected must be of the same type. In the above simulation code we use this protocol to connect the output message of the C module 1 to the input message of C++ module 2. Next the output of C++ module 2 is connected to the input of C module 1 to create a sample closed-loop messaging setup.

Warning

You can only subscribe an input message to an output message that already exists! Don’t try to subscribe to the message before it has been created. In this simulation the subscriptions are all occurring after the modules are created.

If you execute this python code you should see the following terminal output:

default
source/codeSamples % python3 bsk-3.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 5.000000s
BSK_INFORMATION: C++ Module ID 2 ran Update at 5.000000s

Note that here the two modules are added without setting a priority. Thus, they are executed in the order that they were added to the Basilisk task.

If you want to undo any existing connection to a module input message someModule.xxxInMsg, you need to use the .unsubscribe() method:

default
someModule.xxxInMsg.unsubscribe()