--[[
---------------------------------------------------------------------------
Remote Train Control Program for Windows

© Copyright 2019 by Mark DiVecchio

	This file is part of Remote Train Control.

	Remote Train Control is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Remote Train Control is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

 Home Page : http://www.silogic.com/trains/RTC_Running.html
---------------------------------------------------------------------------]]
-- Semicolons are not required in Lua code
title = "DTO Loop Test"

require("defines");
require("functions");

local MyEngineNo, MyTIUNo;

TagCount = 0;

MyEngineNo = 0;
MyTIUNo = 0;
LastRunTime = 0;
LastDTO = 0;
LastDOD = 0;
CurrentRunTime = 0;
CurrentDTO = 0;
CurrentDOD = 0;

SaveAcc = 0;
SaveDec = 0;

SHORTLOOP = 1.09628;
LONGLOOP = 1.5884;
LoopLength = LONGLOOP;

Detr = 1;
Rdr = 0;

Tags = {};		-- table to hold all of the tags detected in sequence

-- Configuration Variables
AutoBellWhistle = true;
	
--[[---------------------------------------------------------------------------------------]]
function setup(Engine)
	-- this function is called once when the [Run Script] button is pushed
	-- Engine : Engine number selected on the RTC Main window
	-- TIU : TIU number selected on the RTC Main window
	print("setup()");
	MyEngineNo = Engine;	-- Engine number selected on the RTC Main window
	MyTIUNo = GetTIUNo(MyEngineNo);	-- TIU associated with MyEngineNo
	-- make sure that RFID is connected
	TIUSerialPortConnected, TIUConnected, RFIDSerialPortConnected = CommStatus();	
	--print("TIUSerialPortConnected : " .. tostring(TIUSerialPortConnected) .. " - TIUConnected : " .. tostring(TIUConnected) .. " - RFIDSerialPortConnected : " .. tostring(RFIDSerialPortConnected));
	if (not RFIDSerialPortConnected) then
		print("RFID Port must be enabled for this script");
		return false;	-- false=setup failed
		end
	
	MyEngineNo = Engine;	-- Engine number selected on the RTC Main window
	
	Resp = InputQuery("Numeric Input Required", "Enter Engine Number", MyEngineNo);
	if (Resp == false or Resp == nil) then	-- user pressed [Cancel]
		return false;	-- false=setup failed, true=setup succeeded
		end;
	Enum = tonumber(Resp);
	if (Enum ~= nil) then	-- if the result is a number, then use it, otherwise, ignore it
		MyEngineNo = Enum;
		end
	print("Engine Number = " .. MyEngineNo);
	MyTIUNo = GetTIUNo(MyEngineNo);	-- TIU associated with MyEngineNo
	print("TIU : " .. MyTIUNo);
	assert((MyTIUNo >= 1 and MyTIUNo <= 5),"TIU Number out of range");
	
	-- initialize the layout
	print(string.format("Startup Engine : %d", MyEngineNo));
	EngineStartUp(MyEngineNo, MyTIUNo);
	Sleep(25);			

	if (IsEngineRunning(MyEngineNo)) then	-- IsEngineRunning() REQUIRES use of StartUpActiveEngines() or EngineStartUp()
		print(string.format("Initalizing Engine %d", MyEngineNo));
		-- Set smoke off
		Smoke(OFF, 0, MyEngineNo, MyTIUNo);
		-- Cab Chatter Off
		CabChat(OFF, 0, MyEngineNo, MyTIUNo);
		-- print odometer information
		LastDTO = DTO(MyEngineNo, MyTIUNo);
		LastDOD = DOD(MyEngineNo, MyTIUNo);
		LastRunTime = RunTime();
		print(string.format(" Run Time = %.2f seconds", LastRunTime)); 
		print(string.format(" Trip Odometer = %.6f Smiles", LastDTO)); 
		print(string.format(" Odometer = %.6f Smiles", LastDOD));
	
		-- read up and save the Acc/Dec rates
		SaveAcc, SaveDec = GetRate(MyEngineNo, MyTIUNo);
		if (SaveAcc == nil or SaveDec == nil) then
			print(string.format("Get Acc/Dec Rates Failed for Engine : %d", MyEngineNo));
			return false;	-- false=setup failed, true=setup succeeded
			else
			print(string.format("Saved Acc/Dec Rates for Engine : %d : %d - %d", MyEngineNo, SaveAcc, SaveDec));
			end
		-- Set Acc/Dec rate to 25 Smph/sec
		Rate(25, 25, 0, MyEngineNo, MyTIUNo);
		print("Acc/Dec rates set to 25 Smph/sec");
		else
		print(string.format("Startup Failed for Engine : %d", MyEngineNo));
		return false;	-- false=setup failed, true=setup succeeded
		end
		
	TagDetection = false;
 
	print(string.format("setup(): Debug level %d", Debug())); 
	print("setup(): Engine Number " .. MyEngineNo .. " TIU Number " .. MyTIUNo);
	--
	print("Loop " .. LoopLength .. " Smiles");
	print("Detector " .. Detr .. " Reader " .. Rdr);
	iSpeed = 20;
	print(string.format("SetSpeed %d Smph for Engine : %d", iSpeed, MyEngineNo));
	SetSpeed(iSpeed, 0, MyEngineNo, MyTIUNo);
	Sleep(20);
		
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function tag(Detector, Reader, EngineNo, TagLocation, CarModel, Railroad, CarNumber, TagPacket)
	-- this function is called each time a tag is detected
	-- Line below shows a different way to quote a string using brackets
	Beep();	-- sound a tone
	if (EngineNo > 0) then	-- its an engine
		print(string.format([[tag(RunTime(%.2f)) : Detector %d Reader %d EngineNo %d TagLocation %d EngineModel %d]], 
			RunTime(), Detector, Reader, EngineNo, TagLocation, CarModel));
		if (Debug() > 2) then 
			s = string.format([[Engine Number %s]], string.gsub(string.char(
				tonumber(TagPacket:sub(29, 30), 16), 
				tonumber(TagPacket:sub(31, 32), 16), 
				tonumber(TagPacket:sub(33, 34), 16), 
				tonumber(TagPacket:sub(35, 36), 16), 
				tonumber(TagPacket:sub(37, 38), 16), 
				tonumber(TagPacket:sub(39, 40), 16)
				), "^%s*", ""));
			print(s);
			end;		
		if (isTagFront(TagLocation) and isDetector(Detector, Detr) and isReader(Reader, Rdr)) then
			-- read up the trip odometer
			CurrentDTO = DTO(MyEngineNo, MyTIUNo);
			CurrentDOD = DOD(MyEngineNo, MyTIUNo);
			CurrentRunTime = RunTime();
			if (TagDetection) then
				print(string.format("Run Time = %.2f seconds (%.2f-%.2f)", CurrentRunTime - LastRunTime, CurrentRunTime, LastRunTime)); 
				print(string.format("Trip Odometer = %.6f Smiles (%.6f-%.6f)", CurrentDTO - LastDTO, CurrentDTO, LastDTO)); 
				print(string.format("Odometer = %.6f Smiles (%.6f-%.6f)", CurrentDOD - LastDOD, CurrentDOD, LastDOD));
				--[[
				LONGLOOP = 174' 8.8" = 174.7333 feet = 1.5884 Smiles
				SHORTLOOP = 120' 7.1" = 120.5916 feet = 1.09628 Smiles
				--]]
				DODAcc =  ((CurrentDOD - LastDOD) - (LoopLength)) / ((LoopLength));
				DTOAcc =  ((CurrentDTO - LastDTO) - (LoopLength)) / ((LoopLength));
				print(string.format("DTO Distance accurate to within %.2f%%", 100.0 * DTOAcc));
				print(string.format("DOD Distance accurate to within %.2f%%", 100.0 * DODAcc));
				print(string.format("Difference %.4f%%", 100.0 * (DTOAcc - DODAcc)));
				else
				print(string.format(" Run Time = %.2f seconds", CurrentRunTime)); 
				print(string.format(" Trip Odometer = %.6f Smiles", CurrentDTO)); 
				print(string.format(" Odometer = %.6f Smiles", CurrentDOD));								
				end;
			TagDetection = true;
			LastDTO = CurrentDTO;
			LastDOD = CurrentDOD;
			LastRunTime = CurrentRunTime;
			end;

		else	-- it's a car
		print(string.format([[tag(%.2f) : Detector %d Reader %d CarModel %d]], 
			RunTime(), Detector, Reader, CarModel));
		if (Debug() > 2) then 
			s = string.format([[Car Number %s]], string.gsub(string.char(
				tonumber(TagPacket:sub(29, 30), 16), 
				tonumber(TagPacket:sub(31, 32), 16), 
				tonumber(TagPacket:sub(33, 34), 16), 
				tonumber(TagPacket:sub(35, 36), 16), 
				tonumber(TagPacket:sub(37, 38), 16), 
				tonumber(TagPacket:sub(39, 40), 16)
				), "^%s*", ""));
			print(s);
			end;		
		-- ignore non engine tags
		return true;	-- true=continue to process tags, false=stop processing tags
		end
	-- The complete packet received is in TagPacket 
	-- (some of the most used fields have already been extracted into the first parameters to tag()
	--   First 8 digits are the 4 byte Tag UID in hexidecimal
	--   Next 32 digits are the 16 bytes of Block 4 Information read from the Tag in hexidecimal
	--   Final 4 digits are the checksum and End of Packet character
	if (Debug() >= 4) then print(string.format("Packet  %s", string.sub(TagPacket, 1, 40))); end
	-- index the table Tags[] by tag ID, value is the tag packet itself
	Tags[string.sub(TagPacket,1,8)] = string.sub(TagPacket, 9, 40);	-- remove checksum and EOP
	TagCount = TagCount + 1;	-- count the number of tags
	return true;	-- true=continue to process tags, false=stop processing tags
	end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine)
	-- this function is called once when the user presses the [STOP] button
	print("All Newly Detected Tags:");
	for k,v in  pairs(Tags) do		-- dump out the Tags table
		print(k .. "  " .. v);
		end
	
	-- Restore Acc/Dec rates if saved
	if (SaveAcc > 0 and SaveDec > 0) then
		Rate(SaveAcc, SaveDec, 0, MyEngineNo, MyTIUNo);
		Sleep(5);
		-- read up the restored Acc/Dec rates
		SaveAcc, SaveDec = GetRate(MyEngineNo, MyTIUNo);
		if (SaveAcc == nil or SaveDec == nil) then
			print(string.format("Get Acc/Dec Rates Failed for Engine : %d", MyEngineNo));
			return false;	-- false=setup failed, true=setup succeeded
			else
			print(string.format("Saved Acc/Dec Rates for Engine : %d : %d - %d", MyEngineNo, SaveAcc, SaveDec));
			end
		SetSpeed(0, 0, MyEngineNo, MyTIUNo);
		print("begin cleanup() Sleep(5) call");
		Sleep(5)			
		print("end cleanup() Sleep(5) call");
		print("Shutdown Engine " .. MyEngineNo .. " - Please Wait");
		EngineShutdown(MyEngineNo, MyTIUNo);
		end;
	
	print(string.format("Script Finished at %.2f", RunTime()));
	--
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function tick()
	-- this is called once per second while waiting for RFID tags to arrive
	-- Do not call Sleep() from this function
	if (Debug() > 4) then print(string.format("RunTime = %.2f seconds", RunTime())); end;
	return true;
	end
