Cyber-Physical Systems (BIL-435), week 4 of 14

Sensors and Actuators: Sampling, Quantisation, Noise and Smart Meters

Prof. Dr. Utku Kose, Süleyman Demirel University

Overview

The models of Weeks 2 and 3 used exact values of temperatures, currents and frequencies. A real controller knows only what its sensors report, at the moments when it asks, with limited resolution and with noise. This week follows the way from a physical quantity to a number and back from a number to a physical effect [1]. It closes with the smart meters that count the energy of the campus.

flowchart LR
  A["Sensors and calibration"] --> B["Sampling"]
  B --> C["Quantisation and noise"]
  C --> D["Smart meters"]
  D --> E["Actuators"]
  E --> S["The campus microgrid, stage 4"]
  S --> FT["In the field: A reversed transformer"]
  FT --> H["Learning from the meter"]
Python code of this week

Every method on this page is also written as Python code in the Colab notebook of the week. Its code is divided into numbered sections. Each orange box on this page names the section whose code carries out what the text above the box explains. The box says what to run and what to look at in that section, and its button opens the Colab notebook at that place. The virtual laboratories, the practice parts and the self-assessment of the week are in the interactive lab. Each blue box opens one part of the interactive lab. The part ends with a link back to its box on this page, and the Colab notebook is always opened from the orange boxes here.

Python step 4: Signals as arrays

Open in Colab

Python code in the Colab notebook, right after Section 0 (setup). Open the Colab notebook and run this step cell by cell: It consists of short pieces of Python code with their explanations, a quick check and three exercises. Topics: Sampling a signal · Rounding and quantisation · Random noise · Moving averages.

The step treats a measured signal as a NumPy array [2]. It samples the voltage of the grid at regular times and rounds the samples to the levels of a converter. It then adds random noise and smooths the result with a moving average. Students who have never programmed start with the start-here notebook of the course.

The campus microgrid so far

Weeks 1 to 3 computed the balance, the dynamics and the modes of the campus with exact values. The controller of the island needs the frequency, the thermostat needs the temperature of the library, and the relays need the currents of the feeders.

This week asks how well these quantities can be measured, how often, and what the meters of the campus record.

The campus microgrid through the fourteen weeks of the course. The figure marks the stage of this week.
The campus microgrid through the fourteen weeks of the course. The figure marks the stage of this week.

Sensors: From a physical quantity to a number

A sensor turns a physical quantity into an electrical signal. A current transformer turns the current of a feeder into a small secondary current, a resistive divider scales a voltage down, and a thermistor changes its resistance with the temperature. A conditioning stage amplifies and filters the signal, and an analog-to-digital converter (ADC) turns it into a number of counts [1].

The measurement chain from a physical quantity to the value that the software sees, with the errors that enter at each part.
The measurement chain from a physical quantity to the value that the software sees, with the errors that enter at each part.

Each part adds errors. A sensor whose output differs from its data sheet has a gain error and an offset error. A two-point calibration measures the counts \(c_1\) and \(c_2\) at two known values \(x_1\) and \(x_2\) and maps every later reading onto a straight line:

\[x = a\,c + b, \qquad a = \frac{x_2 - x_1}{c_2 - c_1}, \qquad b = x_1 - a\,c_1\]

A worked example. The temperature sensor of the library gives 635 counts in an ice bath and 1243 counts at 50 °C. The gain is \(a = 50/(1243 - 635)\), which is \(50/608 = 0.08224\) K per count, and the offset is \(b = 0 - 0.08224 \times 635 = -52.22\) °C. A reading of 1000 counts then means \(0.08224 \times 1000 - 52.22 = 30.02\) °C. The values of the data sheet would give 30.59 °C, an error of more than half a kelvin.

Scene: Inside the smart meter. The smart meter of the library, opened and taken apart. The five buttons follow one measurement through the meter. The current transformer and the divider sense, the converter samples and quantises, the processor computes power and energy, and the light and the radio report. Each step lights its part and shows its numbers for the library at 21:00.

Check your understanding. A sensor reads 0 at 0 °C and 2000 counts at 100 °C after its calibration. Which temperature belongs to 500 counts?

Open in Colab

Python code in the Colab notebook, Section 1. Open Section 1 of the Colab notebook and run it. The function adc carries out the converter, and gain and offset hold the two-point calibration of this section. The code compares the readings of the data sheet with the calibrated ones at three temperatures and plots both errors.

