Overview
This first week asks what a cyber-physical system is and why it needs its own engineering. It starts with the definition and with examples from daily life. It then shows why the hardware and the software of such a system work only together. A physical process, sensors that measure it, computers that decide and actuators that act on it are joined in a feedback loop. The campus microgrid, the project of the course, is introduced with its balance of power and energy. Simple models turn sunshine and wind into power, and a first loop lets the battery shave the peak of the power taken from the city grid. The campus microgrid reaches stage 1, the power balance of a whole day. The Python step introduces values, lists and decisions, and the week closes with a look at artificial intelligence in physical systems.
Week at a glance
flowchart LR A["What makes a system cyber-physical"] --> HW["Hardware and software together"] HW --> B["Energy and power"] B --> C["Models of sun and wind"] C --> D["Closing the loop"] D --> S["The campus microgrid, stage 1"] S --> FT["In the field: The sign of the meter"] FT --> H["Learning machines in physical systems"]
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 1: Values, variables, lists and decisions
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: Values, variables and units · Lists of devices and loops · Decisions with if · Functions.
The course uses Python throughout [1]. The first step stores the sizes of the devices of the campus in variables, with their units in the names. It walks through a list of buildings with a loop and makes the first decision of a controller with if. It ends with a function that computes the power taken from the city grid. Students who have never programmed start with the start-here notebook of the course.
A university campus joins a solar plant of 400 kW, a wind turbine of 250 kW and a battery of 500 kWh with an inverter of 250 kW. Five buildings and a car park with ten chargers for electric vehicles use the power. A transformer at the point of common coupling connects it to the city grid, and it can also run on its own as an island [2, 3].
The campus and its numbers are invented, so that every result can be computed and checked. Each week adds one layer: Models, controllers, sensors, networks, schedules, forecasts, defences and a digital twin.

What makes a system cyber-physical
Cyber-physical systems (CPS) are integrations of computation with physical processes. Embedded computers and networks monitor and control the physical processes, usually with feedback loops in which the physical processes affect the computations and the computations affect the physical processes [4]. The term covers power grids, cars, aircraft, medical devices, factories and buildings. What they share is that time matters: A correct answer that comes too late is a wrong answer [5].
Such systems surround daily life, often unseen. The table names a few of them, with the physical process that each one watches and the decision that its computer makes.
| System | Physical process | What the computer decides |
|---|---|---|
| Anti-lock brakes of a car | A wheel on the road | When to release the brake of a wheel that starts to lock |
| Pacemaker | The rhythm of the heart | When to send an electrical pulse |
| Heating of a building | The temperature of the rooms | How much heat each room receives |
| Traffic lights of a junction | The queues of cars | When each light changes |
| Elevator | The car in its shaft | Where to stop and when to open the door |
| The campus microgrid | The flow of power | How much the battery charges or discharges |
A computer inside a device whose main task is not computing, such as the controller of a washing machine, is called an embedded system. Lee and Seshia treat such systems from the cyber-physical point of view: What matters is the joint behaviour of the computers, the networks and the physical processes [5].
A cyber-physical system has four kinds of parts. The physical process, or plant, obeys the laws of physics, such as the flow of power in a grid. Sensors turn physical quantities into numbers, controllers compute decisions from these numbers, and actuators turn the decisions back into physical effects. A network carries the numbers between them [6, 7].
A controller can act in two ways. In an open loop, it follows a plan without looking at the result, like a timer that charges the battery every night. In a closed loop, it measures the result and corrects its action, which is called feedback [8]. With the reference \(r\), the measured output \(y\) and a gain \(K\), the simplest feedback computes the error and an action proportional to it:
\(e\) is the error and \(u\) the action that the actuator applies. Feedback corrects disturbances that no plan foresaw, such as a cloud over the solar plant. It acts only after the error has appeared, however, and a gain that is too large makes the loop overshoot.
A worked example. A thermostat keeps a room at \(r = 21\) °C and measures \(y = 19.5\) °C. With \(K = 4\) kW/K, the error is \(e = 21 - 19.5 = 1.5\) K, and the heater receives \(u = 4 \times 1.5 = 6\) kW. At \(y = 20.5\) °C, the action falls to 2 kW, and at the reference it is zero.
Scene: The loop of the campus. A model of the campus with its solar plant, wind turbine, battery container, five buildings, the car park and the transformer at the grid connection. The buttons run one turn of the loop: The meter at the transformer senses the import, the controller computes a new set point and the battery acts. A cloud over the solar plant disturbs the balance, and the loop corrects it. With the loop opened, the controller no longer looks at the meter and keeps its last set point.
Check your understanding. A street light switches on at 18:00 every day, whatever the light outside. Which kind of control is it?
- Is a smartphone a cyber-physical system? Is a washing machine? Which parts of each are physical processes?
- Name a system in which an answer that comes one second too late is wrong.
Hardware and software together
The definition joins two sides. The physical side is hardware: The process itself, its sensors and actuators, the power electronics, the computers and the cables. The cyber side is software: Algorithms that turn measurements into decisions, schedules that decide when each computation runs and protocols that carry the numbers. Neither side works alone. A controller without a meter is blind, and a meter whose readings no program uses changes nothing.
The behaviour of a cyber-physical system therefore arises from the joint dynamics of its computers, its software, its networks and its physical processes [5]. Models of such systems must capture both sides and the timing between them [9]. The table shows the two sides of the campus microgrid and the weeks of the course that take them up.
| Part of the loop | Hardware on the campus | Software that runs with it | Weeks |
|---|---|---|---|
| Physical process | Solar plant, wind turbine, battery, buildings | Models: Balances, differential equations, state machines | 1 to 3 |
| Sensing | Current transformers, meters, pyranometer, temperature sensors | Sampling, calibration, filters, estimators | 4 and 6 |
| Computing | Controller of the battery, logic controller of the switchboard, servers | Control laws, real-time scheduling, optimisation, learning | 5, 7, 9 and 10 |
| Actuation | Inverter, contactors, breakers, relays | Set points, operating modes, protection logic | 3 and 5 |
| Communication | Serial buses, Ethernet, network switches | Protocols for polling and for messages | 8 |
| The whole system | The campus microgrid | Security, digital twins, safety arguments | 11 to 14 |

