How to write code for flowchart

조회 수: 1 (최근 30일)
Mahesh Abdare
Mahesh Abdare 2022년 5월 22일
댓글: Walter Roberson 2022년 5월 28일
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
Mahesh Abdare
Mahesh Abdare 2022년 5월 28일
Showing This error
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?
  댓글 수: 2
Walter Roberson
Walter Roberson 2022년 5월 24일
They said that they want to simulate without hardware.
shivakumar pambala
shivakumar pambala 2022년 5월 24일
Ok

댓글을 달려면 로그인하십시오.

커뮤니티

더 많은 답변 보기:  Power Electronics Community

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by