https://github.com/msaf9/cyber-physical-systems-framework
Our project focuses on developing a simulation framework for evaluating Cyber-Physical Systems (CPS) under real-time constraints.
https://github.com/msaf9/cyber-physical-systems-framework
cyber-physical-systems python real-time-systems
Last synced: 7 months ago
JSON representation
Our project focuses on developing a simulation framework for evaluating Cyber-Physical Systems (CPS) under real-time constraints.
- Host: GitHub
- URL: https://github.com/msaf9/cyber-physical-systems-framework
- Owner: msaf9
- License: mit
- Created: 2025-01-30T01:55:31.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-01T05:39:34.000Z (over 1 year ago)
- Last Synced: 2025-02-01T06:24:40.777Z (over 1 year ago)
- Topics: cyber-physical-systems, python, real-time-systems
- Language: Python
- Homepage:
- Size: 494 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cyber-Physical System (CPS) Simulation Framework
## Overview
This project simulates a Cyber-Physical System (CPS) involving a control loop for managing temperature, pressure, and emergency conditions. The system integrates sensors, actuators, a PID controller, and network communication to control various components in real-time. Additionally, the system tracks resource usage, logs simulation data, and analyzes real-time metrics like deadline misses and response times.
## Project Structure
```
Cyber-Physical-System-Simulation/
├── src/
│ ├── components/
│ │ ├── actuators.py
│ │ ├── controller.py
│ │ ├── network.py
│ │ ├── pid_controller.py
│ │ └── sensors.py
│ ├── utilities/
│ │ ├── data_logger.py
│ │ ├── graph_plotter.py
│ │ ├── realtime_metrics.py
│ │ └── resource_monitor.py
│ └── cps_simulation.py
├── tests/
│ ├── test_actuators.py
│ ├── test_controller.py
│ ├── test_network.py
│ ├── test_pid_controller.py
│ └── test_sensors.py
├── simulation_config.ini
├── README.md
└── requirements.txt
```
## Components
### `components/actuators.py`
Defines the `Actuator` class, which represents a physical actuator (e.g., a valve). Includes the `EmergencyValve` subclass for handling emergency shutdowns.
### `components/controller.py`
The `Controller` class coordinates sensor readings, performs PID control, and handles the activation of actuators. It ensures the system meets predefined deadlines and manages emergency shutdowns when critical conditions (like high pressure) are detected.
### `components/network.py`
Simulates a network for transmitting data between sensors and the controller. Packet loss and jitter are modeled to simulate realistic communication conditions.
### `components/pid_controller.py`
Implements a PID controller to manage temperature and pressure control loops. The controller adjusts actuator states based on sensor feedback to maintain desired system parameters.
### `components/sensors.py`
Defines the `Sensor` class to simulate temperature, pressure, and flow rate sensors. The sensors generate random values within specified ranges to mimic real-world readings.
### `utilities/data_logger.py`
Handles logging of simulation data to both the console and a CSV file. Logs include timestamps, component names, and data values.
### `utilities/graph_plotter.py`
Provides methods to visualize simulation data and resource usage. Generates graphs for sensor data, resource consumption (CPU and memory), and real-time metrics (e.g., deadline misses and response times).
### `utilities/realtime_metrics.py`
Calculates and stores real-time metrics, such as deadline misses, average response times, and maximum response times, based on simulation logs.
### `utilities/resource_monitor.py`
Monitors system resources (CPU and memory usage) during the simulation. Logs resource usage at regular intervals.
### `cps_simulation.py`
The main entry point of the simulation. It initializes all components, configures the simulation environment, and runs the control loop. It also monitors system resources and generates plots based on the collected data.
## Simulation Configuration
The simulation parameters (e.g., packet loss probability, simulation duration) are configured in the `simulation_config.ini` file.
Example `simulation_config.ini`:
```ini
[NETWORK]
packet_loss_probability = 0.1
[SIMULATION]
duration = 1000
```
## Running the Simulation
1. Install required dependencies (e.g., simpy, matplotlib, psutil, pytest):
```pip
pip install -r requirements.txt
```
2. To run the simulation:
```python
python src/cps_simulation.py
```
3. The simulation will log data, including sensor values, actuator actions, and real-time metrics. At the end of the simulation, it will generate graphs for analysis.
## Testing
Unit tests are provided for the Actuator, Controller, Network, PIDController, and Sensor components. These tests use pytest to verify the functionality of each component.
To run the tests:
```python
pytest tests/
```
## Key Features
- Sensor Data Simulation: Random values for temperature, pressure, and flow rate are generated by sensors.
- PID Control Loop: A PID controller adjusts actuator states to maintain temperature and pressure within desired setpoints.
- Network Simulation: Models packet loss and jitter to simulate real-world communication conditions between sensors and the controller.
- Emergency Shutdown: In case of critical pressure conditions, the emergency shutdown valve is triggered.
- Real-Time Metrics: The system tracks and visualizes real-time metrics, such as deadline misses and response times, for performance evaluation.
- Resource Monitoring: CPU and memory usage are tracked and visualized during the simulation.
## Visualizing Results
After the simulation, the following graphs are generated:
- Sensor Data Over Time: Plots of sensor values (temperature, pressure) over time.
- Resource Usage: Graphs showing CPU and memory usage during the simulation.
- Real-Time Metrics: A bar chart displaying deadline misses, average response time, and maximum response time.
## Future Improvements
- Enhanced PID Tuning: Implement dynamic PID tuning based on changing system conditions.
- Advanced Network Simulation: Model more complex network scenarios, such as latency spikes and congestion.
- Integration with Real Hardware: Interface with actual sensors and actuators for real-world testing.
## License
This project is licensed under the [MIT License](LICENSE). See the LICENSE file for more information.