Four agreements connect the two sides, and each of them can break. Units and signs: A number means something only with its unit and its direction. Timing: The software must finish before the process moves on. Limits: The software must not ask for more than the hardware can give. Faults: Hardware drifts, wears out and fails, and the software must notice it. The weeks of the course return to these agreements. Examples are the sign of the meter in the field task of this week, the offset of a sensor in Week 6 and the deadlines of Week 7.
A worked example. A controller asks the battery for the excess of the net load over a target of 400 kW, as the loop of this week will do. At a net load of 530 kW it asks for 130 kW, which the inverter of 250 kW delivers, so the import falls to 400 kW. At 700 kW it asks for 300 kW. The inverter delivers only 250 kW, so the import stays at \(700 - 250 = 450\) kW, 50 kW above the target. The software computes the right request, and the hardware decides what happens.
When a phone focuses, a small motor moves the lens, the image sensor measures the sharpness and software decides the next move. A faster processor does not help a slow motor, and a better motor does not help an algorithm that measures the sharpness in the wrong part of the image. The photo is sharp only when both sides fit.
Check your understanding. The net load is 640 kW and the target 400 kW, but the inverter delivers at most 200 kW. Where does the import settle?
- Name a device at home whose software could not be written without knowing its hardware. Which limit of the hardware must the software respect?
- A team writes the control software before the hardware is chosen. Which of the four agreements is most likely to break first?
Energy and power: The balance of the microgrid
Power is the rate at which energy flows, measured in kilowatts (kW). Energy is power accumulated over time, measured in kilowatt hours (kWh): A load of 1 kW that runs for one hour uses 1 kWh. Over a day sampled every hour, the energy is the sum of the powers times the step \(\Delta t = 1\) h.
A grid has almost no storage of its own, so the power that enters it must equal the power that leaves it at every moment [10]. For the campus, with the battery counted positive when it discharges, the city grid supplies the rest:
\(P_{load}\) is the load of the buildings, \(P_{ev}\) that of the chargers, \(P_{pv}\) and \(P_{wind}\) the power of the solar plant and the wind turbine and \(P_{bat}\) that of the battery. A positive \(P_{grid}\) is an import from the city grid, a negative one an export. The share of the demand that the campus covers itself is its self-sufficiency:
A worked example. At noon on the spring day of the course, the buildings use 575 kW and the chargers 22 kW. The solar plant delivers 348.9 kW and the wind turbine 14.4 kW. Without the battery, \(P_{grid} \)\({}= 575 \)\({}+ 22 \)\({}- 348.9 \)\({}- 14.4 \)\({}= 233.7\) kW. Over the whole day, the demand is 11,197 kWh and the import 7,570 kWh, so \(SS = 1 - 7570 / 11197 = 0.324\).
The import peaks at 585.7 kW at 19:00, when the sun has set, the wind is weak and the chargers are busy. A simple rule for the battery lowers this peak. It discharges whenever the net load exceeds a threshold and charges whenever the net load falls below a lower limit, within the limits of the state of charge (SoC). Part A of the interactive lab runs such days on a model of the campus.

Check your understanding. A building uses 50 kW for 6 hours and 20 kW for 18 hours. How much energy does it use in the day?
Interactive lab, Part A. The campus on the model table in 3D: A model of the campus with power flows on its cables and a screen with the balance. Run days with a rule for the battery and find the lowest threshold that holds the peak. The results can be copied into the Colab notebook.
Python code in the Colab notebook, Section 1. Open Section 1 of the Colab notebook and run it. The function make_day builds the balance of all 24 hours with the formula of this section, and self_sufficiency computes the share of the demand that the campus covers. Paste the results of Part A into MY_DAYS; the function run_day repeats the rule of the battery hour by hour.
- A charger is rated 22 kW, and a car battery 60 kWh. Which number tells how long charging takes, and which how far the car goes?
- Why does a contract with the city grid often charge for the peak power and not only for the energy?
Models of the sources: Sun and wind
A model is a description of a system that is simpler than the system and good enough for a purpose [5]. The power of a solar plant grows with the irradiance \(G\), the power of sunlight per square metre, and falls slightly as its cells warm up [11]:
\(P_{peak}\) is the power at the standard irradiance of 1000 W/m² and 25 °C, and \(\gamma\) is the temperature coefficient, about -0.004 per kelvin for crystalline silicon. \(T_c\) is the temperature of the cells, which the model estimates from the air temperature \(T_{amb}\) and \(k_t = 0.03\) K·m²/W.
A wind turbine starts at the cut-in speed \(v_{in}\), reaches its rated power \(P_r\) at the rated speed \(v_r\) and stops at the cut-out speed for safety. Between cut-in and rated speed, a common simple model of the power curve grows with the cube of the wind speed [12]:
→ These formulas with numbers, step by step: Lab Part B, calculator, steps 1 to 5.
A worked example. At \(G = 800\) W/m² and \(T_c = 45\) °C, the solar plant of 400 kW delivers \(P_{pv} \)\({}= 400 \times 0.8 \times (1 - 0.004 \times 20) \)\({}= 320 \times 0.92 \)\({}= 294.4\) kW. The wind turbine with \(P_r = 250\) kW, \(v_{in} = 3\) m/s and \(v_r = 12\) m/s delivers at 8 m/s \(P_w \)\({}= 250 \times (512 - 27) / (1728 - 27) \)\({}= 250 \times 485 / 1701 \)\({}= 71.28\) kW.
The cube makes the wind turbine sensitive: A wind speed that is 10 percent higher gives about a third more power. A forecast of the wind with a small error therefore gives a large error in the power, a topic of Week 10.

