Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
"stop" command during m-file?

Subject: "stop" command during m-file?

From: Craig

Date: 10 Dec, 2008 00:02:01

Message: 1 of 5

I am trying to code an m-file that will control servos through a controller via USB. I've got the basic code down, and everything works fine, attached below. Basically what I am doing now is running the infinite while loop, then interrupting the file with "ctrl + c" but I would like to add code that asks the user if they would like to terminate the loop. The only way I can think to do this is to have the file ask after every iteration of the loop, which would mean the code pauses, and I cannot be there to hit continue every time (the code needs to run for around 14 hours unattended). Is it possible to have one line output to the command window asking something like "to terminate this test hit "y"" while the m-file is running so I can interrupt the loop at anytime, but if I don't hit "y" the code will skip over this and continue the while loop? I hope this explains what I am trying to do well enough, if I am unclear on anything please let me know, thanks!



function []=ServoTest()

port = 'COM10'; % Opens COM Port associated with Pololu (Check device
                    % manager and edit to correct port)

ser1 = serial(port);
set(ser1, 'InputBufferSize', 2048);
set(ser1, 'BaudRate', 9600);
set(ser1, 'DataBits', 8);
set(ser1, 'Parity', 'none');
set(ser1, 'StopBits', 1);

fopen(ser1); % Opens communication line between MATLAB and Pololu
                    % Expect a significant time delay beween fopen command
                    % and first fwrite command

while x==1

    % The fwrite command is used to send a specified servo a position
    % command. There must be one fwrite command for each servo, i.e. 6
    % servos = 6 fwrite commands. The third number in the bracket
    % specifies the servo channel and the first number in bitget()
    % specifies servo position
    
    fwrite(ser1, [128, 1, 4, 0, binvec2dec(bitget(4500,8:13)), binvec2dec(bitget(4500,1:7))]);
    fwrite(ser1, [128, 1, 4, 1, binvec2dec(bitget(4500,8:13)), binvec2dec(bitget(4500,1:7))]);
    
    % The pause command is used to temporarily stop the flow of commands to
    % the Pololu regulating the speed of a cycle
    
    pause(0.5)
    
    fwrite(ser1, [128, 1, 4, 0, binvec2dec(bitget(3000,8:13)), binvec2dec(bitget(3000,1:7))]);
    fwrite(ser1, [128, 1, 4, 1, binvec2dec(bitget(3000,8:13)), binvec2dec(bitget(3000,1:7))]);
    
    pause(0.5)
    
end

fclose(ser1); % Closes communication line between MATLAB and Pololu
delete(ser1);

Subject: "stop" command during m-file?

From: Chris

Date: 10 Dec, 2008 00:16:02

Message: 2 of 5

Whats wrong with using control+c to stop your loop? If you need to save your position before stopping it, perhaps use a save at the end of each iteration? Or is it just to make it look nice for the user?

Chris

"Craig " <csossi@vt.edu> wrote in message <ghn0tp$slr$1@fred.mathworks.com>...
> I am trying to code an m-file that will control servos through a controller via USB. I've got the basic code down, and everything works fine, attached below. Basically what I am doing now is running the infinite while loop, then interrupting the file with "ctrl + c" but I would like to add code that asks the user if they would like to terminate the loop. The only way I can think to do this is to have the file ask after every iteration of the loop, which would mean the code pauses, and I cannot be there to hit continue every time (the code needs to run for around 14 hours unattended). Is it possible to have one line output to the command window asking something like "to terminate this test hit "y"" while the m-file is running so I can interrupt the loop at anytime, but if I don't hit "y" the code will skip over this and continue the while loop? I hope this explains what I am trying to do well enough, if I am unclear on anything please let me know, thanks!
>
>
>
> function []=ServoTest()
>
> port = 'COM10'; % Opens COM Port associated with Pololu (Check device
> % manager and edit to correct port)
>
> ser1 = serial(port);
> set(ser1, 'InputBufferSize', 2048);
> set(ser1, 'BaudRate', 9600);
> set(ser1, 'DataBits', 8);
> set(ser1, 'Parity', 'none');
> set(ser1, 'StopBits', 1);
>
> fopen(ser1); % Opens communication line between MATLAB and Pololu
> % Expect a significant time delay beween fopen command
> % and first fwrite command
>
> while x==1
>
> % The fwrite command is used to send a specified servo a position
> % command. There must be one fwrite command for each servo, i.e. 6
> % servos = 6 fwrite commands. The third number in the bracket
> % specifies the servo channel and the first number in bitget()
> % specifies servo position
>
> fwrite(ser1, [128, 1, 4, 0, binvec2dec(bitget(4500,8:13)), binvec2dec(bitget(4500,1:7))]);
> fwrite(ser1, [128, 1, 4, 1, binvec2dec(bitget(4500,8:13)), binvec2dec(bitget(4500,1:7))]);
>
> % The pause command is used to temporarily stop the flow of commands to
> % the Pololu regulating the speed of a cycle
>
> pause(0.5)
>
> fwrite(ser1, [128, 1, 4, 0, binvec2dec(bitget(3000,8:13)), binvec2dec(bitget(3000,1:7))]);
> fwrite(ser1, [128, 1, 4, 1, binvec2dec(bitget(3000,8:13)), binvec2dec(bitget(3000,1:7))]);
>
> pause(0.5)
>
> end
>
> fclose(ser1); % Closes communication line between MATLAB and Pololu
> delete(ser1);

