Why is the pause function not activating?

10 views (last 30 days)
Grant Jones
Grant Jones on 15 Aug 2017
Edited: Stephen23 on 26 Jun 2019
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

Answers (4)

Stephen23
Stephen23 on 16 Aug 2017
Edited: Stephen23 on 16 Aug 2017
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 Comments
Grant Jones
Grant Jones on 16 Aug 2017
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 on 18 Aug 2017
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)).

Sign in to comment.


John BG
John BG on 17 Aug 2017
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
  1 Comment
Jan
Jan on 18 Aug 2017
Shadowing the built-in step function is not the problem here.

Sign in to comment.


Divyabharathi V
Divyabharathi V on 26 Jun 2019
Try this command before using pause in your code
pause on;
  1 Comment
Stephen23
Stephen23 on 26 Jun 2019
Edited: Stephen23 on 26 Jun 2019
+1 Well spotted. Also
pause('query')
to get the current status.

Sign in to comment.


John BG
John BG on 18 Aug 2017
Edited: John BG on 19 Aug 2017
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 Comment
Walter Roberson
Walter Roberson on 19 Aug 2017
"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.

Sign in to comment.

Categories

Find more on Arduino Hardware in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!