Thread Subject: Probelm with matlab loops

Subject: Probelm with matlab loops

From: Lukasz K

Date: 5 Jan, 2012 10:31:09

Message: 1 of 21

Hi. I'm doing a program that control's 3 motor's. I have 3 start (start1, start2, start3) button , and each of them has a while loop. The problem is , when i try to start more than one. Becouse when I for example, press the start1 button (the loop on start1 is going on) , but when i press the start2 button, the loop in start1 is interrupted by the start2, and only start2 is working.

Anyone knows how to solve this problem?.

Subject: Probelm with matlab loops

From: Nasser M. Abbasi

Date: 5 Jan, 2012 10:54:36

Message: 2 of 21

On 1/5/2012 4:31 AM, Lukasz K wrote:

>Becouse when I for example, press the start1 button (the loop on start1 is going on) , but when
>i press the start2 button, the loop in start1 is interrupted by the start2, and only start2 is working.
>
> Anyone knows how to solve this problem?.

You mean you want the 3 loops to be running in parallel?

may be you need the parallel computing toolbox then

http://www.mathworks.com/products/parallel-computing/

good luck

--Nasser

Subject: Probelm with matlab loops

From: Lukasz K

Date: 5 Jan, 2012 11:15:09

Message: 3 of 21

"Nasser M. Abbasi" <nma@12000.org> wrote in message <je3vhb$ro8$1@speranza.aioe.org>...
> On 1/5/2012 4:31 AM, Lukasz K wrote:
>
> >Becouse when I for example, press the start1 button (the loop on start1 is going on) , but when
> >i press the start2 button, the loop in start1 is interrupted by the start2, and only start2 is working.
> >
> > Anyone knows how to solve this problem?.
>
> You mean you want the 3 loops to be running in parallel?
>
> may be you need the parallel computing toolbox then
>
> http://www.mathworks.com/products/parallel-computing/
>
> good luck
>
> --Nasser


And is there any option to do this without paralllel computing toolbox?, cause my school doesn't have it.

Subject: Probelm with matlab loops

From: Nasser M. Abbasi

Date: 5 Jan, 2012 11:28:38

Message: 4 of 21

On 1/5/2012 5:15 AM, Lukasz K wrote:
  
>
> And is there any option to do this without paralllel computing toolbox?,
> cause my school doesn't have it.

I do not know how to do it in parallel without external tools.
Matlab does not supports threads as build-in (like say Java threads).
So, Matlab runs as a single process. But I am no expert on this, may be
someone here can help you better.

Subject: Probelm with matlab loops

From: Romesh

Date: 5 Jan, 2012 11:45:09

Message: 5 of 21

"Lukasz K" wrote in message <je3u5d$qgu$1@newscl01ah.mathworks.com>...
> Hi. I'm doing a program that control's 3 motor's. I have 3 start (start1, start2, start3) button , and each of them has a while loop. The problem is , when i try to start more than one. Becouse when I for example, press the start1 button (the loop on start1 is going on) , but when i press the start2 button, the loop in start1 is interrupted by the start2, and only start2 is working.
>
> Anyone knows how to solve this problem?.

One simple solution, if I understand correctly- make a single loop that is capable of controlling all the motors. An if statement can be used within the loop to decide if a particular motor should be running. Then each button could set the variable for its motor, and all of the buttons would execute the same loop (put the loop in a function)

Subject: Probelm with matlab loops

From: Lukasz K

Date: 5 Jan, 2012 13:39:08

Message: 6 of 21

"Romesh " <r.abeysuriya@physics.usyd.edu.au> wrote in message <je42g5$99k$1@newscl01ah.mathworks.com>...
> "Lukasz K" wrote in message <je3u5d$qgu$1@newscl01ah.mathworks.com>...
> > Hi. I'm doing a program that control's 3 motor's. I have 3 start (start1, start2, start3) button , and each of them has a while loop. The problem is , when i try to start more than one. Becouse when I for example, press the start1 button (the loop on start1 is going on) , but when i press the start2 button, the loop in start1 is interrupted by the start2, and only start2 is working.
> >
> > Anyone knows how to solve this problem?.
>
> One simple solution, if I understand correctly- make a single loop that is capable of controlling all the motors. An if statement can be used within the loop to decide if a particular motor should be running. Then each button could set the variable for its motor, and all of the buttons would execute the same loop (put the loop in a function)


That would be good solution, but I didint mention that each of the loops has diffrent frequency. Somethink like that

start1
while(x==1)
/* code here*/
pause(0.1);
end;

start2
while(y ==1)
/*code here*/
pause(0.01);
end;

