Advertisement
hockeygoalie5

Train

Nov 18th, 2015
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 6.58 KB | None | 0 0
  1. function runTrain()
  2.     delete(timerfindall);
  3.     clear;
  4.     close all;
  5.     delete(instrfindall);
  6.     clc;
  7.    
  8.     simulator = input('Run the train on a simulator? (0/1)\nsimulator = ');
  9.     dSpeed = input('What is the speed limit in the departure phase?\ndSpeed = ');
  10.     aSpeed = input('What is the speed limit in the approach phase?\naSpeed = ');
  11.     runTime = input('For how many seconds should the train run? (0 for forever)\nrunTime = ');
  12.    
  13.     if(simulator > 0)
  14.         train = arduino_sim('COM3');
  15.     else
  16.         train = arduino('COM3');
  17.     end
  18.    
  19.     % Load audio file
  20.     [soundY, soundFs] = audioread('train.wav');
  21.    
  22.     % Initialize LEDs; 6 / 7 is red / green on approach. 8 / 9 is red /
  23.     % green on departure. 14 and 15 are the crossing LEDs.
  24.     train.pinMode(6, 'output');
  25.     train.pinMode(7, 'output');
  26.     train.pinMode(8, 'output');
  27.     train.pinMode(9, 'output');
  28.     train.pinMode(14, 'output');
  29.     train.pinMode(15, 'output');
  30.    
  31.     % Set up the crossing gate and default it to raised.
  32.     train.servoAttach(1);
  33.     train.servoWrite(1, 90);
  34.    
  35.     % Initialize the variable which tracks which LED on the crossing gate
  36.     % is currently on.
  37.     global flashState;
  38.     flashState = 1;
  39.    
  40.     % Found using ratio 3 / stopDelay = aSpeed / 170, where the 3 is the
  41.     % time it took to stop at a speed of 170.
  42.     stopDelay = 510 / aSpeed;
  43.    
  44.     % Starts the train 2.5 seconds after it stops
  45.     startTimer = timer('StartDelay', stopDelay + 2.5, 'Name', 'startTimer');
  46.     startTimer.TimerFcn = {@startTrain, train, aSpeed};
  47.    
  48.     % Stop the train at the calculated stop delay.
  49.     stopTimer = timer('StartDelay', stopDelay, 'Name', 'stopTimer');
  50.     stopTimer.TimerFcn = {@stopTrain, train};
  51.    
  52.     % Alternate the crossing lights every .5 seconds for as many times
  53.     % until the period adds up to about the time it takes the train to
  54.     % stop. When the timer finishes, both lights are turned off.
  55.     flashTimer = timer('Period', .5, 'TasksToExecute', ceil((stopDelay / .5)) + 1, 'ExecutionMode', 'fixedRate', 'Name', 'flashTimer');
  56.     flashTimer.TimerFcn = {@flashLights, train};
  57.     flashTimer.StopFcn = {@turnOffLights, train};
  58.    
  59.     % Raise the gate when the train stops.
  60.     raiseGateTimer = timer('StartDelay', stopDelay, 'Name', 'raiseGateTimer');
  61.     raiseGateTimer.TimerFcn = {@raiseGate, train};
  62.    
  63.     % Lower the gate .5 seconds after the train hits approach.
  64.     lowerGateTimer = timer('StartDelay', .5, 'Name', 'lowerGateTimer');
  65.     lowerGateTimer.TimerFcn = {@lowerGate, train};
  66.    
  67.     % Start the train.
  68.     train.motorRun(1, 'forward');
  69.     train.motorSpeed(1, dSpeed);
  70.    
  71.     % Makes sure the train is stopped when the program exits.
  72.     cleanupFcn = onCleanup(@() cleanup(train));
  73.    
  74.     % The program will 'officially' start when the train first hits the
  75.     % approach sensor, so assume it's not initially in approach.
  76.     inApproach = 0;
  77.    
  78.     % Run until the time reaches the specify length, or always run if 0 was
  79.     % specified.
  80.     curTime = tic;
  81.     while toc(curTime) <= runTime || runTime == 0
  82.         % Read the sensor's values
  83.         approach = train.analogRead(3);
  84.         departure = train.analogRead(2);
  85.        
  86.         % Knowing these values will help debug if neccessary.
  87.         clc
  88.         disp('In approach?')
  89.         disp(inApproach)
  90.         disp('Approach:')
  91.         disp(approach)
  92.         disp('Departue:')
  93.         disp(departure)
  94.        
  95.         % If the approach sensor goes high and the train is currently not
  96.         % in approach.
  97.         if approach > 300
  98.             if ~inApproach
  99.                 % Turn the green departure light on and the red approach
  100.                 % light on.
  101.                 train.digitalWrite(6, 1);
  102.                 train.digitalWrite(7, 0);
  103.                 train.digitalWrite(8, 0);
  104.                 train.digitalWrite(9, 1);
  105.                
  106.                 % Tell the program the train's in approach now.
  107.                 inApproach = 1;
  108.                
  109.                 % Set the train to the approach speed.
  110.                 train.motorSpeed(1, aSpeed);
  111.                
  112.                 % All timers start at once and are timed relative to when
  113.                 % the train should stop. Some timers will execute
  114.                 % simultaneously.
  115.                 start(flashTimer);
  116.                 start(raiseGateTimer);
  117.                 start(lowerGateTimer);
  118.                 start(stopTimer);
  119.                 start(startTimer);
  120.                
  121.                 % Play the train sound. The timers are asynchronous, so
  122.                 % this executes immediatally after the timers are started.
  123.                 sound(soundY, soundFs);
  124.             end
  125.         % If the departure sensor goes high and the train is in approach.
  126.         elseif departure > 300
  127.             if inApproach
  128.                 % Turn the green approach light on and the red departure
  129.                 % light on.
  130.                 train.digitalWrite(6, 0);
  131.                 train.digitalWrite(7, 1);
  132.                 train.digitalWrite(8, 1);
  133.                 train.digitalWrite(9, 0);
  134.                
  135.                 % We're no longer in the approach phase.
  136.                 inApproach = 0;
  137.                
  138.                 % Just in case the sensor triggers when it's not supposed
  139.                 % to, cancel the timers to prevent unexpected behavior.
  140.                 stop(timerfindall);
  141.                
  142.                 % Return to departure speed.
  143.                 train.motorSpeed(1, dSpeed);
  144.             end
  145.         end
  146.     end
  147. end
  148.  
  149. function flashLights(~, ~, train)
  150.     % Each time this fcn runs, flashState alternates its value, so we can
  151.     % alternate the lights by checking its value.
  152.     global flashState;
  153.     if flashState == 1
  154.         train.digitalWrite(14, 1);
  155.         train.digitalWrite(15, 0);
  156.     else
  157.         train.digitalWrite(14, 0);
  158.         train.digitalWrite(15, 1);
  159.     end
  160.     flashState = ~flashState;
  161. end
  162.  
  163. function turnOffLights(~, ~, train)
  164.     train.digitalWrite(14, 0);
  165.     train.digitalWrite(15, 0);
  166. end
  167.  
  168. function stopTrain(~, ~, train)
  169.     train.motorRun(1, 'release');
  170. end
  171.  
  172. function raiseGate(~, ~, train)
  173.     train.servoWrite(1, 90);
  174. end
  175.  
  176. function lowerGate(~, ~, train)
  177.     train.servoWrite(1, 180)
  178. end
  179.  
  180. function startTrain(~, ~, train, aSpeed)
  181.     train.motorRun(1, 'forward');
  182.     train.motorSpeed(1, aSpeed);
  183. end
  184.  
  185. function cleanup(train)
  186.     train.motorRun(1, 'release');
  187.     % Cancel the timers to make sure the train doesn't start back up after
  188.     % the program stops.
  189.     stop(timerfindall);
  190. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement