I asked ChatGPT to give me Schedule Timer in Function Block Structured Text.
FUNCTION_BLOCK FB_ScheduleTimer
VAR_INPUT
StartDateTime: DT; // Start date and time
Duration: TIME; // Duration to keep the output on
CurrentDateTime: DT; // Current date and time
END_VAR
VAR_OUTPUT
TimerOutput: BOOL; // Output of the timer
END_VAR
VAR
EndDateTime: DT; // End date and time
TimerStarted: BOOL; // Flag indicating if the timer has started
END_VAR
// Initialize the end date and time
IF NOT TimerStarted THEN
EndDateTime := StartDateTime + Duration;
TimerStarted := TRUE;
END_IF
// Check if the current date and time is within the scheduled period
IF CurrentDateTime >= StartDateTime AND CurrentDateTime < EndDateTime THEN
TimerOutput := TRUE;
ELSE
TimerOutput := FALSE;
TimerStarted := FALSE; // Reset the timer for next activation
END_IF
END_FUNCTION_BLOCK
PROGRAM Main
VAR
ScheduleTimer: FB_ScheduleTimer; // Instance of the schedule timer function block
CurrentTime: DT; // Current date and time
OutputSignal: BOOL; // Output signal from the timer
END_VAR
// Assume CurrentTime is being updated regularly by an RTC or system clock
// Call the schedule timer function block with example start date/time and duration
ScheduleTimer(
StartDateTime := DT#2024-11-14-15:00:00,
Duration := T#2h,
CurrentDateTime := CurrentTime
);
OutputSignal := ScheduleTimer.TimerOutput;
I compare my implementation from
plc.st file that look like this. (They are the same)
Is the data type DT = current time ??
I couldn't get it to work 🙁
TYPE
LOGLEVEL : (CRITICAL, WARNING, INFO, DEBUG) := INFO;
END_TYPE
FUNCTION_BLOCK LOGGER
VAR_INPUT
TRIG : BOOL;
MSG : STRING;
LEVEL : LOGLEVEL := INFO;
END_VAR
VAR
TRIG0 : BOOL;
END_VAR
IF TRIG AND NOT TRIG0 THEN
{{
LogMessage(GetFbVar(LEVEL),(char*)GetFbVar(MSG, .body),GetFbVar(MSG, .len));
}}
END_IF;
TRIG0:=TRIG;
END_FUNCTION_BLOCK
FUNCTION_BLOCK FB_ScheduleTimer
VAR_INPUT
StartDateTime : DT;
Duration : TIME;
CurrentDateTime : DT;
END_VAR
VAR_OUTPUT
TimerOutput : BOOL;
END_VAR
VAR
EndDateTime : DT;
TimerStarted : BOOL;
END_VAR
(* Initialize the end date and time *)
IF NOT TimerStarted THEN
EndDateTime := StartDateTime + Duration;
TimerStarted := TRUE;
END_IF;
(* Check if the current date and time is within the scheduled period *)
IF CurrentDateTime >= StartDateTime AND CurrentDateTime < EndDateTime THEN
TimerOutput := TRUE;
ELSE
TimerOutput := FALSE;
TimerStarted := FALSE; (* Reset the timer for next activation *)
END_IF;
END_FUNCTION_BLOCK
PROGRAM main
VAR
ScheduleTimer : FB_ScheduleTimer;
CurrentTime : DT;
END_VAR
VAR
OutputSignal AT %QX0.3 : BOOL;
END_VAR
ScheduleTimer(StartDateTime := DT#2024-11-14-16:10:00, Duration := T#1h, CurrentDateTime := CurrentTime);
OutputSignal := ScheduleTimer.TimerOutput;
END_PROGRAM
CONFIGURATION Config0
RESOURCE Res0 ON PLC
TASK task0(INTERVAL := T#20ms,PRIORITY := 0);
PROGRAM instance0 WITH task0 : main;
END_RESOURCE
END_CONFIGURATION