Subject: Probelm with matlab loops

From: dpb

Date: 5 Jan, 2012 14:02:31

Message: 7 of 21

On 1/5/2012 7:39 AM, Lukasz K wrote:
> "Romesh " <r.abeysuriya@physics.usyd.edu.au> wrote in message
> <je42g5$99k$1@newscl01ah.mathworks.com>...
...

>> One simple solution, if I understand correctly- make a single loop
>> that is capable of controlling all the motors. An if statement can be
>> used within the loop to decide if a particular motor should be
>> running. Then each button could set the variable for its motor, and
>> all of the buttons would execute the same loop (put the loop in a
>> function)
>
>
> That would be good solution, but I didint mention that each of the loops
> has diffrent frequency. Somethink like that
>
...

This is a common issue.

Think lowest common denominator...and the solution to the dilemma should
become apparent. :)

BTW, in that regard, you'll not get very good timing reproducibility at
all on a PC w/ pause(); if your logic is based on needing a very stable
time step this is unlikely to be satisfactory. All depends on what
you're actually trying to do, but derivatives, computed as differences
between samples will likely be quite noisy this way...

--

Subject: Probelm with matlab loops

From: Lukasz K

Date: 5 Jan, 2012 15:07:08

Message: 8 of 21

> This is a common issue.
>
> Think lowest common denominator...and the solution to the dilemma should
> become apparent. :)
>
> BTW, in that regard, you'll not get very good timing reproducibility at
> all on a PC w/ pause(); if your logic is based on needing a very stable
> time step this is unlikely to be satisfactory. All depends on what
> you're actually trying to do, but derivatives, computed as differences
> between samples will likely be quite noisy this way...
>

I have 6 bit's, two for each motor.

I want to achive somethink like that :

001100 (and sent to lpt)
001001
001100
001000
001101
001000
001100
001101

Setting first bit from 1 to 0 (for example 10 time per second)
Setting third bit form 1 to 0 ( let's say 20 time per second)

To send to LPT I'm using http://www.alivelearn.net/?p=691 (Xu Cui)

It's easy to run one motor, but i'm out of ideas how to run 2 or 3 in diffrent freq.

Subject: Probelm with matlab loops

From: dpb

Date: 5 Jan, 2012 15:27:27

Message: 9 of 21

On 1/5/2012 9:07 AM, Lukasz K wrote:
>> This is a common issue.
>>
>> Think lowest common denominator...and the solution to the dilemma
>> should become apparent. :)
...

> It's easy to run one motor, but i'm out of ideas how to run 2 or 3 in
> diffrent freq.

Set up a counter running in your loop on the timer at the shortest
interval needed (and as noted above w/ a LCD relationship to each
desired frequency). Use a logical test to determine when the counter
reaches the appropriate value for each motor to take its specific action
using branching as suggested earlier in the thread.

Ideal would be if could have the loop be interrupt driven but unless
it's a new feature since my version of Matlab, think you'll have to use
the resolution of the clock and pause.

I've not tried linking it to Matlab, but don't see any reason one
couldn't write a mex file and get access to the higher resolution timers
(assuming a PC here) to control the timing a little better.

--

--

Subject: Probelm with matlab loops

From: dpb

Date: 5 Jan, 2012 15:45:29

Message: 10 of 21

On 1/5/2012 9:27 AM, dpb wrote:
> On 1/5/2012 9:07 AM, Lukasz K wrote:
>>> This is a common issue.
>>>
>>> Think lowest common denominator...and the solution to the dilemma
>>> should become apparent. :)
> ....
>
>> It's easy to run one motor, but i'm out of ideas how to run 2 or 3 in
>> diffrent freq.
>
> Set up a counter running in your loop on the timer at the shortest
> interval needed (and as noted above w/ a LCD relationship to each
> desired frequency). Use a logical test to determine when the counter
> reaches the appropriate value for each motor to take its specific action
> using branching as suggested earlier in the thread.
...

cnt=zeros(3,1); % a counter for each of three
m(1)=10/dt; % a counter limit based on freq over time interval
m(2)=20/dt;
m(3)=30/dt;

while ~flg
   cnt=cnt+1; % increment each counter
   pause(0.01); % release time slice
   if mod(cnt(1),m(1)==0
     cnt(1) = 0; % reset this one
     % do M1 stuff here
   end

   if mod(cnt(2),m(2)==0
     cnt(2) = 0; % reset this one
     % do M1 stuff here
   end

% etc., etc., etc., ...

end

HTH...

(BTDT from HP BASIC on HP85 to VAX FORTRAN to embedded 6809 in Forth... ) :)

--

Subject: Probelm with matlab loops

From: Bruno Luong

Date: 5 Jan, 2012 16:35:08

Message: 11 of 21

"Lukasz K" wrote in message <je3u5d$qgu$1@newscl01ah.mathworks.com>...
> Hi. I'm doing a program that control's 3 motor's. I have 3 start (start1, start2, start3) button , and each of them has a while loop. The problem is , when i try to start more than one. Becouse when I for example, press the start1 button (the loop on start1 is going on) , but when i press the start2 button, the loop in start1 is interrupted by the start2, and only start2 is working.
>
> Anyone knows how to solve this problem?.

help timer

Bruno

Subject: Probelm with matlab loops

From: dpb

Date: 5 Jan, 2012 19:11:54

Message: 12 of 21

On 1/5/2012 10:35 AM, Bruno Luong wrote:
> "Lukasz K" wrote in message <je3u5d$qgu$1@newscl01ah.mathworks.com>...
>> Hi. I'm doing a program that control's 3 motor's. I have 3 start
...
> help timer
>
> Bruno

Ah, that _is_ a newer feature, not one I overlooked... :)

Indeed, that would make it simpler to code as long as the accuracy isn't
terribly critical (of course, the way I showed won't be any better; only
either realtime or a direct mex connection will have any chances there
over "whatever one gets").

--

Subject: Probelm with matlab loops

From: Lukasz K

Date: 6 Jan, 2012 09:17:08

Message: 13 of 21

Thank a lot for help. I must achieve max 1000 Hz (It can be 900 or little lower). In monday i try to check it on osciloscope.

Subject: Probelm with matlab loops

From: Lukasz K

Date: 8 Jan, 2012 23:03:08

Message: 14 of 21

I'm trying to do this with timer. And I have problem with TimerFcn. In short example
I created a timer

I'm using http://www.alivelearn.net/?p=691 function to write to lpt.

t = timer;
t.ExecutionMode = 'fixedrate';
t.Period = 0.01;
t.TimerFcn = @callback;
lptport = hex2dec('378');

function callback(xxx, event)
global data;
lptwrite(lptport, data);
end


And when i want start(t) I get error "Input must be noncomplex scalar double".
The problem seems to be in "lptwrite(lptport, data);" , becouse if I remove that, then it works.

but If I do for example:

remove lptwrite(lptport, data);

then start(t) ---> it works

then ----> stop stop(t)

then add "lptwrite(lptport, data)

then start(t) -----> it works

Anyone has idea how to fix that problem , or what is causing it ?


The other idea was to create a timer, who only counts (cnt = cnt +1)

t = timer;
t.ExecutionMode = 'fixedrate';
t.Period = 0.01;
t.TimerFcn = @callback;
lptport = hex2dec('378');
global cnt;

then start(t)

and in the loop for example

while(1)
 if(cnt ==10)

// CODE here///
end

end

But the while loop is stoping the timer (I thought it would work in background)

Subject: Probelm with matlab loops

From: dpb

Date: 9 Jan, 2012 00:37:15

Message: 15 of 21

On 1/8/2012 5:03 PM, Lukasz K wrote:
> I'm trying to do this with timer. And I have problem with TimerFcn. In
> short example
> I created a timer
>
> I'm using http://www.alivelearn.net/?p=691 function to write to lpt.
>
> t = timer;
> t.ExecutionMode = 'fixedrate';
> t.Period = 0.01;
> t.TimerFcn = @callback;
> lptport = hex2dec('378');
> function callback(xxx, event)
> global data;
> lptwrite(lptport, data);
> end
>
>
> And when i want start(t) I get error "Input must be noncomplex scalar
> double".
> The problem seems to be in "lptwrite(lptport, data);" , becouse if I
> remove that, then it works.
>
...

I don't have version that includes the timer object so I'm guessing some...

At the point above, there's no definition for data apparent it appears
as though it would be empty.

Also, what's 'xxx' in the callback() call???

--

Subject: Probelm with matlab loops

From: Lukasz K

Date: 9 Jan, 2012 09:44:08

Message: 16 of 21

 
> I don't have version that includes the timer object so I'm guessing some...
>
> At the point above, there's no definition for data apparent it appears
> as though it would be empty.
>

I forget to write that here. But the problem is the same, if the data is for example
"data =1".

> Also, what's 'xxx' in the callback() call???
>
>
It's a vairable that for now I'm not using. I wrote it, becouse if I want create function without input argument "function callback()" it' says "to many input arguments"

Subject: Probelm with matlab loops

From: dpb

Date: 9 Jan, 2012 19:28:44

Message: 17 of 21

