13
1 Problem Solving Lab – Final Report _________________________________________________ Submitted to: Richard Busick Raghav Nijhara Created by: Table E Shawn Hoch Josh Jones Mike Lawless Derek Burkhart Engineering 1181.01 The Ohio State University Columbus, Oh 25 November 2013

Problem Solving Lab – Final Report Submitted to · PDF fileProblem Solving Lab – Final Report _____ Submitted to: Richard Busick Raghav ... In Part B and C, ... Write a Train Control

Embed Size (px)

Citation preview

1

Problem Solving Lab – Final Report _________________________________________________

Submitted to: Richard Busick Raghav Nijhara

Created by:

Table E Shawn Hoch Josh Jones

Mike Lawless Derek Burkhart

Engineering 1181.01

The Ohio State University Columbus, Oh

25 November 2013

2

Table of contents Executive Summary---------------------------------------------------------- Introduction--------------------------------------------------------------------- Purpose of the Problem Solving Project----------------------- Overall Goals of the Problem Solving Project---------------- Objectives of Each Individual Task----------------------------- Task 1--------------------------------------------------------- Task 2--------------------------------------------------------- Task 3--------------------------------------------------------- Task 4--------------------------------------------------------- Task 5--------------------------------------------------------- Task 6--------------------------------------------------------- Task 7--------------------------------------------------------- Program Description---------------------------------------------------------- Program Description for Developers---------------------------- Program Description for Users----------------------------------- Discussion----------------------------------------------------------------------- Conclusion and Recommendations--------------------------------------- Appendix------------------------------------------------------------------------- Speed Calculation--------------------------------------------------- Final Code------------------------------------------------------------- Outline Shawn-------------------------------------------------------- Outline Josh----------------------------------------------------------- Outline Mike----------------------------------------------------------- Outline Derek--------------------------------------------------------- Final Outline----------------------------------------------------------- List of Figures Figure 1-------------------------------------------------------------------------- List of Tables Table 1--------------------------------------------------------------------------- Table 2--------------------------------------------------------------------------- Table 3---------------------------------------------------------------------------

3

Executive Summary

The Objective for Group E was to create a computer code using MATLAB for a train model at 1/160 scale. The program was to decrease or increase train speed, lower or raise a crossing gate, and flash warning lights at the gate. Two sensors initiated the commands, one before the crossing gate and one after the crossing gate. The train setup was controlled by MATLAB code and ran Arduino microcontroller. The code was to run infinitely without human interference once initiated. The main objective was achieved over a period of three weeks and three separate parts of the lab. In part A of this lab, the basic commands of MATLAB in correlation with the Arduino controller were learned. A series of 7 tasks were completed. From these tasks, we set up the Arduino controller by connecting it to the PC. Then the crossing gate, sensors, train speed, and LEDs were tested using basic code. Using the knowledge gained in the previous tasks, we were able to run the train at a known speed in order to calculate the actual speed of the train. In Part B and C, the procedure was simply developing code in order to achieve the objective assigned. For Group E, we had some problems achieving this objective running the code as one file. In other words, we were able to turn on the LEDs, close and open the gate, and adjust train speed separately, but we were not able to correctly run them together. Our final code used the sensors to change the speed of the train, lower the gate, and turn on the LEDs. However, the LED lights did not blink and the gate did not lower after a delay. These problems can be attributed to our misunderstanding of the tic-toc command in MATLAB. We believe that our code was structured correctly, but the tic and toc commands were placed incorrectly. Therefore, we feel that a better understanding of this function would have led to a more efficient written code.

Introduction Purpose of the Problem Solving Project The purpose of the problem-solving project is to take a set of assigned functions and develop a code to carry them out. This purpose was achieved by using our prior knowledge of MATLAB code in agreement with the commands recognized by the Arduino microcontroller. The problem was not like the previous seven labs, which had a specific solution. This problem had a variety of ways of being solved. The Arduino was a very important part of the project as it translated the MATLAB code we created in order to run the train the way that we intended. Overall Goals of the Problem Solving Project

• Understand the correlation between MATLAB code and the Arduino microcontroller

• Run the train at two different speeds of 170 and 255. Fast in the country area and slow in the city area.

• Turn on the LED lights in flashing order when the train approaches the crossing area.

• After a short time of flashing lights, close the gate in order to prevent crossing when the train passes.

• The Train must run through the loop infinitely without human interaction.

4

Objectives of Each Individual Task of Problem Solving Project

• Task 1: Connect your PC to the Arduino The Arduino was connected to the COM:3 port in order to have the correct interaction from the MATLAB code that is typed on the PC. From this connection, commands created on the PC could be run on the train track.

• Task 2: Set up the Servo motor that controls the crossing gate By using the command a.sevoWrite (1,degree) the gate will move to that angle. For example, we used the given a.servoWrite (1,165) to close the gate and a.servoWrite(1,10) to open the gate. This knowledge can be used in our final code to close the gate when the train approaches, and open the gate when the train departs.

