Why is the pause function not activating?

12 次查看(过去 30 天)
I don't understand why this code isn't pausing normally. I'll run this section of the script and it'll speed through without pausing, and then I'll type in the same parts of the code piece-by-piece into the command window and pause WILL work.
Short background on the code: I'm controlling two simple servos using Arduino packages.
stepTime = goalTime/(length(invPath(:,1))-1) % This value is about 0.5
lastq = qStart;
step = 0;
tic
for i=1:length(invPath(:,1))
step = step + 1;
% Simultaneously Map on Plot
simMoveRR(.03,lastq,qend,base,L1,L2); % This plots the results to mock the robot.
lastq = qend;
%Send Command to Arduino; ServoInputVal converts degree input to 0-1 value.
writePosition(s1, ServoInputVal(rad2deg(invPath(i,1)),s1InputRange,s1ArmRange));
pause(stepTime/2); % Doesn't work
writePosition(s2, ServoInputVal(rad2deg(invPath(i,2)),s2InputRange,s2ArmRange));
pause(stepTime/2); % Doesn't work
toc
if mod(step,20)==0; % An attempt to pause the code to review the position.
pause % This doesn't work either.
end
end
EDIT: fixed mod input ; still doesn't explain problem with pause. I'm running MATLAB R2016a

回答(4 个)

Stephen23
Stephen23 2017-8-16
编辑:Stephen23 2017-8-16
The reason is because
mod(step,20)
will never output the value 20, and so your comparison
mod(step,20)==20
will never be true. You need to review how the modulo function is defined. Or simply try it yourself:
>> mod(15:25,20)
ans =
15 16 17 18 19 0 1 2 3 4 5
Do you see any 20's in the output?
To make it work, perhaps either of these will do what you want:
mod(step,20)==19
mod(step,20)==0
  4 个评论
Grant Jones
Grant Jones 2017-8-16
Not in a place to run the script at the moment, but stepTime equals 0.5 . I checked this yesterday to be sure.
I agree that it's not that noticeable in a single iteration, but I'm watching the 'toc' output and I'm completing iterations in the FOR loop at about 10Hz which is ~5x too fast.
Thanks for the support so far.
Jan
Jan 2017-8-18
stepTime = goalTime/(length(invPath(:,1))-1) % This value is about 0.5
Perhaps this is a wrong assumption? Test this:
disp(stepTime / 2)
tic
pause(stepTime/2); % Doesn't work - sure?
toc
Note: Prefer size(invPath,1) instead of length(invPath(:,1)).

请先登录,再进行评论。


John BG
John BG 2017-8-17
Hi Grant Jones
step is a built in function
Apparently, from the help file it looks like it has to be called with at least 2 arguments, for instance
step(obj,1)
Yet when one keys in just step, MATLAB returns
step
Here is an example of how the function step works:
Consider a randomly generated stable Transfer Function Model:
of the form G(s)=num(s)/den(s):
num =
0 -0.1022 0.0316 0.1934 -0.1795 0.1620
.
MATLAB even generates a default damping plot
.
So why don't you try to rename your variable step to something else?
.
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG

Divyabharathi V
Divyabharathi V 2019-6-26
Try this command before using pause in your code
pause on;

John BG
John BG 2017-8-18
编辑:John BG 2017-8-19
Hi Grant Jones
Again, pause is a MATLAB command, shouldn't you be sending the pause wrapped in a Send-To-Arduino command?
When you say that the pause works, keying in single lines, isn't it that you are pausing the MATLAB application, that in turn pauses the connection to the Arduino, so so it looks as if working, but it isn't really?
When you run the script, the lines containing pause commands, are they really sent to your board?
.
  1 个评论
Walter Roberson
Walter Roberson 2017-8-19
"shouldn't you be sending the pause wrapped in a Send-To-Arduino command?"
No. It is not the arduino that needs to be paused. Positioning control commands are being sent by MATLAB, and the arduino needs to react to them, and then after a brief time MATLAB needs to send a different command.
It would probably be possible to rewrite portions of the work as an Arduino sketch that received data from MATLAB and executed the appropriate I/O commands, but that is not necessary.

请先登录,再进行评论。

类别

Help CenterFile 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!

Translated by