Is there a "Wait For" in C programming?

Lamboom

Lifetime Supporting Member
Join Date
Mar 2014
Location
Fallbrook, Ca
Posts
350
Here’s a nice problem I haven't been able to solve using basic C programming. Ya have to use basic C when programming Red Lion G3 HMI's (which are awesome HMI's) .. but I'm not a good programmer .. sniff!

I have an actuator that will be commanded to make sine wave motion. The program that does that, in the actuator’s driver-controller, requires the actuator to first be in a zero position. Homing has already been done, so the drive knows where the actuator is. You cannot modify the sine wave program; but, you can command a Move Absolute program in the Motion Controller, which manages the drive, to take the actuator to zero, if it isn’t already there. When the actuator is at zero, it is “In position” and a digital tag will go True. If the Tag is False, the actuator is not at zero, it’s at some other position, and only the Move Absolute program can return the actuator to zero.

So, when launching this Command program, using only Basic C programming, how would you write a control program that would test the status of the actuator’s position, and finding it at zero (the “InPos” tag is therefore true) will start the Sine Wave program. If the “InPos” Tag is false, will then start the Move Absolute program, which will return the actuator to zero; and then, when the actuator is "In Position", will start the Sine Wave program.

Here’s the tricky part. It will take only a few milliseconds to start the Move Absolute program (if required) .. but how do you get the “C” control program you are writing, to wait until the actuator actually reaches zero, and the “inPos” tag goes true … which could take several seconds?

I don’t believe there is a WaitFor function in “C” , and you cannot send either the Move Absolute, or the Sine Wave program more than once .. so, if a loop is used, it can loop only one time .. presumably with conditional delays. You cannot use Sleep().. that would be cheating.. the full program must run in the least amount of time, and you do not know how far the actuator is from zero.

You could use if (! InPos) or if (InPos) to qualify what programs to run .. but, where does the "wait for In position” fit into C Programming?

Perhaps "Continue" is usable.. but where does the "wait" come from.. ? Thanks Much
 
Hello,

You would use a loop. A "For" or a "While do". Be sure to add some "time out" logic so the loop exits after X time has elapsed, regardless of the input testing.

You might need to call some house keeping logic in your loop to let other threads/processes get some time to work (like read the inputs).

These kinds of loops (for the purpose of waiting) are generally frowned upon. A better method is maintain a "state" variable in the main program loop and in each pass of the main loop call logic to determine what action, if any, to take.
 
You should do more of the coding using the RMCs users programs instead of outside.
The user code is ST and has a build in "wait for" ability.
You can also have two wait conditions in the link. One waits for the InPositionBit. The other waits for an error bit.

You can also use the transition command instead of going to 0. You give the transition command at the beginning of the program. This tells the RMC how to move to the sinewave if the Target position and the start of the sine wave are different. This is a superimposed move.

if the LinMot needs to be "Homed" you need to move back to the home position using an open loop or move velocity command. The MoveAbsolute to 0 or transition command will not work if it doesn't know where 0 is
 
You probably don't want to force the G3 program into a loop in order to create a delay. Doing so will bog down other HMI activities.

I would look into using the triggers tab of your tags to initiate actions, on a data change event.

That is if you need to put this sort of logic in the HMI at all.

If I need to frequently run some logic to update something or check for conditions, I will use the On Tick field (display pages, Global tab) which will run a program once per second.

In your case, you might want to run your MoveToZero program once either at startup or from a program that runs OnTick. Set a flag tag true that says to the HMI "MovingToZero", and make sure that tag is not true in that OnTIck program to bail out before you send the command to run MoveToZero.

Then use the trigger from your tag that indicates InPosition to initiate the SineWave command.
 
It should not be done in the HMI.

You should set a bit from the HMI that triggers a little sequence in the PLC or motion controller that does what you want.

You would not use wait in the PLC either. You would check a bit and when it finally is set you do something.
 
Just read over this again, and it smells of homework.

The correct answer is to learn how to program the driver-controller, and program it in the driver-controller.

Make sure you document what you have done, both in the code, and in some other document.

Good luck!
 
WOW .. what great replies .. Thanks all of you!
To start with, I didn't really want to use the HMI to do this.. I just wondered if it could be done... The Red Lion is a great machine.. I really love it .. and, it's talking to Peter's RMC75E-Q2 (that was 'sposed to be a secret) .. I'm soooo spoiled by Delta's Motion Control programming .. it's just too damn easy to do really complicated things. I already have about 25 user programs in the RMC.. but, early on.. i got hooked into doing things the easy way with Delta Motion.. because, it has many useful Command functions already built in. Frankly, so does the Red Lion... an' there's the rub. Many of my RMC "user programs" already make certain of the actuator's initial position, and correct it if necessary, before starting a positioning sequence. I just wanted to see if the HMI could do something similar when using the RMC's Command routines. It looks like it can't .. because it must follow the practical limits of "C" Programming ... which apparently doesn't have the ability to "Wait" for anything ... without risking unintended consequences.

Mark I did try a FOR and a WhileDo .. but, had some trouble resolving the delays. and your point about creating delays in loops is now understood .. I also didn't understand how to limit the command to run a particular program only "one time" in the loop.

Peter You are always precise .. I have learned all those things from you in the past .. thanks.. :) I'm not well versed in all the tricks in C ... I knew if there was a way to do this .. you guys would say so

