Commit 10f4fe56 authored by Olivier Lantsoght's avatar Olivier Lantsoght 🏁

[Python][Readme] updating outdated informations

parent 25ce07b5
Pipeline #4651 passed with stages
in 12 minutes and 37 seconds
Python interface
=================
This part develops the possibility to drive a simulation from python running C modules.
For now this work is only tested on Linux system (Ubuntu).
This part develops the possibility to drive a simulation from python running C modules.
This work is dedicated to Linux system (Ubuntu) but was also functionnal on some windows release.
This work has only be tested with Python version 2.7.#.
--- Installation ---
--------------------
### Requirements ###
## Requirements ##
* An updated working version of [MBsysC](https://git.immc.ucl.ac.be/robotran/mbsysc).
* Python 2.7, scipy, numpy and an Python IDE.
* Cython.
### For Linux ###
Just run in command line : `sudo apt-get install python-numpy python-scipy cython`.
* In case of troubles with cython, look the [official website](http://cython.org/).
* An updated working version of MBsysC (see [here](http://www.robotran.eu/tutorial/install/Linux/MBsysC.html)).
* Python and some modules: `sudo apt-get install python-numpy python-scipy`
* A python IDE can be usefull, for example spyder: `sudo apt-get install spyder`
* Cython : `sudo apt-get install cython` or see [here](http://cython.org/).
Then check the python version (again in command line) : `python -V`.
Choose the IDE of your choice (we tested it with spyder: `sudo apt-get install spyder`).
### Build MBsysC ###
Open a terminal in *MBsysC* folder (you should see the *mbs_common* folder):
### For windows ###
We were able to get it working with [Anaconda](https://www.anaconda.com/) under windows 7.
* Create the build folder: `mkdir build`
* Go inside this folder: `cd build`
* Create the cmake project, source being in *mbs_common*: `cmake ../mbs_common`
* Build MBsysC modules: `make`
Download and install [Anaconda for Python 2.7](https://www.anaconda.com/download/#windows). __Check twice the version you choose !__
Install additionnal modules:
* Check if numpy and scipy package are installed, if not install them from Anaconda.
* Install cython (there is maybe an GUI to do it now):
* run the "anaconda prompt"
* type and run `conda install cython`
## Building and compiling ##
### MBsysC with the Python interface ##
Follow the installation tutorial:
* [Linux](http://www.robotran.be/tutorial/install/Linux/MBsysC.html#compilation-of-the-user-file-generation-application-and-the-mbsysc-modules)
* [windows](http://www.robotran.be/tutorial/install/Win64/MBsysC.html#compilation-of-the-user-file-generation-application-and-the-mbsysc-modules)
**Warning**: for now the build folder of MBsysC is imposed!
Set the flag "INTERFACE_PYTHON" to "ON":
* With Cmake-gui: Just click on the box.
* In command line: `cmake . -DFLAG_INTERFACE_PYTHON=ON`
### Build the Python interface ###
Open a terminal in *MBsysC* folder (you should see the *mbs_common* folder):
Build and compile.
* Create the build folder: `mkdir build_interface`
* Go inside this folder: `cd build_interface`
* Create the cmake project, source being in *mbs_python*: `cmake ../mbs_interface/mbs_python`
* Build the interface: `make`
###Troubles under Windows###
__Python27 not found__:
The directory of your installation of the Anaconda libaries is not in the environment variables.
Open the "__...GitFolder__/MBsysC/mbs\_interface/mbs\_python/CMakeLists.txt", under the lines
```
# Folder containing interfaces libraries
link_directories (${LIBRARY_OUTPUT_PATH})
```
add
```
link_directories ("W:/This_is_a_wonderful_place_to_install_some_software_but_the_next_folder_is_also_good/Anaconda2/libs")
# The path is obviously wrong, to be sur you will update it!
```
Of course: __LOCATE THE PATH TO THE FOLDER CONTAINING THE LIBRARIES TO THE ONE CORRESPONDING TO YOUR COMPUTER__ (Windows is simple)!
You can look after the file "python27.lib" and select the good folder if multiples files are found (Windows does not waste space on hard drive).
__python_d.exe not found or missing__:
Your are compiling MBsysC in __Debug__ mode, Windows try to find the Python debug excecutable, which is not distributed.
Either, compile it in __Release__ mode, __or__ replace ```#include "Python.h"``` by
```
#ifdef _DEBUG
#undef _DEBUG
#include <Python.h>
#define _DEBUG
#else
#include <Python.h>
#endif
```
in the following files:
* __mbs_data_py.c__
* __mbs_part_py.c__
* __mbs_dirdyn_py.c__
_WARNING:_ the files are generated during the first attempt of compiling by Visual Studio, meaning you cannot edit thme before getting the error!
### Build your project ###
Open a terminal in you project *WorkR* folder (you should see the *src* folder):
Follow the [tutorial](http://robotran-doc.git-page.immc.ucl.ac.be/TutorialModellingFeatures/output_tuto/c-code/bodiesJoint.html#build-an-run-the-project) but set the flag "SEPARATE_BUILD" to "ON":
* With Cmake-gui: Just click on the box.
* In command line: `cmake . -DFLAG_SEPARATE_BUILD=ON`
Build and compile. You can check that your project is fully functionnal (outside python) by running it as usual.
* Create the folder build: `mkdir build`
* Go inside this folder: `cd build`
* Create the cmake project: `cmake ..`
* Set the FLAG_SEPARATE_BUILD to ON: `cmake . -DFLAG_SEPARATE_BUILD=ON`
* Build the project: make
### For windows : copy-paste some files ###
Copy all the __.pyd__ files from __...GitFolder__/MBsysC/build/* to *YourProject/workR/build/__Debug (or/and Release)__*.
If multiple occurences of a file, always take them all from the same folder (release or debug).
### Call your C-compiled project from python ###
## Call your C-compiled project from python ##
Go in the in you project *WorkR* folder (you should see the *src* and *build* folder) and create your python script with:
```python
# Firstly: include the path to your MBsysC-Python interface library
import sys
sys.path.insert(1, ".../MBsysC/build_interface/lib")
# LINUX:
sys.path.insert(1, "GitFolder/MBsysC/build/lib")
# WINDOWS:
sys.path.insert(1, "./build/Debug")
sys.path.insert(1, "./build/Release")
# Secondly: load the interfacing modules
from mbs_data_py import *
from mbs_part_py import *
......@@ -116,6 +172,10 @@ if not mbs_dirdyn.mbs_like_MBsysC:
del mbs_dirdyn # Write the results to file if(not like_MBsysC) and memory released
del mbs_data # Release the memory allocated for the MbsData structure
```
_WARNING_: this script use the __matplotlib__ module of python to plot the graphs.
With python you can run the script in intercative mode in the console (python -i myScrip.py) observe the results and change the initial condition (`mbs_data.q0[2]=7.0`) and restart a dirdyn computation (`mbs_dirdyn = MbsDirdyn(mbs_data)`;`mbs_dirdyn.set_options(...)`; `mbs_dirdyn.mbs_run_dirdyn(mbs_data)`) without any recompilation.
\ No newline at end of file
With python you can run the script in intercative mode in the console (python -i myScrip.py) observe the results and change the initial condition (`mbs_data.q0[2]=7.0`) and restart a dirdyn computation (`mbs_dirdyn = MbsDirdyn(mbs_data)`;`mbs_dirdyn.set_options(...)`; `mbs_dirdyn.mbs_run_dirdyn(mbs_data)`) without any recompilation.
You can put the partitionning and the dirdyn analysis of your project into a function and feed an optimizer with it (for example with [scipy](https://docs.scipy.org/doc/scipy/reference/optimize.html)).
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment