How to write code for flowchart
1 view (last 30 days)
Show older comments
I am unable to write the code for the following flowchart. The voltage need to me sampled for 40 micro second
4 Comments
Mohamad Nazir
on 23 May 2022
Edited: Walter Roberson
on 23 May 2022
A timer object can be used in this case to schedule the sampling with the desired frequency.
You can check out the documentation about timer object in the following link: https://www.mathworks.com/help/matlab//ref/timer.html
Walter Roberson
on 23 May 2022
You cannot use a timer for this purpose. The minimum period for a timer() object is 0.001 which is 25 times too slow for an event that occurs every 40 microseconds.
You could investigate using Stateflow; I do not know how quickly Stateflow can run.
Answers (2)
Walter Roberson
on 23 May 2022
period = 40e-6;
for REPEATS = 1 : 500
%box 1, 40 microsecond interrupt
last_event_time = tic;
while toc(last_event_time) < period; end %busy loop
%box 2, read input voltage
Input_Voltage = randn() * 20;
%box 3, assignments
Array_vin(sample_count) = Input_Voltage;
Alpha = Input_Voltage;
%box 4, increment
sample_count = sample_count + 1;
%box 5, test
if input_voltage >= 0 && input_voltage_previous <= 0 && sample_count > 300
%box 6, "Yes" block
Period = sample_count;
sample_count = 0;
end
%box 7, "no" box, is executed for "Yes" as well.
input_voltage_previous = input_voltage;
%and so on
end
You will find that in practice the timer could be more than 50 times too slow, but with this tic/toc approach the interval can be fast enough. You cannot use pause() for this purpose, as pause() does not accurately simulate event timing, and in practice the minimum pause is about 3 times too slow for your purposes.
You will find that in practice the array indexing is wrong. MATLAB array indices start from 1.
You will find that in practice you need to have initialized some variables that are not initialized in the flow chart.
But you did not ask for repaired logic, you asked for this flow chart to be implemented.
2 Comments
Walter Roberson
on 28 May 2022
Yes, that is a correct implementation of that aspect of the flow chart. The flowchart does not initialize sample_count, so it would have been incorrect for me to have initialized it. The flowchart has bugs, so any accurate implementation must replicate the bugs.
Communities
More Answers in the Power Electronics Control
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!