Advertisement
Guest User

Objects in Space Arduino Sample Code

a guest
Jun 5th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.55 KB | None | 0 0
  1.  
  2. #define DELIMITER '\n'
  3. #define SYNC_WORD   451
  4. #define SYNC_REPLY  "452"
  5. #define REQUEST_BOOLEAN "NIB="
  6. #define REQUEST_COMMAND "CMD="
  7. #define COMMAND_EXC "EXC="
  8. #define COMMAND_SHUTDOWN "SHT"
  9.  
  10. #define COMMAND_MAKE_ACTIVE "ACT"
  11.  
  12. #define GENERATOR_ACTIVE "PWR_REACTOR_ON"
  13. #define EMCON_ACTIVE "EMCON_MODE"
  14. #define TOGGLE_EMCON "TOGGLE_EMCON"
  15. #define TOGGLE_REACTOR "TOGGLE_REACTOR"
  16.  
  17. #define STATE_NONE    0
  18. #define STATE_SYNC    1
  19. #define STATE_ACTIVE  2
  20.  
  21. int currentState = STATE_NONE;
  22.  
  23. const int generatorPin = 4;
  24. const int emconPin = 5;
  25.  
  26. const int emconSwitchPin = 6;
  27. const int generatorSwitchPin = 7;
  28.  
  29. const int errorPin = 13;
  30.  
  31. char line[20];
  32. int currentElement = 0;
  33. bool buttonDown = false;
  34.  
  35. #define BUTTON_TOGGLE true
  36. #define BUTTON_STATECHANGE false
  37.  
  38. struct Button
  39. {
  40.   Button()
  41.   : lastState(HIGH)
  42.   , pin(-1)
  43.   , commandNumber(-1)
  44.   , buttonType(BUTTON_TOGGLE)
  45.   , lastDebounceTime(0)
  46.   , altCommand(-1)
  47.   {}
  48.  
  49.   Button(int p, int command, bool bt)
  50.   : lastState(HIGH)
  51.   , pin(p)
  52.   , commandNumber(command)
  53.   , buttonType(bt)
  54.   , lastDebounceTime(0)
  55.   , altCommand(-1)
  56.   {}
  57.  
  58.   int pin;
  59.   int lastState;
  60.   int commandNumber;
  61.   int altCommand;
  62.   bool buttonType;
  63.   long lastDebounceTime;  // the last time the output pin was toggled
  64. };
  65.  
  66. const long DEBOUNC_DELAY = 50;
  67.  
  68.  
  69. #define NUM_BUTTONS 2
  70. Button buttons[NUM_BUTTONS];
  71.  
  72. void flashLed()
  73. {
  74.   digitalWrite(generatorPin, LOW);
  75.   digitalWrite(generatorPin, HIGH);
  76.   delay(500);
  77.   digitalWrite(generatorPin, LOW);
  78. }
  79.  
  80. void setError(int p)
  81. {
  82.   digitalWrite(errorPin, p);
  83. }
  84.  
  85. void showError()
  86. {
  87.   digitalWrite(errorPin, HIGH);
  88. }
  89.  
  90. void disableErrorLight()
  91. {
  92.   digitalWrite(errorPin, LOW);
  93. }
  94.  
  95. void enableLight(int pin)
  96. {
  97.   digitalWrite(pin, HIGH);
  98. }
  99.  
  100. void disableLight(int pin)
  101. {
  102.   digitalWrite(pin, LOW);
  103. }
  104.  
  105. void configureLights()
  106. {
  107.   pinMode(generatorPin, OUTPUT);
  108.   pinMode(emconPin, OUTPUT);
  109.   pinMode(errorPin, OUTPUT);
  110. }
  111.  
  112. void configurePin(int p)
  113. {
  114.   pinMode(p, INPUT_PULLUP);
  115.   //digitalWrite(p, HIGH);
  116. }
  117.  
  118. void configureInput()
  119. {
  120.   configurePin(emconSwitchPin);
  121.   buttons[0].pin = emconSwitchPin;
  122.   buttons[0].commandNumber = 2;
  123.  
  124.   configurePin(generatorSwitchPin);
  125.   buttons[1].pin = generatorSwitchPin;
  126.   buttons[1].commandNumber = 3;
  127. }
  128.  
  129. void sendBoolRequest(char* req, int channel)
  130. {
  131.   Serial.print(REQUEST_BOOLEAN);
  132.   Serial.print(req);
  133.   Serial.print(",");
  134.   Serial.print(channel);
  135.   Serial.print(DELIMITER);
  136. }
  137.  
  138. void sendCommandRequest(char* req, int channel)
  139. {
  140.   Serial.print(REQUEST_COMMAND);
  141.   Serial.print(req);
  142.   Serial.print(",");
  143.   Serial.print(channel);
  144.   Serial.print(DELIMITER);
  145. }
  146.  
  147. void executeCommand(int cmdNumber)
  148. {
  149.   Serial.print(COMMAND_EXC);
  150.   Serial.print(cmdNumber);
  151.   Serial.print(DELIMITER);
  152. }
  153.  
  154. void sendSimpleCommand(char* req)
  155. {
  156.   Serial.print(req);
  157.   Serial.print(DELIMITER);
  158. }
  159.  
  160. void configureDataRequests()
  161. {
  162.   sendBoolRequest(GENERATOR_ACTIVE, 0);
  163.   sendBoolRequest(EMCON_ACTIVE, 1);
  164.  
  165.   sendCommandRequest(TOGGLE_EMCON, 2);
  166.   //= Button(emconSwitchPin, 2, BUTTON_TOGGLE);
  167.  
  168.   sendCommandRequest(TOGGLE_REACTOR, 3);
  169.   //buttons[1] = Button(-1/*generatorSwitchPin*/, 3, BUTTON_TOGGLE);
  170.  
  171.   sendSimpleCommand(COMMAND_MAKE_ACTIVE);
  172.   currentState = STATE_ACTIVE;
  173.   //showError();
  174. }
  175.  
  176. void processElement()
  177. {
  178.   if (currentState == STATE_ACTIVE) {
  179.     if (strcmp(line, COMMAND_SHUTDOWN) == 0) {
  180.       // Shut down
  181.       disableErrorLight();
  182.       currentState = STATE_NONE;
  183.     } else if (line[0] == '0') {
  184.       // generator
  185.       if (line[2] == '1') {
  186.         enableLight(generatorPin);
  187.       } else {
  188.         disableLight(generatorPin);
  189.       }
  190.     }
  191.       if (line[0] == '1') {
  192.       // EMCON
  193.       if (line[2] == '1') {
  194.         enableLight(emconPin);
  195.       } else {
  196.         disableLight(emconPin);
  197.       }
  198.     } else {
  199.       //showError();
  200.     }
  201.   } else if (currentState == STATE_NONE) {
  202.     // RTG
  203.     if (strcmp(line, SYNC_REPLY) == 0) {
  204.       currentState = STATE_SYNC;
  205.       configureDataRequests();
  206.     }
  207.   }
  208.   currentElement = 0;
  209. }
  210.  
  211. void inputPump()
  212. {
  213.   if (Serial.available() > 0) {
  214.     // read the incoming byte:
  215.     int incomingByte = Serial.read();
  216.     if (incomingByte == DELIMITER) {
  217.       line[currentElement++] = '\0';
  218.       processElement();
  219.     } else {
  220.       line[currentElement++] = incomingByte;
  221.     }
  222.   }
  223. }
  224.  
  225. void outputPump()
  226. {
  227.   for (int i = 0; i < NUM_BUTTONS; i++) {
  228.     if (buttons[i].pin == -1) {
  229.       continue;
  230.     }
  231.     // Input using pull-down resister so they're inverted
  232.     int state = digitalRead(buttons[i].pin);
  233.     if (buttons[i].buttonType == BUTTON_TOGGLE) {
  234.       if (state == LOW && buttons[i].lastState == HIGH) {
  235.         // Pushed
  236.         //flashLed();
  237.         executeCommand(buttons[i].commandNumber);
  238.       }
  239.     } else {
  240.       if (state != buttons[i].lastState) {
  241.         if (state == LOW) {
  242.           executeCommand(buttons[i].altCommand);
  243.         } else if (state == HIGH) {
  244.           executeCommand(buttons[i].commandNumber);
  245.         }
  246.       }
  247.     }
  248.    
  249.     buttons[i].lastState = state;
  250.   }
  251. }
  252.  
  253. void setup() {
  254.   // put your setup code here, to run once:
  255.   configureLights();
  256.   configureInput();
  257.   Serial.begin(9600);
  258. }
  259.  
  260. long int sa = 0;
  261. void syncAttempt()
  262. {
  263.   sa--;
  264.   // Send initialisation code
  265.   if (sa <= 0) {
  266.     sa = 250000;
  267.     Serial.print(SYNC_WORD);
  268.     Serial.print(DELIMITER);
  269.   }
  270. }
  271.  
  272. void loop() {
  273.   if (currentState == STATE_NONE) {
  274.     syncAttempt();
  275.   } else {
  276.    
  277.   }
  278.   inputPump();
  279.  
  280.   if (currentState == STATE_ACTIVE) {
  281.     outputPump();
  282.   }
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement