How to make a Matrix calculation in Flowcode

Post here to discuss any new features, components, chips, etc, that you would like to see in Flowcode.
stefan.erni
Valued Contributor
Posts: 758
http://meble-kuchenne.info.pl
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 149 times
Been thanked: 171 times

How to make a Matrix calculation in Flowcode

Post by stefan.erni »

Hi Ben

How to make a Matrix calculation in Flowcode?
We would like to use a Matrix to calculate better 3D positions from our objects. We can support with more info's.
But i think, actually the company is already called Matrix ;)

regards

Stefan

mnfisher
Valued Contributor
Posts: 962
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 511 times

Re: How to make a Matrix calculation in Flowcode

Post by mnfisher »

Use a 2d array and a couple of loops?

Would expect better support given the name

BenR
Matrix Staff
Posts: 1742
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 443 times
Been thanked: 604 times

Re: How to make a Matrix calculation in Flowcode

Post by BenR »

I have recently done a matrix multiply routine as part of this component which may help.
KinematicsArm.fcfx
(112.23 KiB) Downloaded 62 times

The MatrixMultiple macro multiples the matrix WorkingMatrix with MatrixStep and stores the result into local variable CalcMatrix. CalcMatrix is then copied back into WorkingMatrix once the multiply is complete. All three matrixes in this case are 2D arrays with 4 values in each dimension.

Having something more formal to do matrix maths in a component may be the way forward with this.

stefan.erni
Valued Contributor
Posts: 758
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 149 times
Been thanked: 171 times

Re: How to make a Matrix calculation in Flowcode

Post by stefan.erni »

Hi Ben, Hi Martin

Thanks for the Info, we will have a look at the matrix.
One more question
Can you also calculate integrals with flowcode?

regards

Stefan

mnfisher
Valued Contributor
Posts: 962
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 511 times

Re: How to make a Matrix calculation in Flowcode

Post by mnfisher »

Hi Stefan,

I had a little play with a matrix type last night - basically a struct with width, height and data. Create Matrix allocated memory - with set, get and Delete functions - didn't quite get as far as multiplication.
So the 'matrix' is an integer which is really a pointer.

Will play a little more tonight

Martin

mnfisher
Valued Contributor
Posts: 962
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 511 times

Re: How to make a Matrix calculation in Flowcode

Post by mnfisher »

Didn't get much time to play last night.

So - if anyone would like to play along. Any thoughts/comments/ideas/help...

This is some simple code to create / delete / set values in / get values / display a matrix.

There is a multiply - but it just creates the result matrix (and I'm not sure I have the dimensions right (h2 by w1 which are equal) - I haven't done any of the multiply code. (Multiply rows by columns and sum) There is no error checking - so if you do setvalue(matrix, 0, 10000) on a 3x3 matrix it will overwrite something.
Multiply returns 0 if width1 != height2 (which is a requirement for a multiply)
Create also returns 0 if can't create (no memory) - everywhere needs to check...

I've used an Arduino - but esp32 would be better (much more memory)

Note it should work on Arduino or esp32 - but won't work on small PICs which don't support malloc.

I've used 16 bit integers as values in the matrix. Could change to float easily.

Matrix.fcfx
(24.55 KiB) Downloaded 56 times

Martin

mnfisher
Valued Contributor
Posts: 962
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 511 times

Re: How to make a Matrix calculation in Flowcode

Post by mnfisher »

And with a multiply....

I added a utility function - LoadMatrix (again no checks :-) )

Here it creates /loads two matrices (3x2 and 2x3) multiplies and then deletes all 3 matrices - output is sent to UART... Then repeats (checking for memory leaks!)
Matrix.fcfx
(29.68 KiB) Downloaded 60 times

Martin

stefan.erni
Valued Contributor
Posts: 758
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 149 times
Been thanked: 171 times

Re: How to make a Matrix calculation in Flowcode

Post by stefan.erni »

Hi Martin

Thank you very much.
We will try to use this in the app developer next week.
We just need to move one or two 3D objects and I will let you know if it works.

regards

Stefan

mnfisher
Valued Contributor
Posts: 962
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 511 times

Re: How to make a Matrix calculation in Flowcode

Post by mnfisher »

Hi Stefan,

Hopefully it will work OK. It works fine on the Arduino - but I'd recommend adding some error checking. Not sure what data you need - but it would be straightforward to change to longs or floats.

I've used long ints as pointers - which works in the limited address space of a MCU but if app-developer is 64 bit then some more fiddling might be needed (using an index to a lookup table of addresses for example)

Maybe a plea for pointer support in FC?

Do you need any other functions - scalar multiplication is straight forward :)

One complication - having written some code - I found most examples use M*N to define the matrix - where M is rows and N columns. I used x, y - so it's generally the unexpected way round.. Multiply took a bit more time as a result...

Martin

stefan.erni
Valued Contributor
Posts: 758
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 149 times
Been thanked: 171 times

Re: How to make a Matrix calculation in Flowcode

Post by stefan.erni »

Hi Martin

We try with Flowcode to create an IMU Sensor Fusion.
acceleration (Accelerometer), angular velocity(Gyroscope) and maybe orientation (Magnetometer).
for this a matrix and integral would be useful.
We are aware that this means some effort for us.

regards

Stefan
Attachments
Snag_9789d2.png
Snag_9789d2.png (33.62 KiB) Viewed 1157 times

Post Reply