The block 'xyz/If Action Normal/In1' has a discrete sample time that does not match the sample time 0 of the If block 'abc' controlling its execution - matlab

I've run into the following error message from Simulink:
The block 'xyz/If Action Normal/In1' has a discrete sample time that does not match the sample time 0 of the If block 'abc' controlling its execution
My solver configuration is variable step/auto, and the environment could be seen at the end of this post.
I tried a few methods:
try to locate the error by replacing/comment out blocks;
try to change the port driving if-action block;
try to remove all discrete blocks;
Any suggestion is highly appreciated.

This error is hidden behind a Matlab known bug. Basically, when I tried to add a rate transition to make the same time consistent. The system crashed.
The crash is because of a known Matlab bug. A zero-holder needs to be added following unit delay blocks. After this, I could add rate transit block without model crashing.

Related

Simulink: get step time in a triggered subsystem

I have a question about my triggered subsystem in Simulink:
So the Susbsystem is executed every 0.01 s.
My question is: how can I get this variable (0.01) inside my subsystem?
NB: I don't need the Simulation step time.
I've already thought to two solutions:
1) use a Digital Clock with a Memory block
2) use a Weighted Sample Time block
but, both of them gave me an initial value of 0.
Do you have an elegant solution for my task
Thank you very much.
Have a nice day
The easiest solution is to have a constant block feeding into the subsystem. Define a MATLAB Workspace variable dt=0.01; and have the constant set to dt, and the sample rate of your Pulse Generator set to dt.
Alternatively you can use the Probe block to get the Sample Rate of the signal coming out of the Pulse Generator, and feed that as an input to the Subsystem.
Another way is to use get_param() command in your workspace.
In your case, it should be:
get_param('<path-to-block>/Pulse Generator','Period')
This should output:
>> ans =
0.01
I had this same problem myself. I received the following feedback from MATHWORKS when I reported it as a bug:
If triggered subsystems are executed on edge detection on the triggering event then it will require at least two model execution time steps in order to satisfy the triggering condition. As a result the triggered subsystem will not execute at 0 causing the results you are seeing. This is expected behavior.
If you require the triggered subsystem to execute on the first timestep you can change the "Trigger type" option to "function call." This option will execute the subsystem or model when the control signal receives a function-call event from a Statflow chart, Function-Call Generator block, or an S-function block." In this case you will just need to confirm that your function call block executes prior to the triggered subsystem block. This can be confirmed using the "Blocks - Execution Order" Debug tool under "Information Overlays."

Creating delay with a while loop in Matlab