OkiePC I'm going to look into the triggers tab .. just for fun... I don't really "need" to do this from the HMI .. just sorta wanted to try .. I'm getting the drift it was a bad idea. Your suggestions are interesting and new info.. thanks.

Pete.S. I get the message :) DUH! You are right .. but .... because this is a cool HMI, i had to try :) I just obviously don't have enough understanding about "C" I tried to do this with a Siemens PLC couple of years back .. things worked OK.. but very limited .. Then Peter Nachtway stuck his persuasive thoughts into my head.. and my life got complicated .. yet, no regrets.

I will close this thread as "Resolved" Thanks all.. very much, Regards, Michael lambert
 
Hi Australian!
thanks for the reply... an' nope it's not homework.. but it should be :) I tried to make it sound like a final exam for students of C programming.. hee hee..

Hmmmmm:

While (!InPos)
{
//do nothing

}

This might be OK.. the longest time required to place the actuator on zero would be only a few seconds ... This could work... :)

I like doing nothing... how hard could that be?


Anyone have any comments on this "solution"?
 
Anyone have any comments on this "solution"?

Worse than horrible. :) What happens if the input never achieves the desired state? A frozen program. A counter or timer to breakout of the loop would be advised.
 
I would use switch (case) statement so your program is always executing and you can monitor for aborts etc.

e.g.
Code:
switch Step {

case 0: if start then 
        issue Home();
        Step:=1;
        Break;

case 1: if Homed then
        issue MoveAbsoluteToZero();
        Step:=2;
        Break;

case 2: if Inpos then 
        issue Sinewave();
        Step:=3;
        Break;

case 3: if Abort then 
        issue Stop;
        Step:=4;
        Break;

case 4: if Stopped then 
        Step:=0;
        Break; 
}
 
Hmmmm.. To bad.. I liked Australian's idea.. oh, the unintended consequences!

LD I was going to use switch, but couldn't figure a way to do it .. don't have much experience with that one.. You can see a lot of argument against using the HMI here.. even if it could be done. There's a thing called "Best Practices" of which I have little clue .. but the experienced people on this Forum have great insight. I'm going to try your idea just for fun and experience ... I'll let you know if there are any unintended consequences .. :) thanks.

PS... By The Way .. even tho I have checked the notify me if anyone posts to this thread.. I have not received any email notices of postings.. sniff!
 
Last edited:
This is nuts! It should have been days ago

Lamboom is using a RMC75.
It has fantastic debugging features.
Turn the volume down before running
http://deltamotion.com/peter/Videos/Demo75.mp4
There is a log that logs all calculations, state transitions and commands at the motion controller scan time. 1ms by default.
There is a built in multitasking state machine. The red marker indicates which state is active.
There a are plots that record up to 16 channels of info at the 1 ms scan time.

Lamboom, show us the error log or graph for when the linmot doesn't get to 0. It that simple.

I agree with the comments above about not using the HMI for control. The RMC can do all the control. Most HMIs do not have multi tasking built in.

AustralIan, your technique is not good. There needs to be a yield() function that saves the state of the current blocked task and restores the state of the next free task and runs it.
 
Sorry Peter .. you are thinking about another problem I thought I had, and asked Delta about it before I figured out I had forgotten that some of my programs actually were command functions like Sine Start (72) .. There is an error if you send that command from the HMI and the actuator isn't at zero. It's quite normal, but is bad programming.

This thread is not about that. Here, I just wanted to see if the Red Lion could use it's programming ability in basic "C" .. to avoid making a user program in the RMC (making a new user program was what I was going to do anyway.. and I knew you would yell at me for not doing it right first time .. sigh)

There is no Error, no Fault if things are done correctly with an RMC user program, and I know how to do that (thanks to your manuals & Support team)

I was just exploring the Red Lion's possibilities.. or rather, the possibilities in C ... it's what we newbies do, it's not our fault :)

..The problem isn't about the LinMot not getting to zero... it always goes there, when told to. The RMC is very good at that. It's all about what happens if your HMI commands a Sine Start (20) and, the LinMot isn't at zero .. because ... perhaps, someone moved it with a Jog command :).. then sent the command for Sine, Start...
 
Last edited:
100% agree that a waitFor(something) function, or the while(!something){} are bad programming that have a chance of never ending, and/or of holding up processing of other important tasks. I should have added more disclaimers to that reply, about how it technically answers the question but it should be used with ways of gracefully exiting the loop, yielding to other threads, etc.
 

Similar Topics

Hi everybody, not sure if anyone else has experienced this. I have been reaching out to Rockwell Techconnect this morning to try to ask a simple...
Replies
15
Views
2,347
Fairly new to using this software. Working on making some changes with a trend client for better UI. I'm currently working on a setup where I have...
Replies
7
Views
4,583
Good Morning , Sorry for the dumb question. I need to change a WAIT time from .5 to .2 . I change it with the teach pendant, but it goes back...
Replies
6
Views
3,210
Windows 7 Enterprise RS Logix V28.02 This is a software issue, that is causing me a lot of pain in RsLogix and re-installs don't seems to be...
Replies
0
Views
1,630
Hi. I'm writing an CODESYS program with ST. In diffrent parts of my main program i need my program to pause for a while and continue running...
Replies
3
Views
10,180
Back
Top Bottom