ee472 smart train control system
DESCRIPTION
design a train control systemTRANSCRIPT
1
EE 472 Lab 5
Final Project
6/9/2013
Rebecca Chu
Phongsakorn Liewsrisuk
Wuyue Liu
Haiyu Shi
2
Table of Contents INTRODUCTION ................................................................................................................................. 3
DESIGN SPECIFICATION .................................................................................................................. 3
Part 1 Main .......................................................................................................................................... 3
Part 2 Schedule .................................................................................................................................... 4
Part 3 Train Communication ................................................................................................................ 5
Part 4 Current Train ............................................................................................................................. 7
Part 5 Switch Control......................................................................................................................... 13
Part 6 Serial Communication ............................................................................................................. 13
Part 7 Passengers ............................................................................................................................... 14
Part 8 ENET ...................................................................................................................................... 16
Part 9 JavaScript for Display .............................................................................................................. 16
EXTRA CREDIT (3 extra credits)...................................................................................................... 17
Analog Converter .............................................................................................................................. 17
Enhanced Internet Interface ............................................................................................................... 18
Improved User Usability .................................................................................................................... 19
ERROR ANALYSIS: .......................................................................................................................... 19
CONTRIBUTION AND ESTIMATED HOURS................................................................................ 20
CONCLUSION .................................................................................................................................... 20
3
INTRODUCTION
The final project is a combination of the results from lab 4 and lab 5 which is also the
continuation of the train system we have been working on for the past quarter. Like the previous
labs, this project is still worked from IAR IDE development tool to build and debug the software
and then download on the targeted Stellaris board. The biggest change from our previous train
system is that we now manage the tasks from dynamic task queues to a real-time operating
system called FreeRTOS. In RTOS, tasks are now always continuously running unless
suspended or resumed by built in FreeRTOS functions. Unlike non-preemptive round robin task
schedule, tasks also now have priority to determine which tasks to run first. In addition to the
transition to RTOS, the train system also have several new features.
The new feature introduced in lab 4 is a new task, passenger that simulates passenger
load on the train through measurements from the function generator. This is achieved through
reading from the GPIO pins and measurements are to be displayed on the OLED display along
with all the information. In lab 5, we have a major change in the train system where we removed
the gridlock feature. However, our train system now supports up to two train, while one is
traversing the intersection, the other one that arrived later will have to wait for the first train to
finish traversing before it can cross the intersection. With this new feature, there are two new
accompanying function. If there is a second train, the train system will display the train wait
time. Also, there is an additional flag if the waiting time for the train exceeds the traversal time
of a train with train size of 6. The final project also now have a web-based interface in a browser
that display train information.
All the mandatory features of the final project are met. We also attempt to complete all
three of the extra credits. We were successful in completing the analog measurement where we
read voltage through GPIO pins. We also implemented enhanced internet interface where we not
only display train information, we also support commands that allow controls of the train system
such as starting or pausing the tasks, displaying or not displaying information, and refreshing the
page. Lastly, we attempt to create a more user-friendly web page as an improvement to the
overall train system.
DESIGN SPECIFICATION
Part 1 Main
The main file for this lab is build up on the freeRTOS structure, similar to our previous
projects, we initialized all the global variables and the interrupt function before the actually main
function. we included more global variables to store the information of the second train, also we
took away those global variables relate to gridlock. In freeRTOS, it has its own timer function,
so we got rid of our old hardware timer interrupt in the main, left only the push button and
frequency interrupt.
4
Inside of the main function, first we would call the ENET function which will set up the web
server for the board and then like previous labs we set up all the hardware we would be using in
this project. after hardware setup section, we need to create tasks. In freeRTOS, Task is created
in the following syntax:
xTaskCreate(passanger, "passanger", 100, NULL, 1, &passangersHandle);
the last 2 parameters are relatively more important in this project, the first one is the priority of
the task, we assigned our tasks’ priority as following:
passanger,1
SerialCom,2
CurrentTrain,3
SwitchControl,4
TrainCom,5
Schedule,6
The last parameter is the suspend/resume task handler which we will need in our
scheduler and web Server command, we just set up all handlers in the main so we can easily
suspend or resume tasks in other tasks.
Part 2 Schedule
In the schedule task, this is the task that contains the logic that determines each of the
function task (ie train com, current train, switch control, serial communication, and passenger).
The logic is similar to the previous version’s train system, which instead of being allocated in
designated schedule task like the current version is, it was in main to determine which task to add
to the task queue. The schedule task is like the “boss” task running in FreeRTOS that tells the
operating system what task to suspend and what task to resume. Schedule task will keep running
and not get suspended. This schedule work as expected and does make the overall train system
behavior function as expected by determining what task to run and what not to run.
Pseudocode:
while (1){
increment global count
if (train 2 not present and one of the buttons pressed){
resume train task
}else {
suspend train task
}
if (train 1 present){
suspend passenger task
5
}else{
resume passenger task
}
if (train 1 not present){
suspend switch control task
}else {
resume switch control task
}
if (train 1 present){
resume current train
}else {
clear display for from direction information on OLED
suspend current train
}
display global count
ulvalue = value from ADC for voltage load
display ulvalue
if (train 1 present){
show “moving” on display
}else{
show “train not present” on display
}
}
Part 3 Train Communication
TrainCom is the function that “creates” a train. The logic in TrainCom for each train
information is basically same as the previous version. the train size and train to direction is still
determined by the random number generator. at same time, all of the four possible from direction
are determined by the push buttons. Pressing different push buttons will set variable to 1. We
assigned each push buttons to variables: State, State1, State2, and State3. When State is 1, the
train is coming from the North. When State1 is 1, the train is coming from South. When State2 is
1, the train is coming from West. When State3 is 1, the train is coming from East. The difference
here is we added a new Train. So we need to reconsider the condition. If there is no Train1, we
6
create Train1. If there is a train one, we want to create another Train2 and give it from direction
and to direction like what we did in for the train 1.
Pseudocode:
TrainCom
if there is no train in the intersection
Train1 present
trainSize1 = random integer between 2 to 9
direction1 = random integer between 0 to 3
if (direction1 is 0)
direction1 is west
else if (direction1 is 1)
direction1 is north
else if (direction1 is 2)
direction1 is east
else
direction1 is south
if (State is 1)
fromDirection1 is North
else if (State1 is 1)
fromDirection1 is South
else if (State2 is 1)
fromDirection1 is West
else if (State3 is 1)
fromDirection1 is East
set State, State1, State2, State3 all to zero
else if(train1 in the intersection)
set train2 present
trainSize = random integer between 2 to 9
direction = random integer between 0 to 3
if (direction2 is 0)
direction2 is west
else if (direction2 is 1)
direction2 is north
else if (direction2 is 3)
direction2 is east
else
direction2 is south
if (State is 1)
7
fromDirection2 is North
else if (State1 is 1)
fromDirection2 is South
else if (State2 is 1)
fromDirection2 is West
else if (State3 is 1)
fromDirection2 is East
set State, State1, State2, State3 all to zero
Part 4 Current Train
This function is almost same as the previous version the mainly difference for lab5 is we
need to show the information for not only the train1 but also the train 2. Sounds always depends
on the train1 because train1 is the only train moving. This function mainly provide the OLED
status display and speaker annunciation. When you push one button, then it will create a
correspond direction of train1. If there is train 1 already, the second push on the push buttons
signal will be set for the form direction of train 2. Patterns of annunciation and the OLED are
always same.
When the train goes to north east west and south Train. As similar as the project 2. We
need 2 long blast of 2 seconds followed by 3 short blasts of 1 second each and display flashing at
a 1.0 second rate.
Pseudocode
if (there is no train)
cleanDisplay();
stopsound();
if (there is train1 look at the to directiontrain1)
if train1 to south
give sound and display for to south
if train1 to west
give sound and display for to west
if train1 to east
give sound and display for to south
if train1 to north
give sound and display for to north
if(at this time train 1 and train 2 both at the intersection)
if train2 to south
give sound and display for T2tosouth
if train2 to west
give sound and display for T2towest
if train2 to east
8
give display for T2tosouth
if train2 to north
give display for T2tonorth
Test and Debug:
ToNorth:
NorthTrain should consist of 2 long blasts of 2 seconds each followed by 2 short blast of 1
second each. So 1 long blasts of 2 seconds means 4 time units, 1 second means 2 time units, and
1.5 second means 3 time units. Then we have the whole period of the NorthTrain table see the
following.
In our code, we use
soundtime=localCounter-*(myNorth->globalCount)+20-(traversalTime%20)+1;
By having this equation, whenever there is a NorthTrain first coming in, the soundtime % 20 will
counts by following this trends: 0 19 18 17 16 …
By using the debug function in IAR, I got the following table:
NorthTrain
soundtime displaytime displaytime % 6 soundtime % 20 sound on / off
100 90 0 0 s
99 89 5 19
98 88 4 18
97 87 3 17
96 86 2 16 ns
95 85 1 15
94 84 0 14 s
93 83 5 13
92 82 4 12
91 81 3 11
9
90 80 2 10 ns
89 79 1 9
88 78 0 8 s
87 77 5 7
86 76 4 6 ns
85 75 3 5
84 74 2 4 s
83 73 1 3
82 72 0 2 ns
81 71 5 1
Table 1.1 the result from IAR for NorthTrain
This result is same as what we expected.
To East:
The EastTrain task handles trains going east.
By using the debug function in IAR, I got the following table
soundtime=localCounter-*(myEast->globalCount)+26-(traversalTime%26)+1;
By having this equation, whenever there is a EastTrain first coming in, the soundtime % 26 will
counts by following this trends: 0 25 24 23 22 …
soundtime displaytime displaytime % 8 soundtime%26 sound on / off
52 40 0 0 s
51 39 7 25
50 38 6 24
49 37 5 23
48 36 4 22 ns
47 35 3 21
10
46 34 2 20 s
45 33 1 19
44 32 0 18
43 31 7 17
42 30 6 16 ns
41 29 5 15
40 28 4 14 s
39 27 3 13
38 26 2 12
37 25 1 11
36 24 0 10 ns
35 23 7 9
34 22 6 8 s
33 21 5 7
32 20 4 6 ns
31 19 3 5
30 18 2 4 s
29 17 1 3
28 16 0 2 ns
27 15 7 1
Table 1.2 the result from IAR for EastTrain
This result is same as what we expected.
11
To South:
We need 2 long blast of 2 seconds followed by 3 short blasts of 1 second each. and
display flashing at a 1.0 second rate.
int soundtime=localCounter-*(mycurrentTrainData->globalCount)+24-(traversalTime%24)+1;
displaytime=localCounter-*(mycurrentTrainData->globalCount)+4-(traversalTime%4)+1;
By having this equation, whenever there is a WestTrain first coming in, the soundtime % 24 will
counts by following this trends: 0 23 22 21 …
soundtime display time display time %4 soundtime%24 sound on / off
48 48 0 0 s
47 47 3 23
46 46 2 22
45 45 1 21
44 44 0 20 ns
43 43 3 19
42 42 2 18 s
41 41 1 17
40 40 0 16
39 39 3 15
38 38 2 14 ns
37 37 1 13
36 36 0 12 s
35 35 3 11
34 34 2 10 ns
33 33 1 9
32 32 0 8 s
31 31 3 7
12
30 30 2 6 ns
29 29 1 5
28 28 0 4 s
27 27 3 3
26 26 2 2 ns
25 25 1 1
To West:
WestTrain should consist of 1 long blasts of 2 seconds each followed by 2 short blast of 1
second each.
The entire functional behavior for sounds needs 14 time units. Display will change every 2 units
so we need 4 time units.
In our code, we use
soundtime=localCounter-*(myWest->globalCount)+14-(traversalTime%14)+1;
By having this equation, whenever there is a WestTrain first coming in, the soundtime % 14 will
counts by following this trends: 0 13 12 11 …
In this equation localCounter is given from the switch control, it aim to tell the WestTrain
function that the certain time point when the WestTrain is completely passed.
soundtime display time display time %4 soundtime%14 sound on / off
28 28 0 0 s
27 27 3 13
26 26 2 12
25 25 1 11
24 24 0 10 ns
23 23 3 9
22 22 2 8 s
13
21 21 1 7
20 20 0 6 ns
19 19 3 5
18 18 2 4 s
17 17 1 3
16 16 0 2 ns
15 15 3 1
Table 1.4the result from IAR for WestTrain
This result is same as what we expected.
Part 5 Switch Control
This function controls the train management System. Because this time we replace the
gridlock with train2, we simplify a lot in this function.
Pseudocode
if the first train come
set the localcounter and set the switch control to be true
else if the train1 just passed the intersection
move all the information of train2 to train1(include the from direction, to
direction size)
set the SwitchControlcheck to false
and clear all the information of train2 to zero.
Part 6 Serial Communication
The serial com is being modified to be one of the FREERTOS task. The task is being
called by schedule . (suspend and resume)
The setup of Serial communication is being done the same as the previous lab. However,
we changed the display format on the OLED, therefore, the display format on the hyperterm was
changed to accommodate the change in the OLED display. We tried to follow the format on the
OLED as closely as possible.
Note:The GPIOpin is port A pin3 and pin1
14
Pseudocode:
while(1)
{
if(train1Present)
{
print out trainsize
print out trainTo
print out trainFrom
if (fromNorth2)
{
if(waitingTime>72)
print long wait time
}
print passangers
}else
print train not present
print global count
vTaskDelay(500);
}
Part 7 Passengers
The function generator input is being connected to the GPIO port A pin 0 with the square
wave of 1k - 2k HZ. The frequency of the digital signal are counted by the interrupt. Then, we
converted the these counts and interpreted them into the number of passengers.
(1kHz representing 0 passengers to 2kHz representing 300 passengers )
The method we used to measure is relatively simple, it only relates to the function
generator interrupt. In the main file, we set that once the interrupt occurs, our frequency counter
would increase by 1, also, we have another variable called pastFrequencyCounter, it’s used to
record the value of frequencyCounter 0.5s ago, so we can calculate the frequency based on the
difference these 2 variables. pastFrequencyCounter will be updated every time after the current
frequency is been calculated, and in trainCom, when the train is just been created.
15
Pseudocode:
while(1)
{
if passangerCheck is equal to zero
{
passangerCheck++
passangers=((frequencyCounter-pastFrequencyCounter)*2-1000)*3/10;
pastFrequencyCounter=frequencyCounter;
}
print passenger
vTaskDelay(500);
}
Figure 1 UML Diagram for Passenger
16
Part 8 ENET
In ENET function, we set up the Ethernet connection. In this part, we need to combine
the sample code of ENET with our freeRTOS. Before we combine them, we need to remove the
default Ethernet setup inside the freeRTOS, which include remove all the hardware setup in
order to make sure there will not have any confliction between the setup in ENET and set-up
inside freeRTOS.
We didn’t change too much ENET sample code. We changed the name of main function
to ENET function, deleted OLED display in the ENET, in order to save enough space for train
information, and remove the while loop at the end of this function. Also, we combined the start-
ewarm of previous project with start-ewarm of ENET.
After we combined the ENET with freeRTOS, we then move on to modify the lmi_fs.c
file, this file contains all the functions that connecting the OLED and web server, since we want
to display our own information on the website, so we need to add our own display functions in
this file. For the display functions in fs, we basically just assign ptFile’s data to the information
we would to display, since ptFile’s data is a char array, so in order to display number, we also
need to convert the number into char array, we did it exactly the same way as we did for the
global counter in previous lab. those display functions would be called in the javascript, so the
rest work of display information on the web site is in the Html code.
Overall all, combine ENET with freeRTOS and then add our own display functions in
lmi_fs are the 2 major steps we did on ENET.
Part 9 JavaScript for Display
For the internet interface, we need to display information about the train system on the
browser. Therefore, we need to write JavaScript functions in the html page to communicate with
the ENET c code in IAR in order to obtain values for each component. In the display page, we
need to show the two train’s information that was shown on the OLED, such as train status, to
direction, from direction, train size. For train 2, there is also waiting time as well as long wait
flag. On top of these, there are also global count that shows incrementation in the application.
For each value that we need from the c code to the html page, we write a function in
JavaScript. The JavaScript functions we have are: global counter, passenger, wait time, train 1
status, size, to from direction and train 2 status, size to from direction. The functions will be
called (and displayed information on page will be updated) every time the page is refreshed. In
my html code, there is a table that organizes all these information for train 1 and train 2 to
display to the user. (The C code side of display is explained in the ENET section above)
17
EXTRA CREDIT (3 extra credits)
Analog Converter
Description: Add an analog input for the system. The analog input from DC power supply (0 to
3.3 volts) is connected to one of the GPIO pin on the Stellaris Board, then display it on the
OLED display in the percentage format. (3.3 V = 100 % and 0 V = 0 %)
Procedure: We followed the example of how to set up the ADC by the Stellaris Doc and example
files in the C drive. From then, we tried to set up the GPIO pin and with DCO_BASE, pin 1.
The triggering process of the ADC is in the FREERTOS scheduled task displaying the voltage
percentage alongside the global count.
Figure 2 UML Diagram for Analog Converter
Pseudocode: (for the conversion)
uValue = uValue divide by 10 // making it 100 percent from 1000
if the uValue is more than 100
make uValue equal to 100
Put the uValue into array for printing on the OLED
18
Enhanced Internet Interface
Description: This extra credit is implemented so that the internet interface not only show train
system data on the browser, it also supports four of these listed text commands:
● S: Starts embedded tasks and able to detect the signal and interrupts.
● P: Stops embedded tasks and terminates train management tasks and interrupts
● D: Enables or disables OLED display
● M: Return up to date data
Therefore, we implement this by having a textbox in html and write a JavaScript function called
cmd cmd_set, displayOLEDget, and taskOLEDget. Cmd_set sends the command user typed in
the textbook to the ENET which will do different things based on the command string. If the
command is ‘S’ or ‘s’, all tasks will be resumed. If the command is ‘P’or ‘p’, the tasks will be
suspended. If the command is ‘D’or ‘d’, the display will be cleared and disabled. If the command
is ‘M’or ‘m’, the page will be refreshed. The other two functions, displayOLEDget and
taskOLEDget, requests information from the c code for the status of the display and task. For
display, it is whether it is ON or OFF and for task, it is whether it is resumed or suspended. In the
c code side, there are 3 if statements in lmi_fs that will respond accordingly if the function is
called.
Figure 3 UML Activity Diagram for Enhanced Internet Interface
19
Improved User Usability
Description: For this open ended extra credit option, we try to make modification for the
improved user usability with internet interface. The display page for internet interface is to
showcase our information of our train system. However, the information display is only a small
part of the whole train system. Therefore, to exhibit more sides to our train system project, we
included more relevant pages such as an “About” page that discusses the website navigation as
well as the train system. There is also a “Contribution” page that listed out each group member’s
area of focus which is designed for our demo TA to have a easy glance of who did what and not
have to ask each member one by one. Moreover, we try to make our page more fun by adding
three additional train related game for page visitors to enjoy. Overall, the webpage are created
with an attempted improvement to the layout from the sample website with clean clear
background to improve user visibility.
Figure 4 UML Use Case Diagram for Improved User Usability
ERROR ANALYSIS: All of the functionality of our train system work as expected. Therefore, we illustrate a
debug problem and our process in resolving the issue. One of the problem that we have in
Analog converter is the library file. The library files that were included in FreeRtos doesn’t
include all the definition. Thus, we have definition error. For example in hw_mammap .h, the
ADC0_Base definition was missing.
Therefore, we have to include the complete hw_mammap.h file from inc directory
20
CONTRIBUTION AND ESTIMATED HOURS Rebecca: Hyperterm formatting, web server html code and JavaScript for fs file, part of ENET c
code, debugging
Haiyu: Redesigned and implement the multi-Train system, add display functions and text
command functions in fs file. Participated the whole debugging process.
Wuyue: Integration of ENET and freeRTOS, modified ENET, participated the debugging
process, part of web server.
Phongsakorn: Analog to Digital converter, Passenger, integration of the overall hardware setup
and interrupts, help with part of the debugging process.
Design: 8 hours
Coding: 10 hours
Test/debug: 15 hours
Documentation: 5 hours
CONCLUSION
Through this project, we learnt the concept of real-time scheduling, internet interface,
analog to digital converter and the integration of the overall system. With FreeRtos, all the tasks
are now preemptive with priority queue which can be set through suspend and resume. Also, new
features were added to the train system such as Passengers task, the web interface, Analog to
digital converter as well as the second train function. More GPIO pins and LAN cable were used
to connect with the external these new interfaces.
One of the biggest challenges that we faced was the integration of the FreeRTOS
environment with ENET. Many library files were missing, thus, giving definition error.
However, we managed to solve the problems with the TA and sampling code on the driver
library. Many steps were solved by try and error with logical reasoning and pattern.
In the end, with the combined team effort and dedicated team members we managed to
complete all the required tasks with all the extra credits.