Visual Servoing Platform
version 3.5.0 under development (2022-02-15)
|
This tutorial shows how to invoke MATLAB functions from ViSP using MATLAB Engine. The MATLAB C/C++ engine library contains routines that allow you to call MATLAB from your own programs, using MATLAB as a computation engine. This can be used to extend ViSP functionality using MATLAB.
Standalone programs written using MATLAB engine communicates with MATLAB process using pipes on UNIX system and Component Object Model (COM) interface on a Microsoft Windows system. MATLAB provides an API to start and end MATLAB process, send and receive data, and send commands to be processed in MATLAB.
Using the MATLAB engine requires an installed version of MATLAB; you cannot run the MATLAB engine on a machine that only has the MATLAB Runtime. Also, path to MATLAB runtime must be set in the PATH environment variable. For a 64bit machine running Windows, the path is path\to\MATLAB\R20XXy\bin\win64
.
For this tutorial, we create a vpMatrix object containing a 3x3 matrix and pass it to MATLAB sum function to compute a column wise sum of the vpMatrix.
Note that all the material (source code and image) described in this tutorial is part of ViSP source code and could be downloaded using the following command:
In order to build a source code that mix ViSP and MATLAB you should first create a CMakeLists.txt
file that tries to find ViSP and MATLAB. In the following example we consider the case of the tutorial-matlab.cpp source file.
To build this example when MATLAB is in your PATH, use:
$ cd tutorial/matlab $ mkdir build $ cmake .. -DVISP_DIR=<path to ViSP build dir>
This example shows the use of MATLAB engine in a ViSP program.
The output of the program is the column-wise sum of the matrix stored in vpMatrix object computed using MATLAB.
Now we explain the main lines of the source.
First, we include matrix.h and engine.h libraries from MATLAB for matrix implementation and connection to MATLAB respectively. We also include the ViSP vpMatrix.h library for ViSP based matrix implementation and operation.
The initial input is available in a ViSP matrix, which in our case is a 3x3 matrix. It contains numbers from 1 to 9 sequentially in a row-major order.
We then declare MATLAB variables i.e. Engine object reference and MATLAB matrix references for storing input and output MATLAB data. We also initialize the MATLAB matrix (of the same size as input vpMatrix) for storing input data using mxCreateDoubleMatrix().
Then we start a MATLAB engine process using engOpen() and assign the engine handle to the pre-declared Engine pointer ep. If the process initiation is unsuccessful, then engOpen() will return NULL and this program terminates with a failure.
The contents of the vpMatrix is available in data attribute of vpMatrix class which points to a double array. This content is copied to the MATLAB matrix variable T of type double defined earlier. The mxGetPr() function returns a pointer to the first mxDouble element of the data.
The MATLAB variable is then put onto the MATLAB workspace.
Once the matrix is available in the MATLAB workspace, we can use the engEvalString() function to evaluate an expression in MATLAB environment. So, we pass the MATLAB Engine the expression to determine the sum of each column of input matrix T and obtain an output matrix D which will be again stored in the MATLAB workspace. Since, ViSP matrix is row-major and MATLAB matrix is column-major, so we transpose the matrix T before evaluation.
The MATLAB variable Dm is retrieved from the MATLAB workspace and stored in a local MATLAB array D.
We then copy the contents of MATLAB variable Dm to local double array res and print the result on the screen.
Finally, we free the MATLAB variables, close MATLAB Engine and Exit