• Task 3: Set up and test the Crossing Gate LEDs In this task, the command a.digitalWrite(14,0) or (15,0) was used in MATLAB in order to turn the LEDs off. The command (14,1) or (15,1) was used to turn the lights on. 14 and 15 were the two ports used for the two different lights. The knowledge of how to turn the lights on and off was needed for the final code in order to make them blink when the train was approaching.

• Task 4: Move the Train The movement of the train is the key part to the problem-solving lab. The speed of the train was controlled by the command a.Motorspeed. At a.Motorspeed(1,0) the train is totally stopped. The two speeds that the train was ran at was (1,170) in the back country and (1,255) in the city.

• Task 5: Test the Break Beam Sensors In the final code, the break beam sensors initiate the loop for the train. In this task, we tested these sensors. The command used in MATLAB was a.analogRead. When the sensor is broken, the loop is initiated in the fnal code.

• Task 6: Write a Train Control Program In this task, a train program was created that combined the previous tasks into one. In this case, when the departure sensor is broken, the train speeds up. Hence a.analogread was used to read the sensor, when it was broken; a.motorspeed is used to speed the train up. Therefore, the motor speed is initiated by the reading of the sensor, as it will be in the final code. The approach sensor operates the same way, except it slows the train instead of increasing the speed.

• Task 7: Calculate the Train Speed In this task, the program developed was run in order to obtain the speed of the train. The calculation of the train speed used the distance traveled and the time elapsed during a run of the train. By obtaining these two values, the formula of V=D/T can be used in order to find the speed. The distance is found by taking 2*pi*r of the train track. The time elapsed is found by using the tic-toc function on MATLAB. The speed was found at various motor speeds on the track.

5

Program Description Program Description for Developers In writing the final code, we used a variety of commands in MATLAB, which were translated by the Arduino microcontroller to run the train. The commands included the following: a.analogRead – This function reads the sensors, when a sensor is broken this function can initiate other commands. a.servoWrite – This controls the gate in the train problem. In our final code, we used this command to open and close the gate. a.motorSpeed – This controls the speed that the train moves at. For our code, the motor speed changed by where the train is on the track. The position of the train is determined by the sensors. a.digitalWrite – The LED lights are controlled by this command. There are two ports on the Arduino controller that control the lights, 14 and 15. In the final code, the lights are supposed to blink, so both ports must be used.

• The code begins by starting the train (a.motorSpeed(1,255)) • When the train breaks the approach sensor, the sensor is read in order to

initiate other commands (a.analogRead(approach)) • The train slows down (a.motorSpeed(1,170)) • The LED lights flash between port 14 and 15 using and toc function. When

toc < 0.5 seconds (a.digitalWrite(14,1)a.digitalWrite(15,0)). When toc is < 0.5 +0.5 seconds(a.digitalWrite(14,0)a.digitalWrite(15,1)).

• Using the tic-toc function, a time of 1 second will be used to delay the lowering of the gate. In order to lower the gate, the command a.servoWrite(1,170) will be used

• This process will continue until the train breaks the departure gate, which will be read by a.analogRead(departure)

• At this point the loop is reset • The train increases speed by a.motorSpeed(1,255) • The LEDs stop flashing by a.digitalWrite(14,0)a.digitalWrite(15,0) • The gate raises by a.servoWrite(1,10) • The code will run in an infinite loop until manually stopped

Program Description for Users The main objective of this program is to run a train with a set of various commands around a track without human interaction. The arduino controller acts as a human in this program. The Arduino takes the commands that MATLAB has written in code and turns them into actual processes. For example, MATLAB

6

writes a.motorSpeed(1,255). The Arduino controller translates this like a human would in order to run the train at a fast speed of 255.

• The train starts moving at a fast speed • A sensor placed before the crossing gated is passed • The train slows down to a slower speed • Lights on the Crossing gate begin to flash • The gate closes • This continues until the train moves past a sensor after the crossing gate • The train speeds back up to a fast speed • The lights shut off • The gate opens • This will continue until manually shut off

The list above describes the program as it runs. However, this is not what is read on MATLAB. The purpose of the Arduino microcontroller is taking the complicated code and translating it into usable functions.

Discussion In the beginning of the lab, the team was to follow a guide line that would explain all of the actions to complete in Matlab in order to connect the Arduino board, run the motor, lights, and drop the gate. After finishing this initial script, the team had a general knowledge of what was needed in order to operate the train track. The next step was to begin building an infinite loop that would continue to run, operating the train lights, and drop the gate. Initially, the team started with a while loop that allowed the motor to change its speed as it passed through both gates, going slower when moving towards the cross gate, and faster in the back country. Afterwards, the team integrated the gate dropping, without the delay. Afterwards is where we ran into trouble and had to begin trouble shooting, as we needed to get the lights to blink back and forth. In the end, we were able to use the tic and toc command to control the lights. Some problems we ran into with this were getting the loop to end after the train passed the next gate and go and repeat the previous loop. We fixed this by using a flag in order to exit the loop. Another problem we faced was that the team had used the simulator while not in lab, however, when we tried it on the actual model train, everything was in reverse from how it ran in the simulator, causing us to have to restructure our code at the last minute. This caused us to no longer have time to finalize our code with everything working. However, we did end up with the gate dropping, still without a delay, and some lights coming on, with the train slowing and speeding up properly.

