In this article, we will explore the object-oriented design and implementation of a Traffic Signal Control System using Python.
This system is crucial for managing traffic flow and ensuring safety at intersections.
The Traffic Signal Control System should:
- Signal Timing Management: Control the timing of traffic lights.
- Intersection Management: Manage multiple traffic signals at an intersection.
- Mode Configuration: Support different traffic modes.
- Emergency Override: Allow manual control in emergencies.
- Switching Signals: Change signals from red to green and vice versa.
- Timing Adjustment: Modify the duration of each signal.
- Mode Selection: Choose operational modes.
- Emergency Control: Manual override for emergencies.
TrafficSignalSystem
: Manages the system.TrafficLight
: Represents an individual traffic light.IntersectionController
: Manages lights at an intersection.ControlPanel
: For manual control and emergency overrides.
from enum import Enum
class LightState(Enum):
RED = 1
GREEN = 2
YELLOW = 3
class TrafficLight:
def __init__(self, id):
self.id = id
self.state = LightState.RED
def change_state(self, state):
self.state = state
Manages traffic lights at an intersection.
class IntersectionController:
def __init__(self):
self.traffic_lights = {}
def add_traffic_light(self, light):
self.traffic_lights[light.id] = light
def change_signal(self, light_id, state):
if light_id in self.traffic_lights:
self.traffic_lights[light_id].change_state(state)
#Additional methods...
For manual control and emergencies.
class ControlPanel:
def __init__(self, controller):
self.controller = controller
def override_signal(self, light_id, state):
self.controller.change_signal(light_id, state)
#Additional methods...
Main class managing the traffic signal system.
class TrafficSignalSystem:
def __init__(self):
self.intersection_controllers = []
def add_intersection_controller(self, controller):
self.intersection_controllers.append(controller)
#additional methods to update the system config
def main():
# Initialize the traffic signal system
signal_system = TrafficSignalSystem()
# Create intersection controller
intersection = IntersectionController()
# Create traffic lights and add them to the intersection
traffic_light1 = TrafficLight("001")
traffic_light2 = TrafficLight("002")
intersection.add_traffic_light(traffic_light1)
intersection.add_traffic_light(traffic_light2)
# Add the intersection controller to the traffic signal system
signal_system.add_intersection_controller(intersection)
# Create a control panel for the intersection
control_panel = ControlPanel(intersection)
# Demonstration of changing signals
print("Initial state of traffic lights:")
for light_id, light in intersection.traffic_lights.items():
print(f"Light {light_id}: {light.state.name}")
# Change signal to GREEN at traffic light 1
intersection.change_signal("001", LightState.GREEN)
print("\nAfter changing state to GREEN:")
print(f"Light 001: {intersection.traffic_lights['001'].state.name}")
# Override signal to RED at traffic light 1 using the control panel
control_panel.override_signal("001", LightState.RED)
print("\nAfter overriding state to RED using Control Panel:")
print(f"Light 001: {intersection.traffic_lights['001'].state.name}")
# Change traffic light 2 to YELLOW
intersection.change_signal("002", LightState.YELLOW)
print("\nAfter changing state to YELLOW:")
print(f"Light 002: {intersection.traffic_lights['002'].state.name}")
if __name__ == "__main__":
main()