-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
556 lines (512 loc) · 17.1 KB
/
main.c
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
#include <Arduino_HTS221.h> //Include library to read Temperature and Humidity
#include <algorithm>
#define HIGH 1
#define LOW 0
#define size_of_buffer 8
#define BITLENGTH 8 //1 Byte
//#define FIRSTBIT pow(2,(BITLENGTH-1))//MSB Value
#define FIRSTBIT (1 << (BITLENGTH - 1))
#define TRUE 1
#define sensorPin1 A0
#define sensorPin2 A2
#define sensorPin3 A4
#define LED 4
#define HB 255
#define SB 180
#define EP 255
#define EB 00
uint16_t v_R1=0;
uint16_t v_R2=0;
uint16_t v_R3=0;
uint16_t v_R=0;
uint16_t rawValues[3];
uint8_t token=0;
uint16_t Th_voltages[8];
uint16_t Th_voltage=0;
uint16_t Delay=500;
uint8_t CT_ID_EGC=0;
uint8_t CT_ID_MLC=0;
uint8_t CT_ID_SC=0;
uint8_t CT_ID=0;
uint8_t RecPacket=0;
uint16_t Avg=0;
uint8_t countB1=0;
uint8_t countB0=0;
int input=0;
int cntR1_1=0,cntR2_1=0,cntR3_1=0;
int cntR1_0=0,cntR2_0=0,cntR3_0=0;
int BestRx=0; int bit_data[4]; char SyncBytes[2];uint8_t syncByte;
void DiversityGainEngineInit();
uint16_t ThresholdVoltageEstimator(uint16_t v1, uint16_t v2, uint16_t v3);
void CombiningTEchniqueSelection();
uint8_t EGC_Engine(uint16_t voltage_R1, uint16_t voltage_R2, uint16_t voltage_R3,uint8_t tkn,uint8_t num);
uint8_t MLC_Engine(uint16_t voltage_R1, uint16_t voltage_R2, uint16_t voltage_R3,uint8_t tkn,uint8_t num);
uint8_t SC_Engine(uint16_t voltage_R1, uint16_t voltage_R2, uint16_t voltage_R3,uint8_t num);
uint8_t SC_EngineReception(uint16_t voltage,uint8_t num);int ComMode=0;int integer=0;int diff_rec=0;
float Pressure;
float Temperature[100], Humidity;
float OldTemp, NewTemp, DiffTemp;
//int c = 0;
int chk=0;
float low=-4.0, high=4.0;
int bs=0, be=0,pdc=0; int num=0;
void setup() {
Serial.begin(115200);
pinMode(LED,OUTPUT);
HTS.begin();
if (!HTS.begin()) //Initialize Temperature and Humidity sensor
{ Serial.println("Failed to initialize Temperature and Humidity Sensor!"); while (1);}
}
void loop() {
DiversityGainEngineInit(); //for receiving packets
// BEE(); // for transmitting the dataframe
}
void BEE()
{
int count=0;
OldTemp = HTS.readTemperature();//read the temperature sensor data
NewTemp = HTS.readTemperature();// read the sensor data aft an instant
DiffTemp = OldTemp- NewTemp;
++count;
writeByte(EP);writeByte(EP);writeByte(HB);writeByte(SB); writeByte(NewTemp);writeByte(EB);Serial.println(NewTemp);
if ((chk=inRange(low,high,DiffTemp))== 1)
{
// writeByte(HB);writeByte(SB);
digitalWrite(LED,HIGH);delayMicroseconds(1000);
PulseMapping(DiffTemp);
writeByte(EB);
Serial.println(DiffTemp);
}
else
{
// writeByte(HB);writeByte(SB);
digitalWrite(LED,LOW);delayMicroseconds(1000);
writeByte(NewTemp);
writeByte(EB);Serial.println(NewTemp);
}
}
void DiversityGainEngineInit()
{
for(int j=0;j<BITLENGTH;j++)
{
v_R1=analogRead(sensorPin1);//Read the sensor1 Value
v_R2=analogRead(sensorPin2);//Read the sensor1 Value
v_R3=analogRead(sensorPin3);//Read the sensor1 Value
//alternatively for stm32 platform
// first configure the ADC module
/* while (!convCompleted);
for (uint8_t i = 0; i < hadc1.Init.NbrOfConversion; i++)
{
v_R1 = rawValues[0];
v_R2 = rawValues[1];
v_R3 = rawValues[2];
}
*/
//comment out this section if you are using other platforms
Th_voltages[j]=ThresholdVoltageEstimator(v_R1,v_R2,v_R3);
delayMicroseconds(1000);
}
Th_voltage=(Th_voltages[0]+Th_voltages[1]+Th_voltages[2]+Th_voltages[3]+Th_voltages[4]+Th_voltages[5]+Th_voltages[6]+Th_voltages[7])/8;
CombiningTEchniqueSelection();
}
uint16_t ThresholdVoltageEstimator (uint16_t v1, uint16_t v2, uint16_t v3)
{
return std::max({v1, v2, v3});
}
void CombiningTEchniqueSelection() {
for (uint8_t j = 0; j < BITLENGTH; j++) {
v_R1 = analogRead(sensorPin1); // Read the sensor1 Value
v_R2 = analogRead(sensorPin2); // Read the sensor2 Value
v_R3 = analogRead(sensorPin3); // Read the sensor3 Value
CT_ID_EGC = EGC_Engine(v_R1, v_R2, v_R3, HIGH, j);
CT_ID_MLC = MLC_Engine(v_R1, v_R2, v_R3, HIGH, j);
CT_ID_SC = SC_Engine(v_R1, v_R2, v_R3, j);
delayMicroseconds(1000);
}
countB1 = 0; cntR1_1;
if (CT_ID_EGC > CT_ID_MLC && CT_ID_EGC > CT_ID_SC) { EGC_machine();}
else if (CT_ID_MLC > CT_ID_EGC && CT_ID_MLC > CT_ID_SC) {MLC_machine();}
else if (CT_ID_SC > CT_ID_EGC && CT_ID_SC > CT_ID_MLC) {SC_machine();}
}
void EGC_machine()
{
for (uint8_t k = 0; k < 2; k++) {
for (uint8_t j = 0; j < BITLENGTH; j++) {
v_R1 = analogRead(sensorPin1); // Read the sensor1 Value
v_R2 = analogRead(sensorPin2); // Read the sensor2 Value
v_R3 = analogRead(sensorPin3); // Read the sensor3 Value
delayMicroseconds(1000);
syncByte=EGC_Engine(v_R1, v_R2, v_R3, LOW, j);
SyncBytes[k]=syncByte;}}
if (SyncBytes[0] == HB && SyncBytes[1]==SB) {
v_R1 = analogRead(sensorPin1); // Read the sensor1 Value
v_R2 = analogRead(sensorPin2); // Read the sensor2 Value
v_R3 = analogRead(sensorPin3); // Read the sensor3 Value
delayMicroseconds(1000);
ComMode = EGC_Engine_1bit(v_R1, v_R2, v_R3);
if (ComMode == 0) {
for (uint8_t j = 0; j < BITLENGTH; j++) {
v_R1 = analogRead(sensorPin1); // Read the sensor1 Value
v_R2 = analogRead(sensorPin2); // Read the sensor2 Value
v_R3 = analogRead(sensorPin3); // Read the sensor3 Value
uint8_t RecPacket = EGC_Engine(v_R1, v_R2, v_R3, LOW, j);}
input = 0;
writeByte(EP);
writeByte(EP);
writeByte(HB);
writeByte(SB);
writeByte(RecPacket); // Transmit one byte
writeByte(EB);
}
else if (ComMode == 1) {
for (uint8_t j = 0; j < 4; j++) {
v_R1 = analogRead(sensorPin1); // Read the sensor1 Value
v_R2 = analogRead(sensorPin2); // Read the sensor2 Value
v_R3 = analogRead(sensorPin3); // Read the sensor3 Value
delayMicroseconds(1000);
bit_data[j] = EGC_Engine_1bit(v_R1, v_R2, v_R3);
}
if (bit_data[0] == 1 && bit_data[1] == 0) {
integer = +1;
} else if (bit_data[0] == 0 && bit_data[1] == 1) {
integer = -1;
}
if (bit_data[2] == 1 && bit_data[3] == 1) {
diff_rec = 1;
}
if (bit_data[2] == 0 && bit_data[3] == 0) {
diff_rec = 2;
}
if (bit_data[2] == 1 && bit_data[3] == 0) {
diff_rec = 3;
}
if (bit_data[2] == 0 && bit_data[3] == 1) {
diff_rec = 4;
}
if (integer == +1) {
writeByte(EP);
writeByte(EP);
writeByte(HB);
writeByte(SB);
writeByte(RecPacket - diff_rec); // Transmit one byte
writeByte(EB);
} else if (integer == -1) {
writeByte(EP);
writeByte(EP);
writeByte(HB);
writeByte(SB);
writeByte(RecPacket + diff_rec); // Transmit one byte
writeByte(EB);
}
}}
}
void MLC_machine()
{
for (uint8_t k = 0; k < 2; k++) {
for (uint8_t j = 0; j < BITLENGTH; j++) {
v_R1 = analogRead(sensorPin1); // Read the sensor1 Value
v_R2 = analogRead(sensorPin2); // Read the sensor2 Value
v_R3 = analogRead(sensorPin3); // Read the sensor3 Value
delayMicroseconds(1000);
syncByte=MLC_Engine(v_R1, v_R2, v_R3, LOW, j);
SyncBytes[k]=syncByte;}}
if (SyncBytes[0] == HB && SyncBytes[1]==SB) {
v_R1 = analogRead(sensorPin1); // Read the sensor1 Value
v_R2 = analogRead(sensorPin2); // Read the sensor2 Value
v_R3 = analogRead(sensorPin3); // Read the sensor3 Value
delayMicroseconds(1000);
ComMode = MLC_Engine_1bit(v_R1, v_R2, v_R3);
if (ComMode == 0) {
for (uint8_t j = 0; j < BITLENGTH; j++) {
v_R1 = analogRead(sensorPin1); // Read the sensor1 Value
v_R2 = analogRead(sensorPin2); // Read the sensor2 Value
v_R3 = analogRead(sensorPin3); // Read the sensor3 Value
uint8_t RecPacket = MLC_Engine(v_R1, v_R2, v_R3, LOW, j);}
input = 0;
writeByte(EP);
writeByte(EP);
writeByte(HB);
writeByte(SB);
writeByte(RecPacket); // Transmit one byte
writeByte(EB);
} else if (ComMode == 1) {
for (uint8_t j = 0; j < 4; j++) {
v_R1 = analogRead(sensorPin1); // Read the sensor1 Value
v_R2 = analogRead(sensorPin2); // Read the sensor2 Value
v_R3 = analogRead(sensorPin3); // Read the sensor3 Value
delayMicroseconds(1000);
bit_data[j] = MLC_Engine_1bit(v_R1, v_R2, v_R3);
}
if (bit_data[0] == 1 && bit_data[1] == 0) {
integer = +1;
} else if (bit_data[0] == 0 && bit_data[1] == 1) {
integer = -1;
}
if (bit_data[2] == 1 && bit_data[3] == 1) {
diff_rec = 1;
}
if (bit_data[2] == 0 && bit_data[3] == 0) {
diff_rec = 2;
}
if (bit_data[2] == 1 && bit_data[3] == 0) {
diff_rec = 3;
}
if (bit_data[2] == 0 && bit_data[3] == 1) {
diff_rec = 4;
}
if (integer == +1) {
writeByte(EP);
writeByte(EP);
writeByte(HB);
writeByte(SB);
writeByte(RecPacket - diff_rec); // Transmit one byte
writeByte(EB);
} else if (integer == -1) {
writeByte(EP);
writeByte(EP);
writeByte(HB);
writeByte(SB);
writeByte(RecPacket + diff_rec); // Transmit one byte
writeByte(EB);
}
} }
}
void SC_machine(){
for (uint8_t k = 0; k < 2; k++) {
for (uint8_t j = 0; j < BITLENGTH; j++) {
v_R1 = analogRead(sensorPin1); // Read the sensor1 Value
v_R2 = analogRead(sensorPin2); // Read the sensor2 Value
v_R3 = analogRead(sensorPin3); // Read the sensor3 Value
delayMicroseconds(1000);
v_R=best_SC_Rx(v_R1, v_R2, v_R3);
syncByte=SC_EngineReception(v_R, j);
SyncBytes[k]=syncByte;}}
if (SyncBytes[0] == HB && SyncBytes[1]==SB) {
v_R1 = analogRead(sensorPin1); // Read the sensor1 Value
v_R2 = analogRead(sensorPin2); // Read the sensor2 Value
v_R3 = analogRead(sensorPin3); // Read the sensor3 Value
delayMicroseconds(1000);
v_R=best_SC_Rx(v_R1, v_R2, v_R3);
ComMode = SC_Engine_1bit(v_R);
if (ComMode == 0) {
for (uint8_t j = 0; j < BITLENGTH; j++) {
v_R1 = analogRead(sensorPin1); // Read the sensor1 Value
v_R2 = analogRead(sensorPin2); // Read the sensor2 Value
v_R3 = analogRead(sensorPin3); // Read the sensor3 Value
delayMicroseconds(1000);
v_R=best_SC_Rx(v_R1, v_R2, v_R3);
uint8_t RecPacket = SC_EngineReception(v_R, j);}
input = 0;
writeByte(EP);
writeByte(EP);
writeByte(HB);
writeByte(SB);
writeByte(RecPacket); // Transmit one byte
writeByte(EB);
}
if (ComMode == 1) {
for (uint8_t j = 0; j < 4; j++) {
v_R1 = analogRead(sensorPin1); // Read the sensor1 Value
v_R2 = analogRead(sensorPin2); // Read the sensor2 Value
v_R3 = analogRead(sensorPin3); // Read the sensor3 Value
delayMicroseconds(1000);
v_R=best_SC_Rx(v_R1, v_R2, v_R3);
bit_data[j] = SC_Engine_1bit(v_R);
}
if (bit_data[0] == 1 && bit_data[1] == 0) {
integer = +1;
} else if (bit_data[0] == 0 && bit_data[1] == 1) {
integer = -1;
}
switch ((bit_data[2] << 1) | bit_data[3]) {
case 0b11:
diff_rec = 1;
break;
case 0b00:
diff_rec = 2;
break;
case 0b10:
diff_rec = 3;
break;
case 0b01:
diff_rec = 4;
break;
}
if (integer == +1) {
writeByte(EP);
writeByte(EP);
writeByte(HB);
writeByte(SB);
writeByte(RecPacket - diff_rec); // Transmit one byte
writeByte(EB);
} else if (integer == -1) {
writeByte(EP);
writeByte(EP);
writeByte(HB);
writeByte(SB);
writeByte(RecPacket + diff_rec); // Transmit one byte
writeByte(EB);
}
}}
}
uint8_t best_SC_Rx(uint16_t v_R1, uint16_t v_R2, uint16_t v_R3)
{
v_R = max(v_R1, max(v_R2, v_R3));
return v_R;
}
uint8_t EGC_Engine(uint16_t voltage_R1, uint16_t voltage_R2, uint16_t voltage_R3,uint8_t tkn,uint8_t num)
{
Avg =(voltage_R1+voltage_R2+voltage_R3)/3;
if(tkn==HIGH){
if(Avg>=Th_voltage){countB1++;if(num==7){return countB1;}}
else{countB0++;}
}
else if (tkn==LOW){
if(Avg>=Th_voltage)
{input++;
//Binary shift to store another bit
input=input<<1;
}
if(num==7){input=input>>1;
return input;}
}
return 0; // Default return value
}
uint8_t EGC_Engine_1bit(uint16_t voltage_R1, uint16_t voltage_R2, uint16_t voltage_R3)
{
Avg =(voltage_R1+voltage_R2+voltage_R3)/3;
if(Avg>=Th_voltage){return 1;}
else{return 0;}
}
uint8_t MLC_Engine(uint16_t voltage_R1, uint16_t voltage_R2, uint16_t voltage_R3,uint8_t tkn,uint8_t num)
{
int cnt0=0,cnt1=0;
if(voltage_R1>=Th_voltage){cnt1++;}else{cnt0++;}
if(voltage_R2>=Th_voltage){cnt1++;}else{cnt0++;}
if(voltage_R3>=Th_voltage){cnt1++;}else{cnt0++;}
if(tkn==HIGH){
if(cnt1>cnt0){countB1++;if(num==7){return countB1;countB1=0;}}
else{countB0++;}
}
else if (tkn==LOW){
if(cnt1>=cnt0)
{input++;
//Binary shift to store another bit
input=input<<1;
}
if(num==7){input=input>>1;
return input;}
}
return 0; // Default return value
}
uint8_t MLC_Engine_1bit(uint16_t voltage_R1, uint16_t voltage_R2, uint16_t voltage_R3)
{
int cnt0=0,cnt1=0;
if(voltage_R1>=Th_voltage){cnt1++;}else{cnt0++;}
if(voltage_R2>=Th_voltage){cnt1++;}else{cnt0++;}
if(voltage_R3>=Th_voltage){cnt1++;}else{cnt0++;}
if(cnt1>cnt0){return 1;}
else{return 0;}
}
uint8_t SC_Engine(uint16_t voltage_R1, uint16_t voltage_R2, uint16_t voltage_R3,uint8_t num)
{
if(voltage_R1>=Th_voltage){cntR1_1++;}else{cntR1_0++;}
if(voltage_R2>=Th_voltage){cntR2_1++;}else{cntR2_0++;}
if(voltage_R3>=Th_voltage){cntR3_1++;}else{cntR3_0++;}
if(num==7)
{if(cntR1_1>=cntR2_1 && cntR1_1>=cntR3_1){return cntR1_1;BestRx=0;cntR1_1=0;cntR2_1=0;cntR3_1=0;}
if(cntR2_1>=cntR1_1 && cntR2_1>=cntR3_1){return cntR2_1;BestRx=1;cntR1_1=0;cntR2_1=0;cntR3_1=0;}
if(cntR3_1>=cntR2_1 && cntR3_1>=cntR1_1){return cntR3_1;BestRx=2;cntR1_1=0;cntR2_1=0;cntR3_1=0;}
}
return cntR1_1;
}
uint8_t SC_EngineReception(uint16_t voltage,uint8_t num)
{
if(voltage> Th_voltage)
{input++;
//Binary shift to store another bit
input=input<<1;
}
if(num==7){input=input>>1;
return input;}
return 0; // Default return statement in case none of the conditions are met
}
uint8_t SC_Engine_1bit(uint16_t voltage)
{
if(voltage> Th_voltage)
{return 1;}
else{return 0;}
}
//Sending 1byte number using the digital pin on the mcu
void writeByte(char decimal)
{
//converting the decimal value to binary and sending 8 bit information from MSB to LSB
int i,binary;
for(i=0;i<BITLENGTH;i++)
{
//int a= decimal % 2;
binary = (int)decimal/FIRSTBIT; //Getting the first binary bit value
decimal= (decimal & ((int)FIRSTBIT -1));//Setting the first bit to zero
decimal=decimal<<1; //Shift all bits by one to left
if(binary==TRUE)
//if (a==1)
{
digitalWrite(LED,HIGH);
//for stm32, first configure GPIO
// Set PA5 high
//HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
// delayMicroseconds(5);
//digitalWrite(LED,LOW);
//analogWrite(A0,255);
//Serial.print("1");
}
else
{
digitalWrite(LED,LOW);
//for stm32
// Set PA5 low
//HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
//delayMicroseconds(5);
//digitalWrite(LED,HIGH);
// analogWrite(A0,0);
//Serial.print("0");
}
//delay(DELAY);
delayMicroseconds(1000);
// Insert 1000 ms delay
//HAL_Delay(1000);
}
//Serial.println();
//digitalWrite(LED,LOW);
}
int inRange(float low, float high, float x)
{
if (low <= x && x <= high)
return 1;
else
return 0;
}
void PulseMapping (char Tempdata)
{
if(Tempdata>=0){digitalWrite(LED,HIGH);delayMicroseconds(1000);digitalWrite(LED,LOW);delayMicroseconds(1000);}
else {digitalWrite(LED,LOW);delayMicroseconds(1000);digitalWrite(LED,HIGH);delayMicroseconds(1000);}
if(Tempdata==1||Tempdata==-1){digitalWrite(LED,HIGH);delayMicroseconds(1000);digitalWrite(LED,HIGH);}
else if(Tempdata==2||Tempdata==-2){digitalWrite(LED,LOW);delayMicroseconds(1000);digitalWrite(LED,LOW);}
else if(Tempdata==3||Tempdata==-3){digitalWrite(LED,HIGH);delayMicroseconds(1000);digitalWrite(LED,LOW);}
else if(Tempdata==4||Tempdata==-4){digitalWrite(LED,LOW);delayMicroseconds(1000);digitalWrite(LED,HIGH);}
else if(Tempdata==0){BEE();}
Tempdata=0;
}
//function to calculate the packet success rate
void PSR_calculate(char packet)
{
if (packet == EP) {pdc++;num++}
if (num>=20000){Serial.print("PSR/PDR");Serial.println(pdc/20000);Serial.print("PFR");Serial.println((20000-pdc)/20000);pdc=0;num=0}
}
//function to calculate the bit error rate of the received packet
void BER_calculate(int bit)
{
if (bit == 1) {bs++;num++}
else if (bit!=1){be++;num++}
if (num >= 20000){Serial.print("BER");Serial.println(be/20000);be=0;num=0}
}