Jump to content
The simFlight Network Forums

IAS mode for autopilot - lua script


wawax

Recommended Posts

I created LUA script which adds IAS mode to autopilot. It should work with every aircraft that has autopilot. It works as an overlay to VS mode. It works quite stable, as it uses both speed and acceleration values to determine proper vertical speed. The tests have shown that it works good for climb as well as descent. Additionaly it is able to detect low altitude and limit descent.

 

I use the script for ATR72 in P3D. As you probably know, Flight 1 ATR panel doesn't work with P3D, so I use free panel. This panel lacks IAS mode, which worked very well in F1 ATR. My script seems to be quite good replacement. But it will work with any aircraft.

 

I have no permission to add lua file to this post. If anyone is interested, please let me know, I'll send it using e-mail.

 

 

Link to comment
Share on other sites

Thanks.

-- Autopilot IAS mode
-- Version: 0.1
--
-- This script enables IAS mode for every autopilot. It should work correctly for both climb and descent.
-- IAS mode means, the aircraft keeps IAS unchanged during climb/descent, but adjusts vertical speed.
-- You can use smooth throttle changes to modify pitch of the aircraft.
-- The script uses VS mode but adjusts it every n seconds to keep the IAS stable
-- 
-- In some aircrafts autopilot may be unable to set IAS. It depends on .air config file
-- Use Aired to enable it if needed
-- 
-- To start/restart: 
-- 1. set desired altitude and IAS
-- 2. start usual VS mode on AP
-- 3. run the script
--
-- To stop do only one of following:
-- 1. Set 0x66D0 offset to 0
-- 2. kill the script
-- 3. script will stop itself 1000ft before set altitude
--
-- In all cases, the aircraft will continue to climb/descent until reaching desired altitude!
-- Autopilot usually is able to finish climb/descent smoothly starting approx. 500ft before set altitude.
--

-- Just in case you use colon as decimal separator in your OS.
print(os.setlocale("C"))
assert(os.setlocale'C')

-- Static variables
max_clb_vs = 3500 			-- max climb, ft/min
min_clb_vs = 100 			-- min climb
min_des_vs = -100			-- min descent
max_des_vs_default = -3000  -- max default descent 
max_des_vs = max_des_vs_default -- max descent (will be modified in case of low AGL altitude)
ias_margin = 1 				-- acceptable speed difference, should be > 0, kts
alt_margin = 1000 			-- when to switch IAS mode back to VS, ft
minimum_agl = 500			-- at what AGL we should level off aircraft during descent
frequency = 2 				-- repeat every n seconds, 2 is default, too frequent won't work

-- Initiate variables
last_ias = ipc.readSW(0x02BC) / 128
start = 1					-- run the script
ipc.writeUB(0x66D0, 1) 		-- switch on the led (if used)

while start == 1 do

	-- Check FS variables
	curr_alt = ipc.readDD(0x0570) / 65536 / 65536 * 3.28084
	ap_alt = ipc.readSD(0x07D4) * 3.28084 / 65536
	curr_ias = ipc.readUW(0x02BC) / 128
	ap_ias = ipc.readUD(0x07E2)
	ap_vs = ipc.readSW(0x07F2)
	sleep = frequency * 1000
	curr_agl = ipc.readSW(0x0020) * 3.28084 / 256
	acceleration = curr_ias - last_ias
	
	---------------------------------------------------------------------------
	-- For tests you can enable the lines below
	--
	-- Set ap_ias in case you can't do it in the aircraft
	-- ap_ias = 120
	--
	-- Show variables on screen
	ipc.display("curr_alt="..curr_alt.."\nap_alt="..ap_alt.."\ncurr_ias="..curr_ias.." \nap_ias="..ap_ias.."\nap_vs="..ap_vs.."\nacceleration="..acceleration.."\ncurr_agl="..curr_agl.."\nmax_des_vs="..max_des_vs)
	--
	---------------------------------------------------------------------------

	-- Descent limitation when flying on low altitude 
	if (curr_alt - curr_agl - minimum_agl) * -1 > max_des_vs_default * -1 then
		if curr_alt > ap_alt then -- we are during descent
			max_des_vs = (curr_alt - curr_agl - minimum_agl) * -1 -- level off when less than minimum over AGL
			if max_des_vs > 0 then
				max_des_vs = 0
			end
			if ap_vs < max_des_vs then
				ipc.control(65894) -- increase AP_VS
				ipc.control(65894) -- increase AP_VS
			end
		end
	else
		max_des_vs = max_des_vs_default 
	end
	
	-- CLIMB MODE
	if curr_alt < ap_alt - alt_margin then -- we are below ias_margin
		if curr_ias <= ap_ias - ias_margin then
			if acceleration <= 0 then
				if ap_vs > min_clb_vs then
					ipc.control(65895) -- decrease AP_VS
				end 
			end
			if acceleration < 0.5 then -- decrease AP_VS faster
				if ap_vs > min_clb_vs then
					ipc.control(65895) 
				end 
			end
		elseif curr_ias > ap_ias + ias_margin then -- we are above ias_margin
			if acceleration > 0 then
				if ap_vs < max_clb_vs then
					ipc.control(65894) -- increase AP_VS
				end
			end
			if acceleration > 0.5 then -- increase AP_VS faster
				if ap_vs < max_clb_vs then
					ipc.control(65894) 
				end
			end
		else  -- we are within ias_margin, using acceleration helps react in advance
			if acceleration > 0 then
				ipc.control(65894)
			else
				ipc.control(65895)
			end
		end
		
	-- DESCENT MODE
	elseif curr_alt > ap_alt + alt_margin then -- we are above ias_margin
		if curr_ias <= ap_ias - ias_margin then
			if acceleration <= 0 then
				if ap_vs > max_des_vs then
					ipc.control(65895) -- decrease AP_VS
				end
			end
			if acceleration < 0.5 then -- decrease AP_VS faster
				if ap_vs > max_des_vs then
					ipc.control(65895)
				end
			end
		elseif curr_ias > ap_ias + ias_margin then -- we are below ias_margin
			if acceleration > 0 then
				if ap_vs < min_des_vs then
					ipc.control(65894) -- increase AP_VS
				end
			end
			if acceleration > 0.5 then  -- increase AP_VS faster
				if ap_vs < min_des_vs then
					ipc.control(65894)
				end
			end
		else  -- we are within ias_margin, using acceleration helps react in advance
			if acceleration > 0 then
				ipc.control(65894)
			else
				ipc.control(65895)
			end
		end
	else -- we are within alt_margin
		start = 0 -- let the AP do the rest
		ipc.writeUB(0x66D0, 0) -- switch off the led (if used)
		-- exit loop and stop the script
	end	
	
	--exit loop by pressing button attached to FS offset
	if ipc.readUB(0x66D0) == 0 then
		start = 0
	end
	
	-- we need last_ias to calculate acceleration
	last_ias = curr_ias
	
	-- here we set frequency of adjustments
	ipc.sleep(sleep)
	
end
-- end of the script

Link to comment
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
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.