pathfinder-for-autonomous-navigation / psim

Six DOF flight simulator and related GNC implementations.
MIT License
4 stars 6 forks source link

Python lin and orb Wrappers #226

Closed nhz2 closed 4 years ago

nhz2 commented 4 years ago

Python lin and orb Wrappers

Fixes #215

Python Wrappers

There are python wrappers for some C++ types. I am using pybind11 which has great documentation and many examples. pybind11 is just a header only library that makes it easy to write python extensions, https://thomasnyberg.com/what_are_extension_modules.html .

The C++ sources need to be compiled as a shared library to be imported by python. I am using cppimport to do this automatically on import but it is theoretically possible to compile using cmake, pio, pip ...

To test the compilation run python estimatortest/pythonwrapper/test_compilation.py Which should print a bunch of compiler errors if there is a mistake.

psim/estimatortest/pwrap.cpp Is the main module file.

The giant comment at the top is used in a python script that sets the compiler configuration. cfg['dependencies'] is a list of filenames that are checked for changes to see if recompiling is needed. cfg['sources'] are the sources to compile. cfg['include_dirs'] are the include directories. cfg['compiler_args'] are the compiler args, I also made a section for platform specific compiler args because my mac needed '-mmacosx-version-min=10.9' to compile.

orb wrappers

psim/estimatortest/orb_ext.cpp Has the orb wrappers.

For now orb::Orbit as python type orb_Orbit is mostly wrapped.

lin wrappers

psim/estimatortest/lin_ext.cpp Has the lin wrappers.

Most of this file is from https://pybind11.readthedocs.io/en/stable/advanced/pycpp/numpy.html examples.

Because lin uses templates, each type that you want to use in python must be specifically wrapped. For example, right now in lin_ext.cpp I have:

    WRAPLINMATRIX("lin_Matrix6x6d",m,double,6,6,6,6);
    WRAPLINVECTOR("lin_Vector3d",m,double,3,3);

These create python types named lin_Matrix6x6d and lin_Vector3d for C++ types lin::Matrix<double, 6, 6, 6, 6> and lin::Vector<double, 3, 3>. These are compatible with numpy arrays, so numpy.array(lin_Vector3d([1.0,2.0,3.0])) will work. The wrapper doesn't wrap all of the lin features, so it's usually best to always convert the lin types into numpy arrays if you want to do math with them in python.

If you need other lin types in python make sure to wrap them in this file using the macros.

Testing

I am using pytest to test the wrappers in CI.