Check your understanding. The wind speed rises from 6 to 9 m/s. By roughly how much does the power of the turbine grow?
Interactive lab, Part B. Sun and wind step by step: The calculator of both models with three presets.
Python code in the Colab notebook, Section 2. Open Section 2 of the Colab notebook and run it. The functions pv_power and wind_power carry out the formulas of this section and repeat both worked examples. A slider panel computes the solar power for an irradiance and an air temperature of your choice.
- The model of the solar plant ignores dust, shade and the angle of the panels. For which decisions is it still good enough?
- Why does the turbine stop at the cut-out speed, although the wind holds the most power there?
Closing the loop: Peak shaving with the battery
The rule of Part A acts once an hour on the net load of the hour. A real controller acts every minute on a measurement. The meter at the grid connection reports the import, and the controller compares it with a target and sends a new power set point to the inverter of the battery. Each step adds a correction proportional to the excess [8]:
\(k\) counts the steps of the loop, \(P_{net}\) is the demand minus solar and wind power, and \(K\) is the gain. For a constant \(P_{net}\), the error \(e[k] = P_{grid}[k] - P_{target}\) is multiplied by \(1 - K\) at every step:
For \(0 < K < 1\), the error shrinks without changing its sign. \(K = 1\) removes it in one step, and \(1 < K < 2\) makes the import overshoot and oscillate around the target while it settles. For \(K \ge 2\) or \(K \le 0\), the error grows. The loop is stable only while \(|1 - K| < 1\), a first look at stability, the theme of Week 5.
→ This formula with numbers, step by step: Lab Part C, calculator, steps 1 to 4.
A worked example. The demand is 650 kW and the solar plant delivers 120 kW, so \(P_{net} = 530\) kW, and the target is 400 kW. With \(K = 0.5\), the first error is \(e[0] = 530 - 400 = 130\) kW. The battery then delivers 65 kW, and the error halves to 65 kW, then 32.5, 16.25 and 8.13 kW. After four minutes, the import is 408.13 kW.
Animation. The import at the grid connection step by step for the chosen gain \(K\), starting at 530 kW with a target of 400 kW. The bars show the error at each step, and the readouts give the factor \(|1 - K|\) and the error after four steps.

