Ho realizzato un blocco funzione che effettua il calcolo della potenza istantanea in base alla frequenza di ingresso del contatto. Viene eseguita anche una media sugli ultimi 10 valori acquisiti, allego il progetto per il download.
VAR_INPUT
PTick : BOOL; { DE:”Power tick” }
TPower : REAL; { DE:”Tick power (Kw)” }
END_VAR
VAR_OUTPUT
IPower : REAL; { DE:”Instant power (Kw)” }
END_VAR
VAR
Delay : UDINT; { DE:”Delay time (uS)” }
TPulse : BOOL; { DE:”Tick pulse” }
TimeBf : UDINT; { DE:”Time buffer” }
PValues : ARRAY[ 0..9 ] OF REAL; { DE:”Power values (Kw)” }
RSeq : USINT; { DE:”Read sequencer” }
i : USINT; { DE:”Auxiliary counter” }
END_VAR
{ CODE:ST }
(* Eseguo controllo fronte salita ingresso conteggio. *)
IF (PTick = TPulse) THEN RETURN; END_IF;
TPulse:=PTick; (* Tick pulse *)
IF NOT(PTick) THEN RETURN; END_IF;
(* Fronte attivazione impulso, calcolo ritardo impulsi. *)
Delay:=SysGetSysTime(TRUE)-TimeBf; (* Delay time (uS) *)
TimeBf:=SysGetSysTime(FALSE); (* Time buffer *)
(* Incremento sequenziatore letture e calcolo potenza istantanea. *)
RSeq:=RSeq+1; (* Read sequencer *)
IF (RSeq >= 10) THEN RSeq:=0; END_IF;
PValues[RSeq]:=(3600000000.0/TO_REAL(Delay))*TPower; (* Power values (Kw) *)
(* Calcolo valore medio di potenza istantanea su 10 letture. *)
IPower:=0.0; (* Instant power (Kw) *)
FOR i:=0 TO 9 DO IPower:=IPower+PValues[i]; END_FOR;
IPower:=IPower/10.0; (* Instant power (Kw) *)