Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi, I have given a task to make an automotive machine, I'm stuck with the timer
ok, i.e:

When 'Object a' reach 'Station A', 'Station A' need 5 second to make the process,
after 5 second, it will take few second(define by user) before go through another station, <- here I mean refresh timer..

I got the concept on how to make the refresh timer, but I'm just not sure about the timer needed for the machine to do the process.. is that possible for me to use sleep?
C++
if(INP[0]==0)
{

1st8bits |= 0x01;
Function(address+0xc0,(unsigned char)first8bits);

Sleep(a);

if(a==5000)
{
1st8bits&=0xfe;
Function(address+0xc0,(unsigned char)first8bits);

Sleep(x*1000);
}

}

else
{
.....
}

Sorry for did not use proper English @@

Thanks

Rgds.
Arnold
Posted
Updated 28-Sep-12 20:07pm
v6

1 solution

You should know that this won't be deterministic, your program will have the priority that the OS will give to it and therefore timers can change slightly their final results.

Apart of that, if you would need to do some parallel tasks during that "timer" or sleep call, you would not be able to do it.

Then things can grow up more difficult...

What typically it is being done in the automotion world when you program a PLC or so, is to establish an infinite while loop.

Those loops take a specific amount of time (typically 10ms).

Then all the code happens there and you are in each cycle checking all the conditions and reacting to them to get the desired result... something like:

C++
while (true)
{
  if (move_activated)
  {
    // moving something to a specified/given position (without PID or other advanced positioning systems).
    if (position < destination)
    {
      move_neg = false;
      move_pos = true;
    }
    else if (position > destination)
    {
      move_pos = false; 
      move_neg = true;
    }
    else
    {
      move_pos = false;
      move_neg = false;
    }

    // reacting to a given time.
    if (bTimeCount)
    {
      if (previous_bTimeCount = false)
      {
        StartTime = GetTime();
      }

      CurrentTime = GetTime();

      if (CurrentTime - StartTime > DesiredWaitingTime)
      {
        bTimeCount = false;
        // DoWhateverYouWant...
      }

      bPreviousTimeCount = btimeCount;
    }
  /* other conditions should be placed here. */
  
  sleep(10); //10 ms so you will recheck the condition much faster than the 5 seconds you were speaking about.
}


That was a small pseudocode snippet that can give you some advice in how to proceed.

See that in case you want to process a time and meanwhile you want to control other things, what is done in the automation world is preparing a base cycle of x millisencods and react to the world conditions with that time base.

Again, you can't trust it to be real each cycle, this is what makes industrial controllers special, they are deterministic.

And let me ask you one question... How do you plan to connect the software to the real world? I mean which will be the interface to the inputs and outputs?

Good luck!
 
Share this answer
 
v2
Comments
arnoldxx 29-Sep-12 7:30am    
i'll use an ipc with an IO Card~
Joan M 29-Sep-12 7:57am    
Regarding the cycle time it is important to make it as short as possible: you will react to any input from that io card as soon as your software will be able to do it. Imagine that you would make a code fragment which execution time would take longer than the cycle time (those proposed 10ms) then you would have a problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900