-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
866 lines (605 loc) · 22.8 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
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
/*
* ilm-module.c
*
* Created: 10.08.2021 11:34:03
* Author : qfj
*/
#include <avr/io.h>
#include <stdio.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <string.h>
#include "config.h"
#include "main.h"
#include "adwandler.h"
#include "usart.h"
#include "xbee.h"
#include "xbee_AT_comm.h"
#include "module_globals.h"
#include "status.h"
#include "xbee_utilities.h"
#include "DS3231M.h"
#include "LCD.h"
uint8_t sendbuffer[SINGLE_FRAME_LENGTH]; /**< @brief Send only Buffer used for sending data to the server for which no reply is expected i.e. Measurement data #CMD_send_data_91*/
optionsType EEMEM eeOptions = {
.ping_intervall = PING_INTERVALL_DEF,
.t_transmission_min = T_TRANSMISSION_MIN_DEF,
.t_transmission_max = T_TRANSMISSION_MAX_DEF,
.helium_par.span = HE_SPAN_DEF,
.helium_par.zero = HE_ZERO_DEF,
.N2_1_par.span = N2_1_SPAN_DEF,
.N2_1_par.zero = N2_1_ZERO_DEF,
.N2_2_par.span = N2_2_SPAN_DEF,
.N2_2_par.zero = N2_2_ZERO_DEF,
.measCycles = MEAS_CYCLES_DEF
};
optionsType Options = {
.ping_intervall = PING_INTERVALL_DEF,
.t_transmission_min = T_TRANSMISSION_MIN_DEF,
.t_transmission_max = T_TRANSMISSION_MAX_DEF,
.helium_par.span = HE_SPAN_DEF,
.helium_par.zero = HE_ZERO_DEF,
.N2_1_par.span = N2_1_SPAN_DEF,
.N2_1_par.zero = N2_1_ZERO_DEF,
.N2_2_par.span = N2_2_SPAN_DEF,
.N2_2_par.zero = N2_2_ZERO_DEF,
.measCycles = MEAS_CYCLES_DEF
};
/************************************************************************/
/* Time- and Pressurestamps */
/************************************************************************/
/**
* @brief Time- and Pressurestamps
*
* Timestamps and Pressurevalues for last send/display-reset/ping - Event
*/
lastType last= {.time_send = 0,.time_ping = 0,.time_level_meas = 0};
// holds most recent meas data
MeasType current_meas;
deltaType delta = {
.t_send = 0,
.values_since_last_send.He = 0,
.values_since_last_send.N2_1 = 0,
.values_since_last_send.N2_2 = 0
};
/**
* @brief Measurement Period
*
* Time between Measurements (in s)
*/
const uint8_t Measure_Interval = 2;
/**
* @brief Checks current Connection Status. Possible states are "No Network" if no connection to a coordinator could be established. "No Server", if the Device is connected to a coordinator but, pings sent by #ping_server() are not answered. And "Online" if pings are answered by the server. The State is saved in #NetStatIndex
*
* @param dest_high high 32-bit of coordinator address
* @param dest_low low 32-bit of coordinator address
*
* @return uint8_t 1 if online and 0 if there are any problems with the connection to the server
*/
uint8_t analyze_Connection(void)
{
if (!xbee_reconnect(0))
{
//Associated
CLEAR_ERROR(NETWORK_ERROR);
CLEAR_ERROR(NO_REPLY_ERROR);
if(!ping_server()){
//offline
//LCD_paint_info_line("NoServ",0);
return 0;
}
else{
//online;
CLEAR_ERROR(NETWORK_ERROR);;
CLEAR_ERROR(NO_REPLY_ERROR);;
return 1;
}
}
else
{
//offline
//LCD_paint_info_line("NoNetw",0);
return 0;
}
}
// Pings Server and resets Time
/**
* @brief Pings the server in order to check if the connection is still live. If no Pong was received after #COM_TIMEOUT_TIME the Network error Bit is set in status#device.
*
* @param dest_high high 32-bit of coordinator address
* @param dest_low low 32-bit of coordinator address
*
* @return uint8_t 1 if Ping successful and 0 if no Pong was received
*/
uint8_t ping_server(void)
{
sendbuffer[0]= 0;
uint8_t reply_id = xbee_send_request(PING_MSG,sendbuffer,1);
if( 0xFF == reply_id)
{
_delay_ms(500);
SET_ERROR(NETWORK_ERROR);
return 0;
}
else
{
// Ping Successful --> time is set to the received time
Time.tm_sec = frameBuffer[reply_id].data[0];
Time.tm_min = frameBuffer[reply_id].data[1];
Time.tm_hour = frameBuffer[reply_id].data[2];
Time.tm_mday = frameBuffer[reply_id].data[3];
Time.tm_mon = frameBuffer[reply_id].data[4];
Time.tm_year = frameBuffer[reply_id].data[5];
}
return 1;
}
void debug_ms(char * debug_message,int16_t number, char * format){
#ifdef LCD_DEBUG
LCD_Clear();
LCD_String(debug_message,0,0);
char temp[13];
sprintf(temp,format,number);
LCD_String(temp,0,1);
_delay_ms(500);
#endif
}
/**
* @brief Initializes the interrupts of the controller.
*
*
* @return void
*/
void init_interrupts(void)
{
EICRA |= (1<<ISC21);
EIMSK |= (1<<INT2);
}
/**
* @brief Initializes the ports of the controller.
*
*
* @return void
*/
void init_ports(void)
{
DDRC |= (1<<PINC4);
}
void paint_none(char* infoline,_Bool update){
}
void init(void){
#ifdef LCD_DEBUG
init_LCD();
#endif
version_INIT(FIRMWARE_VERSION,BRANCH_ID,FIRMWARE_VERSION);
init_ports();
_delay_ms(1000);
usart_init(39); //USART0 init with 9600 baud
_delay_ms(100);
init_interrupts();
#ifdef LCD_DEBUG
xbee_init(&LCD_paint_info_line,NULL,0);
#else
xbee_init(&paint_none,NULL,0);
#endif
_delay_ms(10);
init_timer();
_delay_ms(1000);
adc_init(HELIUM);
_delay_ms(100);
adc_init(NITROGEN_1);
_delay_ms(100);
adc_init(NITROGEN_2);
_delay_ms(100);
//=========================================================================
// Enable global interrupts
//=========================================================================
sei();
xbee_hardware_version();
}
/**
* @brief Initializes Timer1 of the controller so it send interrupts with a frequency of 1 Hz.
*
*
* @return void
*/
void init_timer(void)
{
//Timer1 f ~ 1Hz
TCCR1B |= (1<<WGM12) | (1<<CS12) | (1<<CS10);
OCR1A = 5999;
TIMSK1 |= (1<<OCIE1A);
/*
//Timer0 f ~ 40Hz T ~ 25ms
TCCR0A |= (1<<WGM01);
TCCR0B |= (1<<CS00) | (1<<CS02); // N = 1024
OCR0A = 2;
TIMSK0 |= (1<<OCIE1A);
*/
}
void write_optsEEPROM(void){
eeprom_update_block(&Options, &eeOptions, sizeof(Options));
}
uint8_t read_optsEEPROM(void){
optionsType OptionsBuff;
eeprom_read_block(&OptionsBuff,&eeOptions,sizeof(OptionsBuff));
uint8_t Val_outof_Bounds = 0;
CHECK_BOUNDS(OptionsBuff.ping_intervall,PING_INTERVALL_MIN,PING_INTERVALL_MAX,PING_INTERVALL_DEF,Val_outof_Bounds);
CHECK_BOUNDS(OptionsBuff.t_transmission_min,T_TRANSMISSION_MIN_MIN,T_TRANSMISSION_MIN_MAX,T_TRANSMISSION_MIN_DEF,Val_outof_Bounds);
CHECK_BOUNDS(OptionsBuff.t_transmission_max,T_TRANSMISSION_MAX_MIN,T_TRANSMISSION_MAX_MAX,T_TRANSMISSION_MAX_DEF,Val_outof_Bounds);
CHECK_BOUNDS(OptionsBuff.helium_par.span,HE_SPAN_MIN,HE_SPAN_MAX,HE_SPAN_DEF,Val_outof_Bounds);
CHECK_BOUNDS(OptionsBuff.helium_par.zero,HE_ZERO_MIN,HE_ZERO_MAX,HE_ZERO_DEF,Val_outof_Bounds);
CHECK_BOUNDS(OptionsBuff.helium_par.delta,HE_DELTA_MIN,HE_DELTA_MAX,HE_DELTA_DEF,Val_outof_Bounds);
CHECK_BOUNDS(OptionsBuff.N2_1_par.span,N2_1_SPAN_MIN,N2_1_SPAN_MAX,N2_1_SPAN_DEF,Val_outof_Bounds);
CHECK_BOUNDS(OptionsBuff.N2_1_par.zero,N2_1_ZERO_MIN,N2_1_ZERO_MAX,N2_1_ZERO_DEF,Val_outof_Bounds);
CHECK_BOUNDS(OptionsBuff.N2_1_par.delta,N2_1_DELTA_MIN,N2_1_DELTA_MAX,N2_1_DELTA_DEF,Val_outof_Bounds);
CHECK_BOUNDS(OptionsBuff.N2_2_par.span,N2_2_SPAN_MIN,N2_2_SPAN_MAX,N2_2_SPAN_DEF,Val_outof_Bounds);
CHECK_BOUNDS(OptionsBuff.N2_2_par.zero,N2_2_ZERO_MIN,N2_2_ZERO_MAX,N2_2_ZERO_DEF,Val_outof_Bounds);
CHECK_BOUNDS(OptionsBuff.N2_2_par.delta,N2_2_DELTA_MIN,N2_2_DELTA_MAX,N2_2_DELTA_DEF,Val_outof_Bounds);
if (OptionsBuff.t_transmission_min >= OptionsBuff.t_transmission_max * 60)
{
OptionsBuff.t_transmission_min = T_TRANSMISSION_MIN_DEF;
OptionsBuff.t_transmission_max = T_TRANSMISSION_MAX_DEF;
Val_outof_Bounds = 1 ;
}
memcpy(&Options,&OptionsBuff,sizeof(Options));
return Val_outof_Bounds;
}
uint8_t xbee_send_login_msg(uint8_t db_cmd_type, uint8_t *buffer)
{
uint8_t reply_Id = 0;
buffer[0] = 0; // Statusbyte for login 0
uint8_t number_trials = 1;
while(number_trials)
{
reply_Id = xbee_send_request_only(db_cmd_type, buffer, 1);
//#ifdef ALLOW_DEBUG
//sprintf(print_temp,"%i",reply_Id);
//LCD_InitScreen_AddLine(print_temp, 0);
//_delay_ms(10000);
//#endif
if(reply_Id != 0xFF)
{
_delay_ms(1000);
//sprintf(print_temp,"%i",frameBuffer[reply_Id].data_len);
//LCD_InitScreen_AddLine(print_temp,0);
if(frameBuffer[reply_Id].data_len == NUMBER_LOGIN_BYTES) {
return reply_Id; //good options
}
else {
return 0xFF; //bad options
}
}
if(!(--number_trials))
{
//stop trying and go in error mode; no functionality available from here on
_delay_ms(2000);
return 0xFF;
}
}
return 0xFF;
}
void read_channels(void){
current_meas.He = ((double)readChannel_ILM(HELIUM, Options.measCycles) * Options.helium_par.span) + Options.helium_par.zero;
current_meas.N2_1 = ((double)readChannel_ILM(NITROGEN_1,Options.measCycles) * Options.N2_1_par.span) + Options.N2_1_par.zero;
current_meas.N2_2 = ((double)readChannel_ILM(NITROGEN_2,Options.measCycles) * Options.N2_2_par.span) + Options.N2_2_par.zero;
current_meas.He = (current_meas.He > 100)? 100.0 : current_meas.He;
current_meas.N2_1 = (current_meas.N2_1 > 100)? 100.0 : current_meas.N2_1;
current_meas.N2_2 = (current_meas.N2_2 > 100)? 100.0 : current_meas.N2_2;
current_meas.He = (current_meas.He < 0)? 0.0 : current_meas.He;
current_meas.N2_1 = (current_meas.N2_1 < 0)? 0.0 : current_meas.N2_1;
current_meas.N2_2 = (current_meas.N2_2 < 0)? 0.0 : current_meas.N2_2;
//update deltas
//HELIUM
//Avoid Overflow when subtracting
if(current_meas.He > last.Measurement_on_send.He)
{
delta.values_since_last_send.He = current_meas.He - last.Measurement_on_send.He;
}
else
{
delta.values_since_last_send.He = last.Measurement_on_send.He - current_meas.He;
}
//NITROGEN 1
//Avoid Overflow when subtracting
if(current_meas.N2_1 > last.Measurement_on_send.N2_1)
{
delta.values_since_last_send.N2_1 = current_meas.N2_1 - last.Measurement_on_send.N2_1;
}
else
{
delta.values_since_last_send.N2_1 = last.Measurement_on_send.N2_1 - current_meas.N2_1;
}
//NITROGEN 2
//Avoid Overflow when subtracting
if(current_meas.N2_2 > last.Measurement_on_send.N2_2)
{
delta.values_since_last_send.N2_2 = current_meas.N2_2 - last.Measurement_on_send.N2_2;
}
else
{
delta.values_since_last_send.N2_2 = last.Measurement_on_send.N2_2 - current_meas.N2_2;
}
}
void Collect_Measurement_Data(void){
uint16_t He_u16 = (uint16_t) (current_meas.He*10);
uint16_t N2_1_u16 = (uint16_t) (current_meas.N2_1*10);
uint16_t N2_2_u16 = (uint16_t) (current_meas.N2_2*10);
uint16_t_to_Buffer(He_u16,sendbuffer,0);
uint16_t_to_Buffer(N2_1_u16,sendbuffer,2);
uint16_t_to_Buffer(N2_2_u16,sendbuffer,4);
sendbuffer[6] = 0; //TODO status byte!!!
}
/**
* @brief Decodes incoming Messages from the Server and act accordingly
*
* @param reply_id index of the message in #frameBuffer which will be decoded
*
*
* @return void
*/
void execute_server_CMDS(uint8_t reply_id){
switch (frameBuffer[reply_id].type)
{
//=================================================================
case SET_OPTIONS_CMD:// set received Options
set_Options((uint8_t*)frameBuffer[reply_id].data,SET_OPTIONS_CMD);
break;
//=================================================================
case TRIGGER_MEAS_CMD: // Send Measurement Data immediately
read_channels();
Collect_Measurement_Data();
xbee_send_message(TRIGGER_MEAS_CMD,sendbuffer,MEASUREMENT_MESSAGE_LENGTH);
break;
//=================================================================
case GET_OPTIONS_CMD : ;// send current options to Server
sendbuffer[0] = Options.ping_intervall;
uint16_t_to_Buffer(Options.t_transmission_min,sendbuffer,1);
uint16_t_to_Buffer(Options.t_transmission_max,sendbuffer,3);
int32_t_to_Buffer((int32_t)(Options.helium_par.span*SPAN_ZERO_DECIMAL_PLACES),sendbuffer,5);
int32_t_to_Buffer((int32_t)(Options.helium_par.zero*SPAN_ZERO_DECIMAL_PLACES),sendbuffer,9);
uint16_t_to_Buffer((uint16_t)Options.helium_par.delta*10,sendbuffer,13);
int32_t_to_Buffer((int32_t)(Options.N2_1_par.span*SPAN_ZERO_DECIMAL_PLACES),sendbuffer,15);
int32_t_to_Buffer((int32_t)(Options.N2_1_par.zero*SPAN_ZERO_DECIMAL_PLACES),sendbuffer,19);
uint16_t_to_Buffer((uint16_t)Options.N2_1_par.delta*10,sendbuffer,23);
int32_t_to_Buffer((int32_t)(Options.N2_2_par.span*SPAN_ZERO_DECIMAL_PLACES),sendbuffer,25);
int32_t_to_Buffer((int32_t) (Options.N2_2_par.zero*SPAN_ZERO_DECIMAL_PLACES),sendbuffer,29);
uint16_t_to_Buffer((uint16_t)Options.N2_2_par.delta*10,sendbuffer,33);
sendbuffer[35] = Options.measCycles;
xbee_send_message(GET_OPTIONS_CMD,sendbuffer,NUMBER_LOGIN_BYTES);
break;
case SET_PING_INTERVALL_CMD:
;
uint8_t Val_outof_Bounds = 0;
uint8_t buff_ping_Intervall = frameBuffer[reply_id].data[0];
CHECK_BOUNDS(buff_ping_Intervall,PING_INTERVALL_MIN,PING_INTERVALL_MAX,PING_INTERVALL_DEF,Val_outof_Bounds);
if (!Val_outof_Bounds)
{
Options.ping_intervall = buff_ping_Intervall;
}
sendbuffer[0] = Val_outof_Bounds;
//send status ack
xbee_send_message(SET_PING_INTERVALL_CMD,sendbuffer,1);
break;
case GET_RAW_DATA_CMD:
;
uint16_t He_u16 = (uint16_t) readChannel_ILM(HELIUM,Options.measCycles);
uint16_t N2_1_u16 = (uint16_t) readChannel_ILM(NITROGEN_1,Options.measCycles);
uint16_t N2_2_u16 = (uint16_t) readChannel_ILM(NITROGEN_2,Options.measCycles);
uint16_t He_u16_perc = ((((double)He_u16) * Options.helium_par.span) + Options.helium_par.zero)*10;
uint16_t N2_1_u16_perc = ((((double)N2_1_u16)* Options.N2_1_par.span) + Options.N2_1_par.zero)*10;
uint16_t N2_2_u16_perc = ((((double)N2_2_u16)* Options.N2_2_par.span) + Options.N2_2_par.zero)*10;
uint16_t_to_Buffer(He_u16_perc,sendbuffer,0);
uint16_t_to_Buffer(N2_1_u16_perc,sendbuffer,2);
uint16_t_to_Buffer(N2_2_u16_perc,sendbuffer,4);
uint16_t_to_Buffer(He_u16,sendbuffer,6);
uint16_t_to_Buffer(N2_1_u16,sendbuffer,8);
uint16_t_to_Buffer(N2_2_u16,sendbuffer,10);
sendbuffer[12] = 0;
xbee_send_message(GET_RAW_DATA_CMD,sendbuffer,13);
break;
}
//remove Frame from Buffer
buffer_removeData(reply_id);
}
void uint16_t_to_Buffer(uint16_t var, uint8_t * buffer, uint8_t index){
buffer[index] = var >> 8;
buffer[++index] = (uint8_t) var;
}
void int32_t_to_Buffer(int32_t var, uint8_t * buffer, uint8_t index){
buffer[index] = var >> 24;
buffer[++index] = var >> 16;
buffer[++index] = var >> 8;
buffer[++index] = (uint8_t) var;
}
double span_zero_from_Buffer( uint8_t * buffer, uint8_t index){
return ((double) (((int32_t) buffer[index] << 24) |((int32_t) buffer[index+1] << 16) |((int32_t) buffer[index+2] << 8) | buffer[index+3]))/SPAN_ZERO_DECIMAL_PLACES;
}
void set_Options( uint8_t * optBuffer,uint8_t answer_code){
optionsType OptionsBuff ={
.ping_intervall = optBuffer[0],
.t_transmission_min = ((uint16_t) optBuffer[1] << 8) | optBuffer[2] ,
.t_transmission_max = ((uint16_t) optBuffer[3] << 8) | optBuffer[4] ,
.helium_par.span = span_zero_from_Buffer(optBuffer,5),
.helium_par.zero = span_zero_from_Buffer(optBuffer,9) ,
.helium_par.delta = ((double)(((uint16_t) optBuffer[13] << 8) | optBuffer[14]))/10 ,
.N2_1_par.span = span_zero_from_Buffer(optBuffer,15),
.N2_1_par.zero = span_zero_from_Buffer(optBuffer,19) ,
.N2_1_par.delta = ((double)(((uint16_t) optBuffer[23] << 8) | optBuffer[24]))/10 ,
.N2_2_par.span = span_zero_from_Buffer(optBuffer,25) ,
.N2_2_par.zero = span_zero_from_Buffer(optBuffer,29) ,
.N2_2_par.delta = ((double)(((uint16_t) optBuffer[33] << 8) | optBuffer[34]))/10,
.measCycles = optBuffer[35]
};
uint8_t Val_outof_Bounds = 0;
CHECK_BOUNDS(OptionsBuff.ping_intervall,PING_INTERVALL_MIN,PING_INTERVALL_MAX,PING_INTERVALL_DEF,Val_outof_Bounds);
debug_ms("ping interv",Val_outof_Bounds, "%i");
CHECK_BOUNDS(OptionsBuff.t_transmission_min,T_TRANSMISSION_MIN_MIN,T_TRANSMISSION_MIN_MAX,T_TRANSMISSION_MIN_DEF,Val_outof_Bounds);
debug_ms("trans min",Val_outof_Bounds, "%i");
CHECK_BOUNDS(OptionsBuff.t_transmission_max,T_TRANSMISSION_MAX_MIN,T_TRANSMISSION_MAX_MAX,T_TRANSMISSION_MAX_DEF,Val_outof_Bounds);
debug_ms("trans max",Val_outof_Bounds, "%i");
CHECK_BOUNDS(OptionsBuff.helium_par.span,HE_SPAN_MIN,HE_SPAN_MAX,HE_SPAN_DEF,Val_outof_Bounds);
debug_ms("he span",Val_outof_Bounds, "%i");
CHECK_BOUNDS(OptionsBuff.helium_par.zero,HE_ZERO_MIN,HE_ZERO_MAX,HE_ZERO_DEF,Val_outof_Bounds);
debug_ms("he zero", OptionsBuff.helium_par.zero*SPAN_ZERO_DECIMAL_PLACES, "%i");
CHECK_BOUNDS(OptionsBuff.helium_par.delta,HE_DELTA_MIN,HE_DELTA_MAX,HE_DELTA_DEF,Val_outof_Bounds);
debug_ms("he delta",OptionsBuff.helium_par.delta, "%i");
CHECK_BOUNDS(OptionsBuff.N2_1_par.span,N2_1_SPAN_MIN,N2_1_SPAN_MAX,N2_1_SPAN_DEF,Val_outof_Bounds);
debug_ms("N2 1 span",Val_outof_Bounds, "%i");
CHECK_BOUNDS(OptionsBuff.N2_1_par.zero,N2_1_ZERO_MIN,N2_1_ZERO_MAX,N2_1_ZERO_DEF,Val_outof_Bounds);
debug_ms("N2 1 zero",Val_outof_Bounds, "%i");
CHECK_BOUNDS(OptionsBuff.N2_1_par.delta,N2_1_DELTA_MIN,N2_1_DELTA_MAX,N2_1_DELTA_DEF,Val_outof_Bounds);
debug_ms("N2 1 delta",Val_outof_Bounds, "%i");
CHECK_BOUNDS(OptionsBuff.N2_2_par.span,N2_2_SPAN_MIN,N2_2_SPAN_MAX,N2_2_SPAN_DEF,Val_outof_Bounds);
debug_ms("N2 2 span",Val_outof_Bounds, "%i");
CHECK_BOUNDS(OptionsBuff.N2_2_par.zero,N2_2_ZERO_MIN,N2_2_ZERO_MAX,N2_2_ZERO_DEF,Val_outof_Bounds);
debug_ms("N2 2 zero",Val_outof_Bounds, "%i");
CHECK_BOUNDS(OptionsBuff.N2_2_par.delta,N2_2_DELTA_MIN,N2_2_DELTA_MAX,N2_2_DELTA_DEF,Val_outof_Bounds);
debug_ms("N2 2 delta",Val_outof_Bounds, "%i");
if (OptionsBuff.t_transmission_min >= OptionsBuff.t_transmission_max * 60)
{
OptionsBuff.t_transmission_min = T_TRANSMISSION_MIN_DEF;
OptionsBuff.t_transmission_max = T_TRANSMISSION_MAX_DEF;
Val_outof_Bounds = 1 ;
}
if (!Val_outof_Bounds)
{
//no problem with received options --> save them in operational struct
memcpy(&Options,&OptionsBuff,sizeof(Options));
write_optsEEPROM();
sendbuffer[0] = 0; // setting options was successful
xbee_send_message(answer_code,sendbuffer,1);
}else{
sendbuffer[0] = 1; // setting options was not successful
xbee_send_message(answer_code,sendbuffer,1);
}
}
int main(void)
{
init();
//init_LCD();
_delay_ms(4000);
read_optsEEPROM();
if(xbee_reset_connection(1))
{
_delay_ms(100);
if(xbee_get_server_adrr())
{
_delay_ms(100);
if(!CHECK_ERROR(NETWORK_ERROR))
{
//=========================================================================
// Device Login
//=========================================================================
uint8_t reply_id = xbee_send_login_msg(LOGIN_MSG, sendbuffer);
if (reply_id!= 0xFF ){ // GOOD OPTIONS RECEIVED
debug_ms("rec login",1, "%i");
_delay_ms(100);
set_Options((uint8_t*)frameBuffer[reply_id].data,OPTIONS_SET_ACK);
}
else // DEFECTIVE OPTIONS RECEIVED
{
SET_ERROR(OPTIONS_ERROR);
SET_ERROR(INIT_OFFLINE_ERROR);
}
}
else
{ // No stable Connection was reached
SET_ERROR(INIT_OFFLINE_ERROR);
}
}
}
//##########################################################################
// MAIN LOOP
//##########################################################################
while (1)
{
if((count_t_elapsed % Measure_Interval == 0) && ((count_t_elapsed - last.time_level_meas) > 1 )) //every Measure_Intervall
{
last.time_level_meas = count_t_elapsed;
read_channels();
}
delta.t_send = count_t_elapsed - last.time_send;
if(!CHECK_ERROR(NETWORK_ERROR)){
// ONLINE
//============================================================================================================================================
// checks if either:
// 1. the maximum time(t_transmission_max) has passed since last send OR
// 2. delta_HE is reached and at least t_transmission_min time has passed since last send
// 3. delta_N2_1 is reached and at least t_transmission_min time has passed since last send
// 4. delta_N2_2 is reached and at least t_transmission_min time has passed since last send
if((delta.t_send >= Options.t_transmission_max * 60)|| //
(delta.t_send >= Options.t_transmission_min && delta.values_since_last_send.He > Options.helium_par.delta)||
(delta.t_send >= Options.t_transmission_min && delta.values_since_last_send.N2_1 > Options.N2_1_par.delta)||
(delta.t_send >= Options.t_transmission_min && delta.values_since_last_send.N2_2 > Options.N2_2_par.delta))
{
Collect_Measurement_Data();
// send Measurement Data to Server
if( 0xFF ==xbee_send_request(MEAS_MSG,sendbuffer,MEASUREMENT_MESSAGE_LENGTH))
{
SET_ERROR(NETWORK_ERROR);
analyze_Connection();
}
// Reset for delta calculation
delta.values_since_last_send.He = 0;
delta.values_since_last_send.N2_1 = 0;
delta.values_since_last_send.N2_2 = 0;
// Reset for delta calculation
last.Measurement_on_send.He = current_meas.He;
last.Measurement_on_send.N2_1 = current_meas.N2_1;
last.Measurement_on_send.N2_2 = current_meas.N2_2;
// Reset for time since last sent i.e. delta_t_send
last.time_send = count_t_elapsed;
}
//==============================================================================================
// PING
//==========================================================
// since pressure/temp is measured every 5s and ping is done every 60+2 seconds to ensure they dont get triggerd at the same Second
if (((count_t_elapsed % (Options.ping_intervall*60)) == 2) && ((count_t_elapsed - last.time_ping) > 5 ))
{
last.time_ping = count_t_elapsed;
if(!ping_server()){
if(!analyze_Connection()){
continue;
}
}
}
//==============================================================================================
// EXECUTE SERVER COMMANDS
//==========================================================
uint8_t reply_id;
reply_id = xbee_hasReply(LAST_NON_CMD_MSG,GREATER_THAN);
if (reply_id != 0xFF){
execute_server_CMDS(reply_id);
}
// execute Server Commands
}
else
{
// Offline
//========================================================
// RECONNECT
//========================================================
// try to Reconnect after every ping_intervall (Reconnect_after_time)
//========================================================
if (count_t_elapsed % (Options.ping_intervall*60) == 2){
if (!xbee_reconnect(0))
{
//Associated
CLEAR_ERROR(NETWORK_ERROR);
CLEAR_ERROR(NO_REPLY_ERROR);
if(!ping_server()){
CLEAR_ERROR(NETWORK_ERROR);
CLEAR_ERROR(NO_REPLY_ERROR);
}
}
}
}
}
}
//=========================================================================
// Interrupts
//=========================================================================
ISR(TIMER1_COMPA_vect)
{
count_t_elapsed++;
}