looking for data ref for Dial/knob on attitude indicator that adjusts the hoop

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Message
Author
sogibm
Posts: 54
Joined: Fri Dec 01, 2017 2:30 am

looking for data ref for Dial/knob on attitude indicator that adjusts the hoop

#1 Post by sogibm »

Hello,
I'm not sure if i'm calling this the right thing. I want to add a rotary encoder to adjust the hoop up and down instead of using a mouse on the knob of the Attitude indicator. Knob Script reads
--This could be converted to a simulator control
--but for now it is manual.
function new_knob(value)
hoop_height = hoop_height + value
if hoop_height > 27 then
hoop_height = 27
end
if hoop_height < -35 then
hoop_height = -35
end
move(img_hoop, 0, hoop_height, 512, 512)
end

Dial script reads?
function dial_change(direction)
if direction == 1 then
xpl_command( "?????")
if direction == -1 then
xpl_command("????")
end
--Bind to Arduino, Pin 38 and 39
hw_dial_id = hw_dial_add ("ARDUINO_MEGA2560_A_D38" , "ARDUINO_MEGA2560_A_D39" , dial_change)
Am I on the right track?
~Mark
Last edited by sogibm on Wed Feb 14, 2018 10:50 pm, edited 1 time in total.

User avatar
Ralph
Posts: 7926
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: looking for data ref for Dial/knob on attitude indicator that adjusts the hoop

#2 Post by Ralph »

Search for horizon here and you'll find it: http://siminnovations.com/xplane/command/index.php
And I think you need this dataref:
sim/cockpit2/gauges/actuators/artificial_horizon_adjust_deg_pilot [FLOAT]
Artificial horizon pitch-reference adjustment. Pilot side.

sogibm
Posts: 54
Joined: Fri Dec 01, 2017 2:30 am

Re: looking for data ref for Dial/knob on attitude indicator that adjusts the hoop

#3 Post by sogibm »

After watching youtube videos and some reading I came up with

Code: Select all

-- Callback function which is called when the rotary encoder is turned
-- direction 1 : The dial turned clockwise
-- direciton -1: The dial turned counterclockwise
function dial_change(value)

if value == 1 then
    hoop_height = hoop_height + value
	elseif hoop_height > 27 then
		hoop_height = 27

	elseif value == -1 then
	 hoop_height = hoop_height + value
	elseif hoop_height < -35 then
		hoop_height = -35
	end
This works for adjusting the hoop height with a rotary encoder.
Thank you for pointing me in the right "direction" pun intended.

User avatar
Ralph
Posts: 7926
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: looking for data ref for Dial/knob on attitude indicator that adjusts the hoop

#4 Post by Ralph »

That's also an option, but that keeps it in the instrument. If that's enough for you then that's fine.

sogibm
Posts: 54
Joined: Fri Dec 01, 2017 2:30 am

Re: looking for data ref for Dial/knob on attitude indicator that adjusts the hoop

#5 Post by sogibm »

I dont know what you mean? "stays in the Instrument"
I cant figure out how to do it any other way.
This is all new to me, I've been watching videos and was so proud of myself that I figured this much out. I feel deflated now.
Could you perhaps show me how do do it another way?
~Mark

User avatar
Ralph
Posts: 7926
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: looking for data ref for Dial/knob on attitude indicator that adjusts the hoop

#6 Post by Ralph »

It wasn't a comment in a negative way :) Just to say that if you don't necessarily need to influence the simulator, then this is sufficient. If you want the gauge in the sim to move as well, then you will need to use the commands.

sogibm
Posts: 54
Joined: Fri Dec 01, 2017 2:30 am

Re: looking for data ref for Dial/knob on attitude indicator that adjusts the hoop

#7 Post by sogibm »

ok I understand now.
I'm sorry I didn't think it was a comment in a negative way, my post was just my wry humor coming out.
So this just moves the png that I see on my screen if I want to influence the sim I have to use a command.
I tried to use a command but couldn't find one that I saw move this hoop maybe I was using the right command and it was moving the hoop in the sim but not on the AM gauge? is that possible?
these were the commands I was using,

Code: Select all

 sim/instruments/ah_ref_down Horizon reference down a bit.
 sim/instruments/ah_ref_up Horizon reference up a bit.
and I dont know where this goes or if I need to subscribe to it for the command to work.

Code: Select all