--[[---------------------------------------------------------------------------------------]]

function ShortLoop()		
	LoopLength = SHORTLOOP;
	print("Short Loop " .. LoopLength .. " Smiles");
	return true;
end
Function01Name, Function01Label = ShortLoop, "1.09628 Smiles";
--[[---------------------------------------------------------------------------------------]]
function LongLoop()
	LoopLength = LONGLOOP;	
	print("Long Loop " .. LoopLength .. " Smiles");
	return true;
end
Function02Name, Function02Label = LongLoop, "1.58840 Smiles";
--[[---------------------------------------------------------------------------------------]]
function Det1()		
	Detr = 1;
	Rdr = 0;
	print("Detector " .. Detr .. " Reader " .. Rdr);
	return true;
end
Function06Name, Function06Label = Det1, "Detector 1 Rdr 0";
--[[---------------------------------------------------------------------------------------]]
function Det2()		
	Detr = 2;
	Rdr = 1;	
	print("Detector " .. Detr .. " Reader " .. Rdr);
	return true;
end
Function07Name, Function07Label = Det2, "Detector 2 Rdr 1";
--[[---------------------------------------------------------------------------------------]]
--[[------------------------------------------

Success compiling C:\trains\MTH\PC Control\Scripts\DTO Loop Test.lua
Function Function01Name OK
Function Function02Name OK
Function Function06Name OK
Function Function07Name OK
setup()
setup(): Debug level 4
setup(): Engine Number 14 TIU Number 1
TIU : 1
Loop 1.5884 Smiles
Detector 1 Reader 0
FetchDTO=0.248556 raw=59158 DTOFACTOR=11.000000 ScaleFactor=21637.00 
FetchDOD=62.006109 raw=28824 DODFACTOR=46.545455 ScaleFactor=21637.00 
 Run Time = 1.30 seconds
 Trip Odometer = 0.248556 Smiles
 Odometer = 62.006109 Smiles
Waiting for RFID Tag Detection

FetchDTO=1.931221 raw=459644 DTOFACTOR=11.000000 ScaleFactor=21637.00 
FetchDOD=63.690496 raw=29607 DODFACTOR=46.545455 ScaleFactor=21637.00 
Run Time = 287.50 seconds (321.00-33.50)
Trip Odometer = 1.597634 Smiles (1.931221-0.333587)
Odometer = 1.596188 Smiles (63.690496-62.094308)
DTO Distance accurate to within 0.58%
DOD Distance accurate to within 0.49%

FetchDTO=3.528480 raw=839803 DTOFACTOR=11.000000 ScaleFactor=21637.00 
FetchDOD=65.288836 raw=30350 DODFACTOR=46.545455 ScaleFactor=21637.00 
Run Time = 287.50 seconds (608.50-321.00)
Trip Odometer = 1.597260 Smiles (3.528480-1.931221)
Odometer = 1.598340 Smiles (65.288836-63.690496)
DTO Distance accurate to within 0.56%
DOD Distance accurate to within 0.63%

FetchDTO=5.124236 raw=1219604 DTOFACTOR=11.000000 ScaleFactor=21637.00 
FetchDOD=66.885024 raw=31092 DODFACTOR=46.545455 ScaleFactor=21637.00 
Run Time = 287.20 seconds (895.70-608.50)
Trip Odometer = 1.595756 Smiles (5.124236-3.528480)
Odometer = 1.596188 Smiles (66.885024-65.288836)
DTO Distance accurate to within 0.46%
DOD Distance accurate to within 0.49%

FetchDTO=6.718286 raw=1598999 DTOFACTOR=11.000000 ScaleFactor=21637.00 
FetchDOD=68.479062 raw=31833 DODFACTOR=46.545455 ScaleFactor=21637.00 
Run Time = 286.90 seconds (1182.60-895.70)
Trip Odometer = 1.594050 Smiles (6.718286-5.124236)
Odometer = 1.594037 Smiles (68.479062-66.885024)
DTO Distance accurate to within 0.36%
DOD Distance accurate to within 0.35%

FetchDTO=8.311844 raw=1978277 DTOFACTOR=11.000000 ScaleFactor=21637.00 
FetchDOD=70.070947 raw=32573 DODFACTOR=46.545455 ScaleFactor=21637.00 
Run Time = 286.80 seconds (1469.40-1182.60)
Trip Odometer = 1.593558 Smiles (8.311844-6.718286)
Odometer = 1.591886 Smiles (70.070947-68.479062)
DTO Distance accurate to within 0.32%
DOD Distance accurate to within 0.22%

Run Time = 255.90 seconds (1725.30-1469.40)
Trip Odometer = 4.779015 Smiles (13.090859-8.311844)
Odometer = 4.779960 Smiles (74.850908-70.070947)
DTO Distance accurate to within 200.87%
DOD Distance accurate to within 200.93%

Run Time = 286.70 seconds (2012.00-1725.30)
Trip Odometer = 1.592882 Smiles (14.683740-13.090859)
Odometer = 1.594037 Smiles (76.444945-74.850908)
DTO Distance accurate to within 0.28%
DOD Distance accurate to within 0.35%

Run Time = 287.30 seconds (2299.30-2012.00)
Trip Odometer = 1.596256 Smiles (16.279996-14.683740)
Odometer = 1.596188 Smiles (78.041133-76.444945)
DTO Distance accurate to within 0.49%
DOD Distance accurate to within 0.49%

Run Time = 287.30 seconds (2586.60-2299.30)
Trip Odometer = 1.596398 Smiles (17.876394-16.279996)
Odometer = 1.596188 Smiles (79.637322-78.041133)
DTO Distance accurate to within 0.50%
DOD Distance accurate to within 0.49%

Run Time = 287.40 seconds (2874.00-2586.60)
Trip Odometer = 1.596756 Smiles (19.473150-17.876394)
Odometer = 1.596188 Smiles (81.233510-79.637322)
DTO Distance accurate to within 0.53%
DOD Distance accurate to within 0.49%

Run Time = 287.20 seconds (3161.20-2874.00)
Trip Odometer = 1.595407 Smiles (21.068557-19.473150)
Odometer = 1.596188 Smiles (82.829698-81.233510)
DTO Distance accurate to within 0.44%
DOD Distance accurate to within 0.49%

Run Time = 287.30 seconds (3448.50-3161.20)
Trip Odometer = 1.596583 Smiles (22.665140-21.068557)
Odometer = 1.596188 Smiles (84.425887-82.829698)
DTO Distance accurate to within 0.52%
DOD Distance accurate to within 0.49%

Run Time = 287.40 seconds (3735.90-3448.50)
Trip Odometer = 1.596600 Smiles (24.261740-22.665140)
Odometer = 1.596188 Smiles (86.022075-84.425887)
DTO Distance accurate to within 0.52%
DOD Distance accurate to within 0.49%


Success compiling C:\trains\MTH\PC Control\Scripts\DTO Loop Test.lua
setup()
setup(): Debug level 0
setup(): Engine Number 12 TIU Number 1
TIU : 1
Loop 1.5884 Smiles
Detector 1 Reader 0
 Run Time = 1.30 seconds
 Trip Odometer = 0.030045 Smiles
 Odometer = 202.601907 Smiles
Waiting for RFID Tag Detection
tag() : Detector 1 Reader 0 EngineNo 12 TagLocation 1 EngineModel 1
 Run Time = 43.90 seconds
 Trip Odometer = 0.278820 Smiles
 Odometer = 202.849294 Smiles

Run Time = 272.30 seconds (316.20-43.90)
Trip Odometer = 1.588785 Smiles (1.867605-0.278820)
Odometer = 1.589735 Smiles (204.439029-202.849294)
DTO Distance accurate to within 0.02%
DOD Distance accurate to within 0.08%

Run Time = 272.80 seconds (589.00-316.20)
Trip Odometer = 1.590878 Smiles (3.458482-1.867605)
Odometer = 1.591886 Smiles (206.030915-204.439029)
DTO Distance accurate to within 0.16%
DOD Distance accurate to within 0.22%

Run Time = 272.60 seconds (861.60-589.00)
Trip Odometer = 1.590462 Smiles (5.048944-3.458482)
Odometer = 1.589735 Smiles (207.620650-206.030915)
DTO Distance accurate to within 0.13%
DOD Distance accurate to within 0.08%

Run Time = 272.60 seconds (1134.20-861.60)
Trip Odometer = 1.590159 Smiles (6.639103-5.048944)
Odometer = 1.589735 Smiles (209.210385-207.620650)
DTO Distance accurate to within 0.11%
DOD Distance accurate to within 0.08%
1
Run Time = 272.50 seconds (1406.70-1134.20)
Trip Odometer = 1.589764 Smiles (8.228867-6.639103)
Odometer = 1.589735 Smiles (210.800119-209.210385)
DTO Distance accurate to within 0.09%
DOD Distance accurate to within 0.08%

Run Time = 272.30 seconds (1679.00-1406.70)
Trip Odometer = 1.588726 Smiles (9.817594-8.228867)
Odometer = 1.589735 Smiles (212.389854-210.800119)
DTO Distance accurate to within 0.02%
DOD Distance accurate to within 0.08%

Success compiling C:\trains\MTH\PC Control\Scripts\DTO Loop Test.lua
setup()
setup(): Debug level 0
setup(): Engine Number 8 TIU Number 1
TIU : 1
Loop 1.5884 Smiles
Detector 1 Reader 0
 Run Time = 1.30 seconds
 Trip Odometer = 0.292189 Smiles
 Odometer = 794.706038 Smiles
Waiting for RFID Tag Detection
tag() : Detector 1 Reader 0 EngineNo 8 TagLocation 1 EngineModel 8
 Run Time = 21.10 seconds
 Trip Odometer = 0.418605 Smiles
 Odometer = 794.830807 Smiles

Run Time = 247.40 seconds (268.50-21.10)
Trip Odometer = 1.580802 Smiles (1.999408-0.418605)
Odometer = 1.581130 Smiles (796.411937-794.830807)
DTO Distance accurate to within -0.48%
DOD Distance accurate to within -0.46%

Run Time = 234.30 seconds (502.80-268.50)
Trip Odometer = 1.579155 Smiles (3.578563-1.999408)
Odometer = 1.581130 Smiles (797.993067-796.411937)
DTO Distance accurate to within -0.58%
DOD Distance accurate to within -0.46%

Run Time = 216.40 seconds (719.20-502.80)
Trip Odometer = 1.563458 Smiles (5.142021-3.578563)
Odometer = 1.561769 Smiles (799.554837-797.993067)
DTO Distance accurate to within -1.57%
DOD Distance accurate to within -1.68%

Run Time = 213.50 seconds (932.70-719.20)
Trip Odometer = 1.541593 Smiles (6.683614-5.142021)
Odometer = 1.542408 Smiles (801.097245-799.554837)
DTO Distance accurate to within -2.95%
DOD Distance accurate to within -2.90%


Success compiling C:\trains\MTH\PC Control\Scripts\DTO Loop Test.lua
setup()
setup(): Debug level 0
setup(): Engine Number 2 TIU Number 1
TIU : 1
Loop 1.5884 Smiles
Detector 1 Reader 0
 Run Time = 1.20 seconds
 Trip Odometer = 0.419761 Smiles
 Odometer = 532.436157 Smiles
Waiting for RFID Tag Detection

 Run Time = 218.20 seconds
 Trip Odometer = 1.682487 Smiles
 Odometer = 533.703561 Smiles

Run Time = 271.50 seconds (489.70-218.20)
Trip Odometer = 1.583620 Smiles (3.266107-1.682487)
Odometer = 1.582795 Smiles (535.286355-533.703561)
DTO Distance accurate to within -0.30%
DOD Distance accurate to within -0.35%

Run Time = 356.20 seconds (845.90-489.70)
Trip Odometer = -1.759742 Smiles (1.506365-3.266107)
Odometer = -1.759959 Smiles (533.526397-535.286355)
DTO Distance accurate to within -210.79%
DOD Distance accurate to within -210.80%

Run Time = 271.50 seconds (1117.40-845.90)
Trip Odometer = 1.583300 Smiles (3.089666-1.506365)
Odometer = 1.582795 Smiles (535.109191-533.526397)
DTO Distance accurate to within -0.32%
DOD Distance accurate to within -0.35%

Run Time = 271.40 seconds (1388.80-1117.40)
Trip Odometer = 1.582985 Smiles (4.672650-3.089666)
Odometer = 1.582795 Smiles (536.691986-535.109191)
DTO Distance accurate to within -0.34%
DOD Distance accurate to within -0.35%


Success compiling C:\trains\MTH\PC Control\Scripts\DTO Loop Test.lua
setup()
setup(): Debug level 0
setup(): Engine Number 2 TIU Number 1
TIU : 1
Loop 1.5884 Smiles
Detector 1 Reader 0
 Run Time = 1.30 seconds
 Trip Odometer = 0.000004 Smiles
 Odometer = 532.015636 Smiles
Waiting for RFID Tag Detection
tag() : Detector 1 Reader 0 EngineNo 2 TagLocation 1 EngineModel 10
 Run Time = 34.90 seconds
 Trip Odometer = 0.098503 Smiles
 Odometer = 532.114925 Smiles

Run Time = 272.60 seconds (307.50-34.90)
Trip Odometer = 1.586871 Smiles (1.685373-0.098503)
Odometer = 1.590582 Smiles (533.705507-532.114925)
DTO Distance accurate to within -0.10%
DOD Distance accurate to within 0.14%

Run Time = 271.50 seconds (579.00-307.50)
Trip Odometer = 1.583456 Smiles (3.268830-1.685373)
Odometer = 1.582795 Smiles (535.288302-533.705507)
DTO Distance accurate to within -0.31%
DOD Distance accurate to within -0.35%

Run Time = 356.20 seconds (935.20-579.00)
Trip Odometer = -1.759742 Smiles (1.509088-3.268830)
Odometer = -1.758012 Smiles (533.530290-535.288302)
DTO Distance accurate to within -210.79%
DOD Distance accurate to within -210.68%

Run Time = 271.50 seconds (1206.70-935.20)
Trip Odometer = 1.583297 Smiles (3.092384-1.509088)
Odometer = 1.582795 Smiles (535.113085-533.530290)
DTO Distance accurate to within -0.32%
DOD Distance accurate to within -0.35%

Run Time = 271.40 seconds (1478.10-1206.70)
Trip Odometer = 1.583148 Smiles (4.675533-3.092384)
Odometer = 1.582795 Smiles (536.695880-535.113085)
DTO Distance accurate to within -0.33%
DOD Distance accurate to within -0.35%


Success compiling C:\trains\MTH\PC Control\Scripts\DTO Loop Test.lua
setup()
setup(): Debug level 0
setup(): Engine Number 15 TIU Number 1
TIU : 1
Loop 1.5884 Smiles
Detector 1 Reader 0
 Run Time = 1.20 seconds
 Trip Odometer = 0.000000 Smiles
 Odometer = 1201.243594 Smiles
Waiting for RFID Tag Detection
tag() : Detector 1 Reader 0 EngineNo 15 TagLocation 1 EngineModel 1
 Run Time = 29.50 seconds
 Trip Odometer = 0.090216 Smiles
 Odometer = 1201.333944 Smiles

Run Time = 299.10 seconds (328.60-29.50)
Trip Odometer = 1.578727 Smiles (1.668943-0.090216)
Odometer = 1.578979 Smiles (1202.912923-1201.333944)
DTO Distance accurate to within -0.61%
DOD Distance accurate to within -0.59%
Difference -0.0159%

Run Time = 299.60 seconds (628.20-328.60)
Trip Odometer = 1.581290 Smiles (3.250232-1.668943)
Odometer = 1.583281 Smiles (1204.496204-1202.912923)
DTO Distance accurate to within -0.45%
DOD Distance accurate to within -0.32%
Difference -0.1254%

Run Time = 299.40 seconds (927.60-628.20)
Trip Odometer = 1.580458 Smiles (4.830690-3.250232)
Odometer = 1.578979 Smiles (1206.075183-1204.496204)
DTO Distance accurate to within -0.50%
DOD Distance accurate to within -0.59%
Difference 0.0931%

Run Time = 299.80 seconds (1227.40-927.60)
Trip Odometer = 1.582214 Smiles (6.412904-4.830690)
Odometer = 1.583281 Smiles (1207.658464-1206.075183)
DTO Distance accurate to within -0.39%
DOD Distance accurate to within -0.32%
Difference -0.0672%

Run Time = 299.60 seconds (1527.00-1227.40)
Trip Odometer = 1.581105 Smiles (7.994009-6.412904)
Odometer = 1.581130 Smiles (1209.239594-1207.658464)
DTO Distance accurate to within -0.46%
DOD Distance accurate to within -0.46%
Difference -0.0016%


Success compiling C:\trains\MTH\PC Control\Scripts\DTO Loop Test.lua
setup()
TIU : 1
Engine Number = 12
Startup Engine : 12
Initalizing Engine 12
 Run Time = 31.20 seconds
 Trip Odometer = 0.000000 Smiles
 Odometer = 230.257698 Smiles
Saved Acc/Dec Rates for Engine : 12 : 1 - 1
Acc/Dec rates set to 25 Smph/sec
setup(): Debug level 0
setup(): Engine Number 12 TIU Number 1
Loop 1.5884 Smiles
Detector 1 Reader 0
SetSpeed 20 Smph for Engine : 12
Waiting for RFID Tag Detection
tag() : Detector 1 Reader 0 EngineNo 12 TagLocation 1 EngineModel 1
 Run Time = 76.20 seconds
 Trip Odometer = 0.208696 Smiles
 Odometer = 230.468516 Smiles

Run Time = 286.60 seconds (362.80-76.20)
Trip Odometer = 1.592537 Smiles (1.801233-0.208696)
Odometer = 1.591886 Smiles (232.060402-230.468516)
DTO Distance accurate to within 0.26%
DOD Distance accurate to within 0.22%
Difference 0.0410%

Run Time = 286.40 seconds (649.20-362.80)
Trip Odometer = 1.591226 Smiles (3.392459-1.801233)
Odometer = 1.591886 Smiles (233.652288-232.060402)
DTO Distance accurate to within 0.18%
DOD Distance accurate to within 0.22%
Difference -0.0415%

Run Time = 286.50 seconds (935.70-649.20)
Trip Odometer = 1.590235 Smiles (4.982694-3.392459)
Odometer = -77.159142 Smiles (156.493145-233.652288)
DTO Distance accurate to within 0.12%
DOD Distance accurate to within -4957.66%
Difference 4957.7800%

Run Time = 285.80 seconds (1221.50-935.70)
Trip Odometer = 1.589365 Smiles (6.572059-4.982694)
Odometer = 80.338612 Smiles (236.831757-156.493145)
DTO Distance accurate to within 0.06%
DOD Distance accurate to within 4957.83%
Difference -4957.7718%

Run Time = 286.00 seconds (1507.50-1221.50)
Trip Odometer = 1.589201 Smiles (8.161260-6.572059)
Odometer = 1.589735 Smiles (238.421492-236.831757)
DTO Distance accurate to within 0.05%
DOD Distance accurate to within 0.08%
Difference -0.0336%


---------------------------------------------]]