Gestion du Traffic Adaptatif- Code du mbed coordinateur

Dependencies:   XBeeLib mbed mbed-rtos EthernetInterface

Revision:
17:0694b399bd0d
Parent:
16:b4d5eb2ee378
Child:
18:972fb2937e41
Child:
19:a302b82c4357
--- a/main.cpp	Sun Nov 11 18:01:39 2018 +0000
+++ b/main.cpp	Sun Nov 11 19:40:46 2018 +0000
@@ -1,156 +1,133 @@
-#include "mbed.h"
-#include "rtos.h"
-
-//pierre was here 
+/**
+ * Copyright (c) 2015 Digi International Inc.,
+ * All rights not expressly granted are reserved.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
+ * =======================================================================
+ */
 
-DigitalOut nordSudR(p29);
-DigitalOut nordSudJ(p27);
-DigitalOut nordSudV(p25);
-DigitalOut estOuestR(p12);
-DigitalOut estOuestJ(p14);
-DigitalOut estOuestV(p16);
-DigitalIn boutonPlusNS(p22);
-DigitalIn boutonMoinsNS(p21);
-DigitalIn boutonPlusEO(p19);
-DigitalIn boutonMoinsEO(p20);
-Thread t_eo;
-Thread t_ns;
-Thread t_gestion;
-Thread t_nbVoiture;
-
-int nombreDeVoitureNS = 0;
-int nombreDeVoitureEO = 0;
-
-Mutex dPassage;
-bool bns, beo;
+#include "mbed.h"
+#include "XBeeLib.h"
+#if defined(ENABLE_LOGGING)
+#include "DigiLoggerMbedSerial.h"
+using namespace DigiLog;
+#endif
 
 
-/*void sequenceLumiere(DigitalOut lumiere) {
-    lumiere = 1;
-    Thread::wait(4000);
-    lumiere = 0;
-    Thread::wait(500);
-    lumiere = 1;
-    Thread::wait(500);
-    lumiere = 0;
-    Thread::wait(500);
-    lumiere = 1;
-    Thread::wait(500);
-    lumiere = 0;
-    Thread::wait(500);
-    lumiere = 1;
-    Thread::wait(500);
-    lumiere = 0;
-    Thread::wait(500);
-    lumiere = 1;
-    Thread::wait(500);
-    lumiere=0;
-    Thread::wait(2000);
-}*/
+#define REMOTE_NODE_ADDR64_MSB  ((uint32_t)0x0013A200)
+
+//#error "Replace next define with the LSB of the remote module's 64-bit address (SL parameter)"
+#define REMOTE_NODE_ADDR64_LSB  ((uint32_t)0x40C0E3A1)
+
+#define REMOTE_NODE_ADDR64      UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB)
+
+using namespace XBeeLib;
+
+//feux gauche
+    //rouge
+DigitalOut feux_gauche_rouge(p29);
+    //orange
+DigitalOut feux_gauche_orange(p27);
+    //vert
+DigitalOut feux_gauche_vert(p25);
 
