Sample time of sfunction that needs long to execute

I want to run a simulink model on an external device (code generation, external mode) to do model predictive control (MPC). My simulink model contains an sfunction that takes N*Ts time to execute (Ts: sample time) and compute a new control trajectory. However I want to run my model on the external device with the sampling time Ts. Therefore I want the MPC sfunction to be executed every (N+1)*Ts, because it needs N*Ts to calculate new controls. In the meanwhile I want to output the "old" control trajectory every Ts.
My question:
  • How can I run the sfunction at a different sample time than the rest of the model, while ensuring that the simulink model won't crash because it is waiting for an output from the sfunction while it is still calculating but instead outputting the old trajectory?
  • How can I hold the old control trajectory in my simulink model?
Further information:
  • Used MPC packages: ACADO, GRAMPC
  • External device: Quanser QUBE Servo 2

4 Commenti

Joel Handy
Joel Handy il 15 Ago 2019
Modificato: Joel Handy il 15 Ago 2019
Are you asking if there is a way start execution of a function on one time step, continue running the rest of your model while the function executes, and fetching the output at another time step?
Thats different than simply running the function at a different rate than the rest of the model and I don't know if thats possible, but I'd be very curious to see if it is. In a pinch You may be able to gin something up where you run two seperate models that communicate. You'd either need the interface control toolbox or to make some custom modifications to the auto generated code.
That would be the ideal case for me yes. But it seems, like you said, quite advanced and I do not have too much time at hand unfortunately.
Could you elaborate a little in how it is different then running the sfunction at a different sampling time as I wrote below. I see that you have a point there but I can't grasp the difference.
Essentially, simulink runs all of the blocks that occur during a time step to completion before moving onto the next timestep. So with the rate transition block, although that function may be running at a much lower frequency (1 out of every N steps), it wont run in the background as the rest of the model continues on without it. You will simply have one time step out of N that takes N*Ts seconds to complete.
Ah I see, thanks! Quanser apparently supplies a server client solution for communication between different simulink models that then could run on different sample times I guess. Unfortunately I do not have the required license and time to look into this right now.

Accedi per commentare.

Risposte (1)

Hendrik Lorenz
Hendrik Lorenz il 15 Ago 2019
Modificato: Hendrik Lorenz il 15 Ago 2019
I managed to give different sampling time to the sfunction block as documented here:
under Block-Based Sample Times
I had to add some rate transition blocks to account for the different sampling times.
If I run now on my device it gives outputs, however in my Simulink i do not see my measured data in the scopes anymore. Seems like just the last value is displayed. Also the time down in the right corner stays at 0.0 now. Any advice?

Categorie

Scopri di più su Simulink Coder in Centro assistenza e File Exchange

Prodotti

Release

R2018b

Richiesto:

il 15 Ago 2019

Commentato:

il 16 Ago 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by