Check your understanding. The gain is \(K = 1.5\) and the first error 100 kW. What is the error after two steps?
Interactive lab, Part C. The battery controller in 3D: A test bench with the real controller and its gain knob between a meter simulator and an inverter simulator. A screen draws every step. Classify five gains, and the calculator of the loop. The results can be copied into the Colab notebook.
Python code in the Colab notebook, Section 3. Open Section 3 of the Colab notebook and run it. The function run_loop senses, computes and actuates once per step with the formula of this section, and error_after gives the error by the formula. The printout compares both for five gains. Paste the results of Part C into MY_GAINS.
- A faster loop removes the excess sooner. What could go wrong if the meter of the import were noisy?
- Why do the inverters of real batteries limit how fast their power may change?
Development templates of the week
The folder templates/ of the repository collects small working programs that carry out the methods of the course. Week 1 adds two of them, each with a description, its inputs and outputs and an example run.
| Template | What it does |
|---|---|
power_balance_day.py | Computes the hourly balance of a site from its demand, solar and wind models and a battery rule, with the energies, the peak import and the self-sufficiency |
sense_compute_actuate.py | A skeleton of a periodic control loop with a sensor, a controller and an actuator as small classes, run against a simulated plant |
The engineering side: Time, safety and scale
Three properties set cyber-physical systems apart from ordinary software. They run in real time: The plant does not wait for the computer. They are safety-critical: An error can damage equipment or hurt people. They are concurrent: Many devices act at the same moment and share a network [4, 7]. The weeks of the course take up these properties one after another.
| Property | Example on the campus | Week |
|---|---|---|
| Continuous dynamics | The frequency falls within seconds after a load step | 2 and 5 |
| Discrete modes | Connected, islanding, island, resynchronising | 3 |
| Sensing and noise | Meters that sample, round and add noise | 4 and 6 |
| Timing | Tasks of the controller with periods and deadlines | 7 and 8 |
| Optimisation and data | A schedule for the battery from forecasts | 9 and 10 |
| Security and safety | False measurements, failures and protection | 11 and 14 |
| Digital twin | A model that follows the microgrid in real time | 12 and 13 |
A car with cruise control measures its speed, compares it with the set speed and opens or closes the throttle. Uphill the speed drops, the error grows and the engine works harder. The driver never computes anything, and the loop corrects a hill that no plan foresaw.
- What changes for a programmer whose code opens a breaker instead of a web page?
- Which property of the table above would you test first in a new charger for electric vehicles?
The campus microgrid, stage 1: The power balance of the campus
Stage 1 computes the balance of the spring day twice: without the battery, and with the rule of Part A, which discharges above an import of 450 kW and charges below 330 kW. The battery starts the day half full.
| Quantity | Without the battery | With the battery rule |
|---|---|---|
| Demand of the day (kWh) | 11,197 | 11,197 |
| Solar and wind energy (kWh) | 2,988 and 638 | 2,988 and 638 |
| Energy imported (kWh) | 7,570 | 7,553 |
| Peak import (kW) | 585.7 | 450.0 |
| Self-sufficiency | 0.324 | 0.325 |
| State of charge: lowest and at midnight | - | 0.144 and 0.393 |
The rule cuts the peak by 135.7 kW, which a contract with a charge for the peak power rewards. The energy imported hardly changes, because the battery only moves energy in time and loses 5 percent in each direction. The battery ends the day at 0.393, below its start of 0.5, so the next night must refill it.
On a cloudy day, the same threshold fails: The battery empties in the afternoon, and the import reaches 608.3 kW. The lowest threshold that holds is 445 kW on the sunny day and 490 kW on the cloudy one. The threshold must therefore follow the weather, a task for the forecasts of Week 10.

