loop in realtime arduino reading
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hi
I want to read a value of the variable each 0.02 seconds I write this code:
c(1)=readCount(encoder);
for i=2:100
c(i)=readCount(encoder)
pause(0.02)
end
actually I want to read the count of the encoder for the loop (i=2:100)
each 0.02 secound
채택된 답변
Walter Roberson
2019년 3월 23일
1 개 추천
Use a timer object with FixedRate . Be sure to pre-allocate c.
You might have some difficulty reaching 50 readings per second over the USB connection, unless you use a bit of code or a sketch on the arduino side to keep sending out the values instead of waiting for general commands. It is not uncommon for people to report they only get about 40 samples per second unless they run more dedicated code on the arduino.
댓글 수: 10
john white
2019년 3월 24일
so how I should write the code of a timer object in matlab?
Walter Roberson
2019년 3월 24일
편집: Walter Roberson
2019년 3월 24일
function readout_fcn = pqrst(encoder)
c = zeros(1,100); %shared variable
t = timer(...., 'TimerFcn', @store_value)
cidx = 0;
start(t)
readout_fcn = @retrieve_info;
function store_value(hobject, event)
this_value = readCount(encoder);
cidx = cidx + 1;
c(cidx) = this_value;
end
function [current_c, current_idx, timeobj] = retrieve_info
current_c = c;
current_idx = cidx;
timeobj = t;
end
end
This will return the handle to a function. The function has three return parameters: the current buffer of values that have been read in, and the count of how many have been read in, and the timer object. Wait 2-ish seconds for the data to be read in and stored in the buffer, and then call the function to retrieve the information, and then delete the timer.
john white
2019년 4월 3일
ok thanks a lot
in this code the number of the readCount is limited isn't it?
I mean it saved 100 numbers of pulses of the encoder in c(cidx) and there is no period of time , I want to read the pulses of the encoder each 0.01 seconds
could you help me please
In that code, readCount will be called each time the timer fires. With the timer set to ExecutionMode FixedRate with a period of 0.01 seconds, it should be called as close as it can get to every 0.01 seconds. To set how many values are stored, set the timer TasksToExecute to the number of samples you want:
NumToCollect = 100;
c = zeros(1, NumToCollect);
t = timer('ExecutionMode', 'FixedRate', 'TasksToExecute', NumToCollect, 'Period', 0.01, 'TimerFcn', @store_value);
and so on.
thank you so much
for the last question
actually I want to control a dc motor , control code wants readCount s as the input and gives the torque for dc motor as the output How could I use the timer object for this loop I mean for the theorical method I used for loop and the answer is correct for example the code is:
th(1)=0
for i=1:100
K(i)=th(i)*5;
T(i)=K(i)+3;%torque
th(i+1)=th(i)+2;
end
but for the practical method I have no Idea how to use the timer object to done this loop! by your code I could readCount each 0.01 second but how to give give the readCount as the input each 0.01 second to the K ?
is it the correct way to use this code:
c(1)=readCount(encoder);
for i=1:100
K(i)=c(i)*5;
T(i)=K(i)+3;
function readout_fcn = pqrst(encoder)
NumToCollect = 100;
c = zeros(1, NumToCollect);
t = timer('ExecutionMode', 'FixedRate', 'TasksToExecute', NumToCollect, 'Period', 0.01, 'TimerFcn', @store_value);
cidx = 0;
start(t)
readout_fcn = @retrieve_info;
function store_value(hobject, event)
this_value = readCount(encoder);
cidx = cidx + 1;
c(cidx) = this_value;
end
function [current_c, current_idx, timeobj] = retrieve_info
current_c = c;
current_idx = cidx;
timeobj = t;
end
end
end
?
Walter Roberson
2019년 4월 5일
Is each value to be used as soon as it is read, or are the values to be used after they are all read?
The code I posted was to give an bit of an abstract interface to a buffered data collection queue. It assumes you might want to go away and do something else while the data is arriving, and that there is a disconnect between the samples arriving and them being processed. In practice if you want to process each sample as it arrives, you would not use this kind of structure: instead you would set the TimerFcn to be a function that reads a sample and immediately uses it.
john white
2019년 4월 5일
actually I want to used each value as soon as it is read and I don't know how to set the TimerFcn for this purpose!! could you help me please
john white
2019년 4월 6일
please help I have no idea about the TimerFcn to do my purpose!
john white
2019년 4월 7일
any way, thank you so much Walter Roberson for your help
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
