Timestamping with RPICT

From lechacal
Revision as of 21:14, 15 November 2020 by Administrator (talk | contribs) (→‎Using a sketch with timestamping)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This guide is for those with precise timing requirements.

Timing with the default firmware

RPICT computes power using Current/Voltage pair which we call CV pairs. The configuration defines these CV pairs for example (ct1 master, v1 master) or (ct1 slave, v1 master). The microcontroller has a list of CV pairs to compute and these are run one at a time.

Computing rms value or active Power is not instantaneous. We need to acquire a certain amount of analogue readings of the waveform before being able to compute the rms value with a good accuracy. Most signals being 50 or 60Hz we can determine the time it takes for one CV pair to complete. The parameter Ncycles in the config is the number of cycles used to compute rms or power. It is set to 20 by default. So the computing length for 50Hz is 20/50 = 0.4s. This is for a single CV pair.

Note there is a difference between a CV pair and a channel. A CV pair can generate several channels. RealPower Irms Vrms PowerFactor are all channels produced by one single CV pair. Therefore it does not take more time to output realPower Vrms Irms and PowerFactor together rather than just realPower alone. In both cases this will take 0.4s.

If we have 7 CV pairs in the config (as with the RPICT7V1) then the total time to complete a full reading of all sensors is 7*0.4 = 2.8s. We call this the full scan interval. This can be considered the biggest timing error possible between the time when the data was sent on the serial port and the time when it was actually measured and computed by the RPICT board.

The debug mode can provide the scan interval. This is useful for complex configurations. Just set debug=1 in the config. Then use the cat command to see the output. The scan interval will be shown.

How can we reduce the full scan interval?

  • The easiest way is to reduce the number of cycles. Changing it from 20 to 5 will reduce it by 4 fold. Becoming 0.7s in the above example. Now bare in mind data might become more jumpy with ncycle=5. Think of this as being similar to an average. Averaging on a long timeframe yield more stable results than on a short timeframe. The value of the right ncycle should be evaluated by the user.
  • Remove unused CV pair from the configuration. If you are using only 3 CT on a RPICT7V1 then it might be better removing the unused CV pair to optimise the full scan interval.

Polling interval. Independently from the full scan interval the data output rate is set by the polling interval set in the config. This is set to 5 seconds by default. The polling interval is like a timer and command to output data stored in memory at regular intervals.

Using a sketch with timestamping

If a more strict timing requirement is needed then we recommend to write an Arduino sketch using timestamping.

Timestamping will output the time in a form of a number of milliseconds since the arduino mcu booted. The timestamp will be added for each reading on each CV pair being measured. An example of the output is shown below.

3 T: 128498 Irms: 0.93 Vrms: 244.12 P: 3.22
1 T: 128540 Irms: 8.77 Vrms: 244.23 P: -2115.67
2 T: 128582 Irms: 0.93 Vrms: 244.16 P: 4.75
3 T: 128624 Irms: 0.93 Vrms: 244.10 P: 2.36
1 T: 128666 Irms: 8.75 Vrms: 244.01 P: -2109.45
2 T: 128709 Irms: 0.93 Vrms: 243.93 P: 2.97
3 T: 128751 Irms: 0.93 Vrms: 244.14 P: 2.00
1 T: 128793 Irms: 8.77 Vrms: 244.13 P: -2114.63
2 T: 128835 Irms: 0.92 Vrms: 244.15 P: 3.81
3 T: 128877 Irms: 0.92 Vrms: 244.01 P: 1.94
1 T: 128919 Irms: 8.76 Vrms: 244.04 P: -2110.90
2 T: 128961 Irms: 0.93 Vrms: 244.18 P: 3.87
3 T: 129003 Irms: 0.93 Vrms: 244.17 P: 3.44
1 T: 129045 Irms: 8.76 Vrms: 244.08 P: -2111.31

In this example only 3 CV pairs are used. The first number is the CV pair ID (also matches the CT number). Then comes the timestamp T in milliseconds followed by some relevant data such as Irms Vrms and realPower.

This method bring up to main advantages:

  • The time when a channel was read is precisely known.
  • The delay between reading and output is much reduced as we print data as soon as computation is done. As opposed to the default sketch explained above which only prints once we have reached the polling interval trigger.

Notice. Be aware the timestamp will resume to 0 every 50 days. If you are capturing the data with any form of programming language you will have to take this into account. The detection of this can be easily implemented by checking when the timestamp get smaller than the previous iteration.

Download the sketch RPICTlib_Example_Timestamping_01.ino

The sketch is a plain example using 3 CV pairs. These are (CT1, V1) (CT2, V1) and (CT3, V1).

The timestamp is collected with the arduino function millis(). This is run just after the computation is complete.

The configuration of the sketch is set for a RPICT7V1 Version 5. Make sure you change ADC_REF and calibration values if using a version 3 instead.

The output rate is not controlled and executed as soon as received.

Note that we have set this up with a really small number of cycles. Only 2 of them. Therefore the data output rate on the screen will look rather fast. 2 cycles computation would suit well for some near real time applications.

Further optimisation could be achieved by increasing the serial port baud rate. Also removing characters from the serial string such (T: Irms: Vrms: P:) will make the output shorter.