LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

What runs on MATLAB should run in LabVIEW? (spoiler: it doesnt.)

Solved!
Go to solution

Hi everyone!

I am trying to implement an MPC Algorithm (already developed and working on MATLAB) in LabVIEW, in order to be able to run on a cRIO controller.

Now, I was (and I am) expecting the same mathematical behaviour from both software despite their programming language. I tried to break the problem down to fewer calculations as possible, before implementing the further steps. I still get two different results from the same calculations. 

The problem:
- With a reference value of "r=1", Labview converge the optimization on a lower value, not 1, as MATLAB does.
- I am forwarding all the matrices (csv files attached, "comma (,)" as delimiter) so LabVIEW should perform only few calculations and re-optimize per iteration.


My worries so far:
- The shift register mechanism, probably I did not grasp it well yet.

- I am not seeing the problem. 

My question:

- Should I expect the same result? Under the assumption of just performing basic matrices calculations? (Inverse, multiplication, array build...)

Forgive me if I was not clear enough,
Thank you.

0 Kudos
Message 1 of 9
(1,484 Views)

Please use zip for attachments. Nobody should be forced to download software (possibly questionable or even malware) just to open you attachment. Windows explorer natively supports zip.

 

We are missing E.csv.

 

Instead of attaching a pile of datafiles, it would be much easier for us of you would create indicators after each file read, run your VI so the indicators have data, delete the file IOs and change these indicators to diagram constants (right-click terminal...change to constant.) now we have the data all in one place and can run it right away without having to change all the paths!)

Message 2 of 9
(1,424 Views)

Thank you for you suggestions,

1. I uploaded the .zip version, I did not notice was a 7z file, I am sorry.
2. The E matrix is intended to be Omega. I forgot to change the destination path.
3. I did as you said, hopefully you should be able to run it without path issues.

0 Kudos
Message 3 of 9
(1,398 Views)

Some (minor) comments:

  • It is disappointing to  find the contents of a folder labelled "MPC_Unconstrained_LabVIEW_v19" cannot be opened in LabVIEW 2019 because they are saved as LabVIEW 2020 files.
  • Is this supposed to run on a Real-Time Target?  If so, what?
  • There is a lot of (complicated) processing going on here.  It seems to me that to answer the Question poised by your Topic, it might be better to create a smaller "demonstration" calculation where the "Answer" is known from First Principles, and then let Matlab and LabVIEW take a swing at it.  Maybe both are doing the "arithmetic" correctly, but one (or the other) is "mis-wired" and doing sequences out-of-order.  Hard to tell without knowing what this code is supposed to do ...

Bob Schor

Message 4 of 9
(1,373 Views)
Solution
Accepted by topic author otergens

So what exactly is the result you expect (sorry, no matlab here).

 

It worries me that that you admit not fully understanding shift registers, which are probably a key part. I also have no idea why there is a 10ms wait and why you are using charts. Wouldn't it make more sense to place a plain graph after the loop?

You also seem to have problems with vectors, which would just be 1D arrays in LabVIEW. Making them into matrices with 1 row is just a detour. Matrix vector multiplications take a mix of matrix&1D array as input and they know what to do. 

For example, this would be a (near?) "literal" rewrite of your code, but of course I have no idea if your implementation is correct: Do you have a web page describing the math?

 

 

altenbach_1-1680635124681.png

 

 

The secret to write clear LabVIEW code is a clean diagram, yours is a mess.

Message 5 of 9
(1,369 Views)

Dear Bob_Schor,

thank you for your comments!

  • Oh 😣 I was reading v19 while saving, so it threw me in confusion, but yes, it is 2020. 
  •  Yes, it is supposed to run on a Real-time target, a cRIO-9049, that should be then inserted in a Hardware-in-the-loop with OPAL-RT simulator.
  • That is a good idea actually, to make a smaller "demonstrator" and see. I was already thinking this was quite reduced (it is good you did not see from what mass I started 😶). I think it is easier to post the code I want to reproduce in LabVIEW. The calculations should be quite straigth-forward, it shouldn't matter the destination or application (in my opinion so far).