sim/cockpit2/gauges/actuators/artificial_horizon_adjust_deg_pilot [FLOAT]
I am just learning this, its all new to me so I try to reverse engineer the script and understand it.
I learn a little bit every day with trial and error and of course with people like you who are willing to help a newbie.
without people like yourself I would be utterly lost so I thank you with a big THANK YOU.
~Mark

JackZ
Posts: 2267
Joined: Mon Feb 22, 2016 1:02 pm

Re: looking for data ref for Dial/knob on attitude indicator that adjusts the hoop

#8 Post by JackZ »

The “Hoop movement” in the XPlane sim and in AM are not automatically related unless you program it that way.
Using an Xplane event(command) using xpl_command(whatever) function in your code does merely this: send a command to XPlane.
If you don’t relate the movement of your instrument’s hoop to that command in a way or another, the sim’s hoop will move, but not the AM’s one.

Two different ways to do that:
1- in your code, read the direction of movement of the dial, then send the proper command to XPlane (hoop up/down) AND at the same time have the AM instrument’s hoop move in the same direction using img_move() function.

2-in your code, read the direction of movement of the dial, then send the proper command to XPlane (hoop up/down). Then in a separate part of the code, subscribe to the corresponding XPlane variable using the xpl_dataref_subscribe(whatever) function,(if ithis variable exists in the case of the hoop’s position). You can then have the graphic hoop element move in your AM gauge using img_move() to the retrieved hoop’s position given by the sim.

The second solution is preferable since you ensure that way that both hoops position are synchronized, but it’s a little more complicated to implement.
In the first solution, on the other hand you’ll have to ensure that the sim’s hoop is at the center position BEFORE even starting the AM gauge, since AM has no way to know the actual position of the sim’s hoop.
If you launch AM with both hoops in the same starting position, as long as you move the graphical A.M. hoop in the same direction and and amount of movement as the sim one, there will be no problem.

Same reasoning applies to switches, dials etc...
Both methods are usable in that case, but the first one (without receiving feedback from the sim) implies that the sim switch/dial/button state is set in a position identical to the initial position of its AM counterpart BEFORE launching your AM gauge. If you use a “before startup” check list that can be done easily.

Jacques
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

sogibm
Posts: 54
Joined: Fri Dec 01, 2017 2:30 am

Re: looking for data ref for Dial/knob on attitude indicator that adjusts the hoop

#9 Post by sogibm »

Thank you, I am starting to understand.
:idea:
I think.
what i dont understand is what function should follow
xpl_dataref_subscribe("sim/cockpit2/gauges/actuators/artificial_horizon_adjust_deg_pilot"," [FLOAT]",dial_change)
which refers it to

function dial_change(dir)
if dir == -1 then
xpl_command("sim/instruments/ah_ref_up")
elseif dir == 1 then
xpl_command("sim/instruments/ah_ref_down")
end

end
-- Bind to Arduino, Header P1, Pin 52 and 53
hw_dial_id = hw_dial_add ("ARDUINO_MEGA2560_A_D52" , "ARDUINO_MEGA2560_A_D53" , dial_change)

but reading your text I should have it refer to an image move function heres the entire code

Code: Select all

---------------------------------------------
--        Load and display images          -- CLONE 2
---------------------------------------------
img_bg = img_add_fullscreen("attbg.png")
img_horizon = img_add_fullscreen("attdial.png")
img_bankind = img_add_fullscreen("attroll.png")
img_hoop = img_add_fullscreen("atthoop.png")
img_flag = img_add("GyroFlag.png", -120, -190, 512, 512)
img_add_fullscreen("attface.png")
--img_add("airknobshadow.png",213,400,85,85)

flag_rotation = 0
flag_state = 0

hoop_height = 0

---------------
-- Functions --
---------------

function new_attitude(roll, pitch)

-- Roll outer ring
    roll = var_cap(roll, -60, 60)
	img_rotate(img_bankind, roll *-1)
    img_rotate(img_bg, roll *-1)
-- Roll horizon
    img_rotate(img_horizon  , roll * -1)
    
-- Move horizon pitch
    pitch = var_cap(pitch, -25, 25)
    radial = math.rad(roll * -1)
    x = -(math.sin(radial) * pitch * 3)
    y = (math.cos(radial) * pitch * 3)
    img_move(img_horizon, x, y, nil, nil)
	

	
end


