Master_Node

Dependencies:   XBeeLib_Master mbed

Revision:
1:f28c98c04cbd
Parent:
0:a6ed4a102013
Child:
2:bd4ef614ebb6
--- a/main.cpp	Mon Mar 05 11:19:32 2018 +0000
+++ b/main.cpp	Tue Mar 27 14:12:26 2018 +0000
@@ -1,86 +1,187 @@
 #include "mbed.h"
 #include "XBeeLib.h"
 #include "buzzer.h"
-  
-using namespace XBeeLib;
+#include "bootChime.h"
+#include "breathLed.h"
 
-uint8_t systemStatus = 0;
-uint8_t emergency = 1;
+using namespace XBeeLib;
+    
+typedef enum
+{
+    BOOTING,    
+    NORMAL_OPERATION,
+    EMERGENCY,
+    WAIT_FOR_RESET,
+    HEARTBEAT_ERROR,
+} state_t;
 
-// Activate Timer
-Ticker loopTimer;
+state_t currentState = BOOTING;
+
 
-// Create DigiMesh Object
-XBeeDM DMLocalNode = XBeeDM(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 115200);
+/*
+STATE_MESSAGES:
+HEARTBEAT   =   49
+EMERGENCY   =   50  
+RESET       =   51
+*/
+
+int receivedData; 
 
-//------------Define Pinouts------------//
-DigitalOut statusLED(PA_11);
+//------------Define Digimesh Variables------------//
+#define channel 0x18
+#define networkId 0xD163
+#define powerLevel 4
+#define nodeId "masterNode"
+//------------Define Digimesh Variables------------//
+
+//------------Define Pinouts-----------------------//
+PwmOut statusLED(PA_11);
 DigitalOut powerLED(PA_8);
-DigitalOut SCL(PA_6); 
-DigitalOut SDA(PA_5);
 
-DigitalIn emergencyStop(PB_7);
+DigitalIn localEstop(PB_7);
 DigitalIn resetButton(PB_0);
-DigitalIn spareButton(PA_12);
-//------------Define Pinouts------------//
+//------------Define Pinouts-----------------------//
 
 // Initialize Buzzer
 Beep piezo(PB_4);
 
-void initialize(){
-    powerLED = 1;
-    // Initialize device. Read the relevant parameters.
-    RadioStatus radioStatus = DMLocalNode.init();
-    if (radioStatus != Success) {
-        printf("Initialization failed!");
-    }
-    systemStatus = 1;
+Serial pc(USBTX, USBRX);
+
+Ticker checkResetButton, stateHandlerTimer, checkLocalEstopTimer, handleMessagesTimer, timerLED;
+Timer runSystemChecksTimer, sendHeartbeatTimer;
+
+void radioConfig(XBeeDM &DMLocalNode){    
+    RadioStatus temp = DMLocalNode.init();
+    temp = DMLocalNode.set_channel(channel);
+    temp = DMLocalNode.set_network_id(networkId);
+    temp = DMLocalNode.set_power_level(powerLevel);
+    temp = DMLocalNode.set_node_identifier("masterNode");
+    temp = DMLocalNode.write_config();
+}
+
+void boot(XBeeDM &DMLocalNode){
+    radioConfig(DMLocalNode);
+    bootChime();
+}
+
+void statusLedFunction(){
+    breathLed(currentState);
+}
+
+void errorHandle(){
+    statusLED = !statusLED;
+}
+
+static void receive_cb(const RemoteXBeeDM& remote, bool broadcast, const uint8_t *const data, uint16_t len){
+    receivedData = (data[0]-3);
 }
 
-static void sendHeartbeat(XBeeDM &DMLocalNode)
-{
-    const char data[] = "1";
+static void sendMessage(XBeeDM &DMLocalNode, char *sendData){
+    const char data[] = {*sendData};
     const uint16_t data_len = strlen(data);
- 
+
     const TxStatus txStatus = DMLocalNode.send_data_broadcast((const uint8_t *)data, data_len);
+    
+    powerLED = !powerLED;
+}
+
+void sendHeartbeat(XBeeDM &DMLocalNode){
+    if (currentState == NORMAL_OPERATION){
+        sendMessage(DMLocalNode, "49");
+    }
 }
 
-void checkBuzzer(){
-    if (systemStatus == 1){
-        piezo.beep(2400, 0.2);
-    }       
+void checkLocalEstop(){
+    if (localEstop == 0){
+        currentState = EMERGENCY;
+    }
+    if (localEstop == 1 && currentState == EMERGENCY){
+        currentState = WAIT_FOR_RESET;
+    }
+}
+
+void checkLocalReset(XBeeDM &DMLocalNode){
+    if (resetButton == 1 && localEstop == 1 && currentState == WAIT_FOR_RESET){
+        sendMessage(DMLocalNode, "101");
+        wait(0.1f);
+        currentState = NORMAL_OPERATION;
+    }
+    else if (resetButton == 1 && localEstop == 0){
+        piezo.beep(2400, 1.0f);
+    }
 }
 
-void checkLoop() 
-{
-    sendHeartbeat(DMLocalNode);
-    checkBuzzer();
+void stateHandler(XBeeDM &DMLocalNode){
+    static int currentMessageCounter;
+    
+    if (currentState == NORMAL_OPERATION){
+        checkLocalEstop();
+        
+        timerLED.detach();
+        timerLED.attach(&statusLedFunction, 0.03f);
+    }
+    if (currentState == EMERGENCY && currentMessageCounter != 5){
+        sendMessage(DMLocalNode, "50");
+        currentMessageCounter++;
+        
+        timerLED.detach();
+        statusLED = 1;
+        piezo.beep(4800, 0.1f);
+    }
+    else if (currentState == WAIT_FOR_RESET){
+        currentMessageCounter = 0;
+    }
+}
+
+void handleMessages(XBeeDM &DMLocalNode){
+    if (receivedData == 50){
+        currentState = EMERGENCY;
+    }
+}
+
+void runSystemChecks(XBeeDM &DMLocalNode){
+    checkLocalEstop();
+    stateHandler(DMLocalNode);
+    checkLocalReset(DMLocalNode);
+    handleMessages(DMLocalNode);
 }
 
-void checkStatus(){
-    if (emergencyStop == 0){
-        emergency = true;
-    }
+int main() {
+    XBeeDM DMLocalNode = XBeeDM(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 115200);
+
+    boot(DMLocalNode);
+
+    DMLocalNode.register_receive_cb(&receive_cb);
+
+    runSystemChecksTimer.start();
+    sendHeartbeatTimer.start();
+
+    timerLED.attach(&statusLedFunction, 0.03f); 
   
-    if (emergencyStop == 1 && resetButton == 1){
-        emergency = false;
-    }
-                
-    if (emergency == true){
-        systemStatus = 0;
+    currentState = NORMAL_OPERATION;
+    
+    static int heartbeatCounter;
+    static int systemTaskCounter;
+    
+    pc.baud(115200);
+
+    while(1){
+        systemTaskCounter = runSystemChecksTimer.read_ms();
+        heartbeatCounter = sendHeartbeatTimer.read_ms();
+    
+        if ( systemTaskCounter > 200){
+            DMLocalNode.process_rx_frames(); 
+            runSystemChecks(DMLocalNode);
+            systemTaskCounter = 0; 
+            runSystemChecksTimer.reset();
+            pc.printf("%d", receivedData);
+            receivedData = 0; 
+        }    
+        
+        if (heartbeatCounter > 257){
+            sendHeartbeat(DMLocalNode);   
+            heartbeatCounter = 0; 
+            sendHeartbeatTimer.reset();
+        }
     }
-    else{
-        systemStatus = 1;
-    }
-                    
-    statusLED = systemStatus;
-}
-int main(){
-    initialize();  
-    
-    loopTimer.attach(checkLoop, 0.5f) ;
-
-    for(;;){
-        checkStatus();
-    }
-} 
\ No newline at end of file
+}
\ No newline at end of file