Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC
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?
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 28-Sep-12 19:52pm
Edited 28-Sep-12 20:07pm
v6

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
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!
  Permalink  
v2
Comments
arnoldxx at 29-Sep-12 7:30am
   
i'll use an ipc with an IO Card~
Joan Murt at 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)

  Print Answers RSS
0 OriginalGriff 493
1 Sergey Alexandrovich Kryukov 213
2 PhilLenoir 150
3 RyanDev 115
4 RajeeshMenoth 110
0 Sergey Alexandrovich Kryukov 6,415
1 OriginalGriff 5,856
2 CPallini 2,473
3 Richard MacCutchan 1,667
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100