Struggling with LUA and the basics
-
- Posts: 581
- Joined: Mon Sep 07, 2020 8:49 am
Struggling with LUA and the basics
I am trying to get down to the basics for setting up a simple hardware switch. The videos I have seen are all about instrument development, which confuses me since what I need is only hardware.
So I would like to start at the beginning with my limited understanding of how to connect a physical switch to Xplane.
I have a momentary switch (normally open) that I want to use to operate a switch in Xplane.
Below is all I know for now; The dataref to write / subscribe to and the Arduino pin I want to use.
I know what I have so far is incorrect but I think those are some basics that are required...correct?
What does this code need to have for a simple momentary switch to work with the Xplane datarefs?
Thanks,
Kirk
So I would like to start at the beginning with my limited understanding of how to connect a physical switch to Xplane.
I have a momentary switch (normally open) that I want to use to operate a switch in Xplane.
Below is all I know for now; The dataref to write / subscribe to and the Arduino pin I want to use.
I know what I have so far is incorrect but I think those are some basics that are required...correct?
What does this code need to have for a simple momentary switch to work with the Xplane datarefs?
Thanks,
Kirk
-
- Posts: 5339
- Joined: Thu Jul 27, 2017 12:22 am
Re: Struggling with LUA and the basics
Hi KirkKaellis991 wrote: ↑Fri Sep 30, 2022 6:45 pm I am trying to get down to the basics for setting up a simple hardware switch. The videos I have seen are all about instrument development, which confuses me since what I need is only hardware.
So I would like to start at the beginning with my limited understanding of how to connect a physical switch to Xplane.
I have a momentary switch (normally open) that I want to use to operate a switch in Xplane.
Below is all I know for now; The dataref to write / subscribe to and the Arduino pin I want to use.
I know what I have so far is incorrect but I think those are some basics that are required...correct?
What does this code need to have for a simple momentary switch to work with the Xplane datarefs?
Thanks,
Kirk
in this case, the "xpl_dataref_write" statement have to be located in the callback function
further we have to check which value get the position variable
Code: Select all
function ap_switch_callback(position)
print("The AP switched to position " .. position)
if position == 1 then
xpl_dataref_write("sim/cockpit/autopilot/autopilot_mode", "INT", 2)
end
end
hw_switch_add("AP Master", 1, ap_switch_callback)
-
- Posts: 581
- Joined: Mon Sep 07, 2020 8:49 am
Re: Struggling with LUA and the basics
Thanks for the clarification. That works.
I have one oher question.
The next thing I need is to send a HIGH output to pin 2 when the AP Master switch is set to 2 (ON) and to send a LOW when the AP master is set to 0.
Would that be something like in the image? I know what I have is not correct.
Should that xpl_command be the dataref instead as in the switch callback? Or is this output code way off base?
I am just not sure how the output code and the switch input should interrelate.
Should the variables be different and not the same (position) value?
I have one oher question.
The next thing I need is to send a HIGH output to pin 2 when the AP Master switch is set to 2 (ON) and to send a LOW when the AP master is set to 0.
Would that be something like in the image? I know what I have is not correct.
Should that xpl_command be the dataref instead as in the switch callback? Or is this output code way off base?
I am just not sure how the output code and the switch input should interrelate.
Should the variables be different and not the same (position) value?
-
- Posts: 5339
- Joined: Thu Jul 27, 2017 12:22 am
Re: Struggling with LUA and the basics
The toggle command is not the best, as it doesn't allow to be sure we are in sync between hardware switch position and the switch position in the sim, so yes, a better solution is to use a direct dataref write, when the related dataref is available for a specific purpose
Additionally, we can simplify the position check part, to rather use it directly with a boolean test inside a single line
[if position == 1] will bring the "true" result when the switch is in the ON position and the "false" result when the switch is in the OFF position
so we can replace
by a single line
I guess we are still able to use similar statement xpl_dataref_write("sim/cockpit/autopilot/autopilot_mode", "INT", n) where "n" would be computed upon position value
Don't miss the "hw_switch_add statement" to trigger the switch_callback function and also remove the two ending square brackets, at the end of line #13 and after the "end" statement
Additionally, we can simplify the position check part, to rather use it directly with a boolean test inside a single line
[if position == 1] will bring the "true" result when the switch is in the ON position and the "false" result when the switch is in the OFF position
so we can replace
Code: Select all
if position == 0 then
hw_output_set_outp_id, false)
else
hw_output_set_outp_id, true)
end
Code: Select all
hw_output_set(outp_id, position == 1)
Don't miss the "hw_switch_add statement" to trigger the switch_callback function and also remove the two ending square brackets, at the end of line #13 and after the "end" statement
-
- Posts: 581
- Joined: Mon Sep 07, 2020 8:49 am
Re: Struggling with LUA and the basics
Hi,
This is what I have discerned from your comments.
The AP switch is working on D12, however the magnet is not getting energized on D12.
I still don't understand how these code segments interact with each other.
So I just add things without full comprehension. There are little bits and pieces that make sense but not the overall picture.
There are two switches that need coding. One for the autopilot ON/OFF and one for the HDG mode ON/OFF.
If I can get the autopilot switch working then I think that same code will work for the HDG mode switch with a change to the dataref.
Each switch has to send a HIGH or LOW signal to energize the magnets that hold each physical switch in place.
The trick will be in getting these two switches to interact with each other correctly. But one step at a time.
This is what I have discerned from your comments.
The AP switch is working on D12, however the magnet is not getting energized on D12.
I still don't understand how these code segments interact with each other.
So I just add things without full comprehension. There are little bits and pieces that make sense but not the overall picture.
There are two switches that need coding. One for the autopilot ON/OFF and one for the HDG mode ON/OFF.
If I can get the autopilot switch working then I think that same code will work for the HDG mode switch with a change to the dataref.
Each switch has to send a HIGH or LOW signal to energize the magnets that hold each physical switch in place.
The trick will be in getting these two switches to interact with each other correctly. But one step at a time.
-
- Posts: 5339
- Joined: Thu Jul 27, 2017 12:22 am
Re: Struggling with LUA and the basics
Hi Kirk
If you are you want help from the community, pasting code from a screenshot is not a good idea
Please use the code tag icon above your post when writing
this will write the tags as follow, between which we have to paste our code extrac
here's a sample
will appear like this :
to help, we can use the <Preview> button to check if all is well
If you are you want help from the community, pasting code from a screenshot is not a good idea
Please use the code tag icon above your post when writing
this will write the tags as follow, between which we have to paste our code extrac
here's a sample
will appear like this :
Code: Select all
sample
-
- Posts: 581
- Joined: Mon Sep 07, 2020 8:49 am
Re: Struggling with LUA and the basics
Just like this? Where does one find the info on this little feature of the forum?
Code: Select all
--Create a new output for AP Magnet
outp_id = hw_output_add("AP Magnet", false)
function switch_callback(position)
--print("The switch got changed to position " .. position)
hw_output_set(outp_id, position == 1)
--print(position)
end
--Create a new switch input AP Master
function ap_switch_callback(position)
--print("The AP switched to position " ..position)
if position == 1 then
xpl_dataref_write("sim/cockpit/autopilot/autopilot_mode", "INT", 2)
else
xpl_dataref_write("sim/cockpit/autopilot/autopilot_mode", "INT", 0)
end
end
hw_switch_add("AP Master", 1, ap_switch_callback)
-
- Posts: 5339
- Joined: Thu Jul 27, 2017 12:22 am
Re: Struggling with LUA and the basics
Perfect, just that both function could be merged into a single one, further we missed the "hw_switch_add("AP Master", 1, switch_callback)" statement without which the "switch_callback" function will never be calledKaellis991 wrote: ↑Sun Oct 02, 2022 4:18 pm Just like this? Where does one find the info on this little feature of the forum?
Code: Select all
--Create a new output for AP Magnet outp_id = hw_output_add("AP Magnet", false) function switch_callback(position) --print("The switch got changed to position " .. position) hw_output_set(outp_id, position == 1) --print(position) No need for this line, as the first print is performing the same expected action !!! end --Create a new switch input AP Master function ap_switch_callback(position) --print("The AP switched to position " ..position) if position == 1 then xpl_dataref_write("sim/cockpit/autopilot/autopilot_mode", "INT", 2) else xpl_dataref_write("sim/cockpit/autopilot/autopilot_mode", "INT", 0) end end hw_switch_add("AP Master", 1, ap_switch_callback)
so it become :
Code: Select all
--Create a new output for AP Magnet
outp_id = hw_output_add("AP Magnet", false)
function switch_callback(position)
--print("The AP switched to position " ..position)
hw_output_set(outp_id, position == 1)
xpl_dataref_write("sim/cockpit/autopilot/autopilot_mode", "INT", position * 2) -- Here, if position = 1, we get a result of 2 and if position = 0, we get a result of 0, which eventually fulfill our need
end
hw_switch_add("AP Master", 1, switch_callback)
The next group of statement lines is for a function, which will never run without a call statement at some point.
This call is made through the hw_switch_add statement, which eventually call the function on switch position change and forward the position value to the function.
In other words, inside the function the switch position is forwarded to the "position" variable when the switch is moved to a different position (we know this trigger an event, which is known by Air Manager or Air Player, which then trigger a named function call from the hw_switch_add statement)
Hope I've not bring too much complicate explanation though and it help a bit to clarify how things are working inside the LUA script in Air Manager
Gilles
-
- Posts: 5339
- Joined: Thu Jul 27, 2017 12:22 am
Re: Struggling with LUA and the basics
I don't think there's explanation for the icons used when writing a post, which eventually are used in multiple similar forums
-
- Posts: 581
- Joined: Mon Sep 07, 2020 8:49 am
Re: Struggling with LUA and the basics
I duplicated that code in my AM hardware code and get this error when I select run, when I press the physical switch and when I change the switch slider from 0 to 1 in the hardware tab next to hardware properties.
ERROR - Error in hardware switch callback: attempt to call a nil value
The lightbulb next to the digital output in the hardware tab does not light up either when I select it with the mouse.
I cant show you the hardware tabs and the console unless I cut and paste a snippet image...
There is an asterisk in this line between ...position and 2. That to me indicates multiplication. Is that correct?
xpl_dataref_write("sim/cockpit/autopilot/autopilot_mode", "INT", position * 2)
Just to be clear, two things have to happen with this function; The pressing of the momentary switch from normally open to closed needs to change the autopilot_mode dataref value from 0 to 2 and also send the HIGH output to the other pin which energizes the magnet to keep the switch in that closed position. When the bottom of the switch (OFF) is pushed then the dataref value needs to go back to 0 and the output to pin D2 needs to go low to cut the power to the magnet....technically.
As an aside....It is possible that the magnets can stay energized since the 14V the magnet coils receive is not too excessive. When pressing the toggle switches at the bottom to the off position, it will pull the top of the switch away from the magnetic field and the magnet will no longer attract the metal plate at the top of the toggle switch. It would be best to cut the electricity to the magnets, but I dont think they generate too much heat that they couldn't stay on.
Is this one function supposed do both?
Right now neither the switch in the sim is getting activated (pin D12) nor is the other pin (pin D2)sending a HIGH signal.
Did I copy the code correctly? Am I missing something?
Your description is very clear....thanks
ERROR - Error in hardware switch callback: attempt to call a nil value
The lightbulb next to the digital output in the hardware tab does not light up either when I select it with the mouse.
I cant show you the hardware tabs and the console unless I cut and paste a snippet image...
There is an asterisk in this line between ...position and 2. That to me indicates multiplication. Is that correct?
xpl_dataref_write("sim/cockpit/autopilot/autopilot_mode", "INT", position * 2)
Just to be clear, two things have to happen with this function; The pressing of the momentary switch from normally open to closed needs to change the autopilot_mode dataref value from 0 to 2 and also send the HIGH output to the other pin which energizes the magnet to keep the switch in that closed position. When the bottom of the switch (OFF) is pushed then the dataref value needs to go back to 0 and the output to pin D2 needs to go low to cut the power to the magnet....technically.
As an aside....It is possible that the magnets can stay energized since the 14V the magnet coils receive is not too excessive. When pressing the toggle switches at the bottom to the off position, it will pull the top of the switch away from the magnetic field and the magnet will no longer attract the metal plate at the top of the toggle switch. It would be best to cut the electricity to the magnets, but I dont think they generate too much heat that they couldn't stay on.
Is this one function supposed do both?
Right now neither the switch in the sim is getting activated (pin D12) nor is the other pin (pin D2)sending a HIGH signal.
Did I copy the code correctly? Am I missing something?
Your description is very clear....thanks
Code: Select all
--Create a new output for AP Magnet
outp_id = hw_output_add("AP Magnet", false)
function switch_callback(position)
--print("The switch got changed to position " .. position)
hw_output_set(outp_id, position == 1)
xpl_dataref_write("sim/cockpit/autopilot/autopilot_mode", "INT", position * 2)
end
hw_switch_add("AP Master", 1, ap_switch_callback) /[code]