How can I change a signal only if a condition is met, and otherwise return the signal's old value without having the signal as an input? - simulink

I am working on converting code into simulink blocks, and this issue is coming up repeatedly in this task.
Take this pseudocode:
int number = 0; // Global variable
updateNumber()
{
if(externalCondition)
then number++; // The number is edited based on an external condition.
// else do nothing;
}
main
{
for(true)
{
updateNumber();
}
}
We have a global variable, with a default value of 0.
Then, when we call a function updateNumber that variable is changed based on an external condition.
I have to create a Simulink model for the updateNumber function, with no inputs, but with number as its output.
number should only change its value if the condition is met, else it shall keep its previous value. Just like in the pseudocode.
I suspect I can use enabled subsystems to achieve this, but I just can't see how this can be done in Simulink.
The main problem is: how can I not change the value of the number when the condition is not met? Can I have an if block that outputs "something or nothing" based on the condition?
This is what I've tried, but even when the enabled subsystem's signal is set to false the subsystem runs and sets the value to 15.
Here's another illustration of what I'm trying to achieve.

Stateflow should solve it. Just increment number everytime condition is met

Related

"While loop" not working in my Anylogic model

I have the model which I posted before on Stack. I am currently running the iterations through 5 Flow Chart blocks contain enter block and service block. when agent fill service block 5 in flow chart 5, the exit block should start to fill block one and so on. I have used While infinite loop to loop between the five flow chart blocks but it isn't working.
while(true)
{
for (Curing_Drying currProcess : collection) {
if (currProcess.allowedDay == (int)time(DAY)) {
currProcess.enter.take(agent);
}
}
if (queue10.size() <= Throughtput1){
break;
}
}
Image for further illustration 1
Image for further illustration 2
Wondering if someone can tell me what is wrong in the code.
Based on the description and the pictures provided, it isn't clear why the while loop is necessary. The On exit action is executed for each Agent arrival to the Exit block. It seems that the intention is to find the appropriate Curing_Drying block based on number of days since the model start time? If so, then just iterating through the collection is enough.
Also, it is generally a good practice to provide more meaningful names to collections. Using simply collection doesn't say anything about the contents and can get pretty confusing later on.

How I can evaluate a condition on next time-step in anylogic?

I am trying to write a code for one state to another state transition where the system will first store the initial model time and then will check a condition (used while loop). It will continue to run the loop until the condition is false and when the condition is false it will record the final model time. So, my main objective is to get the total time that while loop condition is true. The problem is, I don't know how to check the while loop every 1 time step, For example, I tried "wait (1);" in place of "???" section of the below code which is not correct. Can anyone please suggest how I can do this?
My transition code as below:
...
...
initialTime=time();
while ((thisPed.inState(walking) && thisPed.fieldOfVision.contains(pedX, pedY));
{
???
}
finalTime= time();
exposureTime = finalTime - initialTime;
...
...
you can't put while statements in a model that at the same time run with time steps... to do that you have many other ways..
For instance you can generate a transition that goes from that state to the same state (internally) and generate your code there every time step.
Another option is to use a conditional transition in the same way
BUt NOT a while loop

B&R get drive serial number via MC_BR_GetHardwareInfo function block

I'm trying to retrieve the serial number from a drive using the MC_BR_GetHardwareInfo function block. Since the documentation lacks any kind of example code on this topic I'm getting nowhere.
Which information should I provide to the function block in order to get the desired serial number?
Below sample will crash in the PLC, probably because the function block requires certain pointers to be addressed:
MC_HARDWARE_INFO_REF hwinfo;
MC_BR_GetHardwareInfo(&hwinfo);
You are probably getting a page fault, because you provide the MC_BR_GetHardwareInfo function block (FUB) a wrong type, which leads to random behavior.
A function block is basically a function which requires a reference to a specific type as parameter. This type contains the actual in- and outputs which are used, internal state variables, etc. We need this, because of the synchronous execution of the code. This means unlike a function, you need to call a FUB until it is done.
Let's take a look to the help of the FUB:
Guid: 056444ea-2a15-4af6-a5ae-0675894b17d3
So the FUB needs a reference to the Axis object of which you want to know the HW info and an Execute command. It will give you some status bits, an error code and the actual data you want to have within the structure HardwareInfo of the type MC_HARDWARE_INFO_REF.
First we need to instantiate the FUB by create a variable of its type. We do this in the local *.var file of the task:
VAR
fbGetHwInfo : MC_BR_GetHardwareInfo := (0);
END_VAR
Then we call set the parameters of the FUB and call it, which might look like this:
void _CYCLIC ProgramCyclic(void)
{
//should be set by the application or in watch/monitor; now it only
//executes once
fbGetHwInfo.Execute = 1;
//reference to your axis object; when using a wizard the first axis
//will be gAxis01 on default
fbGetHwInfo.Axis = (UDINT)&gAxis01;
//call the FUB
MC_BR_GetHardwareInfo(&fbGetHwInfo);
if(fbGetHwInfo.Error == 1)
{
//TODO: errorhandling
}
else if(fbGetHwInfo.Done == 1)
{
//TODO use output
//fbGetHwInfo.HardwareInfo
}
}
typically you would do this in some statemachine. Also you probably have to wait until the network to the drive is initialized. You could check this with the MC_BR_ReadDriveStatus FUB. Just for testing it should be enough to wait for some seconds after reboot and set the Execute flag in monitor mode.

Anylogic: How to conditionally close/open a valve

I am very new at Anylogic. I have a simple model, using the Fluid dynamics library: two tanks and a valve between them. The valve have to open at a rate, say X, only when the amount in the first tank, say tank_1, were twice of the amount of the second tank, say tank_2
Could you please help me with that?
Regards
You probably have more conditions on what to use with the valve depending on different things. But it's a bad idea to make something occur when then tank_1 is exactly 2 times larger than tank_2... Instead, create a boolean variable that tells you if the current situation is that tank_1 is over or below 2*tank_2. Let's call it "belowTank2". I will assume tank_1 is below 2*tank_2 in the beginning of the simulation. so belowTank2 is true.
Then you create an event that runs cyclically every second or even more often if you want and you use the following code:
if(belowTank2){
if(tank_1.amount()>2*tank_2.amount()){
valve.set_openRate(0.1);
belowTank2=false;
}
}else{
if(tank_1.amount()<2*tank_2.amount()){
valve.set_openRate(0.3);
belowTank2=true;
}
}
So this means that whenever tank_1 surpases 2*tank_2, it will trigger the rate change on the valve. And it will trigger again a change when it's below 2*tank_2

Is it possible to stop or interrupt a code in MATLAB if a condition is reached and end the simulation of the program code ?

Is it possible to stop or interrupt a code in MATLAB if a condition is reached and end the simulation of the program code ? eg I have a loop that involves calculating a parameter and the moment the value becomes a complex no. I would like my code to stop executing and return the value of the counter at which the parameter value became complex.
Yes, it is possible. If you want to exit your script, you can use this:
if complex(parameter)
disp(counter);
return;
end
If you want to exit a function and return the value of the counter to the caller, you can use this:
if complex(parameter)
return(counter)
end
If you just want to break out of a loop, use this:
for ...
if complex(parameter)
break;
end
end
print(counter)