Spoiler
  • The MATLAB code:

f = Psi * Xf;

DeltaU = - Omega^(-1) * f;        % Inverse of Omega times f
deltau = Lzerot * DeltaU;
u = u + deltau;                           % variation plus precedent value of u

% Plant / Model here - There is a state-space response
x(:,i+1) = Ad*x(:,i) + Bd*u;
y(:,i) = Cd*x(:,i+1);


% Update:
Xf = [x(:,i+1) - x(:,i); (y(:,i) - rk(:,i))]; % where rk(:,1) is the reference to follow (here =1)

 

the new u and Xf vector shall are the only iterated vectors. (both shift-registered)

 


Could this be helpful? I will anyhow create something more straigth forward and more "analyzable".

Thank you.

 

0 Kudos
Message 6 of 9
(1,351 Views)

Dear altenbach,

 

Thank you for you insights, very useful.

Unfortunately my LabVIEW knowledge is more rushed than I should have, the shift-register is something that I expect to work as a memory equivalent from MATLAB/Simulink: gives the precedent value u(k-1) and Xf(k-1), and it is initialized from u0 and Xf0. I think and hope that this is the needed knowledge for this application I want to pursue.

The 10ms was there just to low the pace between one for and another (once nothing with sense was working, I tried it) but I see it is a nonsense.

Can I ask you would be benefit of using a Graph outside the loop rather than a chart? 

I do not why, but, yes! 1D vectors they are! Thank you for this. I cleaned a lot up. I re-wired everything using matrix and 1D arrays only. I was hoping this was the issue, but, apparently, nothing changes 😣.

The wiring is a mess: question, do you use Clean_up Diagram button, or I mean, is something that is suggested to avoid? Because at the beginning I was trying to keep it clean, but then I started using clean_up button, and it makes a mess. And big appretiation for your wiring.


In the figure you see what MATLAB with the same values and calculations gives. In the other comment I made, I added the MATLAB code too. Hope it is helpful to the comprehension.

matlab_equivalent.jpg

Thank you.


0 Kudos
Message 7 of 9
(1,341 Views)

@otergens wrote:

Can I ask you would be benefit of using a Graph outside the loop rather than a chart? 


Well, a chart is a very special indicator that keeps a limited internal history (default 1024 points), so if you generate less than that, you have old data from the previous run also displayed, while if you generate more data, the oldest are lost. With a graph after the loop the data from the run is accumulated in the autoindexing output tunnel and then displayed fully (no stale or missing data). It is also much more efficient because you don't need to update indicators with each iteration.

 


@otergens wrote:

The wiring is a mess: question, do you use Clean_up Diagram button, or I mean, is something that is suggested to avoid? Because at the beginning I was trying to keep it clean, but then I started using clean_up button, and it makes a mess. And big appretiation for your wiring.


No, if the "cleanup button" improves your diagram, it must have been almost beyond usable. I never use it except when there is truly scrambled code from a forum attachment here. Keep your diagram logically aligned, horizontal traces, clear branches, etc. and you never need to clean it up. 😄

 

Sorry, I am not familiar with what you are actually calculating (and don't really read matlab or any text code anymore). Do you have a link to a web page? Where do the inputs come from?

 

I did not notice earlier, but your "r" can be a scalar. No need to build an array with identical elements.

(There are exceptions, for example if the 1D array coming from above has more than 2 elements, your result would truncate it to two elements, because the shorter array always wins.)

 

altenbach_0-1680646838100.png

 

Message 8 of 9
(1,330 Views)

Ok. Issue solve. So, yes, LAbVIEW can run whatever runs in MATLAB and vice-versa.

Thank you to everyone contributed. The solution to the problem:

 

- A clean and tidy diagram is something fundamental.

- There was an error in how the code was implemented, I needed a third shift register with the variable x(k+1), instead I was using its variation (x(k+1)-x(k)). So it was just a simple and mathematical error.

I assign the solution to the post about tiding up, because really, is the only thing that can help once you  encounter a problem.

Message 9 of 9
(1,201 Views)