代写MECH ENG 7072 MICROCONTROLLER PROGRAMMING ASSIGNMENT 2: RAMP METER代写数据结构语言程序

- 首页 >> Web

MECH ENG 7072

MICROCONTROLLER PROGRAMMING

ASSIGNMENT 2: RAMP METER

Introduction

This assignment aims to build a program to control ramp meter which could significantly improve traffic in the freeway. The program is based on the C167CR-LM microcontroller, and it should include several functions: freeway car flow counting, traffic light controlling, flexibly adjusting red stop time. Therefore, this report will introduce and explain the design of the program.

Design Specification

The primary objective is to monitor vehicles on both the freeway and ramp. A detector will  generate a digital signal whenever a vehicle passes by. The vehicle count on the freeway is  utilized to determine whether it's peak or off-peak hours. Every 20 seconds (corresponding  to 20 Timer4 interrupts), the program calculates the freeway overload ratio and adjusts the  red light's stop duration accordingly, either increasing or decreasing it. Timer2 and Timer3  manage the control of the red, green, and yellow lights through interrupts. During peak hours, the red light's stop time is extended from 3 to 20 seconds, depending on the traffic flow. The  three lights are connected to port 2. The following table outlines the inputs and outputs of  the program

Table 1: Summary of inputs and outputs connection

I/O type

Name

Allocation

Function Used

Port

Pin

Digital input

Freeway0 detector

2

4

detector_read_freeway

Digital input

Freeway1 detector

2

5

detector_read_freeway

Digital input

Ramp detector

2

6

detector_ramp_on

Digital output

Red lamp

2

1

Led_red

Digital output

Yellow lamp

2

2

Led_yellow

Digital output

Green lamp

2

3

Led_green

Digital Input

The system has three digital inputs. Two freeway detectors will produce a 1-second high pulse, which will be sent to pins 2.5 and 2.4.A ramp detector is used to monitor vehicles passing by. Similar to the freeway detectors, it will generate a 1-second high pulse and send it topin 2.6.

Digital Output

The system includes three digital outputs. The red light is connected topin 2.1, and it turns on when pin 2.1 is set to high. Similarly, pin 2.2 controls the yellow light, and pin 2.3 controls the green light. Each light will be activated when its respective pin is high.Car overload calculation:

In peak time, a maximum of 20 cars are allowed to pass by in 20s(maximum=20*2=40). Therefore, the overload car is the difference between peak maximum vehicles and off-peak maximum vehicles (overload_max=40-10=30). Same as current car overload: car_count – 10.

Overload proportion:

Overload.proportion = (car.count − 10)/ (overload.max)

Red stop time change from 3s to 20s, which means the basic time of red lamp is 3s, and additional 17s changes according to overload_proportion. Therefore: 




In theory, it should be one second longer for every 1.76 cars that pass. However, since the variable is defined as integer in the c program and there is no 0.76 cars, the actual change of red light time will have a error.

Assumptions

Assumptions are made to the system and required to be considered if implementing in the physical system. They are:

•    In 1 second, only one car will pass the detector. For instance, when a car is detected, no other car will pass during the 1-second period while the detector generates its digital output.

•    The number of vehicles on the ramp is significantly smaller than on the freeway, meaning that ramp traffic will not significantly affect freeway capacity..

•    None of the output pins require external pull-up resistors.

The speed of the output pin response, whether fast or slow edge, is not crucial and either can be used.

Program Conceptualization

The system is divided into several components, primarily consisting of the LED module,  detector module, and timer module. The timer module controls both the detectors and the  LEDs. Timer2 manages the flashing of the yellow light, while Timer3 handles the switching  between the red and green lights. Timer4 is tasked with adjusting the red light's stop duration.

 

Figure 1: Architecture of the system state


The maximum time for the timer is around 3 seconds, which is much shorter than the required 20 seconds. Therefore, the program uses multiple counts to reach 20 seconds. The variable "count" holds the theoretical red light stop time, while "timer count" tracks the current red light stop duration. For instance, if "timer count" is 4 and "count" is 5,it indicates that based on the current traffic flow, the red light should be on for 5 seconds, and it has already been on for approximately 4 seconds (between 4 and 5 seconds). Timer2 controls the yellow light, toggling its state every 0.5 seconds (after one timer interrupt). If the toggle is 1, the yellow light turns on; otherwise, all lights are off, resulting in the yellow light flashing at 1 Hz.

Program Implementation

To implement the concept into the system, there are few C source files included in the system with their functions:

•    main.c     Main loop of the system

•    sensor.c   Sensor configuration: passing car detect

•    led.c        Led configuration

•    timer.c    Timing and logic control

Detailed discussion of each source code is as outlined below. main.c

The main.c file is responsible for initializing the LED, sensor, and timer. After checking the flag to determine whether the freeway is currently in peak hours, it activates the appropriate timer and gradually turns on the sensors and LEDs within the timers, based on



the timing sequence.

Figure 2: Flow chart of main.c source file.

