Trouble getting hw_dial_add code to work in a panel

Help creating logic scripts for Air Manager Instruments

Moderators: Ralph, russ

Post Reply
Message
Author
toneill
Posts: 48
Joined: Tue Dec 03, 2019 5:46 pm

Trouble getting hw_dial_add code to work in a panel

#1 Post by toneill » Tue Apr 20, 2021 2:26 am

After several days of debugging I finally solved this issue on my own, but I don't understand why. I would appreciate it if one of the experts reading this could tell me why my original code did not work.

The code is for a hardware/software DIY cockpit for the Hotstart TBM900. The issue was I could not get the hw_dial_add rotaries to work in a panel but they would work in create/edit. In this case I was using a rotaries to dial in altitude or heading. Here is a snippet of code that illustrates the problem

Code: Select all

--hw_dial_right_id=hw_dial_add("rightRotary",RR)--pin A is A10   B is A11

hw_dial_right_id=hw_dial_add("ARDUINO_MEGA2560_D_A10","ARDUINO_MEGA2560_D_A11",RR)--Pin A= A14 B=A13
The top line, commented out, will work in create/edit mode but when I run the instrument in a panel, it will not work. The rotaries will not change the altitude, for example, there is no response. After trying many things trying to isolate the problem I finally changed the method hw_dial_add code to specify the pins in code rather than in the API. This works in the panel.

But I don't understand why. There must be something I am missing in the documentation perhaps. If anyone can enlighten me I'd appreciate it

The actual code in this instrument which runs on an Arduino Mega is about 1000 line. To debug the issue I trimmed that down to the following, in case anyone needs this.
Tony, please forgive the print direction code after the If-then I put that in for debugging purposes. :lol:

Code: Select all

--   T B M   - D -  M A I N     L A R G E      P A N E L 

--    T E S T    T E S T       T E S T 


--================================================================
--            L E F T    R O T A R Y A13  A14  A12(PRESSED)
--================================================================\--
local heading
--+++++++++++   H E A D I N G   dataref and function needed to get heading so we can increment it
function hdg_change_callback(xheading)
	print ("heading ="..xheading)
	heading=xheading
end
xpl_dataref_subscribe ("tbm900/knobs/ap/hdg","INT",hdg_change_callback)
--+++++++++++end dataref code for xheading variable

function A12_pressed() -- L  E  F  T 
	print("A12 Left Dial pressed")
        xpl_command("tbm900/actuators/ap/hdg_sync")
end

hw_button_add("ARDUINO_MEGA2560_D_A12",A12_pressed)


function LR (direction)  --Left Rotary - heading set
	if direction==-1 then print ("LEFT Rotary dial rotated right") 
	  heading=heading+2        elseif direction==1 then print ("Left rotary rotated left")
            heading=heading-2
	end
print (heading)
        xpl_dataref_write("tbm900/knobs/ap/hdg","INT",heading)
end
 -- L E F T     R O T A R Y  DIAL ADD 
--NOTE THE NEXT LINE WORKS IN CREATE EDIT BUT WILL NOT RUN IN A PANEL
--hw_dial_left_id=hw_dial_add("Left_Rotary",LR)--Pin A= A14 B=A13
hw_dial_left_id=hw_dial_add("ARDUINO_MEGA2560_D_A14","ARDUINO_MEGA2560_D_A13",LR)--Pin A= A14 B=A13
--==================================================,

--================================================================
--            R I G H T   R O T A R Y A13  A14  A15(PRESSED)
--================================================================
local altitude
--+++++++++++   H E A D I N G   dataref and function needed to get heading so we can increment it
function alt_change_callback(xaltitude)
	print ("Altitude ="..xaltitude)
	altitude=xaltitude
end
xpl_dataref_subscribe ("tbm900/knobs/ap/alt","INT",alt_change_callback)
--+++++++++++end dataref code for xheading variable

---------------------------------------------------
function A15_pressed() --R  I  G  H  T 
	print("A15 Right Dial pressed")
        xpl_command("sim/GPS/gcu478/alt_sync")
end

hw_button_add("ARDUINO_MEGA2560_D_A15",A15_pressed)
---------------------------------------------------


 function RR(direction)
	print ("RR")
	if direction ==-1 then print ("right rotary turning right")    
            altitude=altitude+500
      	elseif direction ==1 then print ("right rotary turning left") 
                altitude=altitude-500
       
end
    xpl_dataref_write("tbm900/knobs/ap/alt","INT",altitude)
 end
 -- R I G H T     R O T A R Y  DIAL ADD 
--NOTE THE NEXT LINE WORKS IN CREATE EDIT BUT WILL NOT RUN IN A PANEL
--hw_dial_right_id=hw_dial_add("rightRotary",RR)--pin A is A10   B is A11

hw_dial_right_id=hw_dial_add("ARDUINO_MEGA2560_D_A10","ARDUINO_MEGA2560_D_A11",RR)--Pin A= A10 B=A11

--################################################################



User avatar
Sling
Posts: 4419
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Trouble getting hw_dial_add code to work in a panel

#2 Post by Sling » Tue Apr 20, 2021 2:53 am

The first method is using a hardware id. When using this method you need to tell AM in the instrument settings what actual hardware you want to use. When you do this in the create/edit tab it does not pass it onto any panel that uses the instrument as it’s designed for testing and creating only.

I know what the wiki says but I recommend you use the second method anyhow as it will always work with your custom hardware without ever having to set it up again.

The wiki should really say named hardware is recommended for code that you want to be portable. That means code used in instruments that you intend to share. This gives the benefit of another user being able to select different hardware without changing any code. For all other situations method 2 is preferred.

Tony

toneill
Posts: 48
Joined: Tue Dec 03, 2019 5:46 pm

Re: Trouble getting hw_dial_add code to work in a panel

#3 Post by toneill » Tue Apr 20, 2021 3:31 am

Perfect! Thanks Tony

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests