Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A traffic simulator with traffic lights, vehicles and intersections using concurrent programming(threads, mutexes, locks) and message queue.

NotificationsYou must be signed in to change notification settings

tooth2/traffic-simulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A traffic simulator implementation in which vehicles are moving along streets and are crossing intersections.With increasing traffic in the city, traffic lights are needed for road safety. Each intersection will therefore be equipped with a traffic light. This project implemented thread-safe communication protocol between vehicles and intersections to complete the simulation and concurrent programming (such as mutexes, locks and message queues) the traffic lights and integrate them properly.

Project Result

Traffic simulation runs with red lights controlling traffic, just as in the .gif file below. There are two simulation : one is Paris street and the other one is NYC street simulation.

ParisNew York
ParisNew York

Code Structure

src|--|  Graphics.h, Graphics.cpp|  Vehicle.h, Vehicle.cpp|  Street.h, Street.cpp|  Intersection.h, Intersection.cpp|  TrafficLight.h, TrafficLight.cpp|  TrafficObject.h, TrafficObject.cpp|  TrafficSimulator-Final.cpp
  • TrafficLight

    • TrafficLight is a child class ofTrafficObject.
    • public methods:void waitForGreen() ,void simulate() ,TrafficLightPhase getCurrentPhase()
      • TrafficLightPhase TrafficLight::getCurrentPhase(): the return is enumTypeTrafficLightPhase` that can be either red or green
      • void TrafficLight::simulate()
      • void TrafficLight::waitForGreen()
        • The methodwaitForGreen is completed, in which an infinite while loop runs
        • repeatedly calls thereceive function on the message queue.
        • Once it receivesTrafficLightPhase::green, the method returns.
    • a private method:void TrafficLight::cycleThroughPhases()
      • measures the time between two loop cycles
      • toggles the current phase of the traffic light between red and green
      • sends an update method to the message queue using move semantics
      • The cycle duration should be a random value between 4 and 6 seconds,
      • while-loop usesstd::this_thread::sleep_for to wait 1ms between two cycles
      • it is started in a thread when the public methodsimulate() is called in a thread queue
    • private member
      • _currentPhase which can take red or green as its value.
      • _trafficLight a Type of TrafficLight
  • MessageQueue

    • A MessageQueue class is defined in the header of class TrafficLight which has the public methods send and receive.
    • send should take an rvalue reference of typeTrafficLightPhase whereasreceive should return this type.
    • defines astd::dequeue called_queue, which stores objects of type TrafficLightPhase.
    • private members variables:std::condition_variable ,std::mutex
    • public method:send ,receive
    • send():
      • use the mechanismsstd::lock_guard<std::mutex> to add a new message to the queue
      • take an rvalue reference of typeTrafficLightPhase
      • _condition.notify_one() to send a notification.
      • In the classTrafficLight, a private member of typeMessageQueue is created and used within the infinite loop to push each newTrafficLightPhase into it by callingsend in conjunction with move semantics.
    • receive
      • usestd::unique_lock<std::mutex> and_condition.wait() to wait for and receive new messages
      • pull messages from the queue using move semantics
      • The received object is returned by thereceive function
  • Intersection

    • a private member_trafficLight of typeTrafficLight
    • The methodIntersection::simulate() starts the simulation of_trafficLight
    • The methodIntersection::addVehicleToQueue use the methodsTrafficLight::getCurrentPhase and TrafficLight::waitForGreen to block the execution until the traffic light turns green.

Run-time environment

  • cmake >= 3.7
  • make >= 4.1 (Linux, Mac), 3.81 (Windows)
  • gcc/g++ >= 5.4
  • openCV >=4.1

Build instruction

  • Make a build directory in the top level directory: mkdir build && cd build
  • Compile: cmake .. && make
  • Run : ./traffic_simulation
cmake_minimum_required(VERSION 3.7)add_definitions(-std=c++17)set(CXX_FLAGS "-Wall")set(CMAKE_CXX_FLAGS, "${CXX_FLAGS}")project(traffic_simulation)add_executable(traffic_simulation src/TrafficSimulator-Final.cpp)

reference

About

A traffic simulator with traffic lights, vehicles and intersections using concurrent programming(threads, mutexes, locks) and message queue.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp