Go Down

### Topic: Octave/Matlab Vector Programming in Due (Read 20006 times)previous topic - next topic

#### randomvibe

##### Jan 19, 2013, 11:05 amLast Edit: Jan 28, 2013, 04:28 am by randomvibe Reason: 1
I'm very interested in programming complicated matrix operations in Arduino Due, but with the simplicity offered in Octave and Matlab.  For example, the Kalman gain matrix (one of many implementations) is:

K = Pp * H' * inv(H * Pp * H' + R);

Implementing the equation above in standard C would be very complicated, very messy, and difficult to read. In Matlab or Octave, the code is literally the above equation. I literally copied it from one my programs. This syntax is known as vector programming - it's a huge time saver.  Various libraries exist (links below) that allow such syntax in C.  Is it possible to include such a library for the Due?  Has anyone done this?  Thank you.  The answer is yes with the Eigen C++ library ( http://eigen.tuxfamily.org ).

Detailed instructions for Arduino-Due here:
http://arduino.cc/forum/index.php/topic,143602.msg1089368.html#msg1089368

#### robtillaart

#1
##### Jan 19, 2013, 08:23 pm
does this help a bit - http://playground.arduino.cc/Code/MatrixMath -?
probably not tested on DUE yet?
And needs rework to overload the * operators etc. but it might be a start

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### randomvibe

#2
##### Jan 20, 2013, 08:19 amLast Edit: Jan 21, 2013, 10:49 am by randomvibe Reason: 1
There's no reason why the MatrixMath library should not work.  It does need plenty of rework to overload the +, -, *, /, ~ operator for binary matrix math.

I'd rather use the following libraries because they are proven and my C programming skills are weak.  I'd be nice if someone has done this already for the benefit of the community.

http://eigen.tuxfamily.org/index.php?title=Main_Page

http://www.robertnz.net/nm10.htm

#### nosys

#3
##### Jan 20, 2013, 10:58 am
Don't put too much hope in those two library especially Eigen. They are big packages not written for micro-controller.

I don't know much of the other but Eigen in is a very big library with complex inside dependency and even if you use only a very small subset of its functionality it will be very difficult to remove enough things to make it fit in the Due memory.
Better to look for math library specialized for micro controller where algorithm are chosen for their size.

#### jgmdavies

#4
##### Jan 20, 2013, 11:40 am
@randomvibe

One or two people have experimented with Python on Arduinos, possibly easier now we have the Due.  Might be worth looking at if someone could get it going with the 'numpy' library (now a serious contender to Matlab).

HTH
Jim

#### stimmer

#5
##### Jan 20, 2013, 07:03 pm
What have you tried so far? I tried Eigen 3 but got an internal compiler error. Eigen 2 seems to work OK though (but file sizes are rather large, ~170K)
Due VGA library - http://arduino.cc/forum/index.php/topic,150517.0.html

#### randomvibe

#6
##### Jan 21, 2013, 11:12 am
I tried the Arduino matrix math library-->  http://playground.arduino.cc/Code/MatrixMath
and it works if these 2 steps are taken:

Firstly, the library should be copied to:
...\arduino-1.5.1r2\hardware\arduino\sam\libraries

Secondly, "MatrixMath.cpp" and "MatrixMath.h" fail to compile due to the following line:
Code: [Select]
`#include "WProgram.h"`

To correct the problem, replace the above line with the following:
Code: [Select]
`#if defined(ARDUINO) && ARDUINO >= 100#include "Arduino.h"#else#include "WProgram.h"#endif`

I tested the matrix inversion on the Arduino-Due and the results match Octave and Matlab results.

Again, MatrixMath does not do overloaded operations for binary matrix math, as does "Eigen".  I'm more of an Octave and Matlab power user, and not well versed with C++ overhead programming (classes, makefiles, links, etc.).  I have not linked Eigen2 to Arduino.

Eigen 2 seems to work OK though (but file sizes are rather large, ~170K)

stimmer - Did you compile Eigen2 in the Arduino IDE?  If you provide instructions in how to do that, I'd be happy to test it out and compare with Matlab.

#### robtillaart

