You are not registered yet. Please click here to register!


 
 
plc storereviewsdownloads
This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc.
 
Try our online PLC Simulator- FREE.  Click here now to try it.

---------->>>>>Get FREE PLC Programming Tips

New Here? Please read this important info!!!


Go Back   PLCS.net - Interactive Q & A > PLCS.net - Interactive Q & A > LIVE PLC Questions And Answers

PLC training tools sale

Reply
 
Thread Tools Display Modes
Old January 6th, 2018, 06:48 PM   #1
Lamboom
Lifetime Supporting Member
United States

Lamboom is offline
 
Lamboom's Avatar
 
Join Date: Mar 2014
Location: Fallbrook, Ca
Posts: 235
Is there a "Wait For" in C programming?

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
  Reply With Quote
Old January 6th, 2018, 07:53 PM   #2
Mark-
Lifetime Supporting Member
United States

Mark- is offline
 
Join Date: Nov 2008
Location: Texas
Posts: 653
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.
__________________
Good luck,

Mark
PeakHMI
  Reply With Quote
Old January 6th, 2018, 09:25 PM   #3
Peter Nachtwey
Member
United States

Peter Nachtwey is offline
 
Peter Nachtwey's Avatar
 
Join Date: Apr 2002
Location: Vancouver, WA, US
Posts: 6,758
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
__________________
"Living is easy with eyes closed, misunderstanding all you see...." Strawberry Fields Forever, John Lennon
  Reply With Quote
Old January 6th, 2018, 09:49 PM   #4
OkiePC
Lifetime Supporting Member
United States

OkiePC is offline
 
OkiePC's Avatar
 
Join Date: Mar 2005
Location: ENE of Nowhere Oklahoma
Posts: 9,982
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's not all the variables I am most concerned with, it's the undiscovered constants.
  Reply With Quote
Old January 6th, 2018, 10:04 PM   #5
Pete.S.
Member
United States

Pete.S. is offline
 
Join Date: Mar 2016
Location: Fl
Posts: 464
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.
  Reply With Quote
Old January 7th, 2018, 02:29 PM   #6
AustralIan
Member
United Kingdom

AustralIan is online now
 
Join Date: Jan 2013
Location: UK
Posts: 788
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!
  Reply With Quote
Old January 7th, 2018, 02:33 PM   #7
AustralIan
Member
United Kingdom

AustralIan is online now
 
Join Date: Jan 2013
Location: UK
Posts: 788
But in seriousness, for C waitFor is done like this

while ( !inPos )
{
// do nothing
}
  Reply With Quote
Old January 7th, 2018, 02:53 PM   #8
Lamboom
Lifetime Supporting Member
United States

Lamboom is offline
 
Lamboom's Avatar
 
Join Date: Mar 2014
Location: Fallbrook, Ca
Posts: 235
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
  Reply With Quote
Old January 7th, 2018, 03:07 PM   #9
Lamboom
Lifetime Supporting Member
United States

Lamboom is offline
 
Lamboom's Avatar
 
Join Date: Mar 2014
Location: Fallbrook, Ca
Posts: 235
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"?
  Reply With Quote
Old January 7th, 2018, 03:20 PM   #10
Mark-
Lifetime Supporting Member
United States

Mark- is offline
 
Join Date: Nov 2008
Location: Texas
Posts: 653
Quote:
Originally Posted by Lamboom View Post
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.
__________________
Good luck,

Mark
PeakHMI
  Reply With Quote
Old January 7th, 2018, 03:38 PM   #11
L D[AR2,P#0.0]
Lifetime Supporting Member
United Kingdom

L D[AR2,P#0.0] is offline
 
Join Date: Nov 2006
Location: UK
Posts: 5,581
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; 
}
  Reply With Quote
Old January 7th, 2018, 04:26 PM   #12
Lamboom
Lifetime Supporting Member
United States

Lamboom is offline
 
Lamboom's Avatar
 
Join Date: Mar 2014
Location: Fallbrook, Ca
Posts: 235
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 by Lamboom; January 7th, 2018 at 04:30 PM.
  Reply With Quote
Old January 7th, 2018, 04:51 PM   #13
Peter Nachtwey
Member
United States

Peter Nachtwey is offline
 
Peter Nachtwey's Avatar
 
Join Date: Apr 2002
Location: Vancouver, WA, US
Posts: 6,758
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.
__________________
"Living is easy with eyes closed, misunderstanding all you see...." Strawberry Fields Forever, John Lennon
  Reply With Quote
Old January 7th, 2018, 06:32 PM   #14
Lamboom
Lifetime Supporting Member
United States

Lamboom is offline
 
Lamboom's Avatar
 
Join Date: Mar 2014
Location: Fallbrook, Ca
Posts: 235
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 by Lamboom; January 7th, 2018 at 07:23 PM.
  Reply With Quote
Old January 7th, 2018, 06:38 PM   #15
AustralIan
Member
United Kingdom

AustralIan is online now
 
Join Date: Jan 2013
Location: UK
Posts: 788
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.
  Reply With Quote
Reply
Jump to Live PLC Question and Answer Forum

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Topics
Thread Thread Starter Forum Replies Last Post
Unity Pro XL Programming Questions (Timers & Input Activation) cnemeth LIVE PLC Questions And Answers 1 January 13th, 2014 06:16 AM
Best practice for programming in Step 7 glnassaf LIVE PLC Questions And Answers 10 January 3rd, 2013 04:03 AM
sequence programming seee LIVE PLC Questions And Answers 1 August 21st, 2012 10:14 PM
FB programming simulator 100 MOhm LIVE PLC Questions And Answers 3 December 1st, 2004 06:45 AM
SIEMENS S5 - Programming port damaged !! ayman metwally LIVE PLC Questions And Answers 2 August 19th, 2004 12:08 PM


All times are GMT -5. The time now is 10:34 AM.


.