🚆 Building a Railway Interlocking System in Python

🚆 Building a Railway Interlocking System in Python

I developed a Python-based Railway Interlocking Simulation that replicates real-world signalling logic like route setting, point locking, track occupancy, and signal control.

It ensures fail-safe operations using principles similar to Electronic Interlocking systems aligned with EN 50128.

The system checks for conflicts, locks routes, applies approach locking, and releases routes sequentially as the train moves.


(Inspired by Real-World Safety Standards like EN 50128)

Railway signalling is not just engineering—it’s safety, logic, and precision working together in real-time.

So I built a Railway Interlocking Traffic Management System in Python—a simulation that mirrors real-world signalling logic including:

  • Route setting & locking
  • Point (switch) control with detection
  • Signal aspect logic (2/3/4 aspect)
  • Conflict detection
  • Approach locking
  • Sequential route release
  • Flank protection

👉 And yes, it follows concepts aligned with EN 50128 / IEC 62280 safety principles.


🔧 Project Overview

This system is not just code—it’s a mini digital interlocking engine that behaves like actual railway signalling systems used in networks like:

  • Metro systems
  • Mainline railways
  • High-density corridors


🧠 Core Architecture

📌 1. System Components (Enums & States)

class SignalAspect(Enum):
    RED = "DANGER"
    YELLOW = "CAUTION"
    GREEN = "CLEAR"
        

📸 Code Snapshot 1 — Signal Logic

  • Defines how signals behave
  • Supports real railway signalling hierarchy

👉 Why it matters: Signal aspects directly control train movement authority


🚦 2. Track Circuit Logic

@dataclass
class TrackSection:
    state: TrackState = TrackState.CLEAR

    def occupy(self):
        self.state = TrackState.OCCUPIED
        

📸 Code Snapshot 2 — Track Occupancy Detection

👉 This simulates:

  • Train detection
  • Track availability
  • Fail-safe behavior (FAILED = treated as occupied)

💡 In real signalling: Track circuits are the backbone of safety


🔀 3. Point (Switch) Control — Critical Safety Logic

locking_routes: Set[str] = field(default_factory=set)
        

📸 Code Snapshot 3 — Multi-route Point Locking

🚨 Key Innovation (Bug Fix Highlight):

Instead of:

locked_by_route: str
        

We now use:

locking_routes: Set[str]
        

👉 Why this is powerful:

  • Allows parallel non-conflicting routes
  • Prevents unnecessary blocking
  • Matches real interlocking flexibility

💡 Example: Two routes can share a flank protection point safely


🚦 4. Route Definition

@dataclass
class Route:
    track_sections: List[str]
    point_requirements: Dict[str, PointPosition]
        

📸 Code Snapshot 4 — Route Configuration

Each route defines:

  • Track path
  • Required point positions
  • Overlap sections
  • Flank protection

👉 This is exactly how control tables work in real signalling design.


⚙️ Interlocking Engine — The Brain

🧠 Route Request Logic

def request_route(self, route_id: str):
        

📸 Code Snapshot 5 — Route Setting Engine

This function performs:

  1. Availability check
  2. Conflict detection
  3. Point operation
  4. Section locking
  5. Signal clearing

👉 Only if ALL conditions pass → route is set


⚠️ Conflict Detection (Safety Critical)

def _conflicting_sections(self, route: Route)
        

📸 Code Snapshot 6 — Conflict Logic

Checks:

  • Track overlap
  • Opposite point positions
  • Active routes

💡 Real-world impact: Prevents:

  • Collisions
  • Derailments
  • Wrong routing


🔒 Approach Locking

entry_sig.approach_locked = True
        

📸 Code Snapshot 7 — Approach Lock

👉 Once train enters:

  • Signal cannot be cancelled
  • Route remains locked

💡 This is a fail-safe mechanism used globally


🔁 Sequential Route Release

def train_clears_section(self, section_id: str):
        

📸 Code Snapshot 8 — Route Release Logic

As train moves:

  • Sections are released step-by-step
  • Points unlock gradually
  • Route clears automatically

👉 Improves:

  • Throughput
  • Efficiency
  • Safety


🏗️ Real Layout Simulation

The system simulates a 2-platform station with crossover:

Platform 1  ←→  Platform 2
   |               |
  PT1 — PT2 — PT3
        

Routes Implemented:

  • R1 → Platform 1
  • R2 → Platform 2
  • R3 → Crossover route
  • R4 → Reverse crossover


🎯 Simulation Scenarios

The system validates real-world cases:

✅ Parallel Routes

  • R1 + R2 run simultaneously
  • No conflict → allowed

❌ Conflict Detection

  • R3 rejected when overlapping

🔀 Crossover Routing

  • Points dynamically reverse

🚫 Approach Lock

  • Route cannot be cancelled after train entry

⚠️ Track Failure

  • FAILED track blocks route


🚀 Why This Project Matters

This is not just Python.

This is:

Railway signalling logic ✅ Safety-critical system thinking

✅ Real-world engineering simulation ✅ Interview-ready portfolio project


💡 Key Takeaways

  • Interlocking is about fail-safe logic, not just code
  • Small design mistakes (like point locking) can break safety
  • Real systems require:


🔥 Final Thought

“In railway signalling, you don’t just write logic… you design safety for thousands of lives every day.”

📢 If you're in Railway Signalling:

Let’s connect & discuss interlocking, control tables, and system design.


#RailwaySignalling #Interlocking #ElectronicInterlocking #RailwayEngineering

#Python #Simulation #ControlSystems #SafetyCriticalSystems

#EN50128 #CBI #SystemDesign #EngineeringProjects

#SmartMobility #Innovation #EngineersOfLinkedIn


Kudos! as you are digging to understand how it all works. Two more things, first this article is more about interlocking principles and object oriented method of implementing it. Second, article has nothing to do with EN50128 principles. Sometimes AI hypes thing, you have to discern that. Not to discourage but keeping you curious. Keep digging.

To view or add a comment, sign in

More articles by Nikhil Kothavade

  • Fundamentals of Railway Signalling Systems

    Railway signalling is the "brain" of the rail network. It is a critical safety system designed to prevent train…

    6 Comments

Others also viewed

Explore content categories