#7
##### Jan 21, 2013, 10:05 pmLast Edit: Jan 21, 2013, 10:10 pm by robtillaart Reason: 1
@randomVibe
Thanks, I modified -  http://playground.arduino.cc/Code/MatrixMath - to point to your steps / this thread.
and I notified the original writer of the lib..
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### stimmer

#8
##### Jan 21, 2013, 11:07 pm

stimmer - Did you compile Eigen2 in the Arduino IDE?  If you provide instructions in how to do that, I'd be happy to test it out and compare with Matlab.

All I did was to follow the instructions supplied with Eigen 2, and all they said was to put a symlink to the Eigen subdirectory in an include directory (there are several, I chose g++_arm_none_eabi/arm-none-eabi/include). I didn't test thoroughly but overloaded operators and inversion worked on a 3x3 matrix.
Due VGA library - http://arduino.cc/forum/index.php/topic,150517.0.html

#### randomvibe

#9
##### Jan 21, 2013, 11:49 pm
That's very good news that it runs on the Due.

All I did was to follow the instructions supplied with Eigen 2, and all they said was to put a symlink to the Eigen subdirectory in an include directory (there are several, I chose g++_arm_none_eabi/arm-none-eabi/include).

#10

#### randomvibe

#11
##### Jan 22, 2013, 01:51 pmLast Edit: Jan 22, 2013, 01:53 pm by randomvibe Reason: 1

I tried Eigen 3 but got an internal compiler error. Eigen 2 seems to work OK though (but file sizes are rather large, ~170K)

I tried Eigen3 and it does compile properly, and the file size is only 29K compared with Eigen2 at 170K.  I'm using Windows7, so the installation was relatively straightforward - no esoteric linux stuff required.

Defining matrices in Eigen3 is cleaner, and more matrix decomp options are available compared to Eigen2.

#### randomvibe

#12
##### Jan 26, 2013, 09:48 am
Eigen is a C++ library enabling Matlab and Octave-like matrix programming.  Eigen works very well with the Arduino Due.  As a Matlab user that appreciates minimalism, the Eigen library is written as plain header files. So no makefiles, no binary files, nothing to compile upfront, no 20th century nonsense.

Important development regarding Eigen3.  The Arduino/Eigen setup on Windows 7 seems to produce internal compiler errors when using matrices larger than 4x4. After struggling late at night with this, I found a solution.  To add Eigen to the Arduino IDE (1.5.1r2) for Due, follow these instructions and try my example in step-6: (applies to Windows 7)

Step-1:
-> As of 1/25/2013, latest stable release is Eigen 3.1.2

Step-2:
-> Unzip in temporary location
-> Unzip will create folder containing several files and subfolders.
-> The subfolder "Eigen" is all that is needed.

Step-3:
-> Copy "Eigen" subfolder to this precise location in the Arduino IDE directory tree:
...\arduino-1.5.1r2\hardware\arduino\sam\libraries

Step-4:
-> Eigen is written in C++, so header files do not include the *.h extension.
-> The Arduino IDE expects a *.h extension for it to appear in Sketch/Import Library pull-down menu.
...\arduino-1.5.1r2\hardware\arduino\sam\libraries\Eigen
-> Notice "Eigen312.h" goes in the Eigen subdirectory
-> Normally, all that is needed in "Eigen312.h" is a call to the main Eigen Core header as follows: #include <Core>
However, the Arduino and AVR libraries interfere with Eigen for matrices larger than 4x4; this results in internal compiler errors. So @rpavlik came up with a bunch of #define statements that prevents this, and are conveniently included in "Eigen312.h".

Step-5:
-> Again, make sure you download "Eigen312.h" and copy to the Eigen subdirectory!

Step-6:
-> Run example code below demonstrating the Kalman gain equation using 6x6 matrices. Not as clean as Matlab or Octave, but not too shabby.
-> I wrote a function called print_mtxf that serially prints matrices; it's included in the example below.

Good luck.