Conclusion From this lab, we have concluded that in order to run the train properly, the team must use several problem solving skills, troubleshooting, all while understanding the code and communicating clearly with one another. The team had to be able to analyze where the code was wrong, or where there were troubles. To do this,

7

we used comment and uncomment in order to isolate sections of the code. In order to correctly run the train, we had to iron out bugs and try to use the most efficient methods possible. For the team, the overall objective of getting every aspect of the train system running properly was not met. Partially due to a confusion caused by the train simulation that made our model train run backwards. Also, another issue was getting help from several T.A.’s, but being told to do something different than what the other T.A. had told us such as using while loops while inside of a while loop, or using just one while loop. Recommendations: Make sure that all T.A.s are on the same page for the lab. Also, be sure to have all trains and train simulations running together. Also, some parts of the documentation in the lab procedure need to be clarified, especially with correctly getting the train simulation to work, as the way it was worded could be taken several different ways.

Appendix

Speed Calculation Calculated speed of train in miles per hour using the following formula: Radius (in)*pi() * 1 (ft) * 1 (mi) * 60 (sec) * 60 (min) Time (sec) 12(in) 5280 (ft) 1 (min) 1 (hr) Final Code

clear all; close all; delete (instrfindall); clc; clear all; close all; clc; a=arduino('COM3') a.servoAttach(1) a.servoStatus a.servoWrite(1,170) a.pinMode(14,'output') a.pinMode(15,'output') a.digitalWrite(15,1) a.digitalWrite(14,1) a.motorRun(1,'forward') a.motorSpeed(1,230) approach=2 departure=3 approach=2

8

a.analogRead(approach); a.analogRead(approach); flag=1; while (1) a.analogRead(approach); a.analogRead(approach); if a.analogRead(approach)>300 && flag==1 tic; flag=0; a.servoWrite(1,90) a.motorSpeed(1,255) a.digitalWrite(14,0) a.digitalWrite(15,0) end a.analogRead(departure); a.analogRead(departure); if a.analogRead(departure)>200 && flag==0 a.motorSpeed(1,170) t_gate=tic if toc(t_gate)>1.5 a.servoWrite(1,170) end a.digitalWrite(14,1) a.digitalWrite(15,1) toc flag=1 end end

Outline Shawn Hoch While z1=1 speed =255; gate=up; lights =off if z=1 and approach>300 tic; z1=0; speed=170; lights blinky time=? gate=down if z=0 and depart>300 z=1 Outline Josh Jones Set flag=1 While flag=1

9

If flag set off Tic Begin flash lights If tic =1 Toc Lower gate end If Flag set off again Stop lights Tic If tic=1 Toc Raise gate. End Infinate loop Outline Mike Lawless

• Set  the  counter  to  0  • Use  tic  to  start  timer  • While  Counter  is  less  than  1,  run  train  at  normal  speed  (255)    • Use  toc  when  break  beam  is  crossed  • Change  Counter  to  1,  run  train  at  speed  (170)  • After  a  certain  elapsed  time  told  by  the  toc  function  the  servoWrite(gate)  goes  to  170  • As  the  beam  is  crossed,  turn  on  LEDS  with  digitalWrite(14,1)  digitalWrite(15,1  • After  departure  beam  is  crossed,  the  LEDS  turn  to  off;  the  counter  resets  and  the  train  

will  run  through  the  loop  again.   Outline Derek Burkhart Final Outline Table E

10

• Train breaks departure sensor, speed increases to 255 • Flag is set to 1 • Train breaks approach sensor, speed decreases to 170 • Lights begin to blink between (14,15) • After .5 seconds gate lowers • Train breaks departure sensor and loop resets

Initial Values - z1, z2, z3 =1 %flags; speed = 255; gate =90; While z1=1 and approach > 300 Tic; z1=0; z2=0 If z2=0 and z3=1 and approach >300 Z2=1 Fprintf (time is toc)

speed=170; Lights blink Remainder time calc loops alternate lights At time= .5 sec Gate =170 If z2=1 and depart > 300 Fprintf (Time is toc) Z2=0; speed=255; gate=90 Flow Chart

11

12

Table 1

Task 1

Serial Port which the Arduino is attached

4

Task 2

Value at which the gate is

vertical (10-70) 90

Task 3

The first LED to turn on (left or

right) left

Table 2

Task 5 Unobstructed Sensor values 84 84 84

Is there a pattern to

these values? Briefly describe the pattern

<100

Obstructed Sensor values 399 399 399

Is there a pattern to

these values? Briefly describe the pattern

>300

Are the values always

consistent? Why might this be

yes, can be used for logical expressions

13

important when it comes to writing

a program? Table 3

Table 7

motorSpeed (0-255)

Time for 1/2 circuit (sec) Train Speed (mph)

255 2.405 0.88

230 2.615 0.83

200 2.93 0.75

190 3.4 0.64

170 3.55 0.62