Ignition control assistance

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
checkyourmirrors
Posts: 11
Joined: Sat Dec 12, 2020 11:47 pm

Ignition control assistance

#1 Post by checkyourmirrors »

I'm attempting to write an instrument that allows me to turn the ignition key for an R44 (Helicopter) and it accurately reads the status of the key position. However I'd like to modify the key position and I've been able to attempt to "start" the ignition when the position is in Both (3), however I can't seem to turn the key to a lower number and go from 3 to 2, 2 to 1 or 1 to 0. Hoping someone can see something obvious here.....

Code: Select all

img_add_fullscreen("ignition_switch.png")

-- IGNITION KEY
ign_off = img_add("Off.png",67,34,325,325)
ign_left = img_add("Left.png",67,34,325,325)
ign_right = img_add("Right.png",67,34,325,325)
ign_both = img_add("Both.png",67,34,325,325)
ign_start = img_add("Prime.png",67,34,325,325)
visible(ign_off,true)
visible(ign_left,false)
visible(ign_right,false)
visible(ign_both,false)
visible(ign_start,false)

xpl_key_position="sim/cockpit2/engine/actuators/ignition_key"
xpl_key_type="INT[8]"
--
local ign_state = 0

function ignition_callback(direction)

if ign_state == 0 and direction == 1 then
xpl_dataref_write(xpl_key_position,xpl_key_type, 1)
elseif ign_state == 1 and direction == -1 then
xpl_dataref_write(xpl_key_position,xpl_key_type, 0)
elseif ign_state == 1 and direction == 1 then
xpl_dataref_write(xpl_key_position,xpl_key_type, 2)
elseif ign_state == 2 and direction == -1 then
xpl_dataref_write(xpl_key_position,xpl_key_type, 1)
elseif ign_state == 2 and direction == 1 then
xpl_dataref_write(xpl_key_position,xpl_key_type, 3)
elseif ign_state == 3 and direction == -1 then
xpl_dataref_write(xpl_key_position,xpl_key_type, 2)
elseif ign_state == 3 and direction == 1 then
xpl_dataref_write(xpl_key_position,xpl_key_type, 4)
elseif ign_state == 4 and direction == -1 then
xpl_dataref_write(xpl_key_position,xpl_key_type, 3)

end

end

ignition_sw = dial_add(nil,67,34,325,325,ignition_callback)

function new_ignition_xpl(ing_pos)

-- R44 MAGNETO POSITIONS
-- 0 = OFF
-- 1 = LEFT
-- 2 = RIGHT
-- 3 = BOTH
-- 4 = START

if ing_pos[1] == 0 then
ign_state = 0
elseif ing_pos[1] == 1 then
ign_state = 1
elseif ing_pos[1] == 2 then
ign_state = 2
elseif ing_pos[1] == 3 then
ign_state = 3
elseif ing_pos[1] == 4 then
ign_state = 4
end

visible(ign_off, ign_state == 0)
visible(ign_right, ign_state == 1)
visible(ign_left, ign_state == 2)
visible(ign_both, ign_state == 3)
visible(ign_start, ign_state == 4)

end

xpl_dataref_subscribe(xpl_key_position,xpl_key_type,new_ignition_xpl)

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

Re: Ignition control assistance

#2 Post by Sling »

Hi,

The ignition control needs a bit of extra knowledge over control of a standard dataref to get it working nicely.

First though to your code.

You are over complicating both the dial and subscribe callback functions. All those if..else's are not required. They can be really simplified as follows.

You'll notice the additional use of the optional force arguments being used in the dataref_write(). This is the extra bit of knowledge required to get it to work, The forcing is required to get the start operation to behave correctly. It forces start to be wriiten to the sim every frame until you release the key then it sets force to false. Easy once you know this little trick. Believe me this will save you a lot of time trying to figure out why the start is not working correctly. Omit this extra bit and you'll see how it behaves without it.

This should kick start your little project.

Note that this is not complete code and will have to be integrated with the rest of the code you posted.

Tony

Code: Select all

function ignition_callback(direction)
 
    ign_state = var_cap(ign_state + direction, 0, 4)
    xpl_dataref_write(xpl_key_position,xpl_key_type, {ign_state}, 0, true)

end
function ignition_pressed()
--does nothing
end
function ignition_released()
    if (ign_state == 4) then xpl_dataref_write("sim/cockpit2/engine/actuators/ignition_key", "INT[8]", {3}, 0, false) end
end

ignition_sw = dial_add(nil,67,34,325,325,ignition_callback, ignition_pressed, ignition_released)

function new_ignition_xpl(ing_pos)
 
    ign_state = ing_pos[1]
 
    visible(ign_off, ign_state == 0)
    visible(ign_right, ign_state == 1)
    visible(ign_left, ign_state == 2)
    visible(ign_both, ign_state == 3)
    visible(ign_start, ign_state == 4)
 
end


checkyourmirrors
Posts: 11
Joined: Sat Dec 12, 2020 11:47 pm

Re: Ignition control assistance

#3 Post by checkyourmirrors »

Thank you for the help, I've got a lot to learn.... I applied the code and the hold to start is way better with the press and release functionality. The oddity is the simulation key doesn't move/match with the Air Manager Key. When I turn the Sim key, the AM key reacts correctly and matches the position. When I turn the AM key, the sim reacts correctly however the sim key doesn't move. The dataref is being updated and if I had to guess, the Aircraft code (VSKYLABS R44) isn't checking the dataref as the AM code appears to be doing what it should. I tried the other two ignition datarefs but those aren't used.

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

Re: Ignition control assistance

#4 Post by Ralph »

You can use the datareftool to see which dataref is being used for the ignition. Maybe it has commands as well.

checkyourmirrors
Posts: 11
Joined: Sat Dec 12, 2020 11:47 pm

Re: Ignition control assistance

#5 Post by checkyourmirrors »

Thanks for the tool suggestion and I was using the DataRefEdit tool which was much more challenging so the oddity here is when I watch the ignition_on with the VSKYLABS R44 the numbers jump back and forth constantly (with and without AM running) so for example it is going from a 2 to a 3 and back super fast. So I loaded a different companies R44 and that ignition_on dataref is stable and also the sim key follows AM. So I'm back to the VSKYLABS as having the problem.

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

Re: Ignition control assistance

#6 Post by Sling »

I just took a look at a VSkyLabs aircraft i own (not R44) and it is different to the default behaviour. It uses failure datarefs to control the ignition and a starter engage command. Both the datarefs and the command are standard xplane ones. You should be able to see which ones are used in the R44 model by using dataref tool.

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

Re: Ignition control assistance

#7 Post by Ralph »

The Cabri indeed uses failure datarefs to turn on the magnetos for example. Very strange, but I guess there's a logical explanation behind it. It might also be that they have a custom dataref.

Post Reply