Train Project Notebook

Train Project Notebook
Train Project Notebook
Submitted to:
Instr: Dr. Krista Kecskemety
GTA: Zach Imm
Created by:
Group M
Stephen Post
Kyle Niemeyer
Hongzhi Yao
Haocheng Zhu
Engineering1181
TheOhioStateUniversity
Columbus,OH
4April2015
TableofContent
ListofFiguresandTables………………………………………………………………………………………………………….2
ExecutiveSummary…………………………………………………………………………………………………………………..5
Introduction..................................................................................................................................6
ExperimentalMethodology..........................................................................................................6
Programdescriptionfordevelopers………………………………………………………………………………………….7
Programdescriptionforgeneralsuse………………………………….…………………………………………………...9
Results..........................................................................................................................................10
Discussion....................................................................................................................................11
Conclusion………………………………………………………………………………………………………………………………12
Appendix.....................................................................................................................................13
1
ListofFiguresandTables
.
Figure 1: N-Scale train set
Figure 2:N-Scale Train Locomotive
Figure 3: Arduino Control Module
2
Figure 4: Unobstructed sensor values
Figure 5: Obstructed Sensor Values
Table 1: Train Speed times
Variable Name
Uses
approach
Help to track the ports.
departure
Help to track the ports.
int
Set variable for 1 second intervals.
b
Help to estimate if the crossing lights need to flash.
time
The running time of the program.
leftint
Stagger the time frame for left LED.
rightint
Stagger the time frame for right LED.
Table 2: Variable names and uses
Arduino Commands
Descriptions
a=arduino('COM3')
Connect to the Arduino, COM port number 3.
a.servoAttach(1)
Connect to the servo motor which controls the crossing gate.
a.servoStatus
Check the servo status.
3
a.servoWrite(n,θ)
Control the servo and move the crossing gate.
a.pinMode(n,'output')
Issued the LEDs.
a.digitalWrite(n,0)
a.digitalWrite(n,1)
Turn the LED on or off. 0 means off and 1 means on.
a.motorRun(1,'forward')
a.motorRun(1,'release')
Make the train move forward or release
a.motorSpeed(1, X)
Set the speed of the train.
a.analogRead
Help to collect data from the break beam sensors.
Table 3: Arduino Commands and Descriptions
4
ExecutiveSummary
The purpose of Train project (Problem Solving Lab) was to educate student on how to work as a
team to solve a real-world programming scenario by using problem-solving strategies and the
knowledge of MATLAB. After the Problem Solving Lab, Student will learn about the Arduino
microcontroller and how to use data sensor feedback within MATLAB. The team is tasked with
programming two routines for the system to follow. The tracks run through a country side the
top speed of the train can be raised to a higher speed. However when arriving to the town, the
second routine, the train must slow to safe speed. While slowing to the safe speed the crossing
gate must also begin to flash and the gate fall to block oncoming traffic. Once the train reaches
the edge of town, the train crosses the departure gate where it can then resume its previous
speed.
During the lab the team was tasked with completing seven tasks. The first of the five tasks
developed the team's use of the Arduino Control module. The sixth task put to use the
developed code from the prior five tasks to make a functioning program that allowed the train to
run in an infinite loop. Task seven asked for the team to track the speeds of the train. To do
this the team used a tic and toc command to record the train moving at various speeds. It was
observed that the slower the train moved, the longer it took to travel the in a loop around the
track.
Once the team completed the code, we ran the code during in Lab C, we found one error in our
code in which the program functioned as it should, only the code was backwards. So after a
simple change of the departure and arrival gate times the train ran in accordance to the
scenario. Other forms of error that may have occurred would have while recording the speed of
the train. Since the train has a window, it was possible for the break beam sensor to pass
through the train and miss calculate the speed around the track.
To solve the scenario the team met three times over the course of the three lab assignments.
To begin with solving the scenario we decided on brainstorming ideas to come up with plausible
ways to build a correct code. Once an idea was chose we then worked to build an algorithm so
that we could visualize how the code will work once we begin to work on matlab. The team then
moved to programming the scenario in matlab and moved efficiently and quickly through the
process. The team did run into issues with building the flow chart, however with some
additional efforts and trial and error we put together a flowchart that aided us in designing the
code for MatLab.
The train scenario has taught the team how to work together to solve a real world issue as
mechanical and software engineers. The lab has shown the effective uses of an Arduino
controller and how MATLAB functions together with the controller.
5
Introduction
The goal of the Train lab is to teach the students how to solve problems with programming a
train using MATLAB. The students were challenged to use the Arduino controller in an interface
with MATLAB given the scenario that the train must travel through a town and through a country
side which requires two separate routines to run in one fluid continuous program. During the lab
the team needed to achieve seven tasks the first five tasks involve writing small programs to
make the train or the train set function. Once the first five tasks were completed task six utilized
everything from tasks one through five into one program to control the train. The final task had
the team track the speed of the train using tic and toc to determine the speed from one sensor
to the next. This Notebook contains the results of the train lab as well as the code that was
needed to provide the results as well as discussion as to the obstacles that were faced during
the production of the MATLAB code from the starting process of brainstorming to the final
functioning code.
ExperimentalMethodology
The experiment began with first using a standard n-scale model train set shown in figure 1 and
using a N-scale train locomotive shown in figure 2
.
Figure 1: N-Scale train set
Figure 2:N-Scale Train Locomotive
Once the train and track have been set in place the next step is to connect the arduino control
module shown in figure 3. The arduino serves as the brain for the functions of the track. Not
6
only does the arduino operate the track but it serves as the relay to control the break beam
sensors and the crossing gate. To connect the Arduino Controller, a cable must be connected
from the controller into the CPU. From there, a file must be placed into MATLAB for MATLAB to
be able to read the Arduino Controller. This is then followed by typing in a command to make
the connection to the controller.
Figure 3: Arduino Control Module
Using Matlab the team developed a code to satisfy the scenario requirements. To create the
MATLAB script, pre programed functions were used in the script file to communicate with the
Arduino control module. To complete the tasks, pre programmed Arduino codes were used to
manipulate the train in tasks one through five. Tasks six involved running the train in a loop
using the the script that was developed through tasks one through five. Finally, the last task
required the team to add tic and toc into their script to calculate the speed the trained move at
different speeds.
Programdescriptionfordevelopers
The train project mainly need the Matlab and Arduino system. Students write Matlab commands
with Arduino commands to control the train system work as required. Students need to write the
command “clear all; close all; delete(instrfindall); clc;” at the beginning to clear any other data
and command in matlab and then they can begin the other command. First, they connect the
arduino system with the arduino command: a=arduino('COM3'), a.servoAttach(1) and
a.servoStatus. These commands let them connect the right COM port, servo motor and check
their status. The next step is to set the initial status of many things. Include issued the LEDs and
7
turn them off (with arduino command a.pinMode(n,'output') and a.digitalWrite(n,0) ), make the
crossing gate vertical (witht eh arduino command a.servoWrite(1,55) )and make the train move
forward with highest speed (with the arduino command a.motorRun(1,'forward') and
a.motorSpeed(1, 255) ). They also set variables int to show the time of interval and b to help
estimate if the LEDs need to flash. After these, students use the “tic toc” system and while loop
to make the train system work in the required way. To make the system work without stop, they
build while loop with “while 1”, then the loop will run infinitely long. In the while loop, students set
three “if-end” commands. They use “if a.analogRead(approach)>350” and “if
a.analogRead(departure)>350” to determine if the train arrive the approach gate or departure
gate. While a.analogRead is an arduino command which can collect data from the break beam
sensors. When the train pass the break beam sensors, it will run the program in the
corresponding if command. The third if command is “if b” which is used to estimate if the
crossing lights need to flash. If the train arrived the approach gate, students will use
a.servoWrite(1,160) to make the crossing gate horizontal, a.motorSpeed(1, 170) to make the
train move slower and a.digitalWrite to make the LED on departure gate change to green and
LED on approach gate change to red. Then students make the variable b equals true so that the
crossing light can flash. When the train arrived the departure gate, students will use the similar
way to make the crossing gate vertical, the crossing light stop flash, the train move faster, the
LED on departure gate change to red and LED on approach gate change to green. When b
equals true, it can pass the if command and the crossing light flash. To achieve this, students
use “tic toc” system, they stagger the time frame for left and right LEDs that leftint=0.5 and
rightint=1. If time is greater than the left LED interval,use a.digitalWrite command to turn the left
LED on and right off and then increase the left LED interval by 1 second. If time is greater than
the right LED interval,turn the right LED on and left off and then increase the right LED interval
by 1 second.
The final result of this program can make the train move faster when leave the the station and
slower when approach the road and station. When it approach the road, the crossing gate will
close and the crossing will flash. And when the train is moving, the green light in the gate in
front of it will turn on and the red light in the gate behind it will turn on.
Here is the list of command in the program.
Variable Name
Uses
approach
Help to track the ports.
departure
Help to track the ports.
int
Set variable for 1 second intervals.
b
Help to estimate if the crossing lights need to flash.
time
The running time of the program.
leftint
Stagger the time frame for left LED.
8
rightint
Stagger the time frame for right LED.
Table 2: Variable names and uses
Arduino Commands
Descriptions
a=arduino('COM3')
Connect to the Arduino, COM port number 3.
a.servoAttach(1)
Connect to the servo motor which controls the crossing gate.
a.servoStatus
Check the servo status.
a.servoWrite(n,θ)
Control the servo and move the crossing gate.
a.pinMode(n,'output')
Issued the LEDs.
a.digitalWrite(n,0)
a.digitalWrite(n,1)
Turn the LED on or off. 0 means off and 1 means on.
a.motorRun(1,'forward')
a.motorRun(1,'release')
Make the train move forward or release
a.motorSpeed(1, X)
Set the speed of the train.
a.analogRead
Help to collect data from the break beam sensors.
Table 3: Arduino Commands and Descriptions
Program description for general users
This program is build to make the train system work as required. Students use the Matlab and
Arduino system to make it. In the code, students first connect with the arduino system. They can
use it to control the servo motor that can control the crossing gate, the train’s status and the
LEDs. To control the LEDs in the system, they also need to issued the LED first. After the
prepare of the lab, students then go to set the initial status of the train system. The crossing
gate is vertical, the LEDs are turned off and the train moves forward with its highest speed. To
make the system work without stop, students create an infinite while loop. In the while loop, they
make three part to determine what system need to do. They estimate the train’s location with
the help of break beam sensors in the train system. The two sensors are in the approach gate
and departure gate. When the train pass through the two gates, the sensor can collect and send
the data. If the train pass through the gate, students write code that can change the status of the
train, LEDs and crossing gate. When the train pass through the approach gate, the train will
move slow, the crossing gate will be horizontal, the crossing light flash, the LED on departure
gate change to green and LED on approach gate change to red. When the train pass through
the departure gate, the train will move fast, the crossing gate will be vertical, the crossing light
stop flash, the LED on departure gate change to red and LED on approach gate change to
green. To control the flash of the crossing lights, students use a time function. If the light need to
9
flash, time will start and compare with the time interval of left and right LEDs that set by
students. Light will turn on if the time bigger than its interval. The LED’s time interval will also
add an interval after the flash each time. In that way, the left and right LEDs can alternatively
flash. That’s the final effect of the program.
Results
The data that was collected from the lab is found below. The first task was to connect the
Arduino control module to the computer, the data shows the serial port that attaches the Arduino
Module to be COM3. Task two asked to find the vertical gate value. This value was found to
be vertical at 160 using the a.servoWrite(n,θ) command code. Task three was used to set up
the LED’s and find which LED turns on first on the crossing light. Using the a.digitalWrite(15,1)
the first LED to turn on was the left LED. Task four required the train to move, using the
command code a.motorSpeed(1,X), the train would move at different speeds depending on the
number x that was input. Task five involved setting up the break beam sensors, The two
sensors were labeled approach and departure and were used for later in lab. The results are
found in the figures below.
Figure 4: Unobstructed sensor values
Figure 5: Obstructed Sensor Values
The commands that were developed in task one through five allowed for start up conditions to
be met. Task six observes the train moving around the track while the program runs a
continuous while loop. With an operating train, task seven finds the values of mph given the
speed of the train using the motorSpeed command code as well as tic and toc to determine the
speed. The results are shown below.
Table 1: Train Speed times
10
Discussion
Using the data that was acquired through the lab, the data was useful in programming the
MATLAB script that was needed to complete the scenario. During the train run, the train
successfully slowed while in the town and speed up to a highest speed allowed while in the
country. While the train was in the town it was observed that the crossing gate fell to a
horizontal position and the lights flashed in alternating blinks. The team also added lights to the
gates to turn green and red while the train was entering and leaving the respective gate.
The obstacles that were faced while writing the final code was having the train lower it’s speed
in town and then raising it’s speed when leaving town. This was overcome by writing one single
while script instead of writing two separate while codes to run the departure portion of the track
and the arrival portion of the track. The team also had issues with getting started on the flow
chart. The issue was solved by trial and error, eventually agreeing on a flow chart idea that
helped with visualizing the final script. After meeting three times over the course of the three
labs, the team was able to develop a working final script to be used in Lab C.
The potential errors in the data gathering were that the train had see through windows, this
meant that it was possible for the laser to pass through the window causing a flaw in the timing
as the program would not account for a new record and continue to record time until the next
pass. Since MATLAB only runs the code that was programmed other potential error would be
from human error from wrong programing. Some errors that occurred were when we attempted
to use our script in lab C we found that the code was running opposite, to fix this we simply
switched our departure and arrival times from the gate.
Conclusion
11
The Train project (Problem Solving Lab) enabled students to work as a team to solve a realworld programming scenario by using problem-solving strategies. The Train project also gave a
chance for students to apply the knowledge of MATLAB on real-world problem solving.
From the Train project, the operation of train was the same with the real-world scenario. Before
the train crossed the road, the crossing gate off to close the road, and the crossing light flashing
to remind people there will be a train passing here. When the train near the train station, the
train speed slow down, and green light on the departure gate was on, the red light on the
approach gate was off. When train depart, train speed increase, the crossing light off, crossing
gate open, and the light on the departure and approach gate changeover.
Because the train never stop unless engineer stop it, engineer need while loop in MATLAB to
meet this point. Use the code “if a.analogRead(approach)>350” and
“a.analogRead(departure)>350” to set two different condition that the train arrive and depart.
Use tic and toc, and set time interval to make the flashing light.
The Train project gave a chance for students to experience the programming for real-world
scenario, and students learned how to design and write the program they want by using
programming knowledge. All in all, Train Project was helpful for student future or engineering
career, and provide help when they facing more difficult and complex problems.
12
Appendix
MATLAB code
%Group M
%%
clear all;
close all;
delete(instrfindall);
clc;
%Connect to the arduino.
a=arduino('COM3')
%Connect to the servo motor that controls the crossing gate.
a.servoAttach(1)
%Check the servo.
a.servoStatus
%Control the servo and make the gate vertical.
a.servoWrite(1,55)
%Issued the LEDs.
a.pinMode(14,'output')
a.pinMode(15,'output')
a.pinMode(6,'output')
a.pinMode(7,'output')
a.pinMode(8,'output')
a.pinMode(9,'output')
%create variables to track the ports.
approach=2;
departure=3;
%%
%Turn off all six LEDs.
a.digitalWrite(15,0)
a.digitalWrite(14,0)
a.digitalWrite(6,0);
a.digitalWrite(7,0);
a.digitalWrite(8,0);
a.digitalWrite(9,0);
%Make the train move forward with highest speed allowed.
a.motorRun(1,'forward')
a.motorSpeed(1, 255)
%Set variable for 1 second intervals.
int=1;
%Set variable to estimate if the crossing lights need to flash.
b=false;
13
%Start the "stopwatch".
tic;
%Run the program infinite long.
while 1
%If the train arrived the approach gate, make the crossing gate
%horizontal, the crossing light flash, the train move slower, the LED
%on departure gate change to green and LED on approach gate change to
%red.
if a.analogRead(approach)>350
%Change the status of crossing gate.
a.servoWrite(1,160);
%Change the status of LEDs
a.digitalWrite(15,0);
a.digitalWrite(6,1);
a.digitalWrite(7,0);
a.digitalWrite(8,0);
a.digitalWrite(9,1);
%Change the speed of train.
a.motorSpeed(1, 170);
%Make the crossing lights flash.
b=true;
end
time=toc;
%Estimate if the crossing lights need to flash.
if b
%Stagger the time frame for left and right LEDs.
leftint=0.5;
rightint=1;
%If time is greater than the left LED interval,turn the left LED on and right off.
if time>leftint
% Change the status of the crossing light.
a.digitalWrite(15,1);
a.digitalWrite(14,0);
% Increase the left LED interval by 1 second.
leftint=leftint+int;
end
%If time is greater than the right LED interval,turn the right LED on and left off.
if time>rightint
% Change the status of the crossing light.
a.digitalWrite(15,0);
a.digitalWrite(14,1);
% Increase the right LED interval by 1 second.
rightint=rightint+int;
end
14
end
%If the train arrived the departure gate, make the crossing gate
%vertical, the crossing light stop flash, the train move faster, the
%LED on departure gate change to red and LED on approach gate change to
%green.
if a.analogRead(departure)>350;
%Change the status of crossing gate.
a.servoWrite(1,55);
%Change the status of LEDs
a.digitalWrite(15,0);
a.digitalWrite(14,0);
a.digitalWrite(6,0);
a.digitalWrite(7,1);
a.digitalWrite(8,1);
a.digitalWrite(9,0);
%Change the speed of train.
a.motorSpeed(1, 255)
%Make the crossing lights stop flash.
b=false;
end
end
15
Flowchart
16
Was this manual useful for you? yes no
Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Download PDF

advertisement