Check your understanding. Why does the energy imported hardly change, although the peak falls by 135.7 kW?
Interactive lab, Part D. Stage 1 by hand: The balance at noon, the self-sufficiency of the day and the cut of the peak.
Python code in the Colab notebook, Section 4. Open Section 4 of the Colab notebook and run it. The code runs the day without and with the battery rule and collects the results in the dictionary STAGE1. A figure draws the import of both days and the state of charge.
In the field: Checking the sign of the meter
On site, the controller of the battery reads the real meter at the grid connection. Meters differ in the sign of the power: Some count the import as positive, others the export. The rule of this lecture assumes that the import is positive.
A wrong sign turns the negative feedback of the loop into positive feedback [8]. With the export counted as positive, the import of 530 kW reads -530 kW, and the first step gives \(P_{bat} \)\({}= 0.5 \times (-530 - 400) \)\({}= -465\) kW. The battery charges instead of discharging, the import grows, and the command stays at the limit of 250 kW. Controllers therefore show the measured value before the loop is closed, and many open the loop when the command stays at its limit.
Interactive lab, Part E. The field task: The controller of the battery on site, with the real meter of the grid connection. Check the sign of the meter in its menu, enter the design and close the loop.
Your application
Every site that exchanges power with the city grid has the same balance. The Colab notebook holds an application switch with five further sites: A factory with two shifts, a hospital, a farm, a data centre and a ferry port with electric ferries that charge four times a day.
Python code in the Colab notebook, Section 5. Open Section 5 of the Colab notebook, set APP to one of the six names in the comment and run the cell. The code computes the self-sufficiency of the site and its peak import without and with a battery.
Learning machines in physical systems
Artificial intelligence (AI) increasingly enters cyber-physical systems: Models learned from data forecast the load, detect faults and propose set points. A learned model differs from the models of this week in one important way. It is fitted to the data that it has seen. It may answer wrongly and confidently in a situation that it has never seen, such as a heat wave or a new kind of load.
In a physical system, a wrong answer moves equipment. Engineers therefore keep learned models inside a frame of physical laws and limits. The balance of power must hold whatever a model says, and the battery may not go below its minimum state of charge. A simple, verified controller takes over when the learned one leaves its range. Weeks 10, 11 and 14 return to this frame.
Suppose that a model learned from last year's data proposes the battery power every minute. The microgrid controller passes the proposal through checks: The power must stay within 250 kW, the state of charge within 10 and 90 percent, and the import below the contracted limit. If a check fails, the rule of this week takes over and the event is logged.
- Should a learned model be allowed to switch the campus into island mode on its own? Under which conditions?
- Which data would you need before you trust a model that controls the battery of the campus?
Review cards
Select a card to turn it over.
References
[1] Van Rossum, G., & Drake, F. L. (2009). Python 3 Reference Manual. CreateSpace.
[2] Lasseter, R. H., & Paigi, P. (2004). Microgrid: A conceptual solution. In 2004 IEEE 35th Annual Power Electronics Specialists Conference (pp. 4285-4290). IEEE. https://doi.org/10.1109/PESC.2004.1354758
[3] Olivares, D. E., Mehrizi-Sani, A., Etemadi, A. H., Cañizares, C. A., Iravani, R., Kazerani, M., Hajimiragha, A. H., Gomis-Bellmunt, O., Saeedifard, M., Palma-Behnke, R., Jiménez-Estévez, G. A., & Hatziargyriou, N. D. (2014). Trends in microgrid control. IEEE Transactions on Smart Grid, 5(4), 1905-1919. https://doi.org/10.1109/TSG.2013.2295514
[4] Lee, E. A. (2008). Cyber physical systems: Design challenges. In 2008 11th IEEE International Symposium on Object and Component-Oriented Real-Time Distributed Computing (ISORC) (pp. 363-369). IEEE. https://doi.org/10.1109/ISORC.2008.25
[5] Lee, E. A., & Seshia, S. A. (2017). Introduction to Embedded Systems: A Cyber-Physical Systems Approach (2nd ed.). MIT Press. https://ptolemy.berkeley.edu/books/leeseshia/
[6] Rajkumar, R., Lee, I., Sha, L., & Stankovic, J. (2010). Cyber-physical systems: The next computing revolution. In Proceedings of the 47th Design Automation Conference (pp. 731-736). ACM. https://doi.org/10.1145/1837274.1837461
[7] Alur, R. (2015). Principles of Cyber-Physical Systems. MIT Press. https://cis.upenn.edu/~alur/pcps.html
[8] Åström, K. J., & Murray, R. M. (2021). Feedback Systems: An Introduction for Scientists and Engineers (2nd ed.). Princeton University Press.
[9] Derler, P., Lee, E. A., & Sangiovanni-Vincentelli, A. (2012). Modeling cyber-physical systems. Proceedings of the IEEE, 100(1), 13-28. https://doi.org/10.1109/JPROC.2011.2160929
[10] Kundur, P. (1994). Power System Stability and Control. McGraw-Hill.
[11] Skoplaki, E., & Palyvos, J. A. (2009). On the temperature dependence of photovoltaic module electrical performance: A review of efficiency/power correlations. Solar Energy, 83(5), 614-624. https://doi.org/10.1016/j.solener.2008.10.008
[12] Carrillo, C., Obando Montaño, A. F., Cidrás, J., & Díaz-Dorado, E. (2013). Review of power curve modelling for wind turbines. Renewable and Sustainable Energy Reviews, 21, 572-581. https://doi.org/10.1016/j.rser.2013.01.012