Discuss: Trust in a reading
  1. The sensor of a room reads 21 °C. What would you need to know before you trust this number to within half a degree?
  2. How often should the sensors of the campus be calibrated again, and who notices when one drifts?

Sampling: How often to look

A computer takes a value of the signal every \(T_s\) seconds, at the sampling rate \(f_s = 1/T_s\). Between the samples, it knows nothing. Shannon showed that a signal whose frequencies stay below \(f_{max}\) can be reconstructed from its samples when [3]:

\[f_s > 2\,f_{max}, \qquad f_a = |f - k\,f_s|, \quad k = \mathrm{round}(f/f_s)\]

When the condition fails, a component at the frequency \(f\) appears at the lower alias frequency \(f_a\), and the samples alone cannot reveal it. An analog low-pass filter before the converter, the anti-aliasing filter, therefore removes the frequencies above \(f_s/2\) [1].

→ These formulas with numbers, step by step: Lab Part B, calculator, step 1.

A worked example. The voltage of the grid has 50 Hz. A converter that samples it 1000 times a second takes 20 samples in each period and shows 50 Hz. At 60 samples a second, the nearest multiple of the sampling rate is 60 Hz, so the samples show \(|50 - 60| = 10\) Hz. At 90 samples a second they show \(|50 - 90| = 40\) Hz. Only above 100 samples a second does the true frequency appear.

Animation. A sine of the chosen frequency, sampled at the chosen rate. The grey curve is the true signal, the dots are the samples, and the red curve is the slowest sine through the samples, the one that a computer sees. Try 50 Hz at 1000, 110, 90 and 60 samples a second.

Left: The voltage of 50 Hz sampled 60 times a second, and the slow sine of 10 Hz that the samples suggest. Right: The apparent frequency of 50 Hz against the sampling rate.
Left: The voltage of 50 Hz sampled 60 times a second, and the slow sine of 10 Hz that the samples suggest. Right: The apparent frequency of 50 Hz against the sampling rate.

Check your understanding. The voltage of 50 Hz is sampled 70 times a second. Which frequency do the samples show?

Open lab Part A

Interactive lab, Part A. The sampling bench in 3D: A signal generator, a converter board and an oscilloscope. Sample the voltage of the grid at several rates and resolutions and measure what the samples show. The results can be copied into the Colab notebook.

Open in Colab

Python code in the Colab notebook, Section 2. Open Section 2 of the Colab notebook and run it. The function alias carries out the formula of this section, and apparent_from_samples counts the zero crossings of the samples. Paste the results of Part A into MY_SAMPLES, and the code compares each of your measurements with the formulas.

Discuss: Fast enough?
  1. The thermostat of Week 3 reads the temperature once a minute. Is that fast enough, and how would you decide?
  2. Why can a sampled signal look perfectly smooth and still be completely wrong?

Quantisation and noise: How finely to look

A converter with \(n\) bits divides its full-scale (FS) input range \(V_{FS}\) into \(2^n\) levels. The step between two levels is \(q\), and the rounding to the nearest level adds an error between \(-q/2\) and \(+q/2\). For a fine converter, Bennett showed that this error behaves like noise with a uniform distribution [4]. The formulas give the step, the root-mean-square (RMS) value \(e_{rms}\) of the error and the signal-to-quantisation-noise ratio (SQNR):

\[q = \frac{V_{FS}}{2^n}, \qquad e_{rms} = \frac{q}{\sqrt{12}}, \qquad SQNR = 6.02\,n + 1.76\ \text{dB}\]

The SQNR holds for a sine that fills the whole range. Every further bit halves the step and adds about 6 dB.

The sensor itself adds noise, which no converter removes. Averaging \(N\) independent readings divides the standard deviation \(\sigma\) of the noise by \(\sqrt{N}\). A moving average over \(N\) readings, however, reports a value that is \((N-1)/2\) sampling periods old, so a quieter reading is always a later one:

\[\sigma_N = \frac{\sigma}{\sqrt{N}}, \qquad t_d = \frac{N - 1}{2}\,T_s\]

→ These formulas with numbers, step by step: Lab Part B, calculator, steps 2 to 5.

A worked example. A converter of 12 bits over 10 V has \(q = 10/4096 = 2.441\) mV and an error of \(2.441/3.464 = 0.705\) mV. A full-scale sine reaches an SQNR of 74.0 dB. The frequency meter of the island reads 100 times a second with \(\sigma = 25\) mHz. A moving average of \(N = 7\) readings gives \(25/2.646 = 9.45\) mHz and delays the reading by \((7 - 1)/2 \times 10 = 30\) ms.

