How to write code for flowchart

3 次查看(过去 30 天)
I am unable to write the code for the following flowchart. The voltage need to me sampled for 40 micro second
  4 个评论
Mohamad Nazir
Mohamad Nazir 2022-5-23
编辑:Walter Roberson 2022-5-23
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
Walter Roberson 2022-5-23
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.

请先登录,再进行评论。

回答(2 个)

Walter Roberson
Walter Roberson 2022-5-23
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 个评论
Walter Roberson
Walter Roberson 2022-5-28
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.

请先登录,再进行评论。


shivakumar pambala
shivakumar pambala 2022-5-24
Are u writing it for any specific embedded board?

社区

更多回答在  Power Electronics Control

类别

Help CenterFile Exchange 中查找有关 Polar Plots 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by