function timer_callback()
	
	if flag_state == 1 then
		-- extend
		flag_rotation = flag_rotation - 0.25
		if flag_rotation <= -30 then
			flag_rotation = -30
			timer_stop(tmr_flag)
		end
	else
		-- retract
		flag_rotation = flag_rotation + 0.25
		if flag_rotation >= 0 then
			flag_rotation = 0
			timer_stop(tmr_flag)
		end
	end

	img_rotate(img_flag, flag_rotation)
end

function new_vacfail(vacfail1, vacfail2, vac1, vac2)
	if (vacfail1 > 0 and vacfail2 > 0) or (vac1 < 1.8 and vac2 < 1.8) then
		flag_state = 1
	else
		flag_state = 0
	end
    timer_stop(tmr_flag)
	tmr_flag = timer_start(0, 250, timer_callback)
end

function new_vacfail_fsx(fail, vac)
	if fail > 0 or vac < 1.8 then
		flag_state = 1
	else
		flag_state = 0
	end
    
    timer_stop(tmr_flag)
	tmr_flag = timer_start(0, 250, timer_callback)
end

function new_attitude_fsx(roll, pitch)
	
	new_attitude(roll * -57, pitch * -37)

end
function dial_change(dir)
if dir == -1 then
		xpl_command("sim/instruments/ah_ref_up")
elseif dir == 1 then
		xpl_command("sim/instruments/ah_ref_down")
  end

end
-- Bind to Arduino, Header P1, Pin 52 and 53
hw_dial_id = hw_dial_add ("ARDUINO_MEGA2560_A_D52" , "ARDUINO_MEGA2560_A_D53" ,  dial_change)

--This could be converted to a simulator control
--but for now it is manual.
function new_knob(value)
	hoop_height = hoop_height + value
	if hoop_height > 27 then
		hoop_height = 27
	end
	if hoop_height < -35 then
		hoop_height = -35
	end
	move(img_hoop, 0, hoop_height, 512, 512)
end

-- DIALS ADD --
dial_knob = dial_add("airknob.png", 213, 395, 85, 85, new_knob)
dial_click_rotate(dial_knob,6)

-------------------
-- Bus subscribe --
-------------------
xpl_dataref_subscribe("sim/cockpit/gyros/phi_vac_ind_deg", "FLOAT",
					  "sim/cockpit/gyros/the_vac_ind_deg", "FLOAT",	new_attitude)
					
xpl_dataref_subscribe("sim/operation/failures/rel_vacuum", "INT",
					  "sim/operation/failures/rel_vacuum2", "INT",
					  "sim/cockpit/misc/vacuum", "FLOAT", 
					  "sim/cockpit/misc/vacuum2", "FLOAT", new_vacfail)
					
xpl_dataref_subscribe("sim/cockpit2/gauges/actuators/artificial_horizon_adjust_deg_pilot"," [FLOAT]",dial_change)
			   		  
Ill keep trying until i figure it out.
I am learning alot
i remember some DOS from high school and I think a call back is like a goto command.
yes im that old

JackZ
Posts: 2267
Joined: Mon Feb 22, 2016 1:02 pm

Re: looking for data ref for Dial/knob on attitude indicator that adjusts the hoop

#10 Post by JackZ »

the Xplane datatef_subscribe is a function that calls another function each time the subscribed value changes:
In that case let’s name the called function hoop_move.
In this function you’ ll receive the value that has changed (here the hoop’s position in degrees) and act accordingly, ie move the hoop graphical element previously added to AM working memory using img_add().

Note: the called function should always be placed in the code BEFORE the subscribe.

xpl_dataref_subscribe("sim/cockpit2/gauges/actuators/artificial_horizon_adjust_deg_pilot"," [FLOAT]", hoop_move)

I’ m away from my computer until the end of the week so doing it from memory, but I think you should try something like this:

function hoop_move(degrees)
step=XX — Note: here replace the XX by a suitable float value in pixels relevant to your gauge , by trial and error
img_move(img_hoop, math.sin(degrees)*step)
end

Jacques

function dial_change(dir)
if dir == -1 then
xpl_command("sim/instruments/ah_ref_up")
elseif dir == 1 then
xpl_command("sim/instruments/ah_ref_down")
end

end
-- Bind to Arduino, Header P1, Pin 52 and 53
hw_dial_id = hw_dial_add ("ARDUINO_MEGA2560_A_D52" , "ARDUINO_MEGA2560_A_D53" , dial_change)