Left: A sine through a converter of 4 bits. Right: The noise of the frequency reading against the window of the moving average, with its delay.
Left: A sine through a converter of 4 bits. Right: The noise of the frequency reading against the window of the moving average, with its delay.

Check your understanding. A converter gets two more bits. What happens to its step \(q\)?

Open lab Part B

Interactive lab, Part B. The converter and the filter, step by step: A calculator follows the grid voltage through a converter and the frequency reading through a moving average. Its sliders set the sampling rate, the bits and the window.

Open in Colab

Python code in the Colab notebook, Section 3. Open Section 3 of the Colab notebook and run it. The function quantise carries out the first formula of this section for five converters, and the second loop averages simulated frequency readings with five windows. The code prints the measured errors next to the formulas.

Discuss: Quiet or quick?
  1. The controller of Week 5 needs the frequency quickly, the bill of the campus needs it accurately. Should both use the same filter?
  2. When does a better converter help, and when only a better sensor?

Smart meters: Energy in intervals

A smart meter samples the voltage and the current many times in each period and multiplies them to the power. It integrates the energy and stores it in intervals, typically of 15 minutes. An advanced metering infrastructure collects these intervals for billing, for the operation of the grid and for the customers [5]. Its light flashes once for every fixed portion of energy, \(1/c\) kWh for a meter constant \(c\). A large load is measured through a current transformer (CT) with the ratio \(k_{CT}\), so the time \(T_p\) between two flashes gives the power:

\[P = \frac{3600\,k_{CT}}{c\,T_p}\ \text{kW}, \qquad \bar{P}_{15} = \frac{E_{15}}{0.25\ \text{h}}\]

A worked example. The meter of the library has \(c = 1000\) pulses per kWh and a current transformer of 200/5 A, so \(k_{CT} = 40\). Its light flashes every 1.80 s, which gives \(3600 \times 40 / (1000 \times 1.80) \)​\({}= 80\) kW. With the heater off, the light flashes every 7.20 s, which gives 20 kW, so the heater draws 60 kW.

The intervals hide what happens inside them. With a band of 0.2 K, the thermostat of the library runs 18 minutes and pauses 9 minutes. The demands of 15 minutes then read 80, 44, 80, 44, 68 and 56 kW and repeat every 135 minutes, while the 17 switchings of four hours disappear. Such load profiles still reveal much about the people in a building, which makes the data of smart meters a question of privacy [5].

The power of the library second by second and the demands of its intervals of 15 minutes, with the thermostat at a band of 0.2 K.
The power of the library second by second and the demands of its intervals of 15 minutes, with the thermostat at a band of 0.2 K.

Check your understanding. The light of a meter with 1000 pulses per kWh and a current transformer of ratio 40 flashes every 2.4 s. Which power does the meter see?

Open lab Part C

Interactive lab, Part C. The meter cabinet of the library in 3D: A smart meter with its light, the current transformers, a data concentrator and the contactor of the heater. Time the flashes with the heater on and off and compute its power. The results can be copied into the Colab notebook.

Open in Colab

Python code in the Colab notebook, Section 4. Open Section 4 of the Colab notebook and run it. The function power_from_pulses carries out the first formula of this section, and the code computes the demands of 15 minutes of the library. Paste the results of Part C into MY_PULSES, and the code compares your timing with the true power.

Discuss: Who reads the meter?
  1. Which questions about the campus can the interval data answer, and which only data second by second?
  2. Who should have access to the meter data of the dormitory, and why?

Actuators: From a number to a physical effect

An actuator turns a number into a physical effect. A digital-to-analog converter or a pulse-width modulation (PWM) sets an average voltage. A contactor switches a load on or off, a motor moves a valve, and the inverter of the battery sets its power. Real actuators are limited: They saturate at their largest output, they follow a new set point only at a limited rate, and many react with a delay.

\[\bar{V} = D\,V_{supply}, \qquad u(t) = \min\bigl(u_{max},\ u_0 + r\,t\bigr)\]

A worked example. The fan of the fan coil runs on 24 V with a PWM duty cycle \(D\) of 40 percent, an average of \(0.4 \times 24 = 9.6\) V. The inverter of the battery moves its power at most 50 kW a second. A new set point of 200 kW from rest therefore takes \(200/50 = 4\) s. A set point of 300 kW stops at the limit of 250 kW after 5 s.

Check your understanding. The inverter rests at 0 kW and receives a set point of 150 kW. With a ramp of 50 kW a second, when does it deliver 150 kW?

Discuss: The limits of an actuator
  1. A controller asks the battery for 300 kW. What should it do while the inverter delivers only 250 kW?
  2. Why can a slow actuator make a fast sensor useless?

Development templates of the week

The folder templates/ of the repository collects small working programs that carry out the methods of the course. Week 4 adds two of them, each with a description, its inputs and outputs and an example run.

TemplateWhat it does
signal_conditioning.pyCalibrates a sensor at two points, checks a sampling rate for aliasing, quantises a signal and filters its noise with a moving average or an exponential filter
meter_pulses.pyTurns the times of the pulses of a meter into power, integrates a power profile into intervals of 15 minutes and reports the demands

The engineering side: Sensors that fail

Sensors fail in typical ways. A reading can freeze at one value, drift slowly away, jump to absurd values or stop arriving. A controller should never trust a reading blindly, and simple plausibility checks catch most faults before they reach the control loop.

FaultWhat it looks likeA simple check
Stuck valueThe same number for a long timeA change must occur within a given time
DriftA slow growing offsetComparison with a second sensor or a model, calibration
OutlierA single absurd valueLimits of range and of rate of change
DropoutNo new readingA time stamp or a counter in every message, a timeout
In everyday life: The fuel gauge of a car

The fuel gauge of a car sits on a float that sloshes in the tank. A gauge that showed every wave would jump around, so it averages for many seconds. It therefore reacts slowly when the car is filled, the same trade between noise and delay as the frequency reading of the campus.

Discuss: Fail safe
  1. The temperature sensor of the library reports -40 °C. What should the thermostat do?
  2. Which sensor of the campus would you duplicate first, and why?

The campus microgrid, stage 4: Measuring the microgrid

Stage 4 fixes the measurements that the later weeks rely on. How long must the moving average of the frequency be, and what delay does it add? How many bits does a converter need for the frequency? What do the meters of the campus record and send?

QuestionResultModel
Window for a frequency noise below 10 mHz7 readings: 9.45 mHz, 30 ms of delayMoving average
Converter for 1 mHz over 45 to 55 Hz14 bitsQuantisation
Meter of the library, band of 0.2 KHighest demand 80.0 kW; 17 switchings hidden in four hoursSmart meter
Data of eight meters of the campus12.29 kB a dayInterval data

The delay of 30 ms is small against the margin of 1.072 s of Week 8, so the controller of Week 5 can use the quiet reading. The meters, in contrast, see the campus only every 15 minutes, which suits the bill and the planning of Week 9 but not the control of the frequency.

The campus microgrid, stage 4. Left: The frequency readings raw and through the moving average of 7. Right: The measurements of the campus. The values come from Section 5 of the Colab notebook.
The campus microgrid, stage 4. Left: The frequency readings raw and through the moving average of 7. Right: The measurements of the campus. The values come from Section 5 of the Colab notebook.

Check your understanding. Why does the stage choose a window of 7 and not of 6 readings?

Open lab Part D

Interactive lab, Part D. Stage 4 by hand: The alias of a sampled voltage, the error of a converter and the power from the pulses of a meter.

Open in Colab

Python code in the Colab notebook, Section 5. Open Section 5 of the Colab notebook and run it. The code collects the results of stage 4 in the dictionary STAGE4, and a figure draws the frequency readings raw and filtered.

In the field: A reversed current transformer

Current transformers carry polarity marks, P1 and P2 on the primary side and S1 and S2 on the secondary side [6]. A current that enters at P1 leaves the secondary at S1, so the meter sees the direction of the power. If the two secondary wires of one phase are swapped, the meter measures that phase with the wrong sign. In the cabinet of Part C, the heater and the base load draw 80 kW, 26.7 kW per phase. With one phase reversed, the meter shows \(26.7 - 26.7 + 26.7 = 26.7\) kW and registers only a third of the energy.

The secondary of a current transformer must never be opened while its primary carries current. The transformer would drive its current into the open circuit, and the voltage at its terminals could rise to a dangerous level. A test terminal block therefore has a shorting bridge for each transformer and links that separate the meter. The repair runs in a fixed order: Insert the bridge, open the links, correct the wires, close the links and remove the bridge.

Open lab Part E

Interactive lab, Part E. The field task: The meter cabinet of the library with a reversed transformer. Find the phase on the pages of the meter and correct its wires at the test terminal block in the safe order.

Your application

Every cyber-physical system starts with its sensors. The Colab notebook holds an application switch with six sensors. They measure the grid voltage, a pulse at the wrist, the pressure of a tyre, the air temperature, the vibration of a pump and the flow of water.

