Hello,
I hope to understand when does "While" evaluate it's logic condition in this simple program to better understand how execution is sequenced in C programs in general.
Referring to the below program that runs but have some strange behavior.
Q.1: If I don't evaluate the LogicVal after the "while" (**(2)) for the second time, the program starts when bit 1026 is 1, but never stops even it 1026 become 0, why?
I understand the evaluation (**(1)) should be sufficient since after completion of the while loop it will be re-evaluated in the outer (;;) forever loop,
am I missing something?
Q.2: If bit 1026=1 then immediately 1026=0 during execution of the loop, the loop does not end after completion of current 4 iterations but makes another round of 4 iterations! why?
I understand that 1026 status is re-evaluated after the completion of the 4 iterations of the while loop not before.
The logic goes that if the motor is still oscillating then the loop is still running then 1026 is not evaluated, or the program execution is not like this?
Appreciate clarifications.
Thanks.
--------------------------------------------------------
#include "KMotionDef.h"
void main()
{
int LogicVal;
for (;;)
{
LogicVal=ReadBit(1026);//***(1) test output
while (LogicVal==1)
{
LogicVal=ReadBit(1026);//***(2)
SetBit(48);//Servo Enable output of the Pulse/Dir
int i;
for (i=0; i<4; i++)
{
MoveAtVel(0,3500,4000);
while(!CheckDone(0));
Delay_sec(0.3);
MoveAtVel(0,1000,4000);
while(!CheckDone(0));
Delay_sec(0.3);
}
}
ClearBit(48);
}
}
-----------------------------------------------------------
Logic condition evluation timing & While loop
Moderators: TomKerekes, dynomotion
- TomKerekes
- Posts: 2677
- Joined: Mon Dec 04, 2017 1:49 am
Re: Logic condition evluation timing & While loop
At the beginning of the loopwhen does "While" evaluate it's logic condition
nothing in the loop would ever change LogicValQ.1: If I don't evaluate the LogicVal after the "while" (**(2)) for the second time, the program starts when bit 1026 is 1, but never stops even it 1026 become 0, why?
It will never leave the while loop so the outer loop will never re-executeI understand the evaluation (**(1)) should be sufficient since after completion of the while loop it will be re-evaluated in the outer (;;) forever loop,
am I missing something?
After the moves complete even though bit 1026 might be clear LogicVal is still set. So an entire loop will again be required before LogicVal will be cleared.Q.2: If bit 1026=1 then immediately 1026=0 during execution of the loop, the loop does not end after completion of current 4 iterations but makes another round of 4 iterations! why?
I understand that 1026 status is re-evaluated after the completion of the 4 iterations of the while loop not before.
The logic goes that if the motor is still oscillating then the loop is still running then 1026 is not evaluated, or the program execution is not like this?
Appreciate clarifications.
You might pretend to be the computer and yourself execute the program line-by-line and observe when LogicVal is set and when LogicVal is tested.
HTH
Regards,
Tom Kerekes
Dynomotion, Inc.
Tom Kerekes
Dynomotion, Inc.
Re: Logic condition evluation timing & While loop
Thanks Tom,
I understand that nothing in the loop changes the condition.
What I understood from your answer is that when the for ( i=...) iteration is done, execution goes to the while condition check and NOT to the begining of the program at the for (;;).
This is what you mean?
Not like a PLC where at each scan the whole function is executed from start to end.
I understand that nothing in the loop changes the condition.
What I understood from your answer is that when the for ( i=...) iteration is done, execution goes to the while condition check and NOT to the begining of the program at the for (;;).
This is what you mean?
Not like a PLC where at each scan the whole function is executed from start to end.
- TomKerekes
- Posts: 2677
- Joined: Mon Dec 04, 2017 1:49 am
Re: Logic condition evluation timing & While loop
Yes that is correct. Not like ladder logic.
Regards,
Tom Kerekes
Dynomotion, Inc.
Tom Kerekes
Dynomotion, Inc.