Subject: "stop" command during m-file?

From: matt dash

Date: 10 Dec, 2008 00:30:18

Message: 3 of 5

"Craig " <csossi@vt.edu> wrote in message <ghn0tp$slr$1@fred.mathworks.com>...
> I am trying to code an m-file that will control servos through a controller via USB. I've got the basic code down, and everything works fine, attached below. Basically what I am doing now is running the infinite while loop, then interrupting the file with "ctrl + c" but I would like to add code that asks the user if they would like to terminate the loop. The only way I can think to do this is to have the file ask after every iteration of the loop, which would mean the code pauses, and I cannot be there to hit continue every time (the code needs to run for around 14 hours unattended). Is it possible to have one line output to the command window asking something like "to terminate this test hit "y"" while the m-file is running so I can interrupt the loop at anytime, but if I don't hit "y" the code will skip over this and continue the while loop? I hope this explains what I am trying to do well enough, if I am unclear on anything please let me know, thanks!
>
>
>
> function []=ServoTest()
>
> port = 'COM10'; % Opens COM Port associated with Pololu (Check device
> % manager and edit to correct port)
>
> ser1 = serial(port);
> set(ser1, 'InputBufferSize', 2048);
> set(ser1, 'BaudRate', 9600);
> set(ser1, 'DataBits', 8);
> set(ser1, 'Parity', 'none');
> set(ser1, 'StopBits', 1);
>
> fopen(ser1); % Opens communication line between MATLAB and Pololu
> % Expect a significant time delay beween fopen command
> % and first fwrite command
>
> while x==1
>
> % The fwrite command is used to send a specified servo a position
> % command. There must be one fwrite command for each servo, i.e. 6
> % servos = 6 fwrite commands. The third number in the bracket
> % specifies the servo channel and the first number in bitget()
> % specifies servo position
>
> fwrite(ser1, [128, 1, 4, 0, binvec2dec(bitget(4500,8:13)), binvec2dec(bitget(4500,1:7))]);
> fwrite(ser1, [128, 1, 4, 1, binvec2dec(bitget(4500,8:13)), binvec2dec(bitget(4500,1:7))]);
>
> % The pause command is used to temporarily stop the flow of commands to
> % the Pololu regulating the speed of a cycle
>
> pause(0.5)
>
> fwrite(ser1, [128, 1, 4, 0, binvec2dec(bitget(3000,8:13)), binvec2dec(bitget(3000,1:7))]);
> fwrite(ser1, [128, 1, 4, 1, binvec2dec(bitget(3000,8:13)), binvec2dec(bitget(3000,1:7))]);
>
> pause(0.5)
>
> end
>
> fclose(ser1); % Closes communication line between MATLAB and Pololu
> delete(ser1);

THis is a common question though I'm not sure if it has a good answer or not. Here's one solution I just came up with:

Make a figure with handle h and in it put a button that says "stop" and whose callback is to close the figure h. Then make the condition on your while loop be
while ishandle(h)

This way when they click the stop button, the figure disappears and the code stops.

sample code:

f=figure
u=uicontrol('string','stop','callback','delete(get(gcbo,''parent''))');

while ishandle(f)
    disp('hello world')
    pause(0.1)
end

(which you can pretty up to make it look more like a standard dialog box.)

Subject: "stop" command during m-file?

From: Craig

Date: 10 Dec, 2008 00:32:22

Message: 4 of 5

It's mostly just to make the code look nice. I'm going to eventually put this code in my research paper, and I'd rather it have a nice cut-off system versus the crude "ctrl + c" method. I also have to reset the servo controller device when I use control+c, and I think I can avoid that if I shut the program down properly.

Subject: "stop" command during m-file?

From: Craig

Date: 10 Dec, 2008 00:34:02

Message: 5 of 5

Thanks matt, this sounds like a great solution, I will give it a shot!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us