Skip to content

trefeon/CPS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Railroad Crossing Protection System Simulation

This repository contains different implementations of a Railroad Crossing Protection System (CPS) simulation, demonstrating various approaches to visualize and control railway crossing gates.

Project Components

1. Line Graph Simulation (CPS1.py)

A Python-based simulation using Matplotlib that shows the status of:

  • Train presence (blue line)
  • Gate position (red line)
  • Road accessibility (green line)

Features:

  • Real-time plotting of system states
  • Two operation modes:
    • Automatic (Random) - Simulates random train arrivals
    • Manual (Button) - User-triggered train arrivals
  • Interactive control panel with countdown timer
  • Visual representation of gate delays and train passing duration

2. Graphical Simulation (CPS2.py)

A visual representation using Tkinter that provides:

  • Animated train movement
  • Interactive railway crossing gate
  • Status indicators for:
    • Gate position (Open/Closed)
    • Warning lights
    • Sirens
  • Real-time distance monitoring
  • Automatic gate control based on train proximity

3. Hardware Implementation (train_crossing.ino & train_crossing.py)

Implementation for physical simulation using ESP32 microcontroller:

  • train_crossing.ino: ESP32 firmware for controlling:
    • Servo motors (gate mechanism)
    • LED indicators
    • Sensors
  • train_crossing.py: Python interface to communicate with the ESP32 hardware

Requirements

For Python Simulations (CPS1.py & CPS2.py)

python 3.x
tkinter
matplotlib

For Hardware Implementation

  • ESP32 Development Board
  • Python with required libraries
  • Hardware components:
    • ESP32 Dev Kit
    • Ultrasonic Sensor HC-SR04
    • Servo Motor (SG90)
    • Jumper Wires

Hardware Setup

System Workflow

  1. ESP32 Operation:

    • Continuously reads distance from HC-SR04 sensor
    • Logic:
      • If distance < 20cm: Simulates "Train Present" → Closes gate (Servo to 90°)
      • If distance ≥ 20cm: Simulates "Safe/No Train" → Opens gate (Servo to 0°)
    • Sends JSON status updates to computer via USB (train presence and gate position)
  2. Python GUI:

    • Reads serial data from ESP32
    • Displays real-time status:
      • Train Present/Not Present
      • Gate Closed/Open
    • Visualizes data in graphical format

Hardware Connection Scheme

Component ESP32 Pin Notes
Servo Signal GPIO 13 PWM pin for servo control
Servo VCC VIN 5V from USB
Servo GND GND Ground
HC-SR04 VCC VIN 5V from USB
HC-SR04 GND GND Ground
HC-SR04 Trigger GPIO 5 Ultrasonic signal out
HC-SR04 Echo GPIO 18 Ultrasonic signal in

How to Run

  1. Line Graph Simulation:
python CPS1.py
  1. Graphical Simulation:
python CPS2.py
  1. Hardware Simulation:
  • Upload train_crossing.ino to ESP32
  • Run the Python interface:
python train_crossing.py

System Behavior

  • The system automatically detects approaching trains
  • Gates close approximately 5 seconds before train arrival
  • Warning systems (lights and sirens) activate when gates are closing
  • Gates remain closed until the train has completely passed
  • System includes safety delays for gate opening after train passage

License

This project is open source and available under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published