Eddy_Ite
Hey,
I am trying to measure time between two rising edges with the goal of having an accuracy of 10ms or better.
My program should be able to measure a time difference of 20ms.
With the way I implemented it, the accuracy is only 50ms.

Task with an interval of for example 100ms -> resulting into a 200ms time difference between the two rising edges.
It gives me an output signal which is physically connected with an input to test the time measurement
Quote:
SR1(
 S1 := takt,
 R := NOT takt,
 Q1 => pinOutput);
takt := NOT takt;


Task with an interval of 1ms, containing the time measurement
Quote:

R_TRIG_TAKT(CLK := I_3);
RTC1(IN := FALSE);

IF R_TRIG_TAKT.Q
 THEN
  IF boFirst
   THEN (*First Edge detected*)
    tFirst := RTC1.CDT; (*save 1. time measurment*)
    boFirst := FALSE;
   ELSE (*Every edge will be used as the 1. time measurment for the next edge*)
    tSecond := RTC1.CDT; (*save 2. time measurment*)
    tDelta := SUB_DT_DT(IN1:=tSecond, IN2:=tFirst); (*the result is a Time difference)
    tDeltaREAL := TIME_TO_REAL(IN:=tDelta); (*Time converted into seconds with 4 numbers after the coma*)
    tFirst := tSecond;
  END_IF;
END_IF;


I am using an Revpi Connect using the hardware layer from https://openplc.discussion.community/post/revolutionpi-support-10181282?pid=1308954580
Is there a way to increase the accuracy? Is it more of a Software or a hardware problem?
Reply 0 0
thiagoralves
I would be a bit suspicious with a 1ms task. I don't know the internals of the Revpi driver, OpenPLC driver is only a wrapper around the built-in driver on the platform. All I know is that it uses SPI to communicate with the inputs and outputs. In that case, 1ms seems too short to real all data points, especially because the driver reads only one at a time, instead of reading them all together.

Also, you might have better results using a TON to count the time. I don't know if the RTC block has any less accurate resolution internally, but the TON (and TOF) blocks will count time based on the task timing. Just use a TON with a very large PT, and then latch the TON's input once you get the first edge so it will start counting time on ET. Once you detect the second edge you just capture the value on ET (no delta calculation required) and unlatch TON's input to reset ET.
Reply 0 0
Reply