pathfinder-for-autonomous-navigation / psim

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

Python Estimator Testing #208

Closed nhz2 closed 4 years ago

nhz2 commented 4 years ago

Python Estimator Testing

Summary

The goal is to quickly test and see the accuracy of an estimator in Jupyter Notebook. psim/estimatortest/Orbit-estimator-test.ipynb is an example.

Running the Jupyter Notebook on the linux server over ssh

  1. ssh into the server

  2. clone the psim repo add --recursive flag to also get lin

  3. cd psim

  4. git checkout python-wrappers

  5. python -m venv venv

  6. source venv/bin/activate

  7. pip install -r requirements.txt

  8. ipython kernel install --user --name=venv

  9. jupyter notebook --no-browser --port 1234

  10. Start an SSH tunnel to the server, see more info on unix use ssh -NL 1234:localhost:1234 username@serverip in a new local terminal

  11. copy the URL in the output from step 9 into your browser. Something like the following should come up.

    Screenshot 2020-05-08 17 15 54
  12. Click estimatortest and then open Orbit-estimator-test.ipynb

  13. Change the kernel to venv.

    Screenshot 2020-05-08 17 22 39
  14. Everything should be working now. I would recommend opening another ssh connection to do git stuff, or make edits in vim.

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.

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.

Documentation Evidence

I'll make a post to ReadTheDocs about the Orbit estimator.