but reading your text I should have it refer to an image move function heres the entire code

Code: Select all

---------------------------------------------
--        Load and display images          -- CLONE 2
---------------------------------------------
img_bg = img_add_fullscreen("attbg.png")
img_horizon = img_add_fullscreen("attdial.png")
img_bankind = img_add_fullscreen("attroll.png")
img_hoop = img_add_fullscreen("atthoop.png")
img_flag = img_add("GyroFlag.png", -120, -190, 512, 512)
img_add_fullscreen("attface.png")
--img_add("airknobshadow.png",213,400,85,85)

flag_rotation = 0
flag_state = 0

hoop_height = 0

---------------
-- Functions --
---------------

function new_attitude(roll, pitch)

-- Roll outer ring
    roll = var_cap(roll, -60, 60)
	img_rotate(img_bankind, roll *-1)
    img_rotate(img_bg, roll *-1)
-- Roll horizon
    img_rotate(img_horizon  , roll * -1)
    
-- Move horizon pitch
    pitch = var_cap(pitch, -25, 25)
    radial = math.rad(roll * -1)
    x = -(math.sin(radial) * pitch * 3)
    y = (math.cos(radial) * pitch * 3)
    img_move(img_horizon, x, y, nil, nil)
	

	
end


function timer_callback()
	
	if flag_state == 1 then
		-- extend
		flag_rotation = flag_rotation - 0.25
		if flag_rotation <= -30 then
			flag_rotation = -30
			timer_stop(tmr_flag)
		end
	else
		-- retract
		flag_rotation = flag_rotation + 0.25
		if flag_rotation >= 0 then
			flag_rotation = 0
			timer_stop(tmr_flag)
		end
	end

	img_rotate(img_flag, flag_rotation)
end

function new_vacfail(vacfail1, vacfail2, vac1, vac2)
	if (vacfail1 > 0 and vacfail2 > 0) or (vac1 < 1.8 and vac2 < 1.8) then
		flag_state = 1
	else
		flag_state = 0
	end
    timer_stop(tmr_flag)
	tmr_flag = timer_start(0, 250, timer_callback)
end

function new_vacfail_fsx(fail, vac)
	if fail > 0 or vac < 1.8 then
		flag_state = 1
	else
		flag_state = 0
	end
    
    timer_stop(tmr_flag)
	tmr_flag = timer_start(0, 250, timer_callback)
end

function new_attitude_fsx(roll, pitch)
	
	new_attitude(roll * -57, pitch * -37)

end
function dial_change(dir)
if dir == -1 then
		xpl_command("sim/instruments/ah_ref_up")
elseif dir == 1 then
		xpl_command("sim/instruments/ah_ref_down")
  end

end
-- Bind to Arduino, Header P1, Pin 52 and 53
hw_dial_id = hw_dial_add ("ARDUINO_MEGA2560_A_D52" , "ARDUINO_MEGA2560_A_D53" ,  dial_change)

--This could be converted to a simulator control
--but for now it is manual.
function new_knob(value)
	hoop_height = hoop_height + value
	if hoop_height > 27 then
		hoop_height = 27
	end
	if hoop_height < -35 then
		hoop_height = -35
	end
	move(img_hoop, 0, hoop_height, 512, 512)
end

-- DIALS ADD --
dial_knob = dial_add("airknob.png", 213, 395, 85, 85, new_knob)
dial_click_rotate(dial_knob,6)

-------------------
-- Bus subscribe --
-------------------
xpl_dataref_subscribe("sim/cockpit/gyros/phi_vac_ind_deg", "FLOAT",
					  "sim/cockpit/gyros/the_vac_ind_deg", "FLOAT",	new_attitude)
					
xpl_dataref_subscribe("sim/operation/failures/rel_vacuum", "INT",
					  "sim/operation/failures/rel_vacuum2", "INT",
					  "sim/cockpit/misc/vacuum", "FLOAT", 
					  "sim/cockpit/misc/vacuum2", "FLOAT", new_vacfail)
					
xpl_dataref_subscribe("sim/cockpit2/gauges/actuators/artificial_horizon_adjust_deg_pilot"," [FLOAT]",dial_change)
			   		 
Ill keep trying until i figure it out.
I am learning alot
i remember some DOS from high school and I think a call back is like a goto command.
yes im that old
[/quote]
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

Post Reply