The system begins by initializing the LED, timer, and detectors through calls to led_init(), timer2/3/4_init(), and detector_init(), and then starts the peak/off-peak loop. The main function of main.c is to handle switching between peak and off-peak modes, with further operations managed by the timers. At the end of the loop, it activates the detectors and counts the passing vehicles, which will be used by Timer4 to determine whether it is peak or off-peak hours.

Timer.c

The timer.c file is primarily responsible for handling timing and executing different controls at specified intervals. Timer2 manages the flashing of the yellow light, Timer3 controls the switching between the green and red lights, and Timer4 regulates the duration of the red light



and          monitors           whether           the          traffic           flow           is           overloaded.

Figure 2: Flow chart of timer.c file.

In the timer.c file, there are a total of three timers. Timer2 has a period of 0.5 seconds, which means it triggers an interrupt and toggles the value of toggle every 0.5 seconds. When toggle equals 1, the yellow LED will be turned on. As a result, the yellow LED will remain on for 0.5 seconds and then turn off for the next 0.5 seconds, achieving a flashing rate of 1 Hz.

The red light will be activated when Timer3 is running. Each time Timer3 triggers an interrupt, timer_count increments by  1 until it reaches count, meaning the red light remains on until Timer3 completes several cycles. Once the red light turns off, timer_count resets to 0, and the green light is activated. Additionally, Timer3 will be turned off, and the ramp detector will be enabled.

Timer4 remains  active  throughout  the  entire  program.  It  compares  the  current  car_count detected from the freeway with a threshold of 10 to determine whether it is peak time. If it is peak time, the program calculates the overload proportion

Sensor.c

In this section, three detectors is set to separately monitor car passing on freeway0/1 and ramp.

 

Figure 3: Flow chart of detector function



The detector produces a  1-second high pulse. To prevent multiple counts within this  1- second period,  two  variables,  pre_state and  cur_state,  are  defined  to  determine whether the high signal comes from the same pulse. The count is incremented only when pre_state is 0 and cur_state is 1. After each evaluation, the value of cur_state is assigned to pre_state.

Program Operation

The digital pins are set as the diagram showed below:

Figure 4: output bits



P2.1, P2.2, P2.3 are set as output.

 

Figure 4:on/off input bit

P2.4, P2.5, P2.6 are set as input.

Traffic control:

Case 1: off-peak situation

In this case, the yellow lamp is flashing at 1Hz, timer2 and timer4 is working.

 

Figure 4: yellow lamp on



When toggle is 1, yellow lamp is switched on (P2.2 is 1). From table in watch we can see, the timer4, which counts for 20s, have triggered interrupts for 12 times, and now there have been 6 car passed.

Figure 4: yellow lamp off

In off-peak situations, the yellow light remains off until the next Timer2 interrupt occurs when toggle is 0. As a result, the yellow light stays on for 0.5 seconds after being off for 0.5 seconds, achieving a flashing rate of 1 Hz. With P2.2 set to 0, the yellow light is turned off.



Case 2: Peak situation

In this case, red lamp will keep on from 3s to 20s depends on car flow.

Figure 4: red lamp on

According to the figure, the flag is set to 1, indicating that it is peak time. The red light has been on for approximately 2 seconds. However, since the count is 6, the red light should continue to stay on for about 4 more seconds. With P2.1 set to 1, the red light is turned on.

 

Figure 4: green lamp on


From this figure, the count is still 6, but the timer_count1 is 0. This means red lamp have already been switched on for 6s, and then the timer_count will be reset to 0 when the last interrupt was triggered. P2.4 is 1, so the green lamp is switched on.

Discussion

Although this operation has generally met its objectives, there are several areas that could be improved. First, the vehicles passing through the ramp can affect freeway traffic. While it is assumed that only a few vehicles use the ramp, real-world conditions are variable. Therefore, in the actual ramp meter design, the traffic flow must also account for vehicles passing through the ramp. Second, in this operation, we assume that a maximum of one vehicle can pass in one second, as the detector generates a high-level signal that lasts for one second after detecting a car. Consequently, if two or more vehicles pass within that second, the generated pulse can only register as one vehicle passing. Lastly, the maximum traffic flow of 3,400 vehicles per hour, as mentioned in the task, is not represented in this design. My proposed design to address this requirement involves utilizing a 3-second period for convenience, considering that the maximum timeron a 20MHz CPU is 3.36 seconds. If we take one hour as a cycle, the timer needs to trigger 1,200 interrupts. An array of 1,200 data entries will be created, and each time the timer triggers an interrupt, the pointer will move down to store the detected vehicle at the current position. Once 1,200 interrupts are triggered, the pointer will reset to save the traffic flow and overwrite the first data entry during the 1201st 3-second interval.

Conclusion

This assignment presents a valuable opportunity to design a ramp meter. The C167 user manual is an essential resource for accurately implementing the timer and configuring I/O ports. Some registers need to be modified before use, and the user manual provides detailed instructions for these modifications. The simulated results further validate the feasibility of the design project. Overall, this project primarily evaluated the use of timers, understanding interrupts, multiple counting methods, and the configuration of basic I/Oports.


站长地图