Trying to understand LUA and Xplane

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Message
Author
Kaellis991
Posts: 581
Joined: Mon Sep 07, 2020 8:49 am

Trying to understand LUA and Xplane

#1 Post by Kaellis991 »

After working for more than a year with Air Manager to build my cockpit, I have received some great help from many, and specifically some great support from Keith.
I am now down to the last couple of pieces with my Autopilot. Although it is almost complete, there are a few loose ends to tie up that I think Keith will help with on discord when he has the time.

Even after a year I still do not have a clear understanding of the process of LUA scripting. I know it seems simple to the experts, but it is still a foreign language to me that is difficult to get a handle on.
I have even viewed every one of Tony's videos and though I kind of understand the ideas, it's still very hazy to me. I've watched video E12-Switch a few times and still cant completely grasp it all. That video includes the development of instruments, which is not what I need to do. I only need the hardware.

To that end there is another part of this autopilot I have left for last. It involves a simple 5 position rotary switch that I think would be a good exercise for me to learn some basics about LUA scripting.

When looking at the examples for switches, specifically the one shown, I have a difficult time of knowing where to start and how to modify the example to work for my situation.
Instead of having someone actually write the code for me, this would seem to be a good hardware creation exercise for learning.
For me to begin understanding it I think I need a step by step explanation of each line of code and what it is accomplishing.

For my situation I have an autopilot mode selector for those 5 settings shown. It's a simple 5 position rotary switch that with each setting will activate the dataref shown in the images.
Depending on the setting, the dataref cycles through the integers 0 to 4 which I think is an array.

So where do I begin based on the Example 1 for switches as shown? How do I take that example and apply it to my rotary switch?
Is this even the best example to start with?


image.png

SimPassion
Posts: 5336
Joined: Thu Jul 27, 2017 12:22 am

Re: Trying to understand LUA and Xplane

#2 Post by SimPassion »

Here's what is done in the Autocontrol IIIB for the auto panel

Code: Select all

---------------------------------------------------------------------------------
--									AUTOCONTROL IIIB - AP TRACK
---------------------------------------------------------------------------------

--	Author						Date			Release		Comments
--	-----------------			----------		-------		------------------------------------------------
--	enjxp_SimPassion			08.31.2020		1.0.1		Fixed config errors for all aircrafts in CSV file
--	JackZ						08.30.2020		1.0.0		Initial Public Release

img_sw_aptrack_bg = img_add_fullscreen("pa28autoctrl3b_navmode_plate.png")

function pr_pos_mgmt(pos)
	switch_set_position(sw_aptrack, pos)
end

function pr_move_mgmt(pos, dir)
	new_pos = pos + dir
	xpl_dataref_write("thranda/autopilot/APKnob", "INT",new_pos)
	-- Called on switch position request
	pr_pos_mgmt(new_pos)
end

sw_aptrack = switch_add("pa28autoctrl3b_navmode_dial_1.png", "pa28autoctrl3b_navmode_dial_2.png", "pa28autoctrl3b_navmode_dial_3.png" , "pa28autoctrl3b_navmode_dial_4.png", "pa28autoctrl3b_navmode_dial_5.png",90, 150, 320, 320, pr_move_mgmt)

switch_set_position(sw_aptrack, 2)

xpl_dataref_subscribe("thranda/autopilot/APKnob", "INT",pr_pos_mgmt)
I will come back to this later, with the clear and step by step explanation requested. I guess not before this upcoming Friday

image.png

Kaellis991
Posts: 581
Joined: Mon Sep 07, 2020 8:49 am

Re: Trying to understand LUA and Xplane

#3 Post by Kaellis991 »

Thanks. Any help is much appreciated.
Its been about 18 months since i started this cockpit build so another week is no problem.

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

Re: Trying to understand LUA and Xplane

#4 Post by Ralph »

I don't think you should start with a virtual instrument if you want to make a hardware instrument.
Did you check out this tutorial?

https://siminnovations.com/wiki/index.p ... n_tutorial

Kaellis991
Posts: 581
Joined: Mon Sep 07, 2020 8:49 am

Re: Trying to understand LUA and Xplane

#5 Post by Kaellis991 »

Ralph wrote: Mon Sep 12, 2022 7:50 am I don't think you should start with a virtual instrument if you want to make a hardware instrument.
Did you check out this tutorial?

https://siminnovations.com/wiki/index.p ... n_tutorial
Ralph,

That's correct. I do not require a virtual instrument. That is an example I never ran across, but it looks like what i need.
I could take that example and start changing terms and the Xplane dataref, and hopefully find my way to a solution.

Kirk

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

Re: Trying to understand LUA and Xplane

#6 Post by Ralph »

What I meant was that it is probably confusing to start with a graphics version, because it has a lot of stuff that you do not need.

Kaellis991
Posts: 581
Joined: Mon Sep 07, 2020 8:49 am

Re: Trying to understand LUA and Xplane

#7 Post by Kaellis991 »

Ralph wrote: Mon Sep 12, 2022 11:12 am What I meant was that it is probably confusing to start with a graphics version, because it has a lot of stuff that you do not need.
That was my first thought when I saw that example for the Autocontrol IIIB. However, I think the graphics / panel elements could be stripped out to pare it down to a hardware version. But that would require some hand holding.

Kaellis991
Posts: 581
Joined: Mon Sep 07, 2020 8:49 am

Re: Trying to understand LUA and Xplane

#8 Post by Kaellis991 »

After reading through the examples for creating the switch hardware, I see many words and sentences in the instructions for which I could use some additional explanation.
As someone with no experience with programming and the terms used, I need some hand-holding to get a layman's understanding of what these words mean in the context of programming.

Here is the Wiki Switch hardware example I am trying to decipher. Can someone elaborate on these questions I have?

1. The use of hw_switch_id= is confusing. Is that optional or not? I think I have seen comments that it is optional but I am not sure where I read that. See additional questions about hw_switch_id below.
2. The sentence... "switch_add is used to add a hardware switch." does not include the hw_ before switch_add as it shows in the previous two lines under description. Is it supposed to be hw_switch_add or simply switch_add?
3. Under Return Value and hw_switch_id there is a sentence "This value can be used for further reference". What does that mean?
4. Further down there is a section called Hardware Id's and a sentence that states "Hardware id's are not preferred, try to use a named hardware instead" is not clear to me. What's the difference between Hardware id's and named hardware? Where does that come into play?
5. Then the next line is also confusing to me. It states "Define the used pin(s) right away. This is Not preferred, since changing of pin assignment can only be done by changing the instrument/panel lua script".
I understand how to change the pin assignment when the script is run and the hardware connection pops up to connect the particular arduino, it's channel, and assign the pins. But that instruction above seems to be contradictory. It says to define the pins, but then don't.
6. The last image showing instructions about "Switches are pull up." What does "pull up" mean and how is that done? All of that instruction is vague to me. What does "...is normally pulled to the I/O voltage..." mean? What is pulled to VOLTAGE and GND and how is that done? What software is being referred to?

Sorry for the elementary school questions but I feel like the instructions are written by programmers who understand what they are writing because of their prior experience.
But with an audience like me with no experience at all, the instructions do not fill in enough of the blanks in my simple inexperienced brain and thus the need for me to find someone to fill them in with simple non-technical terms.

In other words I could use a translator.
Thanks,
Kirk

image.png
image.png
Last edited by Kaellis991 on Wed Sep 14, 2022 4:38 pm, edited 1 time in total.

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: Trying to understand LUA and Xplane

#9 Post by jph »

You will be more confused after reading this.. :)

I'll leave others to answer the main questions as I prefer to use messageport (it is far easier ;) (caveat - for me!)

To me - the whole concept of 'named' and 'id' is completely 'mind blowingly' badly worded and illogical.
imho -
LOGICALLY - 'named' hardware should be just that - Arduino - A - pin1 - pin2 etc etc etc.. that is LOGICALLY what named hardware intimates - well certainly to me.
'give your hardware a name' ................ well, you are then giving it an ID - an identifier that means something to you.
But that is not how it works in AM. it is the opposite way to what I personally would consider logical.
I think they should be referred to as something like - DIRECT ASSIGNMENT and INDIRECT ASSIGNMENT
Where 'direct' means you NAME all the pins and the hardware in your code.
INDIRECT meaning where you would use an ID - say - "Light Switch", tell it the number of switch positions' and it is selectable at a later stage by pulldown menus and you use the ID - that you called it - 'light switch'

But, then you ALSO have an 'id' :shock: as in id = thing / "name / parameters

so you could have

Code: Select all

hw_switch_add("fred",3,callback) 
but if you want to know the position of the switch 'fred' ..... then you have to give it an 'id' as well as the 'name'

so now you need to have something like

Code: Select all

mike = hw_switch_add("fred",3,callback) 
then you can use

Code: Select all

position = hw_switch_get_position(mike) 


to get the position of fred :?
Why not just have a single 'ID' - the NAME ... that you refer to.
so if you have

Code: Select all

hw_switch_add("fred",3,callback) 
you should be just be able to use

Code: Select all

hw_switch_get_position(fred)   
but you cant. -
you need the 'id' (mike) to refer to the 'connected' and 'position' but you need the 'mike' to assign the pins in the pull down boxes

or I am completely missing something which would not surprise me in the slightest.

Nurse ! ??? :D
Joe. CISSP, MSc.

Kaellis991
Posts: 581
Joined: Mon Sep 07, 2020 8:49 am

Re: Trying to understand LUA and Xplane

#10 Post by Kaellis991 »

Nurse?
I need a brain surgeon to put my blown mind back together.

Post Reply