Master_Node

Dependencies:   XBeeLib_Master mbed

Files at this revision

API Documentation at this revision

Comitter:
basvuyk
Date:
Wed Jul 25 09:14:25 2018 +0000
Parent:
1:f28c98c04cbd
Commit message:
Master_Node

Changed in this revision

XBeeLib.lib Show annotated file Show diff for this revision Revisions of this file
buzzer.lib Show diff for this revision Revisions of this file
config.h Show annotated file Show diff for this revision Revisions of this file
functions/bootChime.cpp Show diff for this revision Revisions of this file
functions/bootChime.h Show diff for this revision Revisions of this file
functions/breathLed.cpp Show diff for this revision Revisions of this file
functions/breathLed.h Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/XBeeLib.lib	Tue Mar 27 14:12:26 2018 +0000
+++ b/XBeeLib.lib	Wed Jul 25 09:14:25 2018 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/Digi-International-Inc/code/XBeeLib/#055802660f2e
+https://os.mbed.com/users/basvuyk/code/XBeeLib_Master/#055802660f2e
--- a/buzzer.lib	Tue Mar 27 14:12:26 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://os.mbed.com/users/Reniboy/code/buzzer/#9133a863f1dd
--- a/config.h	Tue Mar 27 14:12:26 2018 +0000
+++ b/config.h	Wed Jul 25 09:14:25 2018 +0000
@@ -5,4 +5,4 @@
 #define SYNC_OPS_TIMEOUT_MS     1000
 #define RADIO_TX                PA_9
 #define RADIO_RX                PA_10
-#define RADIO_RESET             PF_1
\ No newline at end of file
+#define RADIO_RESET             PA_8
\ No newline at end of file
--- a/functions/bootChime.cpp	Tue Mar 27 14:12:26 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,19 +0,0 @@
-#include "buzzer.h"
-#include "mbed.h"
-
-namespace mbed {
-
-    Beep piezo(PB_4);
-
-    int bootChime()    /* Function definition */
-    {
-        int freq = 300;
-        for (int i = 0; i < 1; i++){
-            freq = freq*1.5;
-            piezo.beep(freq, 0.2);
-            wait(0.2);    
-        }
-
-        return 0;
-    }
-}
\ No newline at end of file
--- a/functions/bootChime.h	Tue Mar 27 14:12:26 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-#ifndef BO0TCHIME_H_  
-#define BO0TCHIME_H_
-
-#include "mbed.h"
-
-namespace mbed{
-
-void bootChime();  
-}
-#endif // BO0TCHIME_H_
\ No newline at end of file
--- a/functions/breathLed.cpp	Tue Mar 27 14:12:26 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,32 +0,0 @@
-#include "mbed.h"
-
-namespace mbed {
-    void breathLed(int systemState){
-        static double ledVal = 0; 
-        static int direction = 1;
-        PwmOut statusLED(PA_11);
-        statusLED.period(0.001f);                           // Set pwm frequency 1000Hz
-
-        if (systemState == 1){
-            if (ledVal > 0.6){
-                direction = 0;
-            }
-            if (ledVal < 0.03){
-                direction = 1;
-            }
-    
-            if (direction == 1){
-                ledVal = ledVal + 0.01;
-                statusLED = ledVal;      
-            } 
-            else{
-                ledVal = ledVal - 0.01;
-                statusLED = ledVal;      
-            }
-        }
-        else{
-            statusLED = 0;
-            ledVal = 0;
-        }
-    }
-}
\ No newline at end of file
--- a/functions/breathLed.h	Tue Mar 27 14:12:26 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-#ifndef BREATHLED_H_  
-#define BREATHLED_H_
-
-#include "mbed.h"
-
-namespace mbed{
-
-void breathLed(int systemError);  
-}
-#endif // BREATHLED_H_
\ No newline at end of file
--- a/main.cpp	Tue Mar 27 14:12:26 2018 +0000
+++ b/main.cpp	Wed Jul 25 09:14:25 2018 +0000
@@ -1,8 +1,5 @@
 #include "mbed.h"
 #include "XBeeLib.h"
-#include "buzzer.h"
-#include "bootChime.h"
-#include "breathLed.h"
 
 using namespace XBeeLib;
     
@@ -12,44 +9,39 @@
     NORMAL_OPERATION,
     EMERGENCY,
     WAIT_FOR_RESET,
-    HEARTBEAT_ERROR,
 } state_t;
 
 state_t currentState = BOOTING;
 
-
-/*
-STATE_MESSAGES:
-HEARTBEAT   =   49
-EMERGENCY   =   50  
-RESET       =   51
-*/
+char *HEARTBEAT_MSG = "49";
+char *EMERGENCY_MSG = "50"; 
+char *RESET_MSG = "101";
 
 int receivedData; 
 
 //------------Define Digimesh Variables------------//
-#define channel 0x18
-#define networkId 0xD163
+#define channel 0x15
+#define networkId 0xD160
 #define powerLevel 4
 #define nodeId "masterNode"
+#define baudRate 230400
 //------------Define Digimesh Variables------------//
 
 //------------Define Pinouts-----------------------//
-PwmOut statusLED(PA_11);
-DigitalOut powerLED(PA_8);
+DigitalOut statusLED(PB_5);
+PwmOut powerLED(PB_4);
 
-DigitalIn localEstop(PB_7);
-DigitalIn resetButton(PB_0);
+DigitalIn localEstop(PA_11);
+DigitalIn resetButton(PA_12);
 //------------Define Pinouts-----------------------//
 