Open in Colab

Python code in the Colab notebook, Section 6. Open Section 6 of the Colab notebook, set APP to one of the six names in the comment and run the cell. The code checks the sampling rate, the resolution and the noise of the sensor.

Learning from the meter

A single meter at the entrance of a building measures the sum of all its loads. Non-intrusive load monitoring recovers the individual appliances from this sum, from the steps and the shapes that they leave in the measured power [7]. Modern methods learn these signatures from data with machine learning.

For the campus, the same idea turns one meter per building into a view of its heaters, chillers and chargers without extra sensors. The same power also tells when people come and go, so the method sharpens the question of privacy of the smart meters. Week 10 returns to learning from the data of the campus.

Finding the heater in the sum

Suppose that the team records the power of the library second by second for a week. A small model learns that a step of about +60 kW followed by a step of -60 kW belongs to the heater. It then counts the switchings of the thermostat from the meter alone and finds the evening on which the heater ran without a break because its sensor had failed.

Discuss: What the sum tells
  1. Which appliances of a home would be easy to find in the power of its meter, and which hard?
  2. Should a utility be allowed to disaggregate the load of its customers?

Review cards

Select a card to turn it over.

Measurement chain
Sensor, conditioning, sampling, converter and digital filter between a physical quantity and the value that the software sees [1].
Two-point calibration
\(x = a\,c + b\) from two readings at known values; it removes errors of gain and offset.
Sampling rate
\(f_s = 1/T_s\), the number of samples a second.
Sampling theorem
A signal below \(f_{max}\) can be reconstructed when \(f_s > 2 f_{max}\) [3].
Alias
A component at \(f\) appears at \(|f - k f_s|\) when the sampling is too slow.
Anti-aliasing filter
An analog low-pass filter before the converter that removes frequencies above \(f_s/2\).
Quantisation step
\(q = V_{FS}/2^n\) for a converter with \(n\) bits.
Quantisation error
Uniform between \(-q/2\) and \(+q/2\), with \(e_{rms} = q/\sqrt{12}\) [4].
Moving average
Divides independent noise by \(\sqrt{N}\) and delays by \((N - 1)/2\) sampling periods.
Meter constant
The pulses of the light of a meter per kWh; \(P = 3600\,k_{CT}/(c\,T_p)\) kW.
Demand
The energy of an interval divided by its length, typically 15 minutes [5].
Actuator limits
Saturation, rate limit and delay between a set point and its physical effect.
Plausibility check
A test of range, rate, freshness or agreement that catches a faulty reading.
Load disaggregation
Recovering appliances from the sum measured by one meter [7].

References

[1] Åström, K. J., & Wittenmark, B. (1997). Computer-Controlled Systems: Theory and Design (3rd ed.). Prentice Hall.

[2] Harris, C. R., Millman, K. J., van der Walt, S. J., Gommers, R., Virtanen, P., Cournapeau, D., Wieser, E., Taylor, J., Berg, S., Smith, N. J., Kern, R., Picus, M., Hoyer, S., van Kerkwijk, M. H., Brett, M., Haldane, A., Fernández del Río, J., Wiebe, M., Peterson, P., Gérard-Marchant, P., Sheppard, K., Reddy, T., Weckesser, W., Abbasi, H., Gohlke, C., & Oliphant, T. E. (2020). Array programming with NumPy. Nature, 585(7825), 357-362. https://doi.org/10.1038/s41586-020-2649-2

[3] Shannon, C. E. (1949). Communication in the presence of noise. Proceedings of the IRE, 37(1), 10-21. https://doi.org/10.1109/JRPROC.1949.232969

[4] Bennett, W. R. (1948). Spectra of quantized signals. Bell System Technical Journal, 27(3), 446-472. https://doi.org/10.1002/j.1538-7305.1948.tb01340.x

[5] Depuru, S. S. S. R., Wang, L., & Devabhaktuni, V. (2011). Smart meters for power grid: Challenges, issues, advantages and status. Renewable and Sustainable Energy Reviews, 15(6), 2736-2742. https://doi.org/10.1016/j.rser.2011.02.039

[6] International Electrotechnical Commission. (2012). Instrument transformers - Part 2: Additional requirements for current transformers (IEC 61869-2:2012, Edition 1.0). IEC.

[7] Hart, G. W. (1992). Nonintrusive appliance load monitoring. Proceedings of the IEEE, 80(12), 1870-1891. https://doi.org/10.1109/5.192069