As a student I am currently working on a Matlab Simulink project. I am quite new to using Matlab/Simulink (few weeks).
I want to implement and run a Matlab “.m” file with which I can open Simulink and start the simulation. The aim is to do a 24h Test with a load cell cut into 1h “pieces” and to save the data to different sheets of an excel file each hour. So my simulation runs for 1h, stops and starts again, and so on. Through Matlab and a “for” loop I do the measures 24 times.
Between measuring steps I have to wait for simulink to finish its measures and saving the file in order for the Simulink window to be able to get closed by close_system('Thesis_SerDatTransm_Simulink').
So I tried to implement the delay with a while loop and by checking if the measures I get fit into an array of the size bigger than 449 (I measure 449 values):
for k=0:1:24
% Load Simulink
load_system('Thesis_SerDatTransm_Simulink.slx')
% Open Simulink
open_system('Thesis_SerDatTransm_Simulink.slx')
% Start Simulation
set_param('Thesis_SerDatTransm_Simulink', 'SimulationCommand', 'Start');
% Save Data
my_cell = sprintf('A%d',k);
xlswrite('file.xlsx',y,my_cell)
% Wait for Simulation
while 1
test=size(y)>=449;
if (test)
close_system('Thesis_SerDatTransm_Simulink')
break
end
end
end
The Problem now is, that program gets stuck at the while loop. Simulink is started, but no simulation or data gathering is done.
So I wondered if anyone could check if something is wrong with my While loop, since the rest of the programm works all fine without the loop (but receiving an error message, that during the simulation, Simulink window can't be closed).
I know there is a way to create a delay with waitforin matlab and create another function which I could call, but I couldn't figure out how to do this yet.
thanks
Regards
hohmchri
The right way to do this is to use the sim function to run your model (not the sequence of load_system, open_system and set_param that you have).
sim will block the execution of m-code until the model completes executing. Data can either be returned into the workspace (when used with no output arguments) or returned as an output from the call to sim. (And then you can write it to Excel as you've done.)
The only reason not to use sim, and perhaps use the commands you have, is if the model takes a long time to initialize, and you don't want to to open and close it every time through the loop. However, even in this case your code isn't correct. The load_system would be outside the loop; the open_system is not required; in your while loop you would poll the model's SimulationStatus property to see if it is still running (not the size of the y variable); and the close_system would be after the loop (as indicated by #m_power in one of the comments).
As written you should use the matlab pause command. This stops your execution for X seconds.
You should also look to optimize your code as m_power states

Illegal rate transition while trying to normalize signal in simulink

I have a signal in simulink which I want to normalize so that the highest value of the signal is always 1. So I use a MaxRunningResettable-Block to remember the highest value that passed so far. I then divide the signal by that value.
A little test with a signal generation block, the running resettable block, the divide block and a scope runs just fine. But when I add this normalizing function to my simulink model I get an error:
"Model initialization failed - Illegal rate transition found involving Unit Delay"
I don't even need to connect the little test case to my other model. Simply by putting it into my model I get this error. Strangely the RunningResettable-block turns yellow when I copy it - indicating that it has a different sampling rate I suppose. I don't get why this happens. I already tried to add a zero-order-hold-block behind the RunningResettable but that didn't help.
As suggest I tried to add another constant block to the R-input of the RunningResettable-Block. I tried several sampling frequencies for that Block (-1, 0, 1/fAb) but that didn't help.
Ok, finally I think that I found my failure. It seems, that the SignalGenerator outputs a continous signal (black). Because in my model there are mostly discrete signals this somehow causes errors. So when I simply add an zero-order-hold block after the signal generator everything seems to work just fine. The sampling frequency of the zero-order-hold has to be adjusted to the rest of the system.

Restarting or resetting input signal independently of simulation time and many times

I have designed a signal by using the Signal Builder block in Simulink. During the run of my simulation, the signal builder is to restart depending on the satisfaction of a condition. The condition can be satisfied many times, so, the signal should start from the beginning each time. If I want to put it in another way: You can take step input instead of the designed signal. Step input is to be reset conditionally. How can I make it?
I haven't seen such a functionality built-in Simulink (maybe the newer versions have it?) but here is a workaround:
You can simulate passed time with an integrator that has a Constant block set to 1 as input. The point is that the integrator block has a reset port which you can connect to your condition. So, when your condition becomes true, the integrator restarts a time variable from 0 (which is set in the initial conditions). Beforehand, you need to have your signal saved in something like a Table Lookup block, which outputs the signal as a function of time. Then you connect the integrator output to the Table Lookup block.
Have you tried putting your Signal Builder block in an enabled subsystem? You probably need to set the states when enabling to reset for it to work. Have a look at Create an Enabled Subsystem in the documentation for more details.

Implementation of Iterative Learning Control in Matlab/Simulink

I am working on the development of an Iterative Learning Controller for a simple transfer function.
The iterations are controlled by the external matlab loop.
But the error e(k) (k is trial number) is not updating ... as the trials increases.
Please detect the error I've commited.
Thanks and Regards.
You might have solved the problem. But as the question is still open, I would like to add something here.
First of all, you might want to check the usage of "memory" block. "The Memory block holds and delays its input by one major integration time step." The reason why the error wasn't updating is that the output of your plant produced was the same in each iteration(you defined external loop). The memory block only delayed one step of your U(K), not the whole iteration.
You might want to store the error of each iteration to workspace, and use it for the next iteration.
The memory should be a vector with a lenght of the single iteration. Not just single value. Delay block can store multiple past samples.
This guy did probably what you were looking for: https://github.com/arthurrichards77/iterative-learning-control