Creating Stand-Alone Messages

The prior example showed how to connect messages that are embedded within the Basilisk modules. However, there are times where you might need to create a stand-alone copy of such a message. Some flight algorithm modules require the input of a stand-alone message that provides information about the spacecraft mass and inertia properties, or thruster or reaction wheel configuration information. For example, the module unit test ideally just runs the module being tested. Any input messages that this module needs should be created as stand-alone messages. This avoids the unit test script depending on other modules output messages, but makes the module test function on its own.

This tutorial shows you how to create a stand-alone message and connect the Module: cppModuleTemplate input message to it. The syntax is identical to connect a stand-alone message to a C module. Further, this example illustrates how the simulation can be started and stopped multiple times while the message or module variables are changed between runs.

../../_images/qs-bsk-5.svg

To create a stand-alone message, the message payload (i.e. data) container must be created first. Let us assume the message is of type someMsg. The corresponding payload is called someMsgPayload. Thus, the payload container is created using:

msgData = messaging.someMsgPayload()

Essentially this is a python instance of the message structure definition found in architecture/msgPayloadDefC/SomeMsg.h. The content of the message payload is zero’d on creating it. If there is a variable in the structure that we want to change, this is done simply with:

msgData.variable = .....

Next, a message object is created and the message data is written to it. The message object is created using:

msg = messaging.someMsg()

The payload is written to the message using:

msg.write(msgData)

These steps can also be combined into a single line using:

msg = messaging.someMsg().write(msgData)

The simulation code below creates a stand-alone message that is then connected to the module input message.

 1
 2import sys
 3from Basilisk.utilities import SimulationBaseClass
 4from Basilisk.utilities import macros
 5from Basilisk.moduleTemplates import cppModuleTemplate
 6from Basilisk.architecture import messaging
 7from Basilisk.utilities import unitTestSupport
 8import matplotlib.pyplot as plt
 9
10
11def run():
12    """
13    Illustration of creating stand-alone messages
14    """
15
16    #  Create a sim module as an empty container
17    scSim = SimulationBaseClass.SimBaseClass()
18
19    #  create the simulation process
20    dynProcess = scSim.CreateNewProcess("dynamicsProcess")
21
22    # create the dynamics task and specify the integration update time
23    dynProcess.addTask(scSim.CreateNewTask("dynamicsTask", macros.sec2nano(1.)))
24
25    # create modules
26    mod1 = cppModuleTemplate.CppModuleTemplate()
27    mod1.ModelTag = "cppModule1"
28    scSim.AddModelToTask("dynamicsTask", mod1)
29
30    # create stand-alone input message
31    msgData = messaging.CModuleTemplateMsgPayload()
32    msgData.dataVector = [1., 2., 3.]
33    msg = messaging.CModuleTemplateMsg().write(msgData)
34
35    # connect to stand-alone msg
36    mod1.dataInMsg.subscribeTo(msg)
37
38    # setup message recording
39    msgRec = mod1.dataOutMsg.recorder()
40    scSim.AddModelToTask("dynamicsTask", msgRec)
41
42    #  initialize Simulation:
43    scSim.InitializeSimulation()
44
45    #   configure a simulation stop time time and execute the simulation run
46    scSim.ConfigureStopTime(macros.sec2nano(10.0))
47    scSim.ExecuteSimulation()
48
49    # change input message and continue simulation
50    msgData.dataVector = [-1., -2., -3.]
51    msg.write(msgData)
52    scSim.ConfigureStopTime(macros.sec2nano(20.0))
53    scSim.ExecuteSimulation()
54
55    # plot recorded data
56    plt.close("all")
57    figureList = {}
58    plt.figure(1)
59    for idx in range(3):
60        plt.plot(msgRec.times() * macros.NANO2SEC, msgRec.dataVector[:, idx],
61                 color=unitTestSupport.getLineColor(idx, 3),
62                 label='$r_{BN,' + str(idx) + '}$')
63    plt.legend(loc='lower right')
64    plt.xlabel('Time [sec]')
65    plt.ylabel('Module Data [units]')
66    figureList["bsk-5"] = plt.figure(1)
67    if "pytest" not in sys.modules:
68        plt.show()
69    plt.close("all")
70
71    return figureList
72
73
74if __name__ == "__main__":
75    run()

After the simulation runs for 10s, the stand-alone message data is changed and written into the message object. Note that the stand-alone message object itself doesn’t have to be re-created as this is still working and connected to the desired modules. Rather, we only have to update the content of the message.

Next, the simulation stop time is extended for an additional 10s to 20s total and the simulation is executed again. The resulting plot of the module output message is shown below.

../../_images/bsk-5.svg