-// Initialize Buzzer
-Beep piezo(PB_4);
+//-----------TIMER CONFIG----------//
+Timer runHeartbeatTimer, runSystemChecksTimer;
+#define systemCheckTimeout 5    // ms   // Check system state every 5ms
+#define heartbeatTimeout 200    // ms   // Check heartbeat every 100ms
+//-----------TIMER CONFIG----------//
 
-Serial pc(USBTX, USBRX);
-
-Ticker checkResetButton, stateHandlerTimer, checkLocalEstopTimer, handleMessagesTimer, timerLED;
-Timer runSystemChecksTimer, sendHeartbeatTimer;
-
+// Initiate XBEE Module
 void radioConfig(XBeeDM &DMLocalNode){    
     RadioStatus temp = DMLocalNode.init();
     temp = DMLocalNode.set_channel(channel);
@@ -61,19 +53,11 @@
 
 void boot(XBeeDM &DMLocalNode){
     radioConfig(DMLocalNode);
-    bootChime();
-}
-
-void statusLedFunction(){
-    breathLed(currentState);
-}
-
-void errorHandle(){
-    statusLED = !statusLED;
+    powerLED.write(255);
 }
 
 static void receive_cb(const RemoteXBeeDM& remote, bool broadcast, const uint8_t *const data, uint16_t len){
-    receivedData = (data[0]-3);
+    receivedData = (data[0]-3); // -3 for offset
 }
 
 static void sendMessage(XBeeDM &DMLocalNode, char *sendData){
@@ -81,13 +65,11 @@
     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");
+        sendMessage(DMLocalNode, HEARTBEAT_MSG);
     }
 }
 
@@ -100,36 +82,53 @@
     }
 }
 
+void checkReset(XBeeDM &DMLocalNode){
+    if (resetButton == 1 && localEstop == 1 && currentState == NORMAL_OPERATION){
+        sendMessage(DMLocalNode, RESET_MSG);
+    }
+}
+
+/* Function that avoids the misuse of the reset button,
+If the button is continuously pressed, this function 
+disables the reset button to prevent excessive network load */
 void checkLocalReset(XBeeDM &DMLocalNode){
-    if (resetButton == 1 && localEstop == 1 && currentState == WAIT_FOR_RESET){
-        sendMessage(DMLocalNode, "101");
-        wait(0.1f);
+    static int resetCounter = 0, sendReset = 1;
+    
+    if (resetButton == 1 && resetCounter < 5){
+        resetCounter++;
+        sendReset = 1;
+    }
+    
+    if (resetCounter == 5 && resetButton == 1){
+        resetCounter = 50;
+        sendReset = 0; 
+    }
+    
+    if (resetButton == 0 && resetCounter > 0){
+        resetCounter--;
+    }
+    
+    if (sendReset == 1 && resetButton == 1 && localEstop == 1 && currentState == WAIT_FOR_RESET){
+        sendMessage(DMLocalNode, RESET_MSG);
         currentState = NORMAL_OPERATION;
     }
-    else if (resetButton == 1 && localEstop == 0){
-        piezo.beep(2400, 1.0f);
-    }
 }
 
 void stateHandler(XBeeDM &DMLocalNode){
     static int currentMessageCounter;
     
     if (currentState == NORMAL_OPERATION){
-        checkLocalEstop();
-        
-        timerLED.detach();
-        timerLED.attach(&statusLedFunction, 0.03f);
+        checkLocalEstop(); 
+        statusLED.write(1);
     }
     if (currentState == EMERGENCY && currentMessageCounter != 5){
-        sendMessage(DMLocalNode, "50");
+        sendMessage(DMLocalNode, EMERGENCY_MSG);
         currentMessageCounter++;
-        
-        timerLED.detach();
-        statusLED = 1;
-        piezo.beep(4800, 0.1f);
+        statusLED = 0;
     }
     else if (currentState == WAIT_FOR_RESET){
         currentMessageCounter = 0;
+        statusLED = 0;
     }
 }
 
@@ -147,41 +146,37 @@
 }
 
 int main() {
-    XBeeDM DMLocalNode = XBeeDM(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 115200);
+    XBeeDM DMLocalNode = XBeeDM(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, baudRate);
 
     boot(DMLocalNode);
 
     DMLocalNode.register_receive_cb(&receive_cb);
 
     runSystemChecksTimer.start();
-    sendHeartbeatTimer.start();
-
-    timerLED.attach(&statusLedFunction, 0.03f); 
-  
+    runHeartbeatTimer.start();
+    
     currentState = NORMAL_OPERATION;
     
-    static int heartbeatCounter;
-    static int systemTaskCounter;
-    
-    pc.baud(115200);
+    static int systemTaskCounter, heartbeatCounter;
 
     while(1){
         systemTaskCounter = runSystemChecksTimer.read_ms();
-        heartbeatCounter = sendHeartbeatTimer.read_ms();
-    
-        if ( systemTaskCounter > 200){
+        heartbeatCounter = runHeartbeatTimer.read_ms();
+        
+        if (systemTaskCounter > systemCheckTimeout){
             DMLocalNode.process_rx_frames(); 
-            runSystemChecks(DMLocalNode);
             systemTaskCounter = 0; 
             runSystemChecksTimer.reset();
-            pc.printf("%d", receivedData);
+            runSystemChecks(DMLocalNode);
             receivedData = 0; 
-        }    
+        }  
         
-        if (heartbeatCounter > 257){
-            sendHeartbeat(DMLocalNode);   
+        if (heartbeatCounter > heartbeatTimeout){
+            sendHeartbeat(DMLocalNode);
+            checkReset(DMLocalNode);
+
             heartbeatCounter = 0; 
-            sendHeartbeatTimer.reset();
-        }
+            runHeartbeatTimer.reset();
+        }   
     }
 }
\ No newline at end of file