Master_Node

Dependencies:   XBeeLib_Master mbed

main.cpp

Committer:
basvuyk
Date:
2018-03-27
Revision:
1:f28c98c04cbd
Parent:
0:a6ed4a102013
Child:
2:bd4ef614ebb6

File content as of revision 1:f28c98c04cbd:

#include "mbed.h"
#include "XBeeLib.h"
#include "buzzer.h"
#include "bootChime.h"
#include "breathLed.h"

using namespace XBeeLib;
    
typedef enum
{
    BOOTING,    
    NORMAL_OPERATION,
    EMERGENCY,
    WAIT_FOR_RESET,
    HEARTBEAT_ERROR,
} state_t;

state_t currentState = BOOTING;


/*
STATE_MESSAGES:
HEARTBEAT   =   49
EMERGENCY   =   50  
RESET       =   51
*/

int receivedData; 

//------------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);

DigitalIn localEstop(PB_7);
DigitalIn resetButton(PB_0);
//------------Define Pinouts-----------------------//

// Initialize Buzzer
Beep piezo(PB_4);

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 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 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 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);
}

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); 
  
    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();
        }
    }
}