On 1/9/2012 3:44 AM, Lukasz K wrote:
>
>> I don't have version that includes the timer object so I'm guessing
>> some...
>>
>> At the point above, there's no definition for data apparent it appears
>> as though it would be empty.
>>
>
> I forget to write that here. But the problem is the same, if the data is
> for example "data =1".
>
>> Also, what's 'xxx' in the callback() call???
>>
>>
> It's a vairable that for now I'm not using. I wrote it, becouse if I
> want create function without input argument "function callback()" it'
> says "to many input arguments"

Need to read the documentation on timer() more closely...

As noted, my version of Matlab predates its inclusion so all I can do is
go and look at doc for current version...

Two obvious problems I see...

First, the callback function as you have called it isn't compatible w/
the definition of how a callback function must be structured. "When you
create a callback function, the first two arguments must be a handle to
the timer object and an event structure. An event structure contains two
fields: Type and Data. ..."

You have neither. Use the link in the doc on timer() to go to the
instructions on how to build the callback function properly.

The second issue I see is that when you get that working, the default
behavior for the timer() is "oneshot" and you want a repetitive one.
You need to set the property value for that on creation.

Again, note the caveat in the documentation the implementation really
isn't meant for realtime use...but you ought to be able to get something
to run. Whether it will meet the requirements is anybody's guess until
you see what performance actually is.

--

Subject: Probelm with matlab loops

From: Rune Allnor

Date: 9 Jan, 2012 20:11:48

Message: 18 of 21

On 5 Jan, 11:31, "Lukasz K" <kura...@wp.pl> wrote:
> Hi. I'm doing a program that control's 3 motor's.  I have 3 start (start1, start2, start3) button , and each of them has a while loop.  The problem is , when i try to start more than one. Becouse when I for example, press the start1 button (the loop on start1 is going on) , but when i press the start2 button, the loop in start1 is interrupted by the start2, and only start2 is working.
>
> Anyone knows how to solve this problem?.

This is rather complicated programming.
Don't expect to be able to do this in matlab.

Rune

Subject: Probelm with matlab loops

From: dpb

Date: 9 Jan, 2012 20:18:47

Message: 19 of 21

On 1/9/2012 1:28 PM, dpb wrote:
...

> First, the callback function as you have called it isn't compatible w/
> the definition of how a callback function must be structured. "When you
> create a callback function, the first two arguments must be a handle to
> the timer object and an event structure. An event structure contains two
> fields: Type and Data. ..."
>
> You have neither....

Specifically, while the function reference has two arguments, they're
not what a callback function expects them to be on entry. That's why I
asked what "xxx" was initially; it must be the handle to the timer
object but afaic(ould)t it wasn't (which you confirmed that you simply
created an argument to make the count mismatch go away, not correcting
the error of not passing the handle. And the question about data was to
hopefully prompt you to look at it as well...it must be a timer
structure; whether it's a read/write or read-only I didn't look at the
doc's in sufficient detail to tell but clearly it can't be just a plain
variable.

I'm guessing again, but my best bet would be that the data you actually
want to write to the port will either have to be generated internally to
the callback function or passed as userData rather than in the timer
internal structure object.

--

Subject: Probelm with matlab loops

From: dpb

Date: 9 Jan, 2012 20:28:41

Message: 20 of 21

On 1/9/2012 2:11 PM, Rune Allnor wrote:
> On 5 Jan, 11:31, "Lukasz K"<kura...@wp.pl> wrote:
>> Hi. I'm doing a program that control's 3 motor's. ...
...
>>
>> Anyone knows how to solve this problem?.
>
> This is rather complicated programming.
> Don't expect to be able to do this in matlab.

Nonsense. The timer object should meet the basic idea quite simply as
long as OP doesn't expect _precise_ timing.

The previously posted "one-loop" solution is also quite straightforward
and although not as "neat" a solution, many systems work quite well w/
such a simple round-robin artificial time-slicing mechanism.

Whether OP's timing requirements are too precise to be handled this way
or not is indeterminate from the information supplied.

--

Subject: Probelm with matlab loops

From: dpb

Date: 10 Jan, 2012 15:23:29

Message: 21 of 21

On 1/9/2012 2:18 PM, dpb wrote:
...

> I'm guessing again, but my best bet would be that the data you actually
> want to write to the port will either have to be generated internally to
> the callback function or passed as userData rather than in the timer
> internal structure object.
...

On reflection and for clarity, there's nothing preventing a user
function from having more than two arguments; only that there must be
two and the first two must be the handle and associated structure of the
timer object.

So, you can pass your data in an argument; it just must be at least the
third one passed to the function.

--

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com