0

I am trying to determine the frequency of a pulsing input, which is tacho feedback of a pump. I have tried to create two timers which are a second each, and counts up every time there is rising edge. Based on the number of rising edges in a second, I can calculate the frequency. Sadly I couldn't find a function or function block. Any help on generating a function would be fantastic!

Also I have attached the code I have tried to use, but doesn't seem to work - I think some fresh eyes might be able to spot something I have missed.

    tonPressureTachoFeedback1(IN:=,PT:=T#1S,Q=>,ET=>);  (*Pressure Side Tacho Timer 1*)
    tonPressureTachoFeedback2(IN:=,PT:=T#1S,Q=>,ET=>);  (*Pressure Side Tacho Timer 2*)

    IF stPRessurePump.iTachoFeedbackRAW>900 (*Threshold to cross to be counted as a rising edge*)
    AND tonPressureTachoFeedback1.Q=FALSE 
    AND tonPressureTachoFeedback2.IN=FALSE THEN
        tonPressureTachoFeedback2.IN:=FALSE;
        iPressureRECount1:=iPressureRECount1+1;     (*Increment Counter*)

    ELSIF tonPressureTachoFeedback1.Q THEN
        tonPressureTachoFeedback2.IN:=TRUE;
        rPRessureRPM:=iPressureRECount1*10;         (*Generate RPM*)
    END_IF

    IF stPRessurePump.iTachoFeedbackRAW>900 (*Threshold to cross to be counted as a rising edge*)
    AND tonPressureTachoFeedback2.Q=FALSE 
    AND tonPressureTachoFeedback1.IN=FALSE THEN
        tonPressureTachoFeedback1.IN:=FALSE;
        iPressureRECount2:=iPressureRECount2+1; (*Increment Counter*)
    ELSIF tonPressureTachoFeedback2.Q THEN
        tonPressureTachoFeedback1.IN:=TRUE;
        rPRessureRPM:=iPressureRECount2*10;     (*Calculate RPM*)
   END_IF
Vinny
  • 48
  • 8
  • can I ask why you multiply the pressure count with 10? because i have to define the duty cycle of a temperature sensor for a school project but i'm stuck at the moment. i've already posted my question in following link. http://stackoverflow.com/questions/37385495/defining-duty-cycle-in-twincat-3 – nick0015 May 23 '16 at 08:53

1 Answers1

0

First of all, none of your timers are ever activated, since their IN:=TRUE both depend on the other one running out. Secondly, you seem to link the counters to having one or either timer run out, they should at least trigger on a positive transition of the raw value. Plus the counters are never reset, meaning that if they did trigger, the frequency would just keep growing.

Following code is just how I would do it, reusing some of your variables plus a new timer and a new rising_trigger. The three sections each has their own fairly independent task, as described by their comment.

I hope this helps :-)

VAR
  tonPulse1s:         TON;
  trigTachoFeedback:  R_TRIG;
END_VAR

tonPulse1s(IN := TRUE, PT := t#1s); (* Always run the timer *)
trigTachoFeedback(CLK := (stPRessurePump.iTachoFeedbackRAW > 900)); (* Always update the trigger on crossed treshold *)

IF trigTachoFeedback.Q THEN
  iPressureRECount1 := iPressureRECount1 + 1; (*Increment Counter*)
END_IF

IF tonPulse1s.Q THEN (* One second elapsed, calc RPM, restart timer and counter *)
  rPRessureRPM := iPressureRECount1 * 10;
  iPressureRECount1 := 0;

  tonPulse1s(IN := FALSE);
  tonPulse1s(IN := TRUE);
END_IF
pboedker
  • 523
  • 1
  • 3
  • 17