-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeedC.nc
711 lines (624 loc) · 13.2 KB
/
HeedC.nc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
#include<UserButton.h>
#include "MoteToMote.h"
#include "printf.h"
#include<string.h>
module HeedC
{
uses // genearl intefaces
{
interface Boot;
interface Leds;
interface Timer<TMilli> as TimerRepeatFunction;
interface Timer<TMilli> as TimerFinalFunction;
interface Timer<TMilli> as TimerInitFunction;
interface Timer<TMilli> as TimerMeasurementFunction;
interface Read<uint16_t> as Temp;
}
uses// button interfaces
{
interface Get<button_state_t>;
interface Notify<button_state_t>;
}
uses //comunication interface
{
interface Packet;
interface AMPacket;
interface AMSend;
interface SplitControl as AMControl;
interface Receive;
}
uses
{
interface Init;
interface ParameterInit<uint16_t> as SeedInit;
interface Random;
}
uses //VoltageC interface
{
interface Read<uint16_t>;
}
}
/*-------------------------------------------------------------------------------------*/
/* */
/*-------------------------------------------------------------------------------------*/
implementation
{
static const int C_PROB= 5;
static const float VOLTAGE = 1.5;
static const float P_max = 0.01;
//global variables
bool radioBusy = FALSE;
message_t pkt;
//Heed Variables
uint8_t heedLevel = 0;
int CHprob;
bool IsFinalCH = FALSE;
bool InitFlag = FALSE;
uint16_t neighbors[10];
uint16_t neighborsBatteryLvl[10];
uint16_t SCH[10];
uint16_t SCHBatteryLvl[10];
uint16_t SCHFinal[10];
uint16_t SCHFinalBatteryLvl[10];
uint16_t Cluster[10];
uint16_t ClusterBatteryLvl[10];
float batteryLvl = 0;
uint8_t myCH = 0;
bool isEmpty = TRUE;
int CHprevious = 0;
uint8_t batteryArgument = 0;
uint32_t t;
uint16_t incrementalNonce = 0;
// Heed prototypes
void sendMsg(int recipient,char MsgId,uint16_t NodeId,uint8_t Level, uint8_t BatteryLvl, uint32_t Timestamp, uint16_t Measurement);
void receiveLevelMsg(Mote_Msg * pkt);
void Init();
void Repeat();
void Finalize();
void receiveTentativeMsg(Mote_Msg * pkt);
void RepeatAfterBatteryRead();
void receiveFinalMsg(Mote_Msg * pkt);
void receiveJoinClusterMsg(Mote_Msg * pkt);
void receiveNewClusterHeadMsg(Mote_Msg * pkt);
void receiveNewClusterHeadElectionMsg(Mote_Msg * pkt);
void receiveMeasurementMsg(Mote_Msg * pkt);
void displayLeds(uint8_t _idNodo);
/*-------------------------------------------------------------------------------------*/
/* EVENTS */
/*-------------------------------------------------------------------------------------*/
event void Boot.booted() //Event fired as the mote is booted
{
int i;
call Notify.enable(); //it is necessary for the button
call AMControl.start(); //it is necessary for the radio
// BaseStation
if(TOS_NODE_ID == 1)
{
heedLevel = 1;
}
for(i = 0; i < 10; i++)
{
neighbors[i] = 0;
SCH[i] = 0;
SCHFinal[i] = 0;
Cluster[i] = 0;
}
//Initial read of the battery
batteryArgument = 0;
call Read.read();
}
// white button pressed
event void Notify.notify(button_state_t val)
{
if(val == BUTTON_RELEASED)
{
}
}
event void AMSend.sendDone(message_t *msg,error_t err)
{
if(msg == &pkt)
{
radioBusy = FALSE;
}
}
event void AMControl.startDone(error_t err)
{
if(err != SUCCESS)
{
call AMControl.start();
}
}
event void AMControl.stopDone(error_t err)
{
}
//Message received event
event message_t * Receive.receive(message_t *msg,void *payload,uint8_t len)
{
if(len == sizeof(Mote_Msg))
{
Mote_Msg * incomingPkt = (Mote_Msg*) payload;
char msgId = incomingPkt->MsgId;
/* We have used letters to switch between different kinds of messages
-l is used for Level Message
-t is used for Tentative CH message
-f is used for Final CH message
-j is used for Join Cluster message
-c is used when a new CH is selected
-n is used by a new CH to inform that he is the new CH
-m is used for Measurements
*/
if(msgId == 'l' && incomingPkt->NodeId != TOS_NODE_ID)
{
receiveLevelMsg(incomingPkt);
}
else if(msgId == 't')
{
receiveTentativeMsg(incomingPkt);
}
else if(msgId == 'f')
{
receiveFinalMsg(incomingPkt);
}
else if(msgId == 'j')
{
receiveJoinClusterMsg(incomingPkt);
}
else if(msgId == 'c')
{
receiveNewClusterHeadElectionMsg(incomingPkt);
}
else if(msgId == 'n')
{
receiveNewClusterHeadMsg(incomingPkt);
}
else if(msgId == 'm')
{
receiveMeasurementMsg(incomingPkt);
}
}
return msg;
}
event void TimerFinalFunction.fired()
{
Finalize();
}
event void TimerInitFunction.fired()
{
Init();
}
event void TimerRepeatFunction.fired()
{
Repeat();
}
event void TimerMeasurementFunction.fired()
{
batteryArgument = 2;
call Read.read();
}
//Read battery event
event void Read.readDone(error_t result, uint16_t val)
{
uint8_t maxBatteryLevelNB = 0;
uint8_t indexMaxBatteryLevelNB = 0;
int i = 0;
if(result == SUCCESS)
{
float v;
v = (float)val/4095 * VOLTAGE;
batteryLvl = v * 100;
CHprob = (C_PROB*(C_PROB * v / VOLTAGE)-6.5)/0.2;
if(CHprob < P_max)
{
CHprob = P_max;
}
/*
When batteryArgument is 1 the CH election will start
when batteryArgument is 2:
- if the battery is under 30% and a new CH is needed
- if the node is not a CH read the temperature
*/
if(batteryArgument == 1)
{
IsFinalCH = FALSE;
call TimerRepeatFunction.startPeriodic(2500);
}
else if(batteryArgument == 2)
{
if(IsFinalCH)
{
if(batteryLvl <= 1.36) // 30%
{
for(i = 0; i < 10; i++)
{
if(neighbors[i] != 0)
{
if(maxBatteryLevelNB < neighborsBatteryLvl[i])
{
indexMaxBatteryLevelNB = i;
maxBatteryLevelNB = neighborsBatteryLvl[i];
}
}
else break;
}
if(maxBatteryLevelNB > 1.36)
{
sendMsg(indexMaxBatteryLevelNB, 'c', TOS_NODE_ID, NULL, NULL, NULL, NULL);
IsFinalCH = FALSE;
}
}
}
else // if a normal node (not CH) reads battery level
{
call Temp.read();
}
}
}
}
//Read the temperature
event void Temp.readDone(error_t result, uint16_t data)
{
if(incrementalNonce >= 100)
{
incrementalNonce = 0;
}
incrementalNonce++;
sendMsg(myCH, 'm', TOS_NODE_ID, heedLevel, batteryLvl, incrementalNonce, data);
}
/*-------------------------------------------------------------------------------------*/
/* FUNCTIONS */
/*-------------------------------------------------------------------------------------*/
void sendMsg(int recipient, char MsgId,uint16_t NodeId,uint8_t Level, uint8_t BatteryLvl, uint32_t Timestamp, uint16_t Measurement)
{
int R = AM_BROADCAST_ADDR;
if(radioBusy == FALSE)
{
// Creo il pacchetto da inviare
Mote_Msg* msg = call Packet.getPayload(&pkt,sizeof(Mote_Msg));
if(&MsgId != NULL)
{
msg->MsgId = MsgId;
}
if(&NodeId != NULL)
{
msg->NodeId = NodeId;
}
if(&Level != NULL)
{
msg->Level = Level;
}
if(&BatteryLvl != NULL)
{
msg -> BatteryLvl = BatteryLvl;
}
if(&Timestamp != NULL)
{
msg -> Timestamp = Timestamp;
}
if(&Measurement != NULL)
{
msg -> Measurement = Measurement;
}
// Invio il pacchetto
if(recipient != NULL)
{
R = recipient;
}
if(call AMSend.send(R,&pkt,sizeof(Mote_Msg)) == SUCCESS)
{
radioBusy = TRUE;
}
}
}
void Init()
{
batteryArgument = 1;
call Read.read();
}
void Repeat()
{
int i, r;
int max;
int maxIndex = 0;
max = 0;
if(SCH[0] != 0)
{
isEmpty = FALSE;
}
t=call TimerRepeatFunction.getNow();
call SeedInit.init(t);
r = call Random.rand16();
r = r % 101;
if(r < 0)
{
r = r * -1;
}
if(!isEmpty)
{
for(i = 0; i < 10; i++)
{
if(SCHBatteryLvl[i] > max)
{
max = SCHBatteryLvl[i];
maxIndex = i;
}
}
myCH = SCH[maxIndex];
if(myCH == TOS_NODE_ID)
{
if(CHprob == 100)
{
sendMsg(NULL,'f',TOS_NODE_ID, NULL, (uint8_t)batteryLvl, NULL, NULL);
IsFinalCH = TRUE;
}
else
{
sendMsg(NULL,'t',TOS_NODE_ID, NULL, (uint8_t)batteryLvl, NULL, NULL);
for(i = 0; i < 10; i++)
{
if(SCH[i] == TOS_NODE_ID)
{
break;
}
else if(SCH[i] == 0)
{
SCH[i] = TOS_NODE_ID;
SCHBatteryLvl[i] = batteryLvl;
break;
}
}
}
}
}
else if(CHprob == 100)
{
sendMsg(NULL,'f',TOS_NODE_ID, NULL, (uint8_t)batteryLvl, NULL, NULL);
IsFinalCH = TRUE;
}
else if(r <= CHprob)
{
sendMsg(NULL,'t',TOS_NODE_ID, NULL, (uint8_t)batteryLvl, NULL, NULL);
for(i = 0; i < 10; i++)
{
if(SCH[i] == TOS_NODE_ID)
{
break;
}
else if(SCH[i] == 0)
{
SCH[i] = TOS_NODE_ID;
SCHBatteryLvl[i] = batteryLvl;
break;
}
}
}
CHprevious = CHprob;
CHprob = CHprob * 2;
if(CHprob > 100)
{
CHprob = 100;
}
if(CHprevious == 100)
{
call TimerRepeatFunction.stop();
call TimerFinalFunction.startOneShot(5000);
}
}
void Finalize()
{
int i;
int max = 0;
int maxIndex = 0;
int myCHid;
if(IsFinalCH == FALSE)
{
if(SCHFinal[0] != 0) //(SCH != empty)
{
for(i = 0; i < 10; i++)
{
if(SCHFinalBatteryLvl[i] > max)
{
max = SCHFinalBatteryLvl[i];
maxIndex = i;
}
}
myCH = SCHFinal[maxIndex];
if(SCHFinal[maxIndex] != TOS_NODE_ID)
{
sendMsg(SCHFinal[maxIndex],'j',TOS_NODE_ID, NULL, (uint8_t)batteryLvl, NULL, NULL);
myCHid = SCHFinal[maxIndex];
}
}
else
{
sendMsg(NULL,'f',TOS_NODE_ID, NULL, (uint8_t)batteryLvl, NULL, NULL);
}
}
else
{
sendMsg(NULL,'f',TOS_NODE_ID, NULL, (uint8_t)batteryLvl, NULL, NULL);
}
call TimerMeasurementFunction.startPeriodic(5000);
displayLeds(myCHid);
}
void receiveLevelMsg(Mote_Msg * pkt)
{
uint16_t lvl = pkt->Level;
uint8_t id = pkt->NodeId;
uint8_t batteryLvlR = pkt -> BatteryLvl;
if(id != TOS_NODE_ID)
{
//setting neighbours
int i = 0;
for(i = 0; i < 10; i++)
{
if(neighbors[i] == id)
{
break;
}
else if(neighbors[i] == 0)
{
neighbors[i] = id;
neighborsBatteryLvl[i] = batteryLvlR;
break;
}
}
if(heedLevel == 0 || (heedLevel != 0 && heedLevel > lvl+1))
{
heedLevel = lvl+1;
sendMsg(NULL, 'l', TOS_NODE_ID, heedLevel, batteryLvl, NULL, NULL);
if(!InitFlag)
{
InitFlag = TRUE;
call TimerInitFunction.startOneShot(3000);
}
}
}
}
void receiveMeasurementMsg(Mote_Msg * pkt)
{
uint16_t lvl = pkt->Level;
uint8_t batteryLvl = pkt->BatteryLvl;
uint8_t id = pkt->NodeId;
uint32_t t = pkt->Timestamp;
uint16_t data = pkt->Measurement;
int i;
if(IsFinalCH)
{
if(batteryLvl != NULL)
{
for(i = 0; i < 10; i++)
{
if(neighbors[i] == id)
{
neighborsBatteryLvl[i] = batteryLvl;
break;
}
}
sendMsg(NULL, 'm', id, heedLevel, NULL, t, data);
}
else
{
if(lvl > heedLevel)
{
sendMsg(NULL, 'm', id, heedLevel, NULL, t, data);
}
}
}
}
void receiveNewClusterHeadElectionMsg(Mote_Msg * pkt)
{
IsFinalCH = TRUE;
sendMsg(NULL, 'n', TOS_NODE_ID, NULL, NULL, NULL, NULL);
}
void receiveNewClusterHeadMsg(Mote_Msg * pkt)
{
uint8_t id = pkt->NodeId;
myCH = id;
}
void receiveTentativeMsg(Mote_Msg * pkt)
{
uint8_t id = pkt->NodeId;
uint8_t batteryLvl = pkt->BatteryLvl;
int i;
for(i = 0; i < 10; i++)
{
if(SCH[i] == id)
{
break;
}
else if(SCH[i] == 0)
{
SCH[i] = id;
SCHBatteryLvl[i] = batteryLvl;
break;
}
}
}
void receiveFinalMsg(Mote_Msg * pkt)
{
uint8_t id = pkt->NodeId;
uint8_t batteryLvl = pkt->BatteryLvl;
int i;
for(i = 0; i < 10; i++)
{
if(SCHFinal[i] == id)
{
break;
}
else if(SCHFinal[i] == 0)
{
SCHFinal[i] = id;
SCHFinalBatteryLvl[i] = batteryLvl;
break;
}
}
}
void receiveJoinClusterMsg(Mote_Msg * pkt)
{
uint8_t id = pkt->NodeId;
uint8_t batteryLvl = pkt->BatteryLvl;
int i;
for(i = 0; i < 10; i++)
{
if(Cluster[i] == 0)
{
Cluster[i] = id;
ClusterBatteryLvl[i] = batteryLvl;
break;
}
}
}
void displayLeds(uint8_t _idNodo)
{
int l;
l = _idNodo % 8;
if(l==0)
{
call Leds.led0Off();
call Leds.led1Off();
call Leds.led2Off();
}
if(l==1)
{
call Leds.led0Off();
call Leds.led1Off();
call Leds.led2On();
}
if(l==2)
{
call Leds.led0Off();
call Leds.led1On();
call Leds.led2Off();
}
if(l==3)
{
call Leds.led0Off();
call Leds.led1On();
call Leds.led2On();
}
if(l==4)
{
call Leds.led0On();
call Leds.led1Off();
call Leds.led2Off();
}
if(l==5)
{
call Leds.led0On();
call Leds.led1Off();
call Leds.led2On();
}
if(l==6)
{
call Leds.led0On();
call Leds.led1On();
call Leds.led2Off();
}
if(l==7)
{
call Leds.led0On();
call Leds.led1On();
call Leds.led2On();
}
}
}