Advertisement
Guest User

RTS_SignalController

a guest
Dec 6th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.08 KB | None | 0 0
  1. /* Main.ino file generated by New Project wizard
  2.  *
  3.  * Created:   Wed Nov 13 2019
  4.  * Processor: Arduino Uno
  5.  * Compiler:  Arduino AVR (Proteus)
  6.  */
  7.  
  8. #include <ArduinoSTL.h>
  9. #include <vector>
  10.  
  11. #define T1 12000
  12. #define T2 7000
  13. #define T3 18000
  14. #define T4 9000
  15. #define T5 500
  16. #define T6 35000
  17. #define T7 25000
  18. #define T8 30000
  19. #define T9 4000
  20.  
  21. #define LEFT_LED_PIN 6
  22. #define MIDDLE_LED_PIN 5
  23. #define RIGHT_LED_PIN 4
  24. #define BOTTOM_LED_PIN 7
  25. #define AUX_LED_PIN 8
  26. #define DEBUG_LED_PIN 3
  27.  
  28. #define Line16_BTN 11
  29. #define Line15_BTN 10
  30. #define Line26_BTN 9
  31. #define Start_BTN 12
  32.  
  33. unsigned long state_timer = 0;
  34. unsigned long finishAt = 0;
  35. unsigned long hyperstate_timer = 0;  
  36.  
  37. typedef enum State {
  38.   S0_IDLE, S1_WAIT, S2, S3, S4_L, S5_LL, S6_LF, S7_F, S8_FF,
  39.   S9_FR, S10_R, S11_RR, S12_LR ,S13_INHIBIT_S, S14_INHIBIT_U, S15_ALL
  40. };
  41.  
  42. enum Mode {u, lf, fr, lr, all};
  43.  
  44. enum Line {
  45.   L1 = 1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11, L12, L13, L14, L15, L16,
  46.   L17, L18, L19, L20, L21, L22, L23, L24, L25, L26, L27, L28, L29, L30, L31
  47. };
  48.  
  49. typedef struct {
  50.     uint8_t mode:3;
  51.     uint8_t PhaseExt_L:1; // XL
  52.     uint8_t PhaseExt_F:1; // XF
  53.     uint8_t PhaseExt_R:1; // XR
  54. } input_config;
  55.  
  56. uint8_t Start = 0;
  57.  
  58. input_config sig = {fr, 1, 1, 1};
  59.  
  60. const std::vector <uint16_t> L_lines {L16};
  61. const std::vector <uint16_t> LF_lines {L16, L15};
  62. const std::vector <uint16_t> F_lines {L15};
  63. const std::vector <uint16_t> All_lines {L15, L16, L26};
  64. const std::vector <uint16_t> FR_lines {L15, L26};
  65. const std::vector <uint16_t> R_lines {L26};
  66. const std::vector <uint16_t> LR_lines {L16, L26};
  67.  
  68. void timed_finite_state_machine_run(input_config mode, uint16_t line);
  69. uint8_t check_line(uint16_t line, const std::vector <uint16_t> &lines_set);
  70. void read_transit_line();
  71.  
  72. volatile State state = S0_IDLE;
  73. volatile uint16_t line = 0;
  74. uint8_t isTransitPresent = 0;
  75. uint8_t pinToAnalize = 0;
  76.  
  77.  
  78. void setup() {
  79.   pinMode(LEFT_LED_PIN, OUTPUT);
  80.   pinMode(MIDDLE_LED_PIN, OUTPUT);
  81.   pinMode(RIGHT_LED_PIN, OUTPUT);
  82.   pinMode(BOTTOM_LED_PIN, OUTPUT);
  83.   pinMode(AUX_LED_PIN, OUTPUT);
  84.   pinMode(DEBUG_LED_PIN, OUTPUT);
  85.   pinMode(Line15_BTN, INPUT);
  86.   pinMode(Line16_BTN, INPUT);
  87.   pinMode(Line26_BTN, INPUT);
  88.   pinMode(Start_BTN, INPUT);
  89.  
  90.   state_timer = millis();
  91.   Serial.begin(9600);
  92. }
  93.  
  94. void loop() {
  95.   Serial.print("Current state: ");
  96.   Serial.println(state, DEC);
  97.   timed_finite_state_machine_run(sig, line);
  98.  
  99.   delay(10);
  100.  
  101.   if (digitalRead(Start_BTN) == LOW) {
  102.     delay(10);
  103.     if (digitalRead(Start_BTN) == LOW) {
  104.       Start = 1;
  105.     }
  106.   }
  107.   else {
  108.     Start = 0;
  109.   }
  110.  
  111.   delay(10);
  112.  
  113.   digitalWrite(LEFT_LED_PIN, (
  114.   state == S1_WAIT || state == S2 || state == S3 || state == S4_L || state == S5_LL || state == S6_LF ||
  115.   state == S12_LR || state == S13_INHIBIT_S || state == S14_INHIBIT_U || state == S15_ALL
  116.   ) ? HIGH : LOW);
  117.  
  118.   delay(10);
  119.  
  120.   digitalWrite(MIDDLE_LED_PIN, (
  121.   state == S1_WAIT || state == S2 || state == S3 || state == S6_LF || state == S7_F || state == S8_FF ||
  122.   state == S9_FR || state == S13_INHIBIT_S || state == S14_INHIBIT_U || state == S15_ALL
  123.   ) ? HIGH : LOW);
  124.  
  125.   delay(10);
  126.  
  127.   digitalWrite(RIGHT_LED_PIN, (
  128.   state == S1_WAIT || state == S2 || state == S3 || state == S9_FR || state == S10_R || state == S11_RR ||
  129.   state == S12_LR || state == S13_INHIBIT_S || state == S14_INHIBIT_U || state == S15_ALL
  130.   ) ? HIGH : LOW);
  131.  
  132.   delay(10);
  133.  
  134.   digitalWrite(BOTTOM_LED_PIN, (
  135.   state == S2 || state == S4_L || state == S5_LL || state == S6_LF || state == S7_F || state == S8_FF ||
  136.   state == S9_FR || state == S10_R || state == S11_RR || state == S12_LR || state == S15_ALL
  137.   ) ? HIGH : LOW);
  138.  
  139.   delay(10);
  140.  
  141.   digitalWrite(AUX_LED_PIN, state == S0_IDLE ? HIGH : LOW);
  142.  
  143.   delay(10);
  144.  
  145.   read_transit_line();
  146.  
  147.   delay(10);
  148. }
  149.  
  150. void timed_finite_state_machine_run(const input_config config, const uint16_t line) {
  151.   switch(state)
  152.   {
  153.     case S0_IDLE:
  154.       if (Start == 0) {
  155.         state = S0_IDLE;
  156.       }
  157.       else if (config.mode == all) {
  158.         state = S14_INHIBIT_U;
  159.       }
  160.       else {
  161.         state = S1_WAIT;
  162.       }
  163.       break;
  164.        
  165.     case S1_WAIT:
  166.       if (Start == 0) {
  167.         state = S0_IDLE;
  168.       }
  169.       else if (check_line(line, All_lines)) {
  170.         state = S2;
  171.         finishAt = millis() + T9;
  172.         hyperstate_timer = millis();
  173.       }
  174.       else {
  175.         state = S1_WAIT;
  176.       }
  177.       state_timer = millis();
  178.       break;
  179.  
  180.     case S2:      
  181.       if (millis() < finishAt) {
  182.         unsigned long currentMillis = millis();
  183.         if (currentMillis - hyperstate_timer >= T5) {
  184.           hyperstate_timer = currentMillis;
  185.           state = S3;
  186.         }
  187.       }
  188.      
  189.       if (millis() - state_timer >= T9) {
  190.         if (check_line(line, L_lines)&((config.mode == fr)|(config.mode == u))) {
  191.           state = S4_L;
  192.         }
  193.         else if (check_line(line, LF_lines)&(config.mode == lf)) {
  194.           state = S6_LF;
  195.         }
  196.         else if (check_line(line, F_lines)&((config.mode == lr)||(config.mode == u))) {
  197.           state = S7_F;
  198.         }
  199.         else if (check_line(line, FR_lines)&(config.mode == fr)) {
  200.           state = S9_FR;
  201.         }
  202.         else if (check_line(line, R_lines)&((config.mode == lf)|(config.mode == u))) {
  203.           state = S10_R;
  204.         }
  205.         else if (check_line(line, LR_lines)&(config.mode == lr)) {
  206.           state = S12_LR;
  207.         }
  208.         else {
  209.           state = S1_WAIT;
  210.         }
  211.         state_timer = millis();
  212.       }
  213.       break;
  214.  
  215.     case S3:      
  216.       if (millis() < finishAt) {
  217.         unsigned long currentMillis = millis();
  218.         if (currentMillis - hyperstate_timer >= T5) {
  219.           hyperstate_timer = currentMillis;
  220.           state = S2;
  221.         }
  222.       }
  223.      
  224.       if (millis() - state_timer >= T9) {
  225.         if (check_line(line, L_lines)&((config.mode == fr)|(config.mode == u))) {
  226.           state = S4_L;
  227.         }
  228.         else if (check_line(line, LF_lines)&(config.mode == lf)) {
  229.           state = S6_LF;
  230.         }
  231.         else if (check_line(line, F_lines)&((config.mode == lr)||(config.mode == u))) {
  232.           state = S7_F;
  233.         }
  234.         else if (check_line(line, FR_lines)&(config.mode == fr)) {
  235.           state = S9_FR;
  236.         }
  237.         else if (check_line(line, R_lines)&((config.mode == lf)|(config.mode == u))) {
  238.           state = S10_R;
  239.         }
  240.         else if (check_line(line, LR_lines)&(config.mode == lr)) {
  241.           state = S12_LR;
  242.         }
  243.         else {
  244.           state = S1_WAIT;
  245.         }
  246.         state_timer = millis();
  247.       }
  248.       break;
  249.  
  250.     case S4_L:
  251.       if (millis() - state_timer >= T3) {
  252.         if (check_line(line, L_lines)&config.PhaseExt_L) {
  253.           state = S5_LL;
  254.         }
  255.         else {
  256.           state = S13_INHIBIT_S;
  257.         }
  258.       state_timer = millis();  // re-initialise state_timer for next period
  259.       }
  260.       break;
  261.  
  262.     case S5_LL:
  263.       if (millis() - state_timer >= T4) {
  264.         if (config.mode == all) {
  265.           state = S14_INHIBIT_U;
  266.         }
  267.         else {
  268.           state = S13_INHIBIT_S;
  269.         }
  270.         state_timer = millis();
  271.       }
  272.       break;
  273.  
  274.     case S6_LF:
  275.       if (millis() - state_timer >= T3) {
  276.         if (check_line(line, L_lines)&config.PhaseExt_L) {
  277.           state = S5_LL;
  278.         }
  279.         else if (check_line(line, F_lines)&config.PhaseExt_F) {
  280.           state = S8_FF;
  281.         }
  282.         else {
  283.           state = S13_INHIBIT_S;
  284.         }
  285.         state_timer = millis();
  286.       }
  287.       break;
  288.  
  289.     case S7_F:
  290.       if (millis() - state_timer >= T1) {
  291.         if (check_line(line, F_lines)&config.PhaseExt_F) {
  292.           state = S8_FF;
  293.         }
  294.         else {
  295.           state = S13_INHIBIT_S;
  296.         }
  297.         state_timer = millis();
  298.       }
  299.       break;
  300.  
  301.     case S8_FF:
  302.       if (millis() - state_timer >= T2) {
  303.         if(config.mode == all) {
  304.           state = S14_INHIBIT_U;
  305.         }
  306.         else {
  307.           state = S13_INHIBIT_S;
  308.         }
  309.         state_timer = millis();
  310.       }
  311.       break;
  312.  
  313.     case S9_FR:
  314.       if (millis() - state_timer >= T3) {
  315.         if (check_line(line, F_lines)&config.PhaseExt_F) {
  316.           state = S8_FF;
  317.         }
  318.         else if (check_line(line, R_lines)&config.PhaseExt_R) {
  319.           state = S11_RR;
  320.         }
  321.         else {
  322.           state = S13_INHIBIT_S;
  323.         }
  324.         state_timer = millis();
  325.       }
  326.       break;
  327.  
  328.     case S10_R:
  329.       if (millis() - state_timer >= T3) {
  330.         if (check_line(line, R_lines)&config.PhaseExt_R) {
  331.           state = S11_RR;
  332.         }
  333.         else {
  334.           state = S13_INHIBIT_S;
  335.         }
  336.         state_timer = millis();
  337.       }
  338.       break;
  339.  
  340.     case S11_RR:
  341.       if (millis() - state_timer >= T4) {
  342.         if (config.mode == all) {
  343.           state = S14_INHIBIT_U;
  344.         }
  345.         else {
  346.           state = S13_INHIBIT_S;
  347.         }
  348.         state_timer = millis();
  349.       }
  350.       break;
  351.  
  352.     case S12_LR:
  353.       if (millis() - state_timer >= T3) {
  354.         if (check_line(line, L_lines)&config.PhaseExt_L) {
  355.           state = S5_LL;
  356.         }
  357.         else if (check_line(line, R_lines)&config.PhaseExt_R) {
  358.           state = S11_RR;
  359.         }
  360.         else {
  361.            state = S13_INHIBIT_S;
  362.         }
  363.         state_timer = millis();
  364.       }
  365.       break;
  366.  
  367.     case S13_INHIBIT_S:
  368.       if (millis() - state_timer >= T8) {
  369.         state = S1_WAIT;
  370.         state_timer = millis();
  371.       }
  372.       break;
  373.  
  374.     case S14_INHIBIT_U:
  375.       if (millis() - state_timer >= T6) {
  376.         if (Start == 0) {
  377.           state = S0_IDLE;
  378.         }
  379.         else {
  380.           state = S15_ALL;
  381.         }
  382.         state_timer = millis();
  383.       }
  384.       break;
  385.  
  386.     case S15_ALL:
  387.       if (millis() - state_timer >= T7) {
  388.         if (check_line(line, L_lines)&config.PhaseExt_L) {
  389.           state = S5_LL;
  390.         }
  391.         else if (check_line(line, F_lines)&config.PhaseExt_F) {
  392.           state = S8_FF;
  393.         }
  394.         else if (check_line(line, R_lines)&config.PhaseExt_R) {
  395.           state = S11_RR;
  396.         }
  397.         else {
  398.           state = S14_INHIBIT_U;
  399.         }
  400.         state_timer = millis();
  401.       }
  402.       break;
  403.  
  404.     default:
  405.       state = S0_IDLE;
  406.   }
  407. }
  408.  
  409. void read_transit_line() {
  410.   if(!isTransitPresent) {
  411.     if (digitalRead(Line15_BTN) == LOW) {
  412.       Serial.println("L15 here");
  413.       line = L15;
  414.       pinToAnalize = Line15_BTN;
  415.       isTransitPresent = 1;
  416.     }
  417.  
  418.     if (digitalRead(Line16_BTN) == LOW) {
  419.       Serial.println("L16 here");
  420.       line = L16;
  421.       pinToAnalize = Line16_BTN;
  422.       isTransitPresent = 1;
  423.     }
  424.  
  425.     if (digitalRead(Line26_BTN) == LOW) {
  426.       Serial.println("L26 here");
  427.       line = L26;
  428.       pinToAnalize = Line26_BTN;
  429.       isTransitPresent = 1;
  430.     }
  431.   }
  432.   else {
  433.     if (digitalRead(pinToAnalize) == HIGH) {
  434.       isTransitPresent = 0;
  435.     }
  436.   }
  437. }
  438.  
  439. uint8_t check_line(const uint16_t line, const std::vector <uint16_t> &lines_set) {
  440.    for (size_t i = 0; i < lines_set.size(); ++i) {
  441.       if (line == lines_set[i]) {
  442.          return 1;
  443.       }
  444.    }
  445.    return 0;
  446. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement