Journal of Advanced Computer Science and Technology Research 1 (2011) 10-24
Computing Simulation for Traffic Control over Two Intersections Takialddin Al-Smadi Department of Communication Engineering, Jerash University, Jordan
[email protected]
Article Info Received: 01 June 2011 Accepted: 20 July 2011 Published online: 01 September 2011
© 2011 Design for Scientific Renaissance All rights reserved
ABSTRACT Simulation for traffic control over two intersections using windows Application Programming Interface (API) and c++ is implemented. The simulation allows the user to add vehicles and pedestrians to the different lanes and observe the animation of vehicles and pedestrians between the two intersections according to the traffic lights. Approach: The user can add vehicles and pedestrians to a specific lane separately or he can add them as patterns of congestion to all lanes. Conclusion: the PCT controller and the VA controller. For the controllers to be able to control the two intersections at the same time, PThreads (POSIX Threads) were used to implement parallel programming in that portion of the code. Keywords: Application Programming Interface (API), traffic lights, Preset Cycle Time (PCT), traffic control, C++ Programming language, Global variables.
1. Introduction The user can choose one of two controllers to control the traffic lights. Two controllers are implemented: Preset Cycle Time (PCT) controller and the Vehicle Actuated (VA) controller. Since these controllers need to apply control to the two intersections at the same time, some type of parallel programming (Belasis et al., 2010) is needed. POSIX threads are used to achieve that, POSIX threads (PThreads) are a POSIX standard for thread. The problem of traffic control over intersections is not a straightforward problem. Designing a solution for this problem requires not only deep understanding of it, but also some way of testing the solution and measuring its efficiency. Testing the solution on real Intersections would be so difficult especially when the designer needs to continuously make changes to his design and use the output data for analysis and other purposes. Another way of modeling is required that gives the designer the flexibility to test his designs and obtain results much easier and faster. One of these ways is using simulation (Vijay et al., 2009)
Journal of Advanced Computer Science and Technology Research 1 (2011) 10-24
1.1 The simulated system characteristics This simulation will be for signalized intersections that use traffic lights to control vehicles flow into the intersection. The red light means that vehicles are forbidden from crossing the intersection, while the green light means that vehicles are allowed to cross the intersection. The amber light is used as a transition between the red and green lights and it usually has a fixed period. The periods of the red and green lights depend on the control strategy used. On the level of one intersection, vehicles flowing from different lanes will be demanding for their turns to be assigned the green light and cross the intersections. Of course, they cannot cross the intersection all at the same time, and then there will not be a problem to solve in the first place. Vehicles of different lanes can be assigned the green light at the same time only if it is impossible for them to collide. At the same time, pedestrians may be demanding to cross the street on one of the lanes. If the light is red, they can pass immediately. Otherwise, they’ll have to wait until the light is red (Rajakumar et al., 2009). 1.2 Using windows API parts of the program Most of the Windows API parts of the program are included in the Simulator.cpp file. Other resources and identifiers definitions are included in the TRAFFIC.h and the TRAFFIC.rc. So the main components of the Windows API in the program are: TRAFFIC.rc TRAFFIC.h The WinMain function The WndProc function The ChildWndProc function The PDlgProc function 1.3 Registering the window class A window is always created based on a window class. A Window Class stores information about a type of window, including its Window Procedure which controls the window, the small and large icons for the window and the background color. This way, the programmer can register a class once and create as many windows as he wants from it, without having to specify all those attributes over and over. In the simulator code, two window classes are needed since two types of windows will be created, one for the parent (top-level) window and the other for the child window that will appear when the user presses the mouse left button or presses “enter” on the keyboard, First a variable is declared to store the name of the window class for the parent window: Const char g_sz Class Name [] = "traffic class.
11
Journal of Advanced Computer Science and Technology Research 1 (2011) 10-24
1.4 Components of the Simulator cpp.file The Windows API components of the simulator.cpp file and other components and functions will be discussed in this study these are: The header files The global variables Other functions The header files: #include #include "TRAFFIC.h" #include The header files to be included in the Simulator.cpp file are: Windows. h: This file is a Windows-specific header file for the C++ programming language which contains declarations for all the functions in the Windows API, all the common macros used by Windows programmers and all the data types used by the various functions and subsystems. It defines a very large number of Windows specific functions that can be used in C++. Traffic.H: This file contains the resources identifiers definitions. Windowsx.h: Windowsx.h is a useful file supplied by Microsoft for the API programmer. It includes the extended form of windows.h 2. Materials and Methods Global variables are variables that are accessible in every scope. These variables are declared global since they will be used by more than one function in the file. These variables are: The structure VEHICLE_INFO: This structure contains members that specify certain characteristics for the vehicles. Each inserted vehicle in the system will be an object of this structure and will have its members. This structure is defined with the statements: struct VEHICLE_INFO int width; int height; int dx; int dy; RECT vehicle_rect; RECT old_rect; HBRUSH hbrush; bool reserved; int rndm; VEHICLE_INFO () { Width = 15; 12
Journal of Advanced Computer Science and Technology Research 1 (2011) 10-24
Height = 10; hbrush = (HBRUSH) GetStockObject (WHITE_BRUSH); reserved = 0; rndm = 0; waiting_time = 0; As can be observed from the code, this structure has these members which are initialized using the constructor VEHICLE_INFO(): Width--- specifies the width of the vehicle. This value is set in the constructor to 15. All vehicles will have this same width Height--- specifies the height of the vehicle. This value is set in the constructor to 10. All vehicles will have this same height dx--- specifies the number of units the vehicle will move each time in the X direction dy--- specifies the number of units the vehicle will move each time in the Y direction vehicle_rect--- defines a rectangle for the current location of the vehicle old_rect--- defines a rectangle for the previous location of the vehicle Hbrush--- defines the color of the vehicle Reserved--- specifies whether the rectangle is reserved or not. That is whether there is a vehicle currently in that rect Rndm--- used to specify the direction that the vehicle will take when it has more than one possible path 2.1 The structure intersection This structure has members that specify the characteristics of the intersection. Two objects will be created of this structure; one for each intersection (Belkasmi et al., 2010) this structre will be defined with the statements: struct intersection{ int active_phase; int prev_phase; bool detect_phases[6]; intersection() { active_phase = 0; prev_phase = 0; for (int i=0; iactive phase!=6) temp->active phase= temp->active_phase+1; else temp->active _phase=1; count =0; This loop is used to change the phases from one to the next of the phases that are defined in the variable phases. The active phase of the intersection is changed each time the count reaches 3. Back to the controller1 function: pthread_join (T [0], NULL); pthread_join (T [1], NULL); These statements will cause the function to wait for the two threads to finish their execution: int k0 = P[0].active_phase; for (int k=0; kactive_phase= temp-> active_phase+1; Else temp->active_phase=0; Go to f; These statements control the phase to be assigned the active phase for each intersection. If the currently active phase has cars detected and it was not active for more than 2 times the function was called, then it will remain the active phase and the count is updated. If there are no cars detected in the active phase or the count equals 2, then the code will start checking other phases, to determine whether they have any cars detected. And decide the next phase depending on that. 3. Results When the application is first initiated, an opening screen will appear to the user; this screen is shown in Fig.5. For the actual simulator to start, the user is prompted to press “enter”. Then the screen in Fig.6 will appear. Then the user will be able to use the interface to start the simulation the way that he wants.
22
Journal of Advanced Computer Science and Technology Research 1 (2011) 10-24
Fig.5.The opening screen of the simulator
Fig.6.The simulator screen
Fig.7.the simulator in operation with the VA controller selected Fig.7 shows the simulator with cars and pedestrians added to the lanes and the traffic lights operating to control the traffic.
23
Journal of Advanced Computer Science and Technology Research 1 (2011) 10-24
4. Conclusion The implemented software used Windows API and C++. In this simulator the user will be able to interact with the system by adding cars and pedestrians to the different lanes of the two intersections and observe their animation in response to the traffic lights. Moreover the user will be able to choose the type of controller to be used to control the traffic lights. Two controllers are available: the PCT controller and the VA controller. For the controllers to be able to control the two intersections at the same time, PThreads (POSIX Threads) were used to implement parallel programming in that portion of the code. References Belasis, M.G., Bouchentouf, T. Azizi, M. and Benazzi, A. (2010). Modeling Projects in elearning course: A case of an information technology project. J. Comput. Sci., 6: 823-829. DOI: 10.3844/jcssp.2010.823.829 Pathiah A. Samat, Abdullah M. Z. and Shukur, Z. (2011). Analysis of the Model Checkers' Input Languages for Modeling Traffic Light Systems. Journal of Computer Science, 7: 225-233. DOI: 10.3844/jcssp.2011.225.233. Rajakumar K. and Ramaiah, P. S. (2009). Personal Computer Based Clinical Programming Software for Auditory Prostheses. Journal of Computer Vijay, J.F. and Manoharan, C. (2009). Initial hybrid method for analyzing software estimation, benchmarking and risk assessment using design of software. J. Comput. Sci., 5: 717-724. DOI: 10.3844/jcssp.2009.717.724
24