Struggling with LUA and the basics
-
- Posts: 5336
- Joined: Thu Jul 27, 2017 12:22 am
Re: Struggling with LUA and the basics
Well, as a start point, we should always specify which addon is used when starting a new thread, because those who will help don't necessary get this point in mind
So the aircraft is the Justflight Piper PA-28 Arrow III, if I'm not wrong : https://siminnovations.com/forums/viewt ... 740#p28740
I will come here again this evening with proper script and explanation
So the aircraft is the Justflight Piper PA-28 Arrow III, if I'm not wrong : https://siminnovations.com/forums/viewt ... 740#p28740
I will come here again this evening with proper script and explanation
-
- Posts: 581
- Joined: Mon Sep 07, 2020 8:49 am
Re: Struggling with LUA and the basics
That is the correct aircraft.SimPassion wrote: ↑Mon Oct 03, 2022 10:15 am Well, as a start point, we should always specify which addon is used when starting a new thread, because those who will help don't necessary get this point in mind
So the aircraft is the Justflight Piper PA-28 Arrow III, if I'm not wrong : https://siminnovations.com/forums/viewt ... 740#p28740
I will come here again this evening with proper script and explanation
-
- Posts: 5336
- Joined: Thu Jul 27, 2017 12:22 am
Re: Struggling with LUA and the basics
So, this one
-
- Posts: 5336
- Joined: Thu Jul 27, 2017 12:22 am
Re: Struggling with LUA and the basics
At first I would mention we have to be very careful on readingKaellis991 wrote: ↑Sun Oct 02, 2022 8:18 pm 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
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)
There's two distinct function names : "ap_switch_callback" and "switch_callback"
So, if you watch carefully both screenshots below, it works properly well and straight forward using the snippet I provided above in the thread and I put again below :
Notice, as it's a Justflight, we could rather well use the available thranda dataref (to be tested in any case and arrangement as required) :
xpl_dataref_write("thranda/autopilot/roll", "INT", position)
xpl_dataref_subscribe("thranda/autopilot/roll", "INT",switch_callback)
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)
-
- Posts: 581
- Joined: Mon Sep 07, 2020 8:49 am
Re: Struggling with LUA and the basics
That's the aircraft, but I re-painted mine with a different N number.
So all that was the problem is that my 69 year old eyes are not what they used to be and I just needed to remove the "ap" before switch_callback in the 'hw_switch_add' line.
With that little typo correction, it mostly works as advertised.
Now all I have to do is duplicate that for the HDG switch.
Again, what does the asterisk do? The * between position and the 2 value.
Thanks for your help with this.
So all that was the problem is that my 69 year old eyes are not what they used to be and I just needed to remove the "ap" before switch_callback in the 'hw_switch_add' line.
With that little typo correction, it mostly works as advertised.
Now all I have to do is duplicate that for the HDG switch.
Again, what does the asterisk do? The * between position and the 2 value.
Thanks for your help with this.
Last edited by Kaellis991 on Tue Oct 04, 2022 7:18 am, edited 1 time in total.
-
- Posts: 581
- Joined: Mon Sep 07, 2020 8:49 am
Re: Struggling with LUA and the basics
The thranda/autopilot/hdg dataref is used for the HDG switch.
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, switch_callback)
--Create a new output for HDG Magnet
outp_id_hdg = hw_output_add("HDG Magnet", false)
function hdg_switch_callback(position)
--print("The switch got changed to position " .. position)
hw_output_set(outp_id_hdg, position == 1)
xpl_dataref_write("thranda/autopilot/hdg", "INT", position * 1)
end
hw_switch_add("HDG Switch", 1, hdg_switch_callback) /[code]
Re: Struggling with LUA and the basics
Kirk, If the real aircraft switch can be energised to pull the lever switch on via the electromagnet, and yours is the same unit, then it should work.
I believe you are using 14V. (I don't know why you chose that figure ?) What is the real aircraft using ? - possibly 28V. - or more importantly, what was the part you have designed for ?
If that is the case then you should change the PSU to 24 / 28 V, but check first.
Joe
I believe you are using 14V. (I don't know why you chose that figure ?) What is the real aircraft using ? - possibly 28V. - or more importantly, what was the part you have designed for ?
If that is the case then you should change the PSU to 24 / 28 V, but check first.
Joe
Joe. CISSP, MSc.
-
- Posts: 5336
- Joined: Thu Jul 27, 2017 12:22 am
Re: Struggling with LUA and the basics
Yes the * is used as the multiply sign in "position * 2"Kaellis991 wrote: ↑Mon Oct 03, 2022 10:18 pm .../...
Again, what does the asterisk do? The * between position and the 2 value.
Thanks for your help with this.
-
- Posts: 581
- Joined: Mon Sep 07, 2020 8:49 am
Re: Struggling with LUA and the basics
Hey Joe,jph wrote: ↑Tue Oct 04, 2022 7:21 am Kirk, If the real aircraft switch can be energised to pull the lever switch on via the electromagnet, and yours is the same unit, then it should work.
I believe you are using 14V. (I don't know why you chose that figure ?) What is the real aircraft using ? - possibly 28V. - or more importantly, what was the part you have designed for ?
If that is the case then you should change the PSU to 24 / 28 V, but check first.
Joe
Its not really that important. 14v is the rating for the landing gear light bulbs and I found 14 v also works well enough to keep the switches in the closed position without creating a lot of heat. Using two different voltages just complicates things even more.
Now that the switches are working with the sim, I just have to press each switch. Not a big deal. Im ready to leave well enough alone.
I do have a varible PSU that goes up to 24v. If I recall from many months ago I initally tried that but even that 24v didnt create a strong enough field to extend across the gap. When the magnets were energized at 24v they seemed to warm up quite a bit if I recall.
Edit; perhaps the sim aircaft was designed to have the HDG switch turn on the autopilot switch while the real autocontrol IIIb does not do that. I looked through the autopilot manual and saw no referece to that capability. It could be something that Justflight added to their Arrow aircraft
Last edited by Kaellis991 on Tue Oct 04, 2022 10:50 am, edited 2 times in total.
-
- Posts: 581
- Joined: Mon Sep 07, 2020 8:49 am
Re: Struggling with LUA and the basics
But why? Never have seen that in any code example.SimPassion wrote: ↑Tue Oct 04, 2022 8:09 amYes the * is used as the multiply sign in "position * 2"Kaellis991 wrote: ↑Mon Oct 03, 2022 10:18 pm .../...
Again, what does the asterisk do? The * between position and the 2 value.
Thanks for your help with this.