CODESYS - the IEC 61131-3 automation software
https://forum.codesys.com/

Getting Real Time Clock Value
https://forum.codesys.com/viewtopic.php?f=1&t=9051
Page 1 of 1

Author:  asy81 [ Tue Aug 14, 2018 11:04 am ]
Post subject:  Getting Real Time Clock Value

Hello,
I am not so familiar with PLC programs and I decided to start learning CoDeSys with an excellent youtube tutorial. But the very first problem is he is using Beckhoff, so I can not use some libraries and function blocks as he did.
https://www.youtube.com/watch?v=BMrVOQJJhkg&index=11&list=PLE1CU6EebvTCJCMIUOSWgMseMaW-2k5zH

While I am trying to get real time values, I used the function block RTCLK.GetDateAndTime. But my problem is I could not figured out a way to get a real time value at second time. I even tried to use add a second function block for solving that but it did not work either. So, what am I missing and what should I do?
I am adding the project and code below so you can check it.

Attachment:
PizzaOven-RTC.project


Code:

FUNCTION_BLOCK FB_PIZZA
VAR_INPUT
   
   bLoadPizza : BOOL;
   bRemovePizza : BOOL;
   
   
END_VAR
VAR_OUTPUT
   
   bBuzzer : BOOL;
   sStateDesc : STRING;
   iTotalPizzasCooked : UDINT;
   fbPizzaTimer : TON;

END_VAR
VAR
   
   iState : DINT;
   iLastState : DINT;
   
   arrStateDescHistory : ARRAY [1..100] OF STRING;
   
   stPizzaOnDeck: ST_PIZZA;
   stPizzaInOven: ST_PIZZA;
   stPizzaAtOut: ST_PIZZA;

   fbLoad_RTRIG: R_TRIG;
   fbRemove_RTRIG: R_TRIG;
   
   arrPizzaHistory: ARRAY[0..100] OF ST_PIZZA;

   fbNT_GetTime: RTCLK.GetDateAndTime;
   dtCurrentTime : DT;
      
END_VAR

VAR CONSTANT
   standard : RTCLK.SetDateAndTime;
   stNULL_PIZZA : ST_PIZZA;
END_VAR

//Structure of ST_PIZZA

TYPE ST_PIZZA :
STRUCT
   
   bExist : BOOL;
   dtTimeEnteredOven : DT;
   dtTimeExitedOven : DT;
   tTimeInOven : TIME;
   eType : E_PIZZA_TYPES;

END_STRUCT
END_TYPE


//We can press the button 'Load Pizza' anytime we want and the
//first function block(fbNT_GetTime) counts and measure the value how much time after we did hit the button..

//It gives the true clock value even there exist a time-difference!

fbNT_GetTime();
dtCurrentTime := fbNT_GetTime.dtDateAndTime ;

IF stPizzaOnDeck.bExist AND NOT stPizzaInOven.bExist THEN
   stPizzaInOven := stPizzaOnDeck;
   stPizzaOnDeck := stNULL_PIZZA;
END_IF

fbLoad_RTRIG(CLK:= bLoadPizza);
IF fbLoad_RTRIG.Q THEN
   //bLoadPizza := FALSE;
   stPizzaOnDeck.bExist := TRUE;
   stPizzaOnDeck.eType := 0;
END_IF

fbRemove_RTRIG(CLK:= bRemovePizza);
IF fbRemove_RTRIG.Q THEN
   //bRemovePizza := FALSE;
   F_FIFO_WRITE(cbData := SIZEOF(stPizzaAtOut), pData := ADR(stPizzaAtOut), cbBuffer := SIZEOF(arrPizzaHistory),
      pBuffer := ADR(arrPizzaHistory));
   stPizzaAtOut := stNULL_PIZZA;
END_IF

CASE iState OF
   0:
      sStateDesc := 'Waiting for pizza!';
      IF stPizzaInOven.bExist THEN
         iState := 10;
      END_IF
   10:
      sStateDesc := 'Baking pizza right now!';
      fbPizzaTimer(IN := TRUE, PT:=T#5S);

      fbNT_GetTime.xExecute := TRUE;
      stPizzaInOven.dtTimeEnteredOven := dtCurrentTime;
      
      IF fbPizzaTimer.Q THEN
         fbPizzaTimer(IN := FALSE);
         
         fbNT_GetTime.xExecute := FALSE;
         
         iState := 20;
      END_IF
   20:
      sStateDesc := 'PIZZA BURNING!!';
      IF NOT stPizzaAtOut.bExist THEN
         fbNT_GetTime.xExecute := TRUE;
         stPizzaAtOut.dtTimeExitedOven := dtCurrentTime;
         
         stPizzaAtOut := stPizzaInOven;
         stPizzaInOven := stNULL_PIZZA;
         
         bBuzzer := FALSE;
         iState := 30;
      ELSE
         bBuzzer := TRUE;
      END_IF
   30:
      iTotalPizzasCooked := iTotalPizzasCooked + 1;
      fbNT_GetTime.xExecute := FALSE;
      iState := 0;
ELSE
   sStateDesc := 'Invalid State!';   
END_CASE

//Start state history
IF iLastState <> iState AND(iState<>30) THEN
   F_FIFO_WRITE(cbData := SIZEOF(sStateDesc), pData := ADR(sStateDesc), cbBuffer :=SIZEOF(arrStateDescHistory), pBuffer := ADR(arrStateDescHistory));
   //Reset for next time.
   iLastState := iState;   
END_IF


Author:  Andreas_K [ Wed Sep 12, 2018 8:07 am ]
Post subject:  Re: Getting Real Time Clock Value

Hi
I got the same issue, I only get the time once.

And here's my code, the counter is increased every 500ms,
so every 3s I want to read out the actual time and display
it at the visualization. The code runs in cyclic task with 500ms cycle time.

For some reasons, I only get in the first cycle a date and time.
The output dtu_getdt.xDone will not reset even
if dtu_getdt.Execute is set back from TRUE to FALSE.
According to the library description the outputs should be reseted upon a
falling edge. I'm not sure whether this is the reason for executing it only once or not.

Using Codesys 3.5 SP11 Patch4+
RaspberryPi Runtime


VAR
dtu_getdt : DTU.GetDateAndTime;
dtDate : DATE_AND_TIME;
END_VAR

Codepart:
IF i_count1 > 5 THEN
dtu_getdt.xExecute := TRUE;
dtu_getdt();
IF dtu_getdt.xError THEN
; // Fehlerbehandlung
END_IF
IF dtu_getdt.xDone THEN
// ohne Fehler abgeschlossen
dtDate := dtu_getdt.dtDateAndTime;
dtu_getdt.xExecute := FALSE;
END_IF
i_count1 :=0;
END_IF
i_count1 := i_count1+1;

Thanks for any comments. I'm not a very experienced user.

Author:  Josep M. Rams [ Thu Sep 13, 2018 6:11 am ]
Post subject:  Re: Getting Real Time Clock Value

You have to take out dtu_getdt() outside "icount if".

For the fb icount, execute is never false, because when you set it to false you dont call anymore dtu_getdt since execute be true.


Be carefull with fb inside ifs, fb are normally though to call it all cycles!

Sent from my Moto G (5S) Plus using Tapatalk

Page 1 of 1 All times are UTC+01:00
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/