04-03-2023 03:24 PM
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.
Solved! Go to Solution.
04-04-2023 09:21 AM - edited 04-04-2023 09:31 AM
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!)
04-04-2023 11:08 AM
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.
04-04-2023 01:26 PM
Some (minor) comments:
Bob Schor
04-04-2023 02:09 PM
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?
The secret to write clear LabVIEW code is a clean diagram, yours is a mess.
04-04-2023 04:05 PM
Dear Bob_Schor,
thank you for your comments!
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.
04-04-2023 04:46 PM
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.
Thank you.
04-04-2023 05:22 PM - edited 04-05-2023 09:37 AM
@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.)
04-12-2023 02:10 AM
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.