-void ns() {
-    while(1)
-    {
-        if(!beo){
-            //Thread::signal_wait(0x1);
-            dPassage.lock();
-            nordSudR=0;
-            nordSudV=1;
-            Thread::wait(5000);
-            //while(bns){}
-            if(!bns){
-                printf("\r\n YO1 \r\n");
-                nordSudV=0;
-                nordSudJ=1;
-                Thread::wait(2000);
-                nordSudJ=0;
-                nordSudR=1;
-                Thread::wait(1000);
-            }
-            dPassage.unlock();
-        }
-        
-    }
+    //liste des message a envoyer rouge et vert seulement pour le coordinateur
+    ////////////////////////// 
+    /////
+    char rouge = 'r';
+    char vert = 'v';
+    /////
+    uint8_t ok = 0x02;
+    //////////////////////////
+
+
+Serial *log_serial;
+
+/** Callback function, invoked at packet reception */
+static void receive_cb(const RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len)
+{
+    const uint64_t remote_addr64 = remote.get_addr64();
+
+    log_serial->printf("\r\nGot a %s RX packet [%08x:%08x|%04x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST", UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16(), len);
+
+    for (int i = 0; i < len; i++)
+        log_serial->printf("%02x ", data[i]);
+
+    log_serial->printf("\r\n");
 }
 
-void eo() {
-    while(1)
-    {
-        if(!bns){
-            //Thread::signal_wait(0x1);
-            dPassage.lock();
-            estOuestR=0;
-            estOuestV=1;
-            Thread::wait(5000);
-            //while(beo){}
-            if(!beo){
-                printf("\r\n YO2 \r\n");
-                estOuestV=0;
-                estOuestJ=1;
-                Thread::wait(2000);
-                estOuestJ=0;
-                estOuestR=1;
-                Thread::wait(1000); 
-            }
-            dPassage.unlock();
-        }   
-    }
+
+static void send_explicit_data_to_remote_node(XBeeZB& xbee, const RemoteXBeeZB& RemoteDevice,char message)
+{
+
+    const uint8_t data_len = 0x01;
+    const uint8_t dstEP = 0xE8;
+    const uint8_t srcEP = 0xE8;
+    const uint16_t clusterID = 0x0011;
+    const uint16_t profileID = 0xC105;
+
+    const TxStatus txStatus = xbee.send_data(RemoteDevice, dstEP, srcEP, clusterID, profileID, (const uint8_t *)message, data_len);
+    if (txStatus == TxStatusSuccess)
+        log_serial->printf("send_explicit_data_to_remote_node OK\r\n");
+    else
+        log_serial->printf("send_explicit_data_to_remote_node failed with %d\r\n", (int) txStatus);
 }
 
-void nbVoiture() {
-    while(1) {
-        if(boutonPlusNS){
-            nombreDeVoitureNS++;  
-        }
-        if(boutonMoinsNS && nombreDeVoitureNS>0){
-            nombreDeVoitureNS--;  
-        }
-        if(boutonPlusEO){
-            nombreDeVoitureEO++;  
-        }
-        if(boutonMoinsEO && nombreDeVoitureEO>0){
-            nombreDeVoitureEO--;  
-        }
-        printf("Nombre de voiture NS : %d \r\n", nombreDeVoitureNS);
-        printf("Nombre de voiture EO : %d \r\n\n", nombreDeVoitureEO);
-        printf("bnf: %d,     beo: %d \r\n",bns,beo);
-    }
-}
+
+int main()
+{
+    log_serial = new Serial(DEBUG_TX, DEBUG_RX);
+    log_serial->baud(9600);
+    log_serial->printf("Sample application to demo how to receive unicast and broadcast data with the XBeeZB\r\n\r\n");
+    log_serial->printf(XB_LIB_BANNER);
+    
+
+
+#if defined(ENABLE_LOGGING)
+    new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
+#endif
+
+    XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
+
+    /* Register callbacks */
+    xbee.register_receive_cb(&receive_cb);
+
+    RadioStatus const radioStatus = xbee.init();
+    MBED_ASSERT(radioStatus == Success);
 
-void gestion() {
-    int max, min;
-    while(1) {       
-        if((nombreDeVoitureNS>(nombreDeVoitureEO+10))
-            || (nombreDeVoitureEO ==0)){
-            bns=1;
-            beo=0;
-        }
-        else if((nombreDeVoitureEO>(nombreDeVoitureNS+10))
-            || (nombreDeVoitureEO ==0)){
-            bns=0;
-            beo=1;
-        }
-        else {
-            bns=0;
-            beo=0;
-            t_ns.signal_set(0x1);
-            t_eo.signal_set(0x1);
-        }
+    /* Wait until the device has joined the network */
+    log_serial->printf("Waiting for device to join the network: ");
+    while (!xbee.is_joined()) {
+        wait_ms(1000);
+        log_serial->printf(".");
     }
-}
+    log_serial->printf("OK\r\n");
 
-int main() {
-    nordSudR = 1;
-    estOuestR = 1;
-    nordSudJ = 0;
-    estOuestJ = 0;
-    nordSudV = 0;
-    estOuestV = 0;
-    t_eo.start(eo);
-    t_ns.start(ns);
-    t_gestion.start(gestion);
-    t_nbVoiture.start(nbVoiture);
-    while(1) {
+    int i =0;
+    const RemoteXBeeZB remoteDevice = RemoteXBeeZB(REMOTE_NODE_ADDR64);
+    while (true) {
+        i++;
+        uint32_t receive_value = xbee.process_rx_frames();
+        log_serial->printf("%d");
+        wait_ms(100);
+        log_serial->printf(".");
+        
         
+        //partie gestion des feux a mettre sur le coordinateur 
+        /////////////////////////////////////////////
+        if(i==10){
+            send_explicit_data_to_remote_node(xbee, remoteDevice,rouge);
+            }
+        if(i==20){
+            send_explicit_data_to_remote_node(xbee, remoteDevice,vert);
+            i=0;
+            }
+        ////////////////////////////////////////////
     }
-}
\ No newline at end of file
+
+   // delete(log_serial);
+}