Swig Interface FileΒΆ

The swig interface file makes it possible to create, setup and manipulate the Basilisk module from python. This file *.i file is in the module folder with the *.h and *.cpp files.

The basic swig interface file looks like this:

 1%module someModule
 3   #include "someModule.h"
 6%pythoncode %{
 7from Basilisk.architecture.swig_common_model import *
10%include "sys_model.h"
11%include "swig_conly_data.i"
13%include "someModule.h"
15%include "architecture/msgPayloadDefC/SomeMsgPayload.h"
16struct SomeMsg_C;
18%pythoncode %{
19import sys

The first line declare the module name through the module command. This sets the module name within in the Basilisk python package. This name typically is written in lower camel case format. For example, if this is a simulation package, then the module is imported using:

from Basilisk.simulation import someModule

Adjust this sample interface code as follows:

  • Replace the any references of someModule to the actual module name.

  • Adjust and repeat the message definition inclusion in lines 15-16 for each input and output message type used.

  • The line struct SomeMsg_C is critical if the message type is C. If the message type is C++ then this line is not required.

  • If you miss including struct SomeMsg_C for your message types, the code will still compile without issue. However, when you set a message variable from python the C++ module variable will not reflect this value and show 0.0 instead.

The line %include "swig_conly_data.i" enables the python interface to read and set basic integer and float values. The following additional includes can be made if a python interface is required for additional types.

%include "std_string.i"

Interface with module string variables

%include "std_vector.i"

Interface with module standard vectors of variables

%include "swig_eigen.i"

Interface with module Eigen vectors and matrices

If you have to interact with a standard vector of input or output messages, the vector interface must also be defined. Assume the message is of type SomeMsg. The at the end of the *.i file add the definition for vectors of output or input messages using:

%template(MoreOutMsgsVector) std::vector<Message<SomeMsgPayload>*>;
%template(MoreInMsgsVector) std::vector<ReadFunctor<SomeMsgPayload>>;

If the vector of messages is used across multiple Basilisk modules, you can add the vector of messages interface definition to the end of the src/architecture/messaging/messaging.i file. This way this definition is available through the messaging package.