Sign in to follow this  
FSC190

Using LUA Script made for FSUIPC

Recommended Posts

Hi Andrew,

to set VSpeed Data in my MJ Dash8 Pro I use theLUA script V-Speed Calculator LUA 1.2 from ttp://www.twitch.tv/mrroper_75

I have to assign a button or key via FSUIPC calling the lua function "lua Q400VSpeeds" out of the FS Control List.

What is the easiest way to assign this via LINDA?

I tried copy and paste into the user.lua of the MJ Q400 but no luck none of the functions start the script in the right way.

 

Here is the content of the lua script


Vspeeds = {}
Vfri = {}
vlanding = {}
minWeight = 39500
maxWeight = 64000
minAlt = 0
maxAlt = 10000
weightDiv = 4500
altDiv = 2000
indexMultiplier = 12
flapsMultiplier = 84
landingFlapsMultiplier = 7
tempOffset = 6
ReadSuccess = 9991999
ReadFail = 9992999

bug1 = 48086
bug2 = 49010
bug3 = 48114
bug4 = 47393
bug5 = 47420

function readSpeeds(filename)
	local file = io.open(filename)

	for n=1, 252 do
		local v1, vr, v2
		v1 = file:read("*n")
		vr = v1
		file:read(1)
		v2 = file:read("*n")
		file:read(1)
		Vspeeds[#Vspeeds+1] = {["v1"] = v1, ["vr"] = vr, ["v2"] = v2}
	end
	for y=1, 7 do
		local vf5, vf10, vf15, vclmb

		vf5 = file:read("*n")
		file:read(1)
		vf10 = file:read("*n")
		file:read(1)
		vf15 = file:read("*n")
		file:read(1)
		vclmb = file:read("*n")
		file:read(1)
		Vfri[#Vfri+1] = {["vf5"] = vf5,["vf10"] = vf10,["vf15"] = vf15, ["vclmb"] = vclmb}
		
	end
		for y=1, 21 do
		local vapp, vreff

		vapp = file:read("*n")
		file:read(1)
		vref = file:read("*n")
		file:read(1)
		vlanding[#vlanding+1] = {["vapp"] = vapp,["vref"] = vref}
		
	end
		file:close()
end

function file_exists(name)
   local f=io.open(name,"r")
   if f~=nil then io.close(f) return true else return false end
end



function GetQ400Value(offset)
	ipc.writeLvar("MJC_VAR_READ_CODE", offset)
	local retVal = ipc.readLvar("MJC_VAR_READ_CODE")

	while retVal ~= ReadSuccess and retVal ~= ReadFail do
		retVal = ipc.readLvar("MJC_VAR_READ_CODE")
		ipc.sleep(100)
	end
	
	return ipc.readLvar("MJC_VAR_READ_VALUE")
end

function SetQ400Value(offset, value)
    ipc.writeLvar("MJC_VAR_WRITE_VALUE", value)
    ipc.writeLvar("MJC_VAR_WRITE_CODE", offset)
end

function GetWeightOffset()
	local weight = math.floor(GetWeight())

	if (weight < minWeight) then return 1 end
	if (weight > maxWeight) then return 7 * indexMultiplier end
	
	--Not zero based so add one
	return (((math.floor((weight - minWeight) / weightDiv)) * indexMultiplier)+1) + indexMultiplier
end

function GetWeightOffsetLanding()

	local weight = math.floor(GetWeight())

	if (weight < minWeight) then return 1 end
	if (weight > maxWeight) then return 7 end
	
	--Not zero based so add one
	return math.floor((weight - minWeight) / weightDiv) + 2
end

function GetVfriOffset()
	local weight = math.floor(GetWeight())

	if (weight < minWeight) then return 1  end
	if (weight > maxWeight) then return 7  end
	
	return (math.floor((weight - minWeight) / weightDiv))+2
end

function GetAltOffset()
	local alt = math.floor(GetAltitude())
	
	local offset
	
	if (alt > maxAlt) then 
		offset = 6 
	else
		offset = math.floor(alt / altDiv)
	end
	
	if (offset < 1) then 
		offset = 0
	end
	
	if (GetTemp() > 20) then
		offset = offset + tempOffset
	end
	
	return offset

end

function SetBugs(v1, vref, vfri, vclmb)
	SetQ400Value(bug1, v1)
	ipc.sleep(50)
	SetQ400Value(bug2, v1)
	ipc.sleep(50)
	SetQ400Value(bug3, vref)
	ipc.sleep(50)
	SetQ400Value(bug4, vfri)
	ipc.sleep(50)
	SetQ400Value(bug5, vclmb)
	ipc.sleep(50)

end

function SetLandingBugs(vapp, vref)
	SetQ400Value(bug4, vapp)
	ipc.sleep(50)
	SetQ400Value(bug5, vref)
	ipc.sleep(50)
end

function GetIcingSwitched()
	return ipc.readLvar("OHD_AICE_ENG_INTAKE_REF_SW")
end

function GetTemp()
	return math.floor(ipc.readSW(0x0e8c) / 255)
end

function GetAltitude()
	return ipc.readFLT(0x0664)
end

function GetWeight()
	return ipc.readDBL(0x30C0)
end

function GetOnGround()
	return ipc.readSW(0x0366)
end


function GetEngine1CombustionFlag()
	return ipc.readSW(0x0894)
end

function GetEngine2CombustionFlag()
	return ipc.readSW(0x092C)
end

function GetIcingIncrease()
	if (GetIcingSwitched() == 0) then return 0 else return 20 end
end

--returns 1 if on ground and engines not running, 2 if in air and 3 if on ground with engines running
function GetState()
	local onGround = GetOnGround()
	local enginesRunning = 0
	
	local eng1 = GetQ400Value(56911)
	local eng2 = GetQ400Value(56888)
	
	if (eng1 > 0 or eng2 > 0) then enginesRunning = 1 end
	
	if (onGround == 1) then 
		if (enginesRunning == 1) then return 3  else return 1 end
	else
		return 2
	end
end

function DisplayTakeOffData()
	
	ipc.display ("TOM	: " .. math.floor(GetWeight()) .. "\n" .. 
				 "Temp	: " .. math.floor(GetTemp()) .. "C \n" ..
				 "Alt	: " .. math.floor(GetAltitude()) .. "ft \n" ..
				 "Ice	: " .. GetIcingSwitched() .. "\n \n" ..
				 "1> Flps 5		: " .. 	Vspeeds[GetWeightOffset()+GetAltOffset()].v1 .. "/" .. 
									Vspeeds[GetWeightOffset()+GetAltOffset()].vr .. "/" .. Vspeeds[GetWeightOffset()+GetAltOffset()].v2 + GetIcingIncrease() .. "/" .. Vfri[GetVfriOffset()].vf5 + GetIcingIncrease() .. "/" .. Vfri[GetVfriOffset()].vclmb + GetIcingIncrease() ..
				 "\n2> Flps 10	: " .. 	Vspeeds[GetWeightOffset()+GetAltOffset() + (flapsMultiplier *1)].v1 .. "/" .. 
									Vspeeds[GetWeightOffset()+GetAltOffset() + (flapsMultiplier *1)].vr .. "/" .. Vspeeds[GetWeightOffset()+GetAltOffset() + (flapsMultiplier *1)].v2 + GetIcingIncrease() .. "/" .. Vfri[GetVfriOffset()].vf10 + GetIcingIncrease() .. "/" .. Vfri[GetVfriOffset()].vclmb + GetIcingIncrease() ..
				 "\n3> Flps 15	: " .. 	Vspeeds[GetWeightOffset()+GetAltOffset() + (flapsMultiplier *2)].v1 .. "/" .. Vspeeds[GetWeightOffset()+GetAltOffset() + (flapsMultiplier *2)].v1 .. "/" .. Vspeeds[GetWeightOffset()+GetAltOffset() + (flapsMultiplier *2)].v2 + GetIcingIncrease() .. "/" .. Vfri[GetVfriOffset()].vf15 + GetIcingIncrease() .. "/" .. Vfri[GetVfriOffset()].vclmb + GetIcingIncrease()

				 )
end

function DisplayLandingData()
	
	ipc.display ("LM	: " .. math.floor(GetWeight()) .. "\n" .. 
				 "Ice	: " .. GetIcingSwitched() .. "\n \n" ..
				 "1> Flps 10 :" .. vlanding[GetWeightOffsetLanding()].vapp + GetIcingIncrease() .. "/" .. vlanding[GetWeightOffsetLanding()].vref + GetIcingIncrease() .. "\n" ..
				 "2> Flps 15 :" .. vlanding[GetWeightOffsetLanding() + (landingFlapsMultiplier)].vapp + GetIcingIncrease() .. "/" .. vlanding[GetWeightOffsetLanding() + (landingFlapsMultiplier)].vref + GetIcingIncrease() .. "\n" ..
				 "3> Flps 35 :" .. vlanding[GetWeightOffsetLanding() + (landingFlapsMultiplier) * 2].vapp + GetIcingIncrease() .. "/" .. vlanding[GetWeightOffsetLanding() + (landingFlapsMultiplier * 2)].vref + GetIcingIncrease() .. "\n"
				 )
end

function SetSpeeds(config)
	if (config == "1") then
		--Get the speeds we need for flaps 5
		vr = Vspeeds[GetWeightOffset()+GetAltOffset()].v1
		v2 = Vspeeds[GetWeightOffset()+GetAltOffset()].v2+ GetIcingIncrease()
		vfri = Vfri[GetVfriOffset()].vf5+ GetIcingIncrease()
		vclmb = Vfri[GetVfriOffset()].vclmb+ GetIcingIncrease()
		
		SetBugs(vr, v2, vfri, vclmb)
	elseif (config == "2") then
		--Get the speeds we need for flaps 10
		vr = Vspeeds[GetWeightOffset()+GetAltOffset() + (flapsMultiplier *1)].v1
		v2 = Vspeeds[GetWeightOffset()+GetAltOffset() + (flapsMultiplier *1)].v2+ GetIcingIncrease()
		vfri = Vfri[GetVfriOffset()].vf10+ GetIcingIncrease()
		vclmb = Vfri[GetVfriOffset()].vclmb+ GetIcingIncrease()
		
		SetBugs(vr, v2, vfri, vclmb)
	elseif (config == "3") then
			--Get the speeds we need for flaps 10
		vr = Vspeeds[GetWeightOffset()+GetAltOffset() + (flapsMultiplier *2)].v1
		v2 = Vspeeds[GetWeightOffset()+GetAltOffset() + (flapsMultiplier *2)].v2+ GetIcingIncrease()
		vfri = Vfri[GetVfriOffset()].vf15+ GetIcingIncrease()
		vclmb = Vfri[GetVfriOffset()].vclmb+ GetIcingIncrease()
		
		SetBugs(vr, v2, vfri, vclmb)
	end
end

function SetLandingSpeeds(config)
	if (config == "1") then
			vapp = vlanding[GetWeightOffsetLanding()].vapp + GetIcingIncrease()
			vref = vlanding[GetWeightOffsetLanding()].vref + GetIcingIncrease()
			SetLandingBugs(vapp, vref)
	elseif (config == "2") then
			vapp = vlanding[GetWeightOffsetLanding()+ (landingFlapsMultiplier)].vapp + GetIcingIncrease()
			vref = vlanding[GetWeightOffsetLanding()+ (landingFlapsMultiplier)].vref + GetIcingIncrease()
			SetLandingBugs(vapp, vref)

	elseif (config == "3") then
			vapp = vlanding[GetWeightOffsetLanding()+ (landingFlapsMultiplier * 2)].vapp + GetIcingIncrease()
			vref = vlanding[GetWeightOffsetLanding()+ (landingFlapsMultiplier * 2)].vref + GetIcingIncrease()
			SetLandingBugs(vapp, vref)
	
	end
end

function PerformAfterLandingFlow()
	ipc.writeLvar("Q400_CONTROL_LOCK", 100)
	ipc.sleep(100)
	ipc.writeLvar("OHD_EXT_LT_ACOL_SW", 1)
	ipc.sleep(100)
	ipc.control(65597)
	ipc.sleep(100)
	ipc.control(65595)
	ipc.sleep(100)
	ipc.writeLvar("PED_WR_MODE", 1)
	ipc.sleep(100)
	SetQ400Value(63194, 0)
	ipc.sleep(100)
	SetQ400Value(63210, 0)
	ipc.sleep(100)
	SetQ400Value(81038, 0)
	ipc.sleep(100)
	SetQ400Value(61497, 0)
	ipc.sleep(100)
	SetQ400Value(180709, 0)
	ipc.sleep(100)
	SetQ400Value(93613, 0)
	ipc.sleep(100)
	SetQ400Value(55267, 0)
	ipc.sleep(100)
	SetQ400Value(55267, 0)
	ipc.sleep(100)
	SetQ400Value(70232, 0)
	ipc.sleep(100)
	SetQ400Value(70269, 0)
	ipc.sleep(100)
	SetQ400Value(54207, 0)
	ipc.sleep(100)
	SetQ400Value(54327, 0)
	ipc.sleep(100)
	SetQ400Value(82531, 0)
	ipc.sleep(100)
	SetQ400Value(58492, 1)
	ipc.sleep(100)
	SetQ400Value(31461, 0)

end


function ShowData()
	if (GetState() == 1) then
			DisplayTakeOffData()
			local n = ipc.ask("Press 1, 2 or 3 for required flaps configuration")
			ipc.display("Setting Bugs")
			SetSpeeds(n)
	end
	if (GetState() == 2) then
			DisplayLandingData()
			local n = ipc.ask("Press 1, 2 or 3 for required flaps configuration")
			ipc.display("Setting Bugs")
			SetLandingSpeeds(n)
	end
	if (GetState() == 3) then
		ipc.ask("Press enter to perform the After Landing Flow")
		PerformAfterLandingFlow()
		ipc.display("After Landing Flow complete")
		ipc.sleep(2000)
	end
end

if (file_exists("speeds.dat")) then
	ipc.log(math.floor(GetWeight()))
	ipc.log(math.floor(GetTemp()))
	ipc.log(math.floor(GetAltitude()))
	ipc.log("weightoffset " .. GetWeightOffset())
	ipc.log("altOffset " .. GetAltOffset())
	ipc.display("Reading VSpeeds")
	readSpeeds("speeds.dat")
	ShowData()
else
	ipc.display("Error reading V speeds")
end

Share this post


Link to post
Share on other sites
Help AVSIM continue to serve you!
Please donate today!

Hi Roland

 

I am not experienced in Macros but I will have a look at your question when I get time. Unfortunately, I am extremely busy with my 'day job' and can not give any attention to it at the moment.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this