Matrix Operations on the propeller
atlstang
Posts: 20
So i needed a challenge in order to learn more about the propeller and spin, but i also wanted to have something that may be useful (for myself anyway) in the future. I figured why not a matrix library, since math is easy . As far as i know its not been done?
Well here is a matrix library (pure spin) i been working on, its early and probably a bit messy but it works awesomely! Its probably slow, i can think of one optimization off the top of my head to gain like 25%. Most importantly it was a really good experience not only in object programming, but how the prop using the variable space.
Some key features
any mxn matrix can be made (limited only by practicality : memory space,speed)
Matrix inversion
Solving [A][X] = systems using LU decomp, with pivoting
Matrix multiply,add,subtract, scalar multiply, transpose, Trace etc.
Uses F32 library, all values are floating point, Thanks to to who made this!
Demo/testing files are included: (demos use the serial terminal)
MatrixLibDemo.spin --- just a screen doing many different operations on matrices to demo/test
MatrixSpringMassdemo.spin -- An engineering example of solving a stiffness matrix to find displacements
I probably wont be able to work on it for a while, so i figured i would share. I love trying other code people post here on the prop, so if your interested and do try this let me know what you think. I figure the least i could do is share a bit too, like everyone else does.
Well here is a matrix library (pure spin) i been working on, its early and probably a bit messy but it works awesomely! Its probably slow, i can think of one optimization off the top of my head to gain like 25%. Most importantly it was a really good experience not only in object programming, but how the prop using the variable space.
Some key features
any mxn matrix can be made (limited only by practicality : memory space,speed)
Matrix inversion
Solving [A][X] = systems using LU decomp, with pivoting
Matrix multiply,add,subtract, scalar multiply, transpose, Trace etc.
Uses F32 library, all values are floating point, Thanks to to who made this!
Demo/testing files are included: (demos use the serial terminal)
MatrixLibDemo.spin --- just a screen doing many different operations on matrices to demo/test
MatrixSpringMassdemo.spin -- An engineering example of solving a stiffness matrix to find displacements
I probably wont be able to work on it for a while, so i figured i would share. I love trying other code people post here on the prop, so if your interested and do try this let me know what you think. I figure the least i could do is share a bit too, like everyone else does.
zip
28K
Comments
Great work, I look forward to the high speed assembly integer version Actually that might be of use in robotics.
Graham
So the integers is interesting, i assume using integers you just scale the values to avoid decimal places? avoiding float values, and speeding up things? Obviously assembly would be cool to try, still total prop newbie.
You pique the interest in robotics, as my current thesis is on computational dynamics/kinematics (matlab and tons of matrices). Although most my work is forward kinematics/dynamics, are you thinking inverse kinematics with robotics? that would be cool.
Graham
From what i understand asm code goes in its own cog, but I can keep the spin routines i have now and start slowly replacing the spin functions 1 by 1 and have them send calls to the assembly and wait for a return. So really, i just need the intensive portions coded in assembly such as the repeat loops etc. Im assuming the 512 longs could be limiting, so accessing matrix values from the hub memory is probably the best route, but does that make assembly slow? hopefully not spin slow?. addition and subtraction are hardware 4 cycle instructions and are fast but not multiplication and divide are not, so i would have to find an assembly routine for these.
Also i take it that starting and stopping cogs with specific functions as required is not possible or ideal
Something tells me when the prop 2 comes out, with the processing power and hardware multiplication and divide, this could really do very complex things. I was surprised even with spin it takes 1-15ms to solve a system ,depending on matrix size, i thought was quiet good enough for most simple things.
Absolutely. That is the approach I took with my Fast Fourier Transform. It's good to have working Spin code as a model. You might want to make an integer version in Spin first.
Yes, put the data in HUB. There is no way around it. That's what my FFT does, it is still 80 times faster than the Spin equivalent.
Quite. The better idea is to have start the COG running all the time and have it sit in a loop waiting for some command to perform an operation. The command is written into some HUB memory "mail box" after the input data is set up by a Spin caller program. When the operation is complete the PASM sets the command back to zero to indicate "done".
You will have to be careful about overflow when working with fixed point arithmetic, but I'm sure you know that.
Now that i know matrices are even possible, i very much want to try a 3 link robotic arm. using the 2d planar kinematic code i already have in matlab, i think i can setup a system to where you input the x,y,rotation of the last link and it solves the rest. Cartesian coordinates probably not the most efficient, but is more general purpose which could lead to more then just arms.
It would require solving a 9x9 system with a few newtron raphson loops. Currently i did some timing, With a full 9x9 system it takes 112ms to solve once, with a pivoting system that works smarter and not harder i could shave about 20ms off that. I noticed zeros in the code tend to calc faster, which is good since the 9x9 matrix is usually very sparse. so about 4 NR iterations would be around half a second currently.
So if assembly speeds it up near the 80 times mark, using integers speeds it up, improve the pivoting, and perhaps do zero checking to avoid processing the multiplication etc. This could potentially be very very fast and work quiet well. Im definitely going to have to try this
thanks guys
I'm not sure I have a clue what you are talking about. Some of it sounds familiar from my numerical analysis days back in tech school about a million years ago:) Any simple examples of what we might do with this?
Anyway you could have a look at my FFT for an example of command/parameter passing to a PASM process:
http://forums.parallax.com/showthread.php?128292-Heater-s-Fast-Fourier-Transform.&highlight=fast+fourier+transform
What i want to finally build is a constraint matrix, then solve that matrix by utilizing a root finding algorithm(newton raphson). Once solved you have all the X,Y, PHIZ position of each link etc. so if you have servos should just be able to say go to this PHI.
Since i have the code completed in matlab anyway i threw together some examples. The way it works, you just set the X,Y,PHI of the 3rd link and it solves the positions of the others. (but you can pick any 3 coordinate of any bodies)
Example 1: Tracing a circle in space while the 3rd link is straight up.
http://www.youtube.com/watch?v=LVeZMLWa1sY
Example 2: tracing a straight 45degree line, while 3rd link straight horizontal.
http://www.youtube.com/watch?v=TEbBeHID9u4
Example 3: fixed at a point in space, link is circling it
http://www.youtube.com/watch?v=rtU4WpPBVnA
Here is another application of matrices:
It is used to digitize objects. Beware before getting two excited that you need extremely good encoders and good build quality to make anything but the very smallest arm.
Graham