coreDS™ for Python – Implementation sample

Here are two samples Python Federates that send and receive information from the HLA Federation. A DIS simulator would work using exactly the same code, the data mapping being done via the GUIs.

A more information about the configuration GUIs can be found here.

Curious about using the LUA scripting engine? Take a look here.

Sending data

The first example defines one object and one message the federate can receive (L10 to L17). Once defined (without any knowledge of the FOM file), the end user will be able to map that value to an HLA Object. L37 calls the dialog used to do the mapping and various configurations. This call is optional.

L39 creates the connection to the simulation framework (HLA or DIS depending on the configuration passed as an argument).

L42 to L47 updates the value of the object. It is important to notice the Python programmer NEVER has to know what is the real object type it’s updating. The programmer ALWAYS use local name representation.

See the code
import coreDSUIPython #Import the coreDS UI library
import coreDSPython #Import the coreDS library
import os

DSConnection = coreDSPython.createDSInstance()
extraConfigParams = coreDSPython.createDSConfigurationInstance()
outputvals = coreDSPython.createDSMapping()


#First we have to register the local object and properties known by this federate
coreDSUIPython.addDSOutputObjectVariables("HumanInSpaceOutput", "HumanInSpaceOutputX")
coreDSUIPython.addDSOutputObjectVariables("HumanInSpaceOutput", "HumanInSpaceOutputY")
coreDSUIPython.addDSOutputObjectVariables("HumanInSpaceOutput", "HumanInSpaceOutputZ")

coreDSUIPython.addDSOutputMessageVariables("MessageOut", "MessageOutX")
coreDSUIPython.addDSOutputMessageVariables("MessageOut", "MessageOutY")
coreDSUIPython.addDSOutputMessageVariables("MessageOut", "MessageOutZ")

#showUI(lSelectedConfig, mEnableScripting, lSelectedLUAEditor, mDisableWord, mDebugEnabled, mReload,mEnableLoggingToWindow, mEnabledLogging, mLogFilePath,  false, false, INT_MAX);
  
#We are lazy, we always pop de configuration dialog   
        
lSelectedConfig             = "sender.json" #Name of the config file
lEnableScripting            = 0         #no need for the LUA scripting engine
lSelectedLUAEditor          = ""       #Irrelevant
lDisableWord                = 0        #Disabled - used by the scripting engine
lDebugEnabled               = 0        #Disabled - used by the scripting engine
lReload                     = 0        #Disabled - used by the scripting engine
lEnableLoggingToWindow      = 0        #Disabled - If true, this would use the built-in coreDS error window
lEnabledLogging             = 0        #Disabled - If true, this would output all the errors to a file (lLogFilePath)
lLogFilePath                = ""
lbDirectShow                = 0        #If true, the configuration GUI will go directly to the configuration (bypassing the configuration selection window)
lbInMemory                  = 0        #If true, the configuration will be kept in  memory. The configuration won't be saved to disk. Not supported in Python
lPanelSelector              = 65535  #Alter which configuration panel are enabled - Don't touch

ret = coreDSUIPython.showDSUI(lSelectedConfig, os.path.dirname(os.path.realpath(__file__)), lEnableScripting, lSelectedLUAEditor, lDisableWord, lDebugEnabled, lReload, lEnableLoggingToWindow, lEnabledLogging, lLogFilePath, lbDirectShow,  lbInMemory, lPanelSelector)

if (ret[0] == 1): #A configuration has been activated
    coreDSPython.init(DSConnection, ret[1], extraConfigParams)

    while True:
        outputvals = coreDSPython.createDSMapping();
        coreDSPython.setDSMapping(outputvals, "HumanInSpaceOutputX", str(1))
        coreDSPython.setDSMapping(outputvals, "HumanInSpaceOutputY", str(2))
        coreDSPython.setDSMapping(outputvals, "HumanInSpaceOutputZ", str(3))
        #Send the object data back to the simulation network (either DIS or HLA, depending on the configuration)
        coreDSPython.updateObject(DSConnection, "Human1", "HumanInSpaceOutput", outputvals) 

#we are done, disconnect and clean up
coreDSPython.disconnect(DSConnection)

 

Receiving data

Most of the code is the same as sending data except you define a callback when values are received (L9-11). The callback is registered on L39. As usual, the programmer uses local objects name representation. The mapping is done via the configuration GUIs.

See the code
import coreDSUIPython #Import the coreDS UI library
import coreDSPython #Import the coreDS library
import os

DSConnection = coreDSPython.createDSInstance()
extraConfigParams = coreDSPython.createDSConfigurationInstance()
outputvals = coreDSPython.createDSMapping()

def inputfct(localUniqueObjectIdentifier, objectType, values):
    print("Received update for object: ", localUniqueObjectIdentifier, " of type ", objectType)
    print("values: ",values)

#First we have to register the local object and properties known by this federate
coreDSUIPython.addDSInputObjectVariables("HumanInSpaceOutput", "ShuttleInputX")
coreDSUIPython.addDSInputObjectVariables("HumanInSpaceOutput", "ShuttleInputY")
coreDSUIPython.addDSInputObjectVariables("HumanInSpaceOutput", "ShuttleInputZ")

coreDSUIPython.addDSInputMessageVariables("MessageIn", "MessageInX")
coreDSUIPython.addDSInputMessageVariables("MessageIn", "MessageInY")
coreDSUIPython.addDSInputMessageVariables("MessageIn", "MessageInZ")

#We are lazy, we always pop de configuration dialog   
        
lSelectedConfig             = "receiver.json" #Name of the config file
lEnableScripting            = 0        #no need for the LUA scripting engine
lSelectedLUAEditor          = ""       #Irrelevant
lDisableWord                = 0        #Disabled - used by the scripting engine
lDebugEnabled               = 0        #Disabled - used by the scripting engine
lReload                     = 0        #Disabled - used by the scripting engine
lEnableLoggingToWindow      = 0        #Disabled - If true, this would use the built-in coreDS error window
lEnabledLogging             = 0        #Disabled - If true, this would output all the errors to a file (lLogFilePath)
lLogFilePath                = ""
lbDirectShow                = 0        #If true, the configuration GUI will go directly to the configuration (bypassing the configuration selection window)
lbInMemory                  = 0        #If true, the configuration will be kept in  memory. The configuration won't be saved to disk. Not supported in Python
lPanelSelector              = 65535  #Alter which configuration panel are enabled - Don't touch

ret = coreDSUIPython.showDSUI(lSelectedConfig, os.path.dirname(os.path.realpath(__file__)), lEnableScripting, lSelectedLUAEditor, lDisableWord, lDebugEnabled, lReload, lEnableLoggingToWindow, lEnabledLogging, lLogFilePath, lbDirectShow,  lbInMemory, lPanelSelector)

coreDSPython.registerObjectUpdateHandler(DSConnection, "HumanInSpaceOutput", inputfct)

if (ret[0] == 1): #A configuration has been activated
    coreDSPython.init(DSConnection, ret[1], extraConfigParams)

    for x in range(0, 100000):
        _ = coreDSPython.step(DSConnection) 

#we are done, disconnect and clean up
_ = coreDSPython.deinit(DSConnection)