Advanced: Enabling and Disabling Tasks

Next we study how the Basilisk tasks can be both disabled and enabled. Why would we do this? You might setup a set of BSK modules to mimic a sun-pointing behavior in a task. Next, you setup another set of BSK modules to create a science pointing mode. Being able to enable and disable tasks means that you can creates these tasks once, but then control which flight software modules are actually executed.

../../_images/qs-bsk-8.svg

The sample script below sets up a single process which contains 2 tasks called cTask and cppTask. The Module: cModuleTemplate and Module: cppModuleTemplate are added to these tasks respectively.

 1
 2from Basilisk.moduleTemplates import cModuleTemplate
 3from Basilisk.moduleTemplates import cppModuleTemplate
 4from Basilisk.utilities import SimulationBaseClass
 5from Basilisk.utilities import macros
 6
 7
 8def run():
 9    """
10    Illustration of enabling and disabling tasks
11    """
12
13    #  Create a sim module as an empty container
14    scSim = SimulationBaseClass.SimBaseClass()
15
16    #  create the simulation process
17    dynProcess = scSim.CreateNewProcess("dynamicsProcess")
18
19    # create the dynamics task and specify the integration update time
20    dynProcess.addTask(scSim.CreateNewTask("cTask", macros.sec2nano(1.)))
21    dynProcess.addTask(scSim.CreateNewTask("cppTask", macros.sec2nano(1.)))
22
23    # create modules
24    mod1 = cModuleTemplate.cModuleTemplate()
25    mod1.ModelTag = "cModule1"
26    scSim.AddModelToTask("cTask", mod1)
27
28    mod2 = cppModuleTemplate.CppModuleTemplate()
29    mod2.ModelTag = "cppModule2"
30    scSim.AddModelToTask("cppTask", mod2)
31
32    #  initialize Simulation:
33    scSim.InitializeSimulation()
34
35    # execute BSK for a single step
36    scSim.TotalSim.SingleStepProcesses()
37
38    dynProcess.disableAllTasks()
39    print("all tasks disabled")
40    scSim.TotalSim.SingleStepProcesses()
41    print("BSK executed a single simulation step")
42
43    scSim.enableTask("cTask")
44    scSim.TotalSim.SingleStepProcesses()
45    print("BSK executed a single simulation step")
46
47    scSim.enableTask("cppTask")
48    scSim.TotalSim.SingleStepProcesses()
49    print("BSK executed a single simulation step")
50
51    scSim.disableTask("cppTask")
52    scSim.TotalSim.SingleStepProcesses()
53    print("BSK executed a single simulation step")
54
55    return
56
57
58if __name__ == "__main__":
59    run()

After performing the typical module initialization the script executes a single simulation step. The terminal output below shows that both the C and C++ modules have been executed, meaning both cTask and cppTask are enabled.

To disable all tasks within a process, the command disableAllTasks() can be called on the process variable. A single simulation step is executed with print statements before and after to illustrate not no tasks are being executed, as expected.

Next, the SimulationBaseClass command enableTask(name) is used to turn on the cTask. The string argument is the name of the task being enabled. After executing another simulation step the terminal output illustrates that the C module is again executed. This is repeated for enabling cppTask.

To disable a single task, this is done with the SimulationBaseClass method disableTask(name). The string argument is the name of the task being disabled. The expected terminal output for this script is illustrated below.

(.venv) source/codeSamples % python bsk-8.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
all tasks disabled
BSK executed a single simulation step
BSK_INFORMATION: C Module ID 1 ran Update at 2.000000s
BSK executed a single simulation step
BSK_INFORMATION: C Module ID 1 ran Update at 3.000000s
BSK_INFORMATION: C++ Module ID 2 ran Update at 3.000000s
BSK executed a single simulation step
BSK_INFORMATION: C Module ID 1 ran Update at 4.000000s
BSK executed a single simulation step