Code: [Select]
`// Example By: RandomVibe// Eigen Doc: http://eigen.tuxfamily.org/dox/// Quick Reference: http://eigen.tuxfamily.org/dox/QuickRefPage.html#include <Eigen312.h>     // Calls main Eigen3.1.2 matrix class library#include <LU>             // Calls inverse, determinant, LU decomp., etc.using namespace Eigen;    // Eigen related statement; simplifies syntax for declaration of matricesvoid print_mtxf(const Eigen::MatrixXf& K);void setup() {    Serial.begin(9600);        // DECLARE MATRICES     //--------------------    MatrixXf Pp(6,6);   // Produces 6x6 float matrix class    MatrixXf H(6,6);    // Note: without "using namespace Eigen", declaration would be: Eigen::MatrixXf H(6,6);    MatrixXf R(6,6);      MatrixXf X(6,6);      MatrixXf K(6,6);      MatrixXf Z(6,6);      // INPUT MATRICES (so-called comma-initialize syntax)    //---------------------------------------------------------    Pp << 0.3252,  0.3192,  1.0933, -0.0068, -1.0891, -1.4916,         -0.7549,  0.3129,  1.1093,  1.5326,  0.0326, -0.7423,          1.3703, -0.8649, -0.8637, -0.7697,  0.5525, -1.0616,         -1.7115, -0.0301,  0.0774,  0.3714,  1.1006,  2.3505,         -0.1022, -0.1649, -1.2141, -0.2256,  1.5442, -0.6156,         -0.2414,  0.6277, -1.1135,  1.1174,  0.0859,  0.7481 ;    H << 0.8147, 0.2785, 0.9572, 0.7922, 0.6787, 0.7060,         0.9058, 0.5469, 0.4854, 0.9595, 0.7577, 0.0318,         0.1270, 0.9575, 0.8003, 0.6557, 0.7431, 0.2769,         0.9134, 0.9649, 0.1419, 0.0357, 0.3922, 0.0462,         0.6324, 0.1576, 0.4218, 0.8491, 0.6555, 0.0971,         0.0975, 0.9706, 0.9157, 0.9340, 0.1712, 0.8235;    R << 0.3252,  0.3192,  1.0933, -0.0068, -1.0891, -1.4916,        -0.7549,  0.3129,  1.1093,  1.5326,  0.0326, -0.7423,         1.3703, -0.8649, -0.8637, -0.7697,  0.5525, -1.0616,        -1.7115, -0.0301,  0.0774,  0.3714,  1.1006,  2.3505,        -0.1022, -0.1649, -1.2141, -0.2256,  1.5442, -0.6156,        -0.2414,  0.6277, -1.1135,  1.1174,  0.0859,  0.7481;    // Kalman Gain Example; Matlab form:  K = Pp * H' * inv(H * Pp * H' + R)    //-----------------------------------    X  = H * Pp * H.transpose() + R;        K  = Pp * H.transpose() * X.inverse();       // Print Result    //----------------------------     print_mtxf(K);      // Print Matrix Result (passed by reference)    }void loop() {  // put your main code here, to run repeatedly:   }// PRINT MATRIX (float type)//-----------------------------void print_mtxf(const Eigen::MatrixXf& X)  {    int i, j, nrow, ncol;        nrow = X.rows();    ncol = X.cols();    Serial.print("nrow: "); Serial.println(nrow);    Serial.print("ncol: "); Serial.println(ncol);           Serial.println();        for (i=0; i<nrow; i++)    {        for (j=0; j<ncol; j++)        {            Serial.print(X(i,j), 6);   // print 6 decimal places            Serial.print(", ");        }        Serial.println();    }    Serial.println();}`

#### randomvibe

#13
##### Jan 28, 2013, 07:33 pm
Matrices can eat away at SRAM very quickly.  Float type variables are 4 bytes.  So for example, a 12x12 matrix has 144 elements consumes 576 bytes.  Including several matrices, this can add up quickly.

As programs become more complicated and more variables are added, it may be tricky computing RAM usage by hand.

Is there a way for the IDE to report RAM usage is it does for Flash?

#### Arctic_Eddie

#14
##### Jan 28, 2013, 08:14 pm
This may help.

Code: [Select]
`int freeRam() {  extern int __heap_start, *__brkval;  int v;  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);}`

Go Up