'CR1000X Series Datalogger
'Example datalogger program for Apogee Instruments SI-100 series infrared raidometers
'date: December 23rd, 2020
'program author: Jacob Bingham, Noah Leclerc (2020)

'Explanation of Constants and Variables Used in Datalogger Program
'mC2 = polynomial coefficient (C2) used to calculate slope (m)
'mC1 = polynomial coefficient (C1) used to calculate slope (m)
'mC0 = polynomial coefficient (C0) used to calculate slope (m)
'bC2 = polynomial coefficient (C2) used to calculate intercept (b)
'bC1 = polynomial coefficient (C1) used to calculate intercept (b)
'bC0 = polynomial coefficient (C0) used to calculate intercept (b)
'BattV = datalogger battery voltage
'PanelT = datalogger panel temperature
'DetectorTC = detector (sensor body) temperature in degrees Celsius
'DetectorTK = detector (sensor body) temperature in Kelvin
'TargetmV = mV output of thermopile detector (dependent on temperature difference between target and detector)
'm = slope of equation relating target and detector temperatures to mV output of thermopile
'b = intercept of the equation relating target and detector temperatures to mV output of thermopile
'TargetTK = target temperature in Kelvin
'TargetTC = target temperature in degrees Celsius
'Note that all calibration coefficients are sensor-specific; those listed below are examples and must be changed based on the specific sensor being used.

'Declare Public Variables
Public BattV, PanelT
Public DetectorTC, DetectorTK, TargetmV, m, b, TargetTK, TargetTC

'Declare Calibration Coefficients (sensor-specific)
Public mC2 = 82213
Public mC1 = 7841000
Public mC0 = 1419700000
Public bC2 = 13114
Public bC1 = 185020
Public bC0 = -17215000

'Define Data Tables
DataTable (SurfaceT,1,-1)
	DataInterval (0,1,Min,10)
	Minimum(1,BattV,IEEE4,0,False)
	Sample(1,PanelT,IEEE4)
	Average(1,TargetmV,IEEE4,False)
	Average(1,DetectorTC,IEEE4,False)
  Average(1,TargetTC,IEEE4,False)
EndTable

'Main Program
BeginProg
	Scan(1,Sec,0,0)
		Battery(BattV)
		PanelTemp(PanelT,_60Hz)
		
    'Measure Surface Temperature
    Therm109(DetectorTC,1,1,Vx1,0,_60Hz,1.0,0)
		VoltDiff(TargetmV,1,mV200,2,True,0,_60Hz,1.0,0)
		
		m = mC2 * DetectorTC^2 + mC1 * DetectorTC + mC0
		b = bC2 * DetectorTC^2 + bC1 * DetectorTC + bC0
		
		DetectorTK = DetectorTC + 273.15
		TargetTK = ((DetectorTK^4) + m * TargetmV + b)^0.25
		TargetTC = TargetTK - 273.15

    'Call Output Tables
		CallTable SurfaceT
	NextScan
EndProg


