Advertisement
hockeygoalie5

Untitled

Nov 10th, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.76 KB | None | 0 0
  1. function runTrain()
  2.     % The main function, this is what is called first. I use this function to initialize all of our variables and set up the arduino.
  3.     delete(timerfindall);
  4.     clear;
  5.     close all;
  6.     delete(instrfindall);
  7.     clc;
  8.  
  9.     % Get the speed limits and set up the arduino for either sim or physical
  10.     simulator = input('Run the train on a simulator? (0/1)\nsimulator = ');
  11.     dSpeed = input('What is the speed limit in the departure phase?\ndSpeed = ');
  12.     aSpeed = input('What is the speed limit in the approach phase?\naSpeed = ');
  13.     if(simulator > 0)
  14.         train = arduino_sim('COM3');
  15.     else
  16.         train = arduino('COM3');
  17.     end
  18.  
  19.     % Set up the LEDs
  20.     train.pinMode(14, 'output');
  21.     train.pinMode(15, 'output');
  22.  
  23.     % Set up the gates
  24.     train.servoAttach(1);
  25.  
  26.     % Global allows other functions to edit this variable. This is used to flash the lights.
  27.     global flashState;
  28.     flashState = 1;
  29.  
  30.     % Set up all the timing functions
  31.     startTimer = timer('StartDelay', 4.5, 'Name', 'startTimer');
  32.     startTimer.TimerFcn = {@startTrain, train, aSpeed};
  33.     stopTimer = timer('StartDelay', 2, 'Name', 'stopTimer');
  34.     stopTimer.TimerFcn = {@stopTrain, train};
  35.     flashTimer = timer('Period', .5, 'TasksToExecute', 4, 'ExecutionMode', 'fixedRate', 'Name', 'flashTimer');
  36.     flashTimer.TimerFcn = {@flashLights, train};
  37.     flashTimer.StopFcn = {@turnOffLights, train};
  38.     gateTimer = timer('StartDelay', .45, 'ExecutionMode', 'fixedRate', 'Period', 1.55, 'TasksToExecute', 2, 'Name', 'gateTimer');
  39.     gateTimer.TimerFcn = {@toggleGate, train};
  40.    
  41.     % Start the train
  42.     train.motorRun(1, 'forward');
  43.     train.motorSpeed(1, dSpeed);
  44.    
  45.     % Move into the function that actually runs the train
  46.     manageTrain(train, stopTimer, flashTimer, gateTimer, startTimer, dSpeed, aSpeed);
  47. end
  48.  
  49. function manageTrain(train, stopTimer, flashTimer, gateTimer, startTimer, dSpeed, aSpeed)
  50.     % Pause execution until the approach sensor is broken
  51.     train.analogRead(2);
  52.     while train.analogRead(2) < 200
  53.     end
  54.    
  55.     % Once the sensor is broken, the while loop ends and this code runs. The train is slowed, and our timers are started.
  56.     % All timers run at the same time.
  57.     train.motorSpeed(1, aSpeed);
  58.     % Lowers the gate after .45s, then raises it after 1.55s using the toggleGate() function
  59.     start(gateTimer);
  60.     % Alternates the lights every .5 seconds, 4 times = 2 total seconds using flashLights()
  61.     % After the 2 seconds, makes sure both lights are off using turnOffLights()
  62.     start(flashTimer);
  63.     % Stops the train after 2 seconds using stopTrain()
  64.     start(stopTimer);
  65.     % Starts the train after 4.5 seconds using startTrain()
  66.     start(startTimer);
  67.  
  68.     % Wait for the departure sensor to trip
  69.     train.analogRead(3);
  70.     while train.analogRead(3) < 200
  71.     end
  72.  
  73.     % Return to normal speed
  74.     train.motorSpeed(1, dSpeed);
  75.    
  76.     % Call itself to loop
  77.     manageTrain(train, stopTimer, flashTimer, gateTimer, startTimer, dSpeed, aSpeed);
  78. end
  79.  
  80. function flashLights(~, ~, train)
  81.     global flashState;
  82.     if flashState == 1
  83.         train.digitalWrite(14, 1);
  84.         train.digitalWrite(15, 0);
  85.     else
  86.         train.digitalWrite(14, 0);
  87.         train.digitalWrite(15, 1);
  88.     end
  89.     flashState = ~flashState;
  90. end
  91.  
  92. function turnOffLights(~, ~, train)
  93.     train.digitalWrite(14, 0);
  94.     train.digitalWrite(15, 0);
  95. end
  96.  
  97. function stopTrain(~, ~, train)
  98.     train.motorRun(1, 'release');
  99. end
  100.  
  101. function toggleGate(~, ~, train)
  102.     if train.servoRead(1) == 180;
  103.         train.servoWrite(1, 90);
  104.     else
  105.         train.servoWrite(1, 180);
  106.     end
  107. end
  108.  
  109. function startTrain(~, ~, train, aSpeed)
  110.     train.motorRun(1, 'forward');
  111.     train.motorSpeed(1, aSpeed);
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement