-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgpsdo_V4_2.ino
1677 lines (1490 loc) · 67.3 KB
/
gpsdo_V4_2.ino
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
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
ONLY for 328 based Arduinos!!
With modulo 50000
With getcommand from JimH (replaces dipswitches)
Added timer_us to ns TIC values to get much much larger capture range
Changed PI-loop now uses float with remainder and not DI-term for P-term
Changed average TIC+DAC storing to instant TIC+TNCT1 in hold mode
Still EEPROM storage of 3hour averages and dac start value
Added EEPROM storage of time constant and gain and many more parameters(see getcommand "help" below)
Please check gain and timeConst and adapt to your used VCO. Holdmode is very useful to check range of VCO
-Hardware:
This version uses input capture on D8 !!! so jumper D2-D8 if you have an old shield with 1PPS to D2
Uses 1 ns res/ 1 us TIC and internal PWM (2 x 8bits)
1PPS to capture interrupt (D8)and HC4046 pin 14
1MHz from HC390 (div2*5)to HC4046 pin 3
5MHz from HC390 (div2)to timer1 (D5)
1N5711+3.9k in series from HC4046 pin 15 to ADC0. 1nF NPO + 10M to ground on ADC0
16bit PWM DAC with two-pole lowpass filter from D3 (39k+4.7uF+39k+4.7uF) and D11 (10M)
Put a LED + resistor on D13 to show Lock status
Optional temperature sensors on ADC2 (used for temperature compensation) and ADC1 (just indication)
ADC3 can be read and used for whatever you want
For UNO a recommendation is to connect one jumper from reset to a 10uf capacitor connected to ground.
With this jumper shorted the Arduino UNO will not reset every time the serial monitor is started.
For downloading a new program the jumper need to be taken away.
*** paulv modifications V3:
* added interrupt driven decharge of C1 capacitor instead of 10M bleed resistor
* print **** instead of unLock to make it better standout
* changed the temperature labels in the report to a more meaningful OCXO and ambient
* added a function to automatically increase the TC while keeping a lock (especially for the reciprocal counter)
* V3.70 added code to read a DS18B20 sensor to register the room temperatures. This sensor is located outside of the enclosure
* V3.71 added averaging for the temp sensors for use in the report, fixed indentations to easier read the code
* V3.71 changed variable "time" to "Ltime" because the new compiler has time as a reserved name
* V3.80 added code to obtain the qErr data from the NEO and incorporate that. More info on my Blog
* V4.0 changed the dual 16-bit PWM "DAC" to the DAC8531 16-bit DAC
* V4.1 changed the DS18B20 with an LM35DZ as the room temperature sensor on ADC3. Eliminated the ns_raw and qErr columns from the report
*
* My changes start with *** paulv: in comments so they are easy to locate
*
* Select: Arduino Nano - Old Bootloader
*
*/
#include <EEPROM.h>
//*** paulv: added section
String FW_VERSION = "4.2"; // current revision number firmware
String HW_VERSION = "4.2"; // current revision number hardware
#include "DAC8551.h"
// Software SPI pins (SS, CLK, MISO) or Select, Clock, Data
// use software SPI, hardware speed is not required
DAC8531 mydac(12, 9, 7); // SS=D12, MISO=D9, CLK=D7
#include <Wire.h> // *** paulv: added support for obtaining the qErr data from the NEO
#define UBX_ADDR 0x42 // default DDC (is i2c) address
// connect SDA pin 18 on NEO 6/7/8 to A4 on Nano, pull-up is provided by the NEO
// connect SCL pin 19 on NEO 6/7/8 to A5 on Nano, pull-up is provided by the NEO
boolean apply_qErr = true;
unsigned int mValue = 200; // delay period between NEO polling and getting the response
// if this delay is too large, the time counter will stop to the increment
// UR8US used 300, but that's too high. It needs to be below 250 with this code
float qErr = 0;
boolean isSetqErr = false;
// Decharge pin for charging capacitor. Port is toggled between hi-Z input and output driven low
const int decharge = 10;
// setup the automatic TC setting
boolean lock = false; // used for the lock/unlock status
int lockTimer = 0; // used to set a time between timeConst increments
int maxTC = 500; // maximum timeConst for the autoAdvanceTC method
boolean autoTCrun = true; // use to either set execution to auto TC or fixed TC
// added IIR filter for the temperature sensors
// The readings fluctuate every second which is not relevant, average them out
// Lars already uses a filter for ADC1 that is used in the DAC compensation.
// I left that as is, and only use the filtered values for the report.
// The IIR filter algorithm:
// avg = avg + ((new_value - avg) / filter_weight)
long tempADC1_IIR; // PCB temperature
long tempADC2_IIR; // OCXO temperature
long tempADC3_IIR; // room temperature
int IIR_Temp_Filter_Weight = 3; // IIC filter weight value
float roomTempCorr = -4.0; // correcting to the actual temperature
// *** paulv: end of my added section
int warmUptime = 60; // gives 1 minute hold during warmup. Set to 3 for VCTCXO or 500 for 3 minutes for a cold start
long dacValueOut = 32767; // 16bit PWM-DAC setvalue=startvalue Max 65535 (if nothing stored in the EEPROM)
long dacValue; // this is also same as "DACvalueOld" Note: is "0-65535" * timeconst
long dacValue2; // had to add this for calculation in PI-loop
long dacValueWithTempComp; // Note: is "0-65535" * timeconst
// These are no longer required when using the real DAC
//const int PWMhighPin = 3; // high PWM-DAC pin (connected to 39k)
//const int PWMlowPin = 11; // low PWM-DAC pin (connected to 10M)
//int valuePWMhigh; // high PWM-DAC byte
//int valuePWMlow; // low PWM-DAC byte
volatile int TIC_Value; // analog read 0 - time value. About 1ns per bit with 3.9Kohm + 1nF
int TIC_ValueOld;//old not filtered TIC_value
int TIC_Offset = 500; // ADC value for Reference time
long TIC_ValueFiltered; // prefiltered TIC value
long TIC_ValueFilteredOld;// old filtered value
long TIC_ValueFilteredForPPS_lock; // prefiltered value just for PPS lock
volatile unsigned int timer1CounterValue; //counts 5MHz clock modulo 50000
long timer1CounterValueOld = 0 ;
unsigned int TCNT1new = 0; //in main loop
unsigned int TCNT1old = 0;
unsigned long overflowCount = 0; // counter for timer1 overflows
long timer_us; // timer1 value in microseconds offset from 1pps
long timer_us_old; // used for diff_ns
long diff_ns; // difference between old and new TIC_Value
long diff_ns_ForPPS_lock; // prefiltered value just for PPS lock
long tempADC1; // *** paulv: changed to long, analog read 1 - for example for internal NTC if oscillator external to Arduino
long tempADC2; // analog read 2 - for oscillator temp eg LM35 or NTC - used for temperature compensation
long tempADC3; // *** paulv: analog read 3 - added the LM35DT room temperature sensor
long tempADC2_Filtered; // analog read 2 - temp filtered for temp correction
long tempCoeff = 0; // 650 = 1x10-11/°C if 1°C= +10bit temp and 65dac bits is 1x10-11 // set to -dacbits/tempbits*100?
long tempRef = 280; // offset for temp correction calculation - 280 is about 30C for LM35 (set for value at normal room temperature)
unsigned int temperature_Sensor_Type = 0; //
long timeConst = 32; // time constant in seconds
long timeConstOld = 32; // old time constant
int filterDiv = 2; // filterConst = timeConst / filterDiv
long filterConst = 16; // pre-filter time const in secs (TIC-filtering)
long filterConstOld = 16; // old Filter time constant
float I_term; //for PI-loop
float P_term;
long I_term_long;
float I_term_remain;
long gain = 12; //VCO freq DAC bits per TIC bit (65536/VCOrange in ppb (eg. with 1nS/bit and 100ppb DACrange gives gain=655))
float damping = 3.0; //Damping in loop
unsigned long Ltime; //seconds since start
unsigned long timeOld; //last seconds since start
unsigned int missedPPS; // incremented every time pps is missed
unsigned long timeSinceMissedPPS;
volatile boolean PPS_ReadFlag = false; // set true every time pps is received
int lockPPSlimit = 100; // if TIC filtered for PPS within +- this for lockPSfactor * timeConst = PPSlocked
int lockPPSfactor = 5; // see above
unsigned long lockPPScounter; // counter for PPSlocked
boolean PPSlocked; //digital pin and prints 0 or 1
const int ppsLockedLED = 13; // LED pin for pps locked
int i; // counter for 300secs before storing temp and dac readings average
int j; // counter for stored 300sec readings
int k; // counter for stored 3hour readings
unsigned int StoreTIC_A[144]; //300sec storage
unsigned int StoreTempA[144];
unsigned int StoreDAC_A[144];
long sumTIC;
long sumTIC2;
long sumTemp;
long sumTemp2;
unsigned long sumDAC;
unsigned long sumDAC2;
unsigned int totaltime3h; // counter for power-up time updated every third hour
unsigned int restarts; // counter for restarts/power-ups
boolean restartFlag = true;
unsigned int ID_Number;
boolean lessInfoDisplayed;
boolean nsDisplayedDecimals;
// for get command
int incomingByte; // for incoming serial data in getCommand
enum Modes {hold, run};
Modes opMode = run; //operating mode
Modes newMode = hold; // used to reset timer_us when run is set and at to many missing PPS
unsigned int holdValue; //DAC value for Hold mode
// for TIC linearization
float TICmin = 12.0;
float TICmax = 1012.0;
float x3 = 0.03;
float x2 = 0.1;
float x1;
float TIC_Scaled;
float TIC_ValueCorr;
float TIC_ValueCorrOld;
float TIC_ValueCorrOffset;
////////////////////////////////////////////////////////////////////////////////////////////////
// timer1 capture interrupt routine - this runs at rising edge of 1 PPS on D8
// paulv: **** modified to add a digital decharge of the 10nF charging capacitor
ISR(TIMER1_CAPT_vect)
{
timer1CounterValue = ICR1; // read the captured timer1 200ns counter value
TIC_Value = analogRead(A0); // ns value
pinMode(decharge,OUTPUT); // *** paulv change from hi-Z to output
digitalWrite(decharge, 0); // *** paulv bring it low to start the de-charging of C1
PPS_ReadFlag = true;
delayMicroseconds(20); // *** paulv allow sufficient time to fully drain the capacitor
pinMode(decharge,INPUT); // *** paulv revert the port back to hi-Z
}
////////////////////////////////////////////////////////////////////////////////////////////////
void calculation()
{
// set timer1 start value in the beginning
if (Ltime < 2 || (Ltime > warmUptime-2 && Ltime < warmUptime))
{
TCNT1 = 25570; // is a guessed value to get around 25000 next time
}
// TIC linearization
// x2 = (((TICmid-TICmin)/(TICmax-TICmin)*1000) - 500.0)/250.0 - 0.05; // just for info
// x2 = 0.1
// x3 = 0.03
x1 = 1.0 - x3 - x2; // 0.87
TIC_Scaled = ((float)TIC_Offset - TICmin)/(TICmax - TICmin)*1000; // Scaling for TIC_Offset
TIC_ValueCorrOffset = TIC_Scaled * x1 + TIC_Scaled * TIC_Scaled * x2 / 1000.0 + TIC_Scaled * TIC_Scaled * TIC_Scaled * x3 /1000000.0;
TIC_Scaled = ((float)TIC_Value - TICmin)/(TICmax - TICmin)*1000; // Scaling for TIC_Value
TIC_ValueCorr = TIC_Scaled * x1 + TIC_Scaled * TIC_Scaled * x2 / 1000.0 + TIC_Scaled * TIC_Scaled * TIC_Scaled * x3 /1000000.0;
TIC_ValueCorr -= qErr; // apply the qErr compensation
// timer_us
// apply the qErr correction
//timer_us = timer_us + 50000 - (((timer1CounterValue - timer1CounterValueOld) * 200 + TIC_Value - TIC_ValueOld)+50000500)/1000; // old
timer_us = timer_us + 50000 - (((timer1CounterValue - timer1CounterValueOld) * 200 + TIC_Value - int(qErr) - TIC_ValueOld)+50000500)/1000;
if (newMode == run) // reset timer_us if change from hold mode to run mode
{
timer_us = 0 ;
timer_us_old = 0 ;
TIC_ValueFilteredOld = TIC_Offset * filterConst;
newMode = hold;
}
if (Ltime < 3 || (Ltime > warmUptime-1 && Ltime < warmUptime+1)) // reset in the beginning and end of warmup
{
timer_us = 0 ;
}
if ((abs(timer_us)- 2) > timeConst * 65536 / gain / 1000 && opMode == run && Ltime > warmUptime )
{
timer_us = 0 ;
timer_us_old = 0 ;
TIC_ValueFilteredOld = TIC_Offset * filterConst;
}
if (TIC_ValueOld == 1023) // reset if 10MHz was missing
{
timer_us = 0 ;
timer_us_old = 0 ;
TIC_ValueFilteredOld = TIC_Offset * filterConst;
}
// Diff_ns
if (TIC_ValueCorr > TIC_ValueCorrOld)
{
diff_ns = (timer_us - timer_us_old)*1000 + long (TIC_ValueCorr - TIC_ValueCorrOld + 0.5); // = Frequency in ppb if updated every second! Note: TIC linearized
}else{
diff_ns = (timer_us - timer_us_old)*1000 + long (TIC_ValueCorr - TIC_ValueCorrOld -0.5);
}
// time - is supposed to be approximately seconds since start
Ltime = Ltime + (overflowCount + 50)/100;
overflowCount = 0;
// missedPPS
if (Ltime - timeOld > 1)
{
missedPPS = missedPPS + 1;
timeSinceMissedPPS = 0;
}else{
timeSinceMissedPPS = timeSinceMissedPPS + 1;
}
////// PPS locked
// Low Pass Filter of TIC_Value for PPS lock // /16 is used as 500ns error and /16 is about 30ns that seems reasonable
TIC_ValueFilteredForPPS_lock = TIC_ValueFilteredForPPS_lock + (TIC_Value * 16 - TIC_ValueFilteredForPPS_lock) / 16;
// Low Pass Filter of diff_ns for PPS lock
diff_ns_ForPPS_lock = diff_ns_ForPPS_lock + (diff_ns * 16 - diff_ns_ForPPS_lock) / 16;
lockPPScounter = lockPPScounter + 1;
if (abs(TIC_ValueFilteredForPPS_lock / 16 - TIC_Offset) > lockPPSlimit)
{lockPPScounter = 0;}
if (abs(diff_ns_ForPPS_lock/16) > 20)// if freq more than 20ppb wrong (had to add this to avoid certain combinations not covered by above)
{lockPPScounter = 0;}
if (lockPPScounter > timeConst * lockPPSfactor)
{
PPSlocked = 1;
}else{
PPSlocked = 0;
}
// turn on LED 13 if "locked"
digitalWrite(ppsLockedLED,PPSlocked);
////// Read the temperature values
int dummyreadADC = analogRead(A2); //without this ADC1 is influenced by ADC0
tempADC1 = analogRead(A2);
dummyreadADC = analogRead(A1); //without this ADC2 is influenced by ADC1
tempADC2 = analogRead(A1);
dummyreadADC = analogRead(A3); //without this ADC3 is influenced by ADC2
tempADC3 = analogRead(A3); // read the room temperature sensor
dummyreadADC = analogRead(A0); //without this, TIC_Value (ADC0) is influenced by ADC3
// tempADC0 is read by the interrupt routine
// set filter constant
filterConst = timeConst / filterDiv;
filterConst = constrain (filterConst, 1,1024);
if (PPSlocked == 0 || opMode == hold) filterConst = 1;
// recalculation of value
if(timeConst != timeConstOld)
{
dacValue = dacValue / timeConstOld * timeConst;
}
if(filterConst != filterConstOld)
{
TIC_ValueFilteredOld = TIC_ValueFilteredOld / filterConstOld * filterConst;
TIC_ValueFiltered = TIC_ValueFiltered / filterConstOld * filterConst;
}
// Low Pass Filter for TICvalue (Phase Error)
// Remember that TIC_ValueFiltered is multiplied by filterConst
// Don´t update if outlier. Accepts diff_ns less than same ns as vco range in ppb + 200ns
if abs(diff_ns <6500)// First check to avoid overflow in next calculation (also max VCO range is about 6500ns/s)
{
if( abs(diff_ns * gain) < (65535 + 200 * gain))
{ // apply the qErr correction
//TIC_ValueFiltered = TIC_ValueFiltered + ((timer_us*1000 + TIC_Value) * filterConst - TIC_ValueFiltered + (filterConst/2)) / filterConst; // old
TIC_ValueFiltered = TIC_ValueFiltered + ((timer_us*1000 + (TIC_Value - int(qErr))) * filterConst - TIC_ValueFiltered + (filterConst/2)) / filterConst;
}
}
if (Ltime > warmUptime && opMode == run) // Don't change DAC-value during warm-up time or if in hold mode
{
////// PI-loop /////////////////////////////////////
P_term = (TIC_ValueFiltered - TIC_Offset * filterConst) / float(filterConst) * gain; // remember /timeConst is done before dacValue is sent out
I_term = P_term / damping / float(timeConst) + I_term_remain;
I_term_long = long(I_term);
I_term_remain = I_term - I_term_long;
dacValue += I_term_long;
dacValue2 = dacValue + P_term;
///////////////////////////////////////////////////
}else{
(dacValue2 = dacValue); // No change
}
// Low Pass Filter for temperature
tempADC2_Filtered = tempADC2_Filtered + (tempADC2 * 100 - tempADC2_Filtered) / 100;
// Temperature correction for DAC value
dacValueWithTempComp = dacValue2 + ((tempRef * 100 - tempADC2_Filtered) * tempCoeff / 10000 * timeConst);
// Check that dacValue is within limits
if (dacValue < 0)
{ dacValue = 0;}
if (dacValue > (65535 * timeConst))
{ dacValue = (65535 * timeConst);}
dacValueOut = dacValueWithTempComp / timeConst; // PWM-DAC value
if (dacValueOut < 0)
{ dacValueOut = 0;}
if (dacValueOut > 65535)
{ dacValueOut = 65535;}
// manual set of dacvalue if in hold and not 0, if zero hold old value
if (holdValue > 0 && opMode == hold)
{dacValueOut = holdValue ;}
// *** paulv this section is no longer needed when using the real DAC
// Set "16bit DAC"
//valuePWMhigh = highByte(dacValueOut);
//valuePWMlow = lowByte(dacValueOut);
//analogWrite(PWMhighPin,valuePWMhigh);
//analogWrite(PWMlowPin,valuePWMlow);
// *** paulv
mydac.setValue(dacValueOut); // send the 16-bit value over SPI to the DAC
// Increment restart at time 100 (100 chosen arbitrary)
if (Ltime > 100 && restartFlag == true)
{
restarts = restarts + 1;
EEPROM.write(991, highByte(restarts));
EEPROM.write(992, lowByte(restarts));
restartFlag = false;
}
///////////////////////////////////////////////////
//Storage of average readings that is later printed
sumTIC = sumTIC + (TIC_Value * 10);
sumTemp = sumTemp + (tempADC2 * 10);
sumDAC = sumDAC + dacValueOut;
i = i + 1;
// 300 second section
if (i == 300)
{
if (opMode == run)
{StoreTIC_A[j]= sumTIC / i;}
else
{StoreTIC_A[j]= TIC_Value;}
sumTIC2 = sumTIC2 + sumTIC / i;
sumTIC = 0;
StoreTempA[j]= sumTemp / i;
sumTemp2 = sumTemp2 + sumTemp / i;
sumTemp = 0;
if (opMode == run)
{StoreDAC_A[j]= sumDAC / i;}
else
{StoreDAC_A[j]= (49999 - timer1CounterValue);}
sumDAC2 = sumDAC2 + sumDAC / i;
sumDAC = 0;
i = 0;
j = j + 1;
// 3 hour section
if (j % 36 == 0) // store every 36 x 300sec (3 hours)
{
sumTIC2 = sumTIC2 / 36;
if (opMode == run)
{
EEPROM.write(k, highByte(sumTIC2));
EEPROM.write(k+144, lowByte(sumTIC2));
}else{
EEPROM.write(k, highByte(TIC_Value));
EEPROM.write(k+144, lowByte(TIC_Value));
}
sumTIC2 = 0;
sumTemp2 = sumTemp2 / 36;
if (opMode == run)
{
sumTemp2 = sumTemp2 + 20480 ;
if (lockPPScounter > 10800)
{
sumTemp2 = sumTemp2 + 20480 ;
}
}
if (Ltime < 20000) // first after start
{
sumTemp2 = sumTemp2 + 10240 ;
}
EEPROM.write(k+576, highByte(sumTemp2));
EEPROM.write(k+720, lowByte(sumTemp2));
sumTemp2 = 0;
sumDAC2 = sumDAC2 / 36;
if (opMode == run)
{
EEPROM.write(k+288, highByte(sumDAC2));
EEPROM.write(k+432, lowByte(sumDAC2));
}else{
EEPROM.write(k+288, highByte(49999 - timer1CounterValue));
EEPROM.write(k+432, lowByte(49999 - timer1CounterValue));
}
if (opMode == run && lockPPScounter > 10800)
{
EEPROM.write(1017, highByte(sumDAC2));
EEPROM.write(1018, lowByte(sumDAC2));
}
sumDAC2 = 0;
if (j == 144) // 144 x 300sec (12 hours)
{
j = 0;
}
k = k + 1;
if (k == 144) // 144 x 10800sec (18 days)
{
k = 0;
}
// finally...
EEPROM.write(1023, k); // store present k (index of 3 hour average, used in setup)
totaltime3h = totaltime3h + 1;
EEPROM.write(993, highByte(totaltime3h));
EEPROM.write(994, lowByte(totaltime3h));
} // end of 3 hr section
} // end of 300 second section
// storage of old parameters
timer1CounterValueOld = timer1CounterValue;
// applly the qErr value
// TIC_ValueOld = TIC_Value; // old
TIC_ValueOld = TIC_Value - int(qErr); // /0.926
TIC_ValueCorrOld = TIC_ValueCorr;
timer_us_old = timer_us;
timeConstOld = timeConst;
filterConstOld = filterConst;
timeOld = Ltime;
TIC_ValueFilteredOld = TIC_ValueFiltered;
} // end of calculation()
////////////////////////////////////////////////////////////////////////////////////////////////
void poll_qErr()
{
// Request the data string we are interested in from the NEO
// For details, look at the UBX protocol : section 32
Wire.beginTransmission(UBX_ADDR);
// Every Frame starts with a 2-byte Preamble consisting of two synchronization characters
// and ends with a 16-bit checksum
Wire.write(byte(0xB5)); // Sync character 1
Wire.write(byte(0x62)); // Sync character 2
// UBX Class TIM : Timing messages - Page 432
// TIM-TP 0x0D 0x01 16 bytes returned, Periodic/Polled, Time Pulse time data
/*
* This message contains information on the timing of the next pulse at the TIMEPULSE0 output.
*
* The Quantization Error (qErr) is located at Byte Offset 8 has 4 bytes and value is in ps.
*/
Wire.write(byte(0x0D)); // Message Class
Wire.write(byte(0x01)); // Message ID
Wire.write(byte(0x00)); // 1 byte Little Endian length
Wire.write(byte(0x00)); // 1 byte Little Endian length
Wire.write(byte(0x0E)); // 1 byte standard UBX checksum CK_A
Wire.write(byte(0x37)); // 1 byte standard UBX checksum CK_B
Wire.endTransmission();
}
////////////////////////////////////////////////////////////////////////////////////////////////
void get_qErr()
{
// Read the returned data from the NEO and process the qErr data
Wire.requestFrom( UBX_ADDR, 24 );
byte tp_resp[24];
int idx = 0;
isSetqErr = false; // added to original code to eliminate the alternate 0 readings
while(Wire.available())
{
byte b = Wire.read();
if (idx <24)
tp_resp[idx] = b;
idx++;
//Serial.print(int(b), HEX);
//Serial.print(' ');
}
// see if we have the right response; if so process it
if ( (idx==24) && (tp_resp[0]==0xB5) && (tp_resp[1]==0x62) && (tp_resp[2]==0x0D) && (tp_resp[3]==0x01) )
{
int32_t ps;
ps = *( (int32_t*)(tp_resp+14) ); // 4 bytes of qErr
qErr = ((float)ps)/1000.0; // convert to nano seconds to make it the same as the NS values
//Serial.print("qErr = ");
//Serial.print(qErr);
//Serial.print(" ns");
if (fabs(qErr)>50.0) // eliminate outside boundary numbers
qErr = 0;
else
isSetqErr = true;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
void getCommand()
{
char ch;
long zz; // *** paulv Lars used "z" as the name, but I wanted to use it. Sorry Lars
enum Command { // this is the command set
a = 'a', A = 'A', // set damping
b = 'b', B = 'B', // set reference temperature
c = 'c', C = 'C', // set temperature coefficient
d = 'd', D = 'D', // set dacvalue (followed by a value)
e = 'e', E = 'E', // erase (followed by a value)
f = 'f', F = 'F', // help (followed by a value)
g = 'g', G = 'G', // gain (followed by new value)
h = 'h', H = 'H', // hold (followed by a DAC value note: 0 will set only hold)
i = 'i', I = 'I', // toggles less or more info
j = 'j', J = 'J', // set temperature sensor type
// k
l = 'l', L = 'L', // set TIC linearization parameters min max square
m = 'm', M = 'M', // set delay for i2c (UR8US calls this Kounter)
n = 'n', N = 'N', // set ID number
o = 'o', O = 'O', // TIC_Offset (followed by new value)
p = 'p', P = 'P', // set prefilter div
q = 'q', Q = 'Q', // apply qErr
r = 'r', R = 'R', // run
s = 's', S = 'S', // save (followed by a value)
t = 't', T = 'T', // time const (followed by new value)
u = 'u', U = 'U', // ***paulv: added room temp correction factor
// v
w = 'w', W = 'W', // set warmup time (to allow for warm up of oscillator)
x = 'x', X = 'X', // use automatic TC
// y
// z
};
if (Serial.available() > 0) //process if something is there
{
ch = Serial.read();
// process what came in
switch(ch) {
case a: // set damping command
case A:
zz = Serial.parseInt(); //needs new line or carriage return set in Arduino serial monitor
if (zz >=50 && zz <= 1000)
{
damping = zz / 100.0;
Serial.print(F("Damping "));
Serial.println(damping);
}
else { Serial.println(F("Not a valid damping value - Shall be between 50 and 1000"));}
break;
case b: // set temperature offset command
case B:
zz = Serial.parseInt();
if (zz >=1 && zz <= 1023)
{
tempRef = zz;
Serial.print(F("Temperature offset "));
Serial.println(tempRef);
}
else { Serial.println(F("Not a valid temperature offset value - Shall be between 1 and 1023"));}
break;
case c: // Set temperature coefficient
case C:
zz = Serial.parseInt();
if (zz >=0 && zz <= 10000)
{
tempCoeff = zz;
Serial.print(F("Temperature Coefficient "));
Serial.println(tempCoeff);
}
else if (zz >=10001 && zz <= 20000)
{
tempCoeff = (10000-zz);
Serial.print(F("Temperature Coefficient "));
Serial.println(tempCoeff);
}
else { Serial.println(F("Not a valid temperature coefficient value - Shall be between 0 and 20000"));}
break;
case d: // set dacValue command
case D:
zz = Serial.parseInt();
if (zz >=1 && zz <= 65535)
{
dacValue = zz * timeConst;
Serial.print(F("dacValue "));
Serial.println(zz);
}
else { Serial.println(F("Not a valid dacValue - Shall be between 1 and 65535"));}
break;
case e: // erase command
case E:
zz = Serial.parseInt();
switch (zz) {
case 1:
Serial.println(F("Erase 3h storage in EEPROM "));
for (int i = 0; i < 864; i++)
{EEPROM.write(i, 0);}
EEPROM.write(1023, 0);
k = 0 ; //reset 3hours counter
break;
case 22:
Serial.println(F("Erase all EEPROM to zero"));
for (int i = 0; i < 1024; i++)
{EEPROM.write(i, 0);}
k = 0 ; //reset 3hours counter
break;
case 33:
Serial.println(F("Erase all EEPROM to -1"));
for (int i = 0; i < 1024; i++)
{EEPROM.write(i, 255);}
k = 0 ; //reset 3hours counter
break;
default:
Serial.println(F("Not a valid value for erase - Shall be 1 or 22"));
}
break;
case f: // help command
case F:
zz = Serial.parseInt();
switch (zz) {
case 1:
Serial.println("");
Serial.println(F("Info and help - To get values for gain etc type f2 <enter>, f3 <enter> reads ADC3 and f4 <enter> EEPROM"));
printHeader1_ToSerial();
Serial.print("\t");
printHeader2_ToSerial();
Serial.println("");
Serial.println("");
Serial.println(F("Typing a<value><enter> will set a new damping between between 0.50 and 10.00 set 50 to 1000"));
Serial.println(F("Typing b<value><enter> will set a new tempRef between 1 and 1023"));
Serial.println(F("Typing c<value><enter> will set a new tempCoeff set between 0 and 10000. Adding 10000 gives negative tc"));
Serial.println(F("Typing d<value><enter> will set a new dacValue between 1 and 65535"));
Serial.println(F("Typing e<value><enter> will erase the 3 hour storage in EEPROM if value 1 and all EEPROM if 22 (33 sets all EEPROM to FF)"));
Serial.println(F("Typing g<value><enter> will set a new gain between 10 and 65535"));
Serial.println(F(" gain = (65536/settable VCOrange in ppb) (eg. 100ppb DACrange gives gain=655)"));
Serial.println(F("Typing h<value><enter> will set hold mode and the entered dacValue if not h0 that uses the old"));
Serial.println(F("Typing i<value><enter> with value 1 will toggle ns decimal point else will toggle amount of information "));
Serial.println(F("Typing j<value><enter> Set temp sensor type 0=raw 1=LM35 2=10kNTC+68k 3=10kNTC+47k (second digit=adc1 eg 3x)"));
Serial.println(F("Typing l<enter> will set TIC linearization parameters min max square"));
Serial.println(F(" values 1-500 sets min to 0.1-50, values 800-1023 sets max, values 1024-1200 sets square to 0.024-0.200"));
Serial.println(F("Typing n<value><enter> will set ID number 0-65535 that is displayed "));
Serial.println(F("Typing o<value><enter> will set a new TIC_Offset between 10 and 1020 ns"));
Serial.println(F("Typing p<value><enter> will set a new prefilter div between 2 and 4"));
Serial.println(F("Typing q<value><enter> will appy (1) or not (0) the qErr correction"));
Serial.println(F("Typing r<enter> will set run mode"));
Serial.println(F("Typing s<value><enter> will save gain etc to EEPROM if value 1 and dacvalue if 2"));
Serial.println(F("Typing t<value><enter> will set a new time constant between 4 and 32000 seconds"));
Serial.println(F("Typing u<enter> will set the room temp correction factor"));
Serial.println(F("Typing w<value><enter> will set a new warmup time between 2 and 1000 seconds"));
Serial.println(F("Typing x<value><enter> will set autoTC with 1, off with 0")); // *** paulv added command
Serial.println("");
printHeader3_ToSerial();
break;
case 2:
Serial.println("");
Serial.print(F("Gain ")); Serial.print("\t"); Serial.print(gain); Serial.print("\t");
Serial.print(F("Damping ")); Serial.print("\t"); Serial.print(damping); Serial.print("\t");
Serial.print(F("timeConst ")); Serial.print("\t"); Serial.print(timeConst); Serial.print("\t");
Serial.print(F("FilterDiv ")); Serial.print("\t"); Serial.print(filterDiv); Serial.print("\t");
Serial.print(F("TIC_Offset ")); Serial.print("\t"); Serial.println(TIC_Offset);
Serial.print(F("TempRef ")); Serial.print("\t"); Serial.print(tempRef); Serial.print("\t");
Serial.print(F("TempCoeff ")); Serial.print("\t"); Serial.print(tempCoeff); Serial.print("\t");
Serial.print(F("TICmin ")); Serial.print("\t"); Serial.print(TICmin,1); Serial.print("\t");
Serial.print(F("TICmax ")); Serial.print("\t"); Serial.print(TICmax,0); Serial.print("\t");
Serial.print(F("Square comp ")); Serial.print("\t"); Serial.println(x2,3);
Serial.print(F("Warm up time ")); Serial.print("\t"); Serial.print(warmUptime); Serial.print("\t");
Serial.print(F("LockPPScounter ")); Serial.print("\t"); Serial.print(lockPPScounter); Serial.print("\t");
Serial.print(F("MissedPPS ")); Serial.print("\t"); Serial.print(missedPPS); Serial.print("\t");
Serial.print(F("timeSinceMissedPPS ")); Serial.println(timeSinceMissedPPS);
Serial.print(F("ID_Number ")); Serial.print("\t"); Serial.print(ID_Number); Serial.print("\t");
Serial.print(F("Restarts ")); Serial.print("\t"); Serial.print(restarts); Serial.print("\t");
Serial.print(F("Total hours")); Serial.print("\t"); Serial.println(totaltime3h * 3);
Serial.print(F("autoTC ")); Serial.print("\t"); Serial.print(autoTCrun); Serial.print("\t"); // *** paulv added command
Serial.print(F("roomTCorr ")); Serial.print("\t"); Serial.print(roomTempCorr); Serial.print("\t"); //*** paulv added
Serial.println("");
printHeader3_ToSerial();
break;
case 3:
Serial.println ("");
Serial.print (F("ADC3 = "));
Serial.println(analogRead(A3));
Serial.println ("");
break;
case 4:
Serial.println ("");
Serial.println (F("EEPROM content: "));
Serial.print (F("restarts = "));
zz=(EEPROM.read(991)*256 + EEPROM.read(992)); Serial.println((unsigned int)zz);
Serial.print (F("totaltime3h = "));
zz=(EEPROM.read(993)*256 + EEPROM.read(994)); Serial.println((unsigned int)zz);
Serial.print (F("temperature_Sensor_Type = "));
zz=(EEPROM.read(995)*256 + EEPROM.read(996)); Serial.println((unsigned int)zz);
Serial.print (F("ID_Number = "));
zz=(EEPROM.read(997)*256 + EEPROM.read(998)); Serial.println((unsigned int)zz);
Serial.print (F("TICmin = "));
zz=(EEPROM.read(999)*256 + EEPROM.read(1000)); Serial.println((unsigned int)zz);
Serial.print (F("TICmax = "));
zz=(EEPROM.read(1001)*256 + EEPROM.read(1002)); Serial.println((unsigned int)zz);
Serial.print (F("x2 = "));
zz=(EEPROM.read(1003)*256 + EEPROM.read(1004)); Serial.println((unsigned int)zz);
Serial.print (F("TIC_Offset = "));
zz=(EEPROM.read(1005)*256 + EEPROM.read(1006)); Serial.println((unsigned int)zz);
Serial.print (F("filterDiv = "));
zz=(EEPROM.read(1007)*256 + EEPROM.read(1008)); Serial.println((unsigned int)zz);
Serial.print (F("warmUptime = "));
zz=(EEPROM.read(1009)*256 + EEPROM.read(1010)); Serial.println((unsigned int)zz);
Serial.print (F("damping = "));
zz=(EEPROM.read(1011)*256 + EEPROM.read(1012)); Serial.println((unsigned int)zz);
Serial.print (F("tempRef = "));
zz=(EEPROM.read(1013)*256 + EEPROM.read(1014)); Serial.println((unsigned int)zz);
Serial.print (F("tempCoeff = "));
zz=(EEPROM.read(1015)*256 + EEPROM.read(1016)); Serial.println((unsigned int)zz);
Serial.print (F("dacValueOut = "));
zz=(EEPROM.read(1017)*256 + EEPROM.read(1018)); Serial.println((unsigned int)zz);
Serial.print (F("gain = "));
zz=(EEPROM.read(1019)*256 + EEPROM.read(1020)); Serial.println((unsigned int)zz);
Serial.print (F("timeConst = "));
zz=(EEPROM.read(1021)*256 + EEPROM.read(1022)); Serial.println((unsigned int)zz);
Serial.print (F("k = "));
Serial.println (EEPROM.read(1023));
Serial.println ("");
break;
default:
Serial.println(F("Not a valid value for help - Shall be 1 to 4"));
}
break;
case g: // gain command
case G:
zz = Serial.parseInt();
if (zz >=10 && zz <= 65534)
{
gain = zz;
Serial.print(F("Gain "));
Serial.println(zz);
}
else { Serial.println(F("Not a valid gain value - Shall be between 10 and 65534"));}
break;
case h: // hold command
case H:
zz = Serial.parseInt();
if (zz >=0 && zz <= 65535)
{
opMode = hold;
newMode = hold;
Serial.print(F("Hold "));
holdValue = zz ;
Serial.println(holdValue);
}
else { Serial.println(F("Not a valid holdValue - Shall be between 0 and 65535"));}
break;
case i: // help command
case I:
zz = Serial.parseInt();
if (zz == 1)
{ nsDisplayedDecimals = !nsDisplayedDecimals ; }
else
{ lessInfoDisplayed = !lessInfoDisplayed ; }
break;
case j: // temperature_Sensor_Type
case J:
zz = Serial.parseInt();
if (zz >=0 && zz <= 99)
{
temperature_Sensor_Type = zz;
Serial.print(F("temperature_Sensor_Type "));
Serial.println(zz);
}
else { Serial.println(F("Not a valid temperature_Sensor_Type value - Shall be between 0 and 99"));}
break;
case l: // set TIC linearization parameters command
case L:
zz = Serial.parseInt();
if (zz >=1 && zz <= 500)
{
TICmin = zz / 10.0;
Serial.print(F("TICmin "));
Serial.println(TICmin);
}
else if (zz >=800 && zz <= 1023)
{
TICmax = zz;
Serial.print(F("TICmax "));
Serial.println(TICmax);
}
else if (zz >=1024 && zz <= 1200)
{
x2 = (zz -1000 ) / 1000.0;
Serial.print(F("square compensation "));
Serial.println(x2);
}
else { Serial.println(F("Not a valid value"));}
break;
case m: // UR8US calls this 'K'ounter, but is a delay function
case M: // If the delay is too large, the timer (counter) will no longer increment.
zz = Serial.parseInt();
if (zz >=0 && zz <= 65535)
{
Serial.print(F("m "));
mValue = zz ;
Serial.println(zz);
}
break;
case n: // ID_number
case N:
zz = Serial.parseInt();
if (zz >=0 && zz <= 65534)
{
ID_Number = zz;
Serial.print(F("ID_Number "));
Serial.println(ID_Number);
}
else { Serial.println(F("Not a valid ID_Number value - Shall be between 0 and 65534"));}
break;
case o: // TIC_Offset command
case O:
zz = Serial.parseInt();
if (zz >=10 && zz <= 1020) // *** was 200
{
TIC_Offset = zz;
Serial.print(F("TIC_Offset "));
Serial.println(TIC_Offset);
}
else { Serial.println(F("Not a valid TIC_offset - Shall be between 10 and 1020"));}
break;
case p: // set prefilter div command
case P:
zz = Serial.parseInt();
if (zz >=2 && zz <= 4)
{
filterDiv = zz;
Serial.print(F("Prefilter div "));
Serial.println(filterDiv);
}
else { Serial.println(F("Not a valid prefilter value - Shall be between 2 and 4"));}
break;
case r: // run command
case R:
Serial.println(F("Run"));
opMode = run;
newMode = run;
break;
case s: // save command
case S:
zz = Serial.parseInt();
switch (zz) {
case 1:
Serial.print(F("Saved Gain and timeConstant etc "));//
EEPROM.write(995, highByte(temperature_Sensor_Type));
EEPROM.write(996, lowByte(temperature_Sensor_Type));
EEPROM.write(997, highByte(ID_Number));
EEPROM.write(998, lowByte(ID_Number));
EEPROM.write(999, highByte(int(TICmin * 10.0)));
EEPROM.write(1000, lowByte(int(TICmin * 10.0)));
EEPROM.write(1001, highByte(int(TICmax)));
EEPROM.write(1002, lowByte(int(TICmax)));
EEPROM.write(1003, highByte(int(x2 * 1000.0)));
EEPROM.write(1004, lowByte(int (x2 * 1000.0)));
EEPROM.write(1005, highByte(TIC_Offset));
EEPROM.write(1006, lowByte(TIC_Offset));
EEPROM.write(1007, highByte(filterDiv));
EEPROM.write(1008, lowByte(filterDiv));
EEPROM.write(1009, highByte(warmUptime));
EEPROM.write(1010, lowByte(warmUptime));
EEPROM.write(1011, highByte(int (damping *100)));
EEPROM.write(1012, lowByte(int (damping *100)));
EEPROM.write(1013, highByte(tempRef));
EEPROM.write(1014, lowByte(tempRef));
EEPROM.write(1015, highByte(tempCoeff));
EEPROM.write(1016, lowByte(tempCoeff));
EEPROM.write(1019, highByte(gain));
EEPROM.write(1020, lowByte(gain));
EEPROM.write(1021, highByte(timeConst));
EEPROM.write(1022, lowByte(timeConst));
Serial.println("");
break;