Gestion du Traffic Adaptatif- Code du mbed coordinateur

Dependencies:   XBeeLib mbed mbed-rtos EthernetInterface

Committer:
leomerel
Date:
Wed Dec 05 16:13:36 2018 +0000
Revision:
30:f8df12b28719
Parent:
29:109a6834f399
Child:
31:7ca80c48a6d7
v5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pierreblouet 17:0694b399bd0d 1 #include "mbed.h"
pierreblouet 17:0694b399bd0d 2 #include "XBeeLib.h"
leomerel 21:5adfcfd83e95 3 #include "rtos.h"
leomerel 29:109a6834f399 4 #include "EthernetInterface.h"
leomerel 29:109a6834f399 5
leomerel 29:109a6834f399 6 const char* ECHO_SERVER_ADDRESS = "192.168.1.133";
leomerel 29:109a6834f399 7 const int ECHO_SERVER_PORT = 7;
leomerel 25:fdb28df21d44 8
pierreblouet 17:0694b399bd0d 9 #if defined(ENABLE_LOGGING)
pierreblouet 17:0694b399bd0d 10 #include "DigiLoggerMbedSerial.h"
pierreblouet 17:0694b399bd0d 11 using namespace DigiLog;
pierreblouet 17:0694b399bd0d 12 #endif
leomerel 12:9ec78b07c688 13
leomerel 26:e9fc7e552c88 14 //Adressage
pierreblouet 17:0694b399bd0d 15 #define REMOTE_NODE_ADDR64_MSB ((uint32_t)0x0013A200)
leomerel 24:bbde0b873a37 16 #define REMOTE_NODE_ADDR64_LSB1 ((uint32_t)0x40C0E3A1) // adresse du recepteur 1
leomerel 24:bbde0b873a37 17 #define REMOTE_NODE_ADDR64_LSB2 ((uint32_t)0x4086DA0E) // adresse du recepteur 2
leomerel 24:bbde0b873a37 18 #define REMOTE_NODE_ADDR64_1 UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB1)
leomerel 24:bbde0b873a37 19 #define REMOTE_NODE_ADDR64_2 UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB2)
pierreblouet 17:0694b399bd0d 20
pierreblouet 17:0694b399bd0d 21 using namespace XBeeLib;
pierreblouet 17:0694b399bd0d 22
leomerel 21:5adfcfd83e95 23 //Nombre de voitures au feu
leomerel 22:c3b22291cb9d 24 int voituresFeu1 = 0;
leomerel 24:bbde0b873a37 25 int voituresFeu2 = 0;
leomerel 21:5adfcfd83e95 26 Thread t_nbVoiture;
leomerel 26:e9fc7e552c88 27 Thread t_gestion;
leomerel 21:5adfcfd83e95 28
leomerel 26:e9fc7e552c88 29 //Gestion
leomerel 28:50d89e4255da 30 int temps = 5000; //durée (en ms) pour qu'une voiture traverse le chantier
leomerel 25:fdb28df21d44 31 Timer timer;
leomerel 28:50d89e4255da 32 int whosred;
leomerel 28:50d89e4255da 33 Thread t_setFeu1Vert;
leomerel 28:50d89e4255da 34 Thread t_setFeu2Vert;
leomerel 29:109a6834f399 35 bool stay1=0; /* stay est une variable qui est à 1 lorsqu'on veut qu'un feu reste à une */
leomerel 29:109a6834f399 36 bool stay2=0; /* certaine couleur jusqu'à ce qu'une autre commande lui dise de changer de couleur */
pierreblouet 17:0694b399bd0d 37
leomerel 26:e9fc7e552c88 38 //Communication
pierreblouet 17:0694b399bd0d 39 Serial *log_serial;
leomerel 23:2a6ea31d8e23 40 XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
leomerel 24:bbde0b873a37 41 const RemoteXBeeZB remoteDevice1 = RemoteXBeeZB(REMOTE_NODE_ADDR64_1);
leomerel 24:bbde0b873a37 42 const RemoteXBeeZB remoteDevice2 = RemoteXBeeZB(REMOTE_NODE_ADDR64_2);
leomerel 28:50d89e4255da 43 char set_rouge[]="r"; // message pour demander a un recepteur de passer au rouge
leomerel 28:50d89e4255da 44 char set_vert[]="v"; // message pour demander a un recepteur de passer au vert
leomerel 24:bbde0b873a37 45
leomerel 29:109a6834f399 46
leomerel 29:109a6834f399 47 InterruptIn button(p5);
leomerel 29:109a6834f399 48 DigitalOut led1(LED1);
leomerel 29:109a6834f399 49 DigitalOut led2(LED2);
leomerel 29:109a6834f399 50 DigitalOut led3(LED3);
leomerel 29:109a6834f399 51 DigitalOut led4(LED4);
leomerel 29:109a6834f399 52 uint8_t vehicule_urgence=0;
leomerel 29:109a6834f399 53 Thread t_urgence;
leomerel 29:109a6834f399 54 Thread t_blink;
leomerel 30:f8df12b28719 55 Thread t_Ethernet;
leomerel 30:f8df12b28719 56
leomerel 30:f8df12b28719 57 Mail<int,128> mail_box;
leomerel 29:109a6834f399 58
leomerel 29:109a6834f399 59 void sendMessage() {
leomerel 29:109a6834f399 60 EthernetInterface eth;
leomerel 29:109a6834f399 61 eth.init(); //Use DHCP
leomerel 29:109a6834f399 62 eth.connect();
leomerel 29:109a6834f399 63 printf("\nClient IP Address is %s\n", eth.getIPAddress());
leomerel 29:109a6834f399 64
leomerel 29:109a6834f399 65 // Connect to Server
leomerel 29:109a6834f399 66 TCPSocketConnection socket;
leomerel 29:109a6834f399 67 while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
leomerel 29:109a6834f399 68 printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
leomerel 29:109a6834f399 69 wait(1);
leomerel 29:109a6834f399 70 }
leomerel 29:109a6834f399 71 printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
leomerel 30:f8df12b28719 72 int * i = 0;
leomerel 30:f8df12b28719 73 char hello[] = "10";
leomerel 29:109a6834f399 74 while(1) {
leomerel 30:f8df12b28719 75 osEvent evt = mail_box.get();
leomerel 30:f8df12b28719 76 if (evt.status == osEventMail) {
leomerel 30:f8df12b28719 77 i = (int*)evt.value.p;
leomerel 30:f8df12b28719 78 sprintf(hello, "%d", i);
leomerel 30:f8df12b28719 79 printf("Sending message to Server : '%s' \n",hello);
leomerel 30:f8df12b28719 80 socket.send_all(hello, sizeof(hello) - 1);
leomerel 30:f8df12b28719 81 mail_box.free(i);
leomerel 30:f8df12b28719 82 }
leomerel 29:109a6834f399 83 }
leomerel 29:109a6834f399 84
leomerel 29:109a6834f399 85
leomerel 29:109a6834f399 86 }
leomerel 29:109a6834f399 87
leomerel 25:fdb28df21d44 88 static void send_explicit_data_to_remote_node(XBeeZB& xbee, const RemoteXBeeZB& RemoteDevice,const char * data)
leomerel 24:bbde0b873a37 89 {
leomerel 24:bbde0b873a37 90 const uint8_t dstEP = 0xE8;
leomerel 24:bbde0b873a37 91 const uint8_t srcEP = 0xE8;
leomerel 24:bbde0b873a37 92 const uint16_t clusterID = 0x0011;
leomerel 24:bbde0b873a37 93 const uint16_t profileID = 0xC105;
leomerel 24:bbde0b873a37 94
leomerel 25:fdb28df21d44 95 const TxStatus txStatus = xbee.send_data(RemoteDevice, dstEP, srcEP, clusterID, profileID, (const uint8_t *)data, strlen(data));
leomerel 24:bbde0b873a37 96 }
pierreblouet 17:0694b399bd0d 97
leomerel 28:50d89e4255da 98 //Fonction callback invoquée à la réception d'un message
pierreblouet 17:0694b399bd0d 99 static void receive_cb(const RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len)
pierreblouet 17:0694b399bd0d 100 {
leomerel 28:50d89e4255da 101 const uint64_t remote_addr64 = remote.get_addr64(); //adresse de l'expéditeur du message
leomerel 22:c3b22291cb9d 102
leomerel 24:bbde0b873a37 103 if (data[0]==0x6D){
leomerel 24:bbde0b873a37 104 // baisse le compteur de voiture au feu 1
leomerel 24:bbde0b873a37 105 if ( UINT64_LO32(remote_addr64) == UINT64_LO32(REMOTE_NODE_ADDR64_LSB1) && (voituresFeu1!=0)){
leomerel 24:bbde0b873a37 106 voituresFeu1--;
leomerel 24:bbde0b873a37 107 }
leomerel 24:bbde0b873a37 108 // baisse le compteur de voiture au feu 2
leomerel 24:bbde0b873a37 109 else if ( (UINT64_LO32(remote_addr64)==UINT64_LO32(REMOTE_NODE_ADDR64_LSB2)) && (voituresFeu2!=0)){
leomerel 24:bbde0b873a37 110 voituresFeu2--;
leomerel 24:bbde0b873a37 111 }
leomerel 25:fdb28df21d44 112 log_serial->printf("Nombre de voitures feu 1 : %d\r\n",voituresFeu1);
leomerel 25:fdb28df21d44 113 log_serial->printf("Nombre de voitures feu 2 : %d\r\n",voituresFeu2);
leomerel 30:f8df12b28719 114 int *a = mail_box.alloc();
leomerel 30:f8df12b28719 115 a = voituresFeu1+voituresFeu2;
leomerel 30:f8df12b28719 116 mail_box.put(a);
leomerel 22:c3b22291cb9d 117 }
leomerel 23:2a6ea31d8e23 118 else if (data[0]==0x70){
leomerel 24:bbde0b873a37 119 // augmente le compteur de voiture au feu 1
leomerel 24:bbde0b873a37 120 if ( UINT64_LO32(remote_addr64) == UINT64_LO32(REMOTE_NODE_ADDR64_LSB1)){
leomerel 24:bbde0b873a37 121 voituresFeu1++;
leomerel 24:bbde0b873a37 122 }
leomerel 24:bbde0b873a37 123 // augmente le compteur de voiture au feu 2
leomerel 24:bbde0b873a37 124 else if ( UINT64_LO32(remote_addr64) == UINT64_LO32(REMOTE_NODE_ADDR64_LSB2)){
leomerel 24:bbde0b873a37 125 voituresFeu2++;
leomerel 24:bbde0b873a37 126 }
leomerel 25:fdb28df21d44 127 log_serial->printf("Nombre de voitures feu 1 : %d\r\n",voituresFeu1);
leomerel 25:fdb28df21d44 128 log_serial->printf("Nombre de voitures feu 2 : %d\r\n",voituresFeu2);
leomerel 30:f8df12b28719 129 int *a = mail_box.alloc();
leomerel 30:f8df12b28719 130 a = voituresFeu1+voituresFeu2;
leomerel 30:f8df12b28719 131 mail_box.put(a);
leomerel 22:c3b22291cb9d 132 }
leomerel 23:2a6ea31d8e23 133 else if (data[0]==0x72){
leomerel 28:50d89e4255da 134 // confirmation que r1 est bien rouge, donc peut demander au r2 de passer au vert
leomerel 24:bbde0b873a37 135 if ( UINT64_LO32(remote_addr64) == UINT64_LO32(REMOTE_NODE_ADDR64_LSB1)){
leomerel 29:109a6834f399 136 if(vehicule_urgence==0){
leomerel 29:109a6834f399 137 whosred = 1;
leomerel 29:109a6834f399 138 t_setFeu2Vert.signal_set(0x1);
leomerel 29:109a6834f399 139 }
leomerel 24:bbde0b873a37 140 }
leomerel 28:50d89e4255da 141 // confirmation que r2 est bien rouge, donc peut demander au r1 de passer au vert
leomerel 24:bbde0b873a37 142 else if ( UINT64_LO32(remote_addr64) == UINT64_LO32(REMOTE_NODE_ADDR64_LSB2)){
leomerel 29:109a6834f399 143 if(vehicule_urgence==0){
leomerel 29:109a6834f399 144 whosred = 2;
leomerel 29:109a6834f399 145 t_setFeu1Vert.signal_set(0x1);
leomerel 29:109a6834f399 146 }
leomerel 24:bbde0b873a37 147 }
leomerel 23:2a6ea31d8e23 148 }
pierreblouet 17:0694b399bd0d 149 log_serial->printf("\r\n");
emilmont 1:491820ee784d 150 }
leomerel 12:9ec78b07c688 151
leomerel 28:50d89e4255da 152 void setFeu1Vert(){
leomerel 28:50d89e4255da 153 while(1){
leomerel 28:50d89e4255da 154 Thread::signal_wait(0x1);
leomerel 28:50d89e4255da 155 Thread::wait(temps);
leomerel 29:109a6834f399 156 if(vehicule_urgence==0){
leomerel 29:109a6834f399 157 send_explicit_data_to_remote_node(xbee, remoteDevice1,set_vert);
leomerel 29:109a6834f399 158 log_serial->printf("r1 passe au vert");
leomerel 29:109a6834f399 159 }
leomerel 28:50d89e4255da 160 }
leomerel 28:50d89e4255da 161 }
leomerel 28:50d89e4255da 162
leomerel 28:50d89e4255da 163 void setFeu2Vert(){
leomerel 28:50d89e4255da 164 while(1){
leomerel 28:50d89e4255da 165 Thread::signal_wait(0x1);
leomerel 28:50d89e4255da 166 Thread::wait(temps);
leomerel 29:109a6834f399 167 if(vehicule_urgence==0){
leomerel 29:109a6834f399 168 send_explicit_data_to_remote_node(xbee, remoteDevice2,set_vert);
leomerel 29:109a6834f399 169 }
leomerel 28:50d89e4255da 170 log_serial->printf("r2 passe au vert");
leomerel 28:50d89e4255da 171 }
leomerel 28:50d89e4255da 172 }
leomerel 28:50d89e4255da 173
leomerel 23:2a6ea31d8e23 174 void connect_Xbee()
pierreblouet 17:0694b399bd0d 175 {
pierreblouet 17:0694b399bd0d 176 log_serial = new Serial(DEBUG_TX, DEBUG_RX);
pierreblouet 17:0694b399bd0d 177 log_serial->baud(9600);
pierreblouet 17:0694b399bd0d 178
leomerel 22:c3b22291cb9d 179 #if defined(ENABLE_LOGGING)
pierreblouet 17:0694b399bd0d 180 new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
leomerel 22:c3b22291cb9d 181 #endif
pierreblouet 17:0694b399bd0d 182
leomerel 28:50d89e4255da 183 // Register callbacks
pierreblouet 17:0694b399bd0d 184 xbee.register_receive_cb(&receive_cb);
pierreblouet 17:0694b399bd0d 185
pierreblouet 17:0694b399bd0d 186 RadioStatus const radioStatus = xbee.init();
pierreblouet 17:0694b399bd0d 187 MBED_ASSERT(radioStatus == Success);
leomerel 12:9ec78b07c688 188
leomerel 28:50d89e4255da 189 //Attend que l'appareil rejoigne le réseau
leomerel 28:50d89e4255da 190 log_serial->printf("En attente de connexion au reseau: ");
pierreblouet 17:0694b399bd0d 191 while (!xbee.is_joined()) {
pierreblouet 17:0694b399bd0d 192 wait_ms(1000);
pierreblouet 17:0694b399bd0d 193 log_serial->printf(".");
leomerel 12:9ec78b07c688 194 }
leomerel 28:50d89e4255da 195 log_serial->printf("Connection reussie.\r\n");
leomerel 28:50d89e4255da 196 whosred=1;
leomerel 23:2a6ea31d8e23 197 }
leomerel 23:2a6ea31d8e23 198
leomerel 23:2a6ea31d8e23 199 void gestion()
leomerel 23:2a6ea31d8e23 200 {
leomerel 23:2a6ea31d8e23 201 while(1)
leomerel 23:2a6ea31d8e23 202 {
leomerel 28:50d89e4255da 203 /* Si il n'y a pas de voitures à un feu ou si il y a beaucoup plus de voitures à l'autre feu, */
leomerel 28:50d89e4255da 204 /* alors l'autre feu passera au vert et le feu avec le moins de voitures passera au rouge. */
leomerel 28:50d89e4255da 205 if ( ((voituresFeu1==0) && (voituresFeu2!=0)) || (voituresFeu2-voituresFeu1>=10) ){
leomerel 25:fdb28df21d44 206 if(stay1==0){
leomerel 25:fdb28df21d44 207 stay2=0;
leomerel 25:fdb28df21d44 208 stay1=1;
leomerel 26:e9fc7e552c88 209 send_explicit_data_to_remote_node(xbee, remoteDevice1,set_rouge);
leomerel 27:0085fca436e1 210 Thread::wait(100);
leomerel 25:fdb28df21d44 211 }
leomerel 23:2a6ea31d8e23 212 }
leomerel 28:50d89e4255da 213 else if ( ((voituresFeu2==0) && (voituresFeu1!=0)) || (voituresFeu1-voituresFeu2>=10) ){
leomerel 25:fdb28df21d44 214 if(stay2==0){
leomerel 25:fdb28df21d44 215 stay1=0;
leomerel 25:fdb28df21d44 216 stay2=1;
leomerel 26:e9fc7e552c88 217 send_explicit_data_to_remote_node(xbee, remoteDevice2,set_rouge);
leomerel 27:0085fca436e1 218 Thread::wait(100);
leomerel 25:fdb28df21d44 219 }
leomerel 23:2a6ea31d8e23 220 }
leomerel 28:50d89e4255da 221 /* Si on est dans aucun de ces deux cas, alors les feux suivront un cycle d'allumage classique. */
leomerel 25:fdb28df21d44 222 else{
leomerel 25:fdb28df21d44 223 stay1=0;
leomerel 25:fdb28df21d44 224 stay2=0;
leomerel 27:0085fca436e1 225 if(whosred==1){
leomerel 26:e9fc7e552c88 226 send_explicit_data_to_remote_node(xbee, remoteDevice2,set_rouge);
leomerel 25:fdb28df21d44 227 }
leomerel 27:0085fca436e1 228 else if(whosred==2){
leomerel 26:e9fc7e552c88 229 send_explicit_data_to_remote_node(xbee, remoteDevice1,set_rouge);
leomerel 25:fdb28df21d44 230 }
leomerel 28:50d89e4255da 231 Thread::wait(10000);
leomerel 25:fdb28df21d44 232 }
leomerel 23:2a6ea31d8e23 233 }
leomerel 22:c3b22291cb9d 234 }
leomerel 22:c3b22291cb9d 235
leomerel 29:109a6834f399 236 void rise() {
leomerel 29:109a6834f399 237 led3 = !led3;
leomerel 29:109a6834f399 238 t_urgence.signal_set(0x1);
leomerel 29:109a6834f399 239 }
leomerel 29:109a6834f399 240 void fall() {
leomerel 29:109a6834f399 241 led2 = !led2;
leomerel 29:109a6834f399 242 }
leomerel 29:109a6834f399 243
leomerel 29:109a6834f399 244 void blink(){
leomerel 29:109a6834f399 245 while(1){
leomerel 29:109a6834f399 246 Thread::signal_wait(0x1);
leomerel 29:109a6834f399 247 while(vehicule_urgence){
leomerel 29:109a6834f399 248 led4 = !led4;
leomerel 29:109a6834f399 249 Thread::wait(200);
leomerel 29:109a6834f399 250 }
leomerel 29:109a6834f399 251 led4 = 0;
leomerel 29:109a6834f399 252 }
leomerel 29:109a6834f399 253 }
leomerel 29:109a6834f399 254
leomerel 29:109a6834f399 255 void urgence(){
leomerel 29:109a6834f399 256 while(1){
leomerel 29:109a6834f399 257 Thread::signal_wait(0x1);
leomerel 29:109a6834f399 258 t_blink.signal_set(0x1);
leomerel 29:109a6834f399 259 vehicule_urgence = 1;
leomerel 29:109a6834f399 260 send_explicit_data_to_remote_node(xbee, remoteDevice1,set_rouge);
leomerel 29:109a6834f399 261 send_explicit_data_to_remote_node(xbee, remoteDevice2,set_rouge);
leomerel 29:109a6834f399 262 Thread::wait(10000);
leomerel 29:109a6834f399 263 stay1=0;
leomerel 29:109a6834f399 264 stay2=0;
leomerel 29:109a6834f399 265 vehicule_urgence = 0;
leomerel 29:109a6834f399 266 }
leomerel 29:109a6834f399 267 }
leomerel 29:109a6834f399 268
leomerel 22:c3b22291cb9d 269 int main()
leomerel 22:c3b22291cb9d 270 {
leomerel 29:109a6834f399 271
leomerel 29:109a6834f399 272 led2=0;
leomerel 29:109a6834f399 273 led3=0;
leomerel 29:109a6834f399 274 led4=0;
leomerel 23:2a6ea31d8e23 275 connect_Xbee();
leomerel 21:5adfcfd83e95 276
leomerel 23:2a6ea31d8e23 277 t_gestion.start(gestion);
leomerel 28:50d89e4255da 278 t_setFeu1Vert.start(setFeu1Vert);
leomerel 28:50d89e4255da 279 t_setFeu2Vert.start(setFeu2Vert);
leomerel 29:109a6834f399 280 t_urgence.start(urgence);
leomerel 29:109a6834f399 281 t_blink.start(blink);
leomerel 30:f8df12b28719 282 t_Ethernet.start(sendMessage);
leomerel 29:109a6834f399 283
leomerel 29:109a6834f399 284 button.rise(&rise); // attach the address of the flip function to the rising edge
leomerel 29:109a6834f399 285 button.fall(&fall);
leomerel 23:2a6ea31d8e23 286
leomerel 25:fdb28df21d44 287 while (true) {
leomerel 25:fdb28df21d44 288 uint32_t receive_value = xbee.process_rx_frames();//nécessaire pour lire (constamment) la valeur recue
leomerel 12:9ec78b07c688 289 }
pierreblouet 17:0694b399bd0d 290 }