I am trying to implement a pulse generator in SIMULINK that needs to know the previous 2 input values i.e. I need to know the previous 2 state values for the input signal. Also, I need to know the previous output value.
My pseudo code is:
IF !input AND input_prevValue AND !input_prevValue2
output = !output_pv
ELSE
output = output_pv;
I know that I can use legacy function importer and use C code to do this job in SIMULINK. However, the problem arises when you apply a configuration reference set to your model. The key problem is the flexibility. When you use this model somewhere else (say share it with a colleague or whoever), unless you have used a configuration reference set, you can rebuild the code (i.e. from S-Function Block) and run your model. But you cannot rebuild the code if the configuration reference set is applied.
My solution would be to implement the logic in a way that I can do the same without C functions. I tried to use the memory block in SIMULINK but apparently it doesn't do it. Does anyone know how to hold previous values for input and output in SIMULINK (for as long as the model is open)?
Have you tried with a MATLAB Function block? Alternatively, if you have a Stateflow license, this would lend itself nicely to a state chart.
EDIT
Based on your pseudo-code, I would expect the code in the MATLAB Function block to look like this
function op = logic_fcn(ip,ip_prev,ip_prev2,op_prev)
% #codegen
if ~ip && ip_prev && ~ip_prev2
op = ~op_prev;
else
op = op_prev;
end
where ip, ip_prev, ip_prev2 and op_prev are defined as boolean inputs and op as a boolean output. If you are using a fixed-step discrete solver, the memory block should work so that you would for example feed the output of the MATLAB Function block to a memory block (with the correct sample time), and the output of the memory block to the op_prev input of the MATLAB Function block.
You could (and should) test your function in MATLAB first (and/or a test Simulink model) to make sure it works and produces the output you expect for a given input.
This is reasonably straight forward to do with fundamental blocks,
Note that for the Switch block the "Criteria for passing first input:" has been changed to "u2~=0".
Related
Are exists in .Net 5 something like Array.Fill(myArray, 1) but with function.
I mean Array.Fill(myArray, myRandom.Next(10)) or another function. Actually, this code is possible but it counts function once and fills the whole array with the result.
My goal can be achieved with myArray = Array.ConvertAll(Of Integer, Integer)(myArray, Function(i) myRandom.Next(10)) but it looks a little strange and also wastes CPU time by redundant array constructor calls.
Of course, it can be simply performed with a loop, but some inline better complies with the style of the current project.
Samples are provided in VB.Net but of course, the question is related to .Net not to any particular programing language.
So, I have tried to do the same in a case of array of structures where 'char name[100]' is the only data member.
1st part of the code
2nd part of the code
The problem that I have encountered here is that once I provide a no. of names during program runtime, the output screen either does not print anything afterwards, or, prints the data without sorting it.
output screen
I did not get any compile time errors so I believe that there is a flaw in the logic.
There's this another method I tried hoping to get positive results. I type-casted characters to integers hoping that ASCII values could be used to compare. But, the results are exactly the same (undesired results).
updated logic of the 2nd part of the code
I hope somebody helps me find a way to correct this logic or provide another logic that is efficient.
the sorting logic you used is good , but from what is see the use of function's in C need's to be provided by pointers. other wise all the data inside the function will born and die inside the function , and the all the variables in the Main will stay the same as given, that explains why the output is the same as the input
try to print inside the sorting function's to see if this is the problem.
I am working with neural network for my little robot in MATLAB.
Here is the code I have,
This returns the lefts and rights' value on realtimeleft.
[realtimeleft realtimeright]=sim(myffnn,[lefts,rights]')
I would like it to return the lefts value on realtimelefts, and rights value on realtimeright.
What am I doing wrong?
The current output is:
realtimeleft =
-107.4691
96.8685
realtimeright =
[]
How can I get 96.86 to be displayed in realtimeright?
It is not working as you expect because sim (sing the legacy interface) is a function with three or more output arguments, of which the first two are time and state.
Now calling [realtimeleft realtimeright]=sim(myffnn,[lefts,rights]') you get the first two output arguments, time is assigned to realtimeleft and state is assigned to realtimeright.
There might be some crazy 1-line version, but I strongly recommend to use three lines of code, everything else will be confusing:
[realtime state]=sim(myffnn,[lefts,rights]');
realtimeleft=realtime(1);
realtimeright=realtime(2);
Instead of the last two assignments, you could also use realtime(1) and realtime(2) directly in your code.
I want to implement the following pseudo-code in SIMULINK:
q^0 = q_init, i = 0
IF q^i ! = q_final
q^(i+1) = q^i + alpha * F(q^i)
i = i + 1
ELSE return <q^0,q^1 ... q^i>
The main problem is, that F(q^i) is a function of the current q and is calculated in every iteration. When trying to impement this in SIMULINK, I run into an algebraic loop that cannot be solved.
What is the proper way of solving the problem (in SIMULINK) ?
Thank you !
Miklos
Understand what to partition inside of a Simulink simulation and what should be the output.
You'd usually cache the outputs q^0 to q^i as outputs from the model, rather than trying to get the whole model to output that.
That means the Simulink model would just be dealing with calculating q^(i+1) from q^(i) (which you store with a unit-delay block).
Regarding the if condition: It is best not to run a simulation forever unless you know it will definitely tend towards q_final. If you're not using integers that might get very hard for the == to exactly evaluate. In that case you should use (abs(q^i - final) > threshold) then, and check it will end.
Ending a simulation exactly at that point might be possible with a breakpoint, but a simpler option would to allowing the simulation to run past it and then trim your output (q^0 to q^i, then some more values) in Matlab.
What is the equivalent of the following for a C-mex s function? That is, how do I set discrete sample time of a block to the top level fixed step size in C?
My problem is, I could not find a way to "get" the top level fixed step size parameter in C-mex.
function setup(block)
block.SampleTimes = [str2double(get_param(bdroot, 'FixedStep')) 0];
Use ssGetFixedStepSize to get the base rate of the model. You may also want to ssSetErrorStatus if the that call returns 0, because that means your model is not configured with a FixedStep solver.
If, for some reason, you really want to go about fetching the information similar to what you have in the question, then you might be able to get access to it if you dig through the SimStruct pointer's fields. To do this, breakpoint in the mex file using a debugger and watch that variable.
Another option would be a few mexCallMATLAB calls to get the information you want.