Jump to content
Sign in to follow this  
Bina

How can I program a LUA script for my aircraft?

Recommended Posts

Hello there,

 

Thanks to LINDA tracing console window, I was able to derive a set of LVars from one of my favourite aircraft. Here is a short list:

 

cowl_flaps_switch
switch_oil_shut
rudder_trim_switch
aileron_trim_switch
elev_trim_switch
 
They aren't bound to any default FSX controls and are activated by mouse clicking on aircraft switches only, i.e. specific commands hard-coded in one of the aircraft DLLs. So, now I want to assign these commands to specific buttons on my Saitek X52 Pro joystick but I don't know how to accomplish this task. As you can see, I just need a set of code lines to position switches in off / on position using "On Repeat" option for the joystick button. No fancy stuff...
 
I've read the LINDA Manual but it doesn't cover writing LUA scripts. I hope it will in the future! :)
 
So, any help on writing / programming an aircraft-specific LUA file is highly appreciated!!!
 
Thanks!

Regards,


Victor Quebec

Share this post


Link to post
Share on other sites

Hello Vic,

 

i am not a LUA guru but made a few LUA modules for different airplanes. I started more with trial and error. What aircraft are you using maybe i have it to so i could have a look into it too.

 

Anyway i try to give you an example on how to program one. I will use the engine fuel valve of the Bell 212 by Cerasim.

Here we go. First you must define the function name. The function will be displayed on the left side of the editor and in the joystick config later on. In my case "function Engine1_Fuel_Valve_On" In your case -lets take the cowl flap switch as an example- 

"function Cowl_Flaps_Down"  Then you need the command which LVar has to be written when you press the button of the stick. That will be "ipc.writeLvar("cowl_flaps_switch", 1)" The digit "1" will command tthe switch to move in the down position, given that the LVar was programmed that way. Some LVars may work with "-1" or even "2". You will find that out in the tracer.  The end of the write process must be stated with the command "end" below the "ipc.writeLvar" command.

 

Below you see the command to operate the number one engine fuel valve switch of the Bell 212. I will edit this for the cowl flap switch further down below and in addition i will add a toggle command as well.

 

function Engine1_Fuel_Valve_On ()
    ipc.writeLvar("SwvalveEng1", 1)
end
 
function Engine1_Fuel_Valve_Off ()
    ipc.writeLvar("SwvalveEng1", 0)
end
 
 
 
 
 
-- ## Engine Control ##########
 
 
function Cowl_Flaps_Down ()
     ipc.writeLvar("cowl_flaps_switch", 1)
end
 
function Cowl_Flaps_Up ()
     ipc.writeLvar("cowl_flaps_switch", 0)
end
 
function Cowl_Flaps_Toggle ()
if _t("cowl_flaps_switch", 1) then
     Cowl_Flaps_Down ()
else
      Cowl_Flaps_Downf ()
end
end
 
I assume you already made an empty module for aircraft go to the editor copy/paste cowl flap command save it and assign it to a button or a switch and test it in the sim. What you want to do is to divide the different functions with header like above. This should give you an idea in how to program very basic ON/OFF and TOGGLE commands.

Greetz


MJ


 


My youtube blog________________________Prepar3D v2.5/v3


youtubefooter.jpg

Share this post


Link to post
Share on other sites

Wow...thanks for that MJ..

 

I have just got to grips with LINDA....and love it...

 

I have my A2A Cessna 172 programmed so i don't use the mouse at all. ..brilliant...

 

I now want to try my hand at writing simple LUA..

 

May I ask for what aircraft you have written scripts for..

 

Dave

Share this post


Link to post
Share on other sites

Hi Dave,

 

you´re welcome :) I have to say LINDA rocks if one want to go off mouse and keyboard. It gave me a hard time when i started my first module. I did modules for Cerasim Bell 212/412 and the H-60, IRIS SIms C-27 Spartan and the PC 9 which all be available on request.

The last module i did was for MilViz Bell 407 which is in a early state. I have the Thrustmaster Warthog HOTAS System, helicopter flight controls by Komodosim and panels by Saitek.


Greetz


MJ


 


My youtube blog________________________Prepar3D v2.5/v3


youtubefooter.jpg

Share this post


Link to post
Share on other sites

Hey Mickey,

Thanks for your simple language and example!!!

After I posted here, I have spent many hours reading both FSUIPC and LINDA manuals, as well as various forum threads and tutorials trying to understand the differences between the methods to control joystick buttons with FSUIPC and LINDA. Below is what I have "discovered" so far. Please correct me, if I'm wrong and bear with me, if this has been discussed in detail in previous threads. I just want to share my findings and assumptions to collect your tips and recommendations under a single thread and to fill in the gaps in my knowledge of joystick control:

# By default FSX requires neither FSUIPC nor LINDA modules to communicate with joystick controls. This can be achieved through the FSX's built-in control panel reading button and axis assignments from the file Standard.XML located at "%AppData%\Microsoft\FSX\Controls". A backup of this file rests inside the root FSX folder.

# FSUIPC can manage buttons, keypresses, and axis using instructions embedded in FSUIPC.INI while LINDA controls buttons and keypresses ONLY.

# LINDA is a "post-FSUIPC" application meaning both simplicity and extendability but, at the same time, there is still room to manage FSX controls employing 'oldie but goodie' macros and LUA files, as it was before LINDA times, i.e. by means of FSUIPC facilities only. However, as far as I understood from FSUIPC manual and tested on Sibwings Antonov An-2, the use of macros is limited in case of some 3rd-party addons.

# LVars and their parameters for most of the 3rd-party aircraft addons can be extracted by FSUIPC, i.e. without using LINDA's tracing method.

# Button and keypress assignments are contained either in FSUIPC.INI (in case of direct control through FSUIPC interface) or ACTIONS.LUA file individual to each aircraft model (in case of LINDA). Macros and LUA files contain instructions / functions only, being referenced from FSUIPC.INI or ACTIONS.LUA. In other words, all macro instructions / LUA functions can be kept inside a single macro / LUA file instead of writing individual macros / LUA files for each aircraft function and such files can be distributed publicly allowing the users to choose buttons / keypresses dependent on their controlling equipment. So, for each aircraft may be hundreds of LVars (and functions) depending on complexity of that particular aircraft model but it's the user who decides which of these functions to assign a button or a keypress to depending on his/her equipment limitations.

 

Now, I have a couple of more questions still unclear to me:

  • LUA files can contain one or more functions, as also shown in your example above. But if I place one such LUA file inside Modules folder, how am I supposed to bind functions contained therein to buttons / keypresses without using a macro or LINDA? The problem is it seems the Sibwings Antonov An-2 that I'm currently using doesn't allow the use of macros, if it's ever possible... I can use macros on other 3rd-party aircraft though.
  • Is it possible to write functions inside macros the same way as in LUA files?
  • Is the syntax of LUA functions used in LINDA aircraft profiles shipped with the application compatible with functions used in .LUA files for FSUIPC?
  • Can you please clarify this code a bit?:
function Cowl_Flaps_Toggle ()
    if _t("cowl_flaps_switch", 1) then
        Cowl_Flaps_Down ()
    else
        Cowl_Flaps_Downf ()
    end
end

I assume "Cowl_Flaps_Downf" is a typo and must be "Cowl_Flaps_Up". And what does that underscore before the local variable t mean?

 

Thank you!


Regards,


Victor Quebec

Share this post


Link to post
Share on other sites

G´morning Vic,

 

i hope i can answer all your questions. First to say i dont have any experience in advanced FSUIPC Macro programming neither in LUA script. What i did was trying to understand how LINDA modules work and copy and paste the commands combined it with the LVars and checked if they work. The LINDA tracer is/was a very good tool to understand things.

 

Now to confirm you discoveries.

 

# that is correct FSX/P3D does not need FSUIPC nor LINDA. 

 

# not only that with FSUIPC you can make individual profiles for different aircraft. Either just button/switch assignments as well as axis settings and behaviours where LINDA is just for button/switches assignment and LVar programming. 

 

# i dont have any experience with Macros i never wrote one. But you can assign buttons with FSUIPC and LINDA at the same time given that you dont want to double assign a button. For ex i have some buttons/switches assigned via FSUIPC with standard FSX/P3D commands as well as switches that operates the models specific LVars via LINDA.

 

# i never did that nor i dont know how to do that guess you are a more advanced FSUIPC user than i am :)

 

# i dont know how FSUIPC and LINDA interact with each other but basically you understand the processes correct. With FSUIPC you can assign only one command to one switch (if i understand that correct) with LINDA you can program a sequence of LVars and assign this to a single button/switch. For ex the Cerasim H-60. The chopper has a lot of switches to press till you can start it. My TM Warthog throttle quadrant is limited in switches but with LINDA i was able to operate 5 LVar commands with on single switch. I know that this will be way of the SOPs according to the Manual but i can start without keyboard and mouse.

 

example from the H-60 LINDA module. One function/button assignment 6 LVar write commands.

 

-- ## AFCS ##########
 
function AFCS_On ()
    ipc.writeLvar("acstab", 1)
    ipc.writeLvar("sas1", 0)
    ipc.writeLvar("sas2", 1)
    ipc.writeLvar("trim", 1)
    ipc.writeLvar("fps", 1)
    ipc.writeLvar("boost", 1)
end
 
function AFCS_Off ()
    ipc.writeLvar("acstab", 0)
    ipc.writeLvar("sas1", 1)
    ipc.writeLvar("sas2", 0)
    ipc.writeLvar("trim", 0)
    ipc.writeLvar("fps", 0)
    ipc.writeLvar("boost", 0)
end
 
 

 

In short LINDA has its own file structure within the standard modules of FSX/P3D so there is no need to put the files you made with LINDA somewhere else. Once you made a LINDA profile for you aircraft and assigned buttons and switches LINDA will automatically detect the aircraft upon loading. Therefore LINDA must be running along with the sim. LINDA is supposed to run that way.

 

I can not  explain the code by itself nor i dont know what the underscore in front of the "t" stands for. As i said i copy/paste the commands from other modules. I wish i had a book that tells me what command will be used for different functions and how to combine them. But i guess we have to stick with trial and error and support each other. Anyway LINDA is pretty much straight forward. If you write a syntax error LINDA will tell you. And she likes and knows case sensitive. Oh and sorry for the typo


Greetz


MJ


 


My youtube blog________________________Prepar3D v2.5/v3


youtubefooter.jpg

Share this post


Link to post
Share on other sites

Thanks for your time, Mickey!!!

 

 

 

example from the H-60 LINDA module. One function/button assignment 6 LVar write commands.

 

It's something new for me. Very good point - to assign several functions to one button. Should help with autostart. for example. Not realistic though...

 

 

 

And she likes and knows case sensitive.

 

Good to know this as well. Thanks!

 

 

 

But i guess we have to stick with trial and error and support each other.

 

Sure, no problem! I'll visit this thread occasionally to share smth new along my learning path and, hopefully, learn new things as well... :)

 

Take care!


Regards,


Victor Quebec

Share this post


Link to post
Share on other sites

Hi @Bina, I am pleased that @Mickeyj has managed to help you with most of your questions.  This is what the LINDA community is for - self-help.  I have been occupied elsewhere but I wanted to answer the few remaining questions.

 

LINDA was designed to make life easier for programming buttons and keys on joysticks and especially the VRI Combo MCP panels.  It allows specific modules of complex functions for each aircraft to be quickly assigned to buttons.  There is an extensive library written in the LUA programming language of aircraft modules available relying on a SDK and LVars made available by particular aircraft developers.  

 

FSUIPC4 is the excellent and widely used software used to interface with FSX, FSX:SE and P3D.  LINDA provides another level of interface on top of FSUIPC4 accepting key/buttons presses from FSUIPC4 and outputting the defined functions via FSUIPC4 to the particular Flt Sim.  FSUIPC4 provides all axis assignment and calibration and is better for rapidly repeating key operations (ie. thrust reverse).

 

FSUIPC4 is responsible for starting LINDA.exe and the LINDA Graphical User Interface (GUI).  For the selected aircraft LINDA runs the actions.lua module for that aircraft (/fsx/modules/linda/aircrafts) along with various system modules (fsx/modules/linda/system).  The functions assigned to the various devices (joysticks) for the aircraft are defined in several configuration files (fsx/modules/linda-cfg/aircrafts).

 

With the exception of the joystick HAT functions, it is recommended that all other device/joystick button assignments are deleted in the Flt Sim to avoid confusing interactions between the Flt Sim, FSUIPC4 and LINDA.  Any require key/button functions should only appear once. 

 

Apart from the core code (linda.exe), which I have been granted access to and am supporting, all the LUA code is open source allowing users to examine and modify.  However, the code is copyrighted and may not be used or reused for commercial purposes.  There is plenty of online documentation about the LUA code and language.  Users are not recommended to modify the core LINDA system files or the aircraft module.  Users can write there own functions and place them in either the general lib-user.lua file (fsx/modules/linda/lib) or the aircraft specific user.lua file (fsx/modules/linda/aircrafts).  The important point is that functions in these files will override functions of the same name in the aircraft and then in the system code files.

 

To answer you nagging question.  Function names are case sensitive and can be made up of alphanumeric characters, dashes (-) and underscores (_). The _t is a test function which if the first part is equal to the second part then the condition is true.  In your example, if _t("cowl_flaps_switch",1) then is simply testing that the LVar cowl_flaps_switch is set to 1.

 

I hope this helps add to your knowledge.


Andrew Gransden

Scotland, UK

LINDA Support/Developer - VATSIM and BAVirtual - Airbus Flyer

i7 1TB SSD GTX980 - FSX/P3D - Aerosoft Airbus A318/A319/A320/A321 - FS2Crew

Share this post


Link to post
Share on other sites

Scot,

 

Thank you very much for your informative reply!!!

 

Sorry for late reply - was busy updating (!) my first-ever LUA script for SibWings An-2. :)

 

All the best!!!


Regards,


Victor Quebec

Share this post


Link to post
Share on other sites

 

# i dont know how FSUIPC and LINDA interact with each other but basically you understand the processes correct. With FSUIPC you can assign only one command to one switch (if i understand that correct) with LINDA you can program a sequence of LVars and assign this to a single button/switch. For ex the Cerasim H-60. The chopper has a lot of switches to press till you can start it. My TM Warthog throttle quadrant is limited in switches but with LINDA i was able to operate 5 LVar commands with on single switch. I know that this will be way of the SOPs according to the Manual but i can start without keyboard and mouse.

 

 

Please forgive me for butting in. I was also under the impression that only one button/key/command could be assigned at one time and it was driving me crazy. I didn't know diddly squat about Lua (just getting my feet wet now), but programming experience told me that a script could accomplish a great deal if I could learn to program it. However, one day, I tried something in desperation, and it worked!

Here's a snippet of what I did in FSUIPC:

 

[buttons.DC-3 HSI]
0=PA,10,C65907,0 ;display GPS window
1=PA,9,C65914,0 ;display radio stack
2=PA,7,C65580,0 ;Autopilot Master
3=PA,6,C65726,0 ;Autopilot Altitude Hold On
4=PA,6,C1017,0 ;incr alt on Ap fast (+1000)
5=PA,6,C1017,0 ;incr alt on Ap fast (+1000)
 
As you can see, these button assignments are aircraft specific - more about why in a minute - an you can see that the last three commands/events are all done on the same button press on my joystick.
 
Obviously, with this approach, there are still significant limitations - full-blown scripting/programming would be more effective and probably more eloquent, but sometime's you just gotta use what ya got!
 
I used this approach in a couple of different planes rather than make the buttons "global" because, on at least one of my planes' autopilot operations, when I activate the autopilot, the HDG HOLD comes on as well. On other planes, however, it doesn't, so I had to make adjustments accordingly.
 
With the above instructions, the following happens - (1) the autopilot gets turned on and - automatically, whether I want it or not - the HDG HOLD ON lights up as well. Now, if I take my hand off the joystick, at least I'm not going to wander all over the landscape. Then, immediately following (2) the ALTITUDE HOLD comes on. If I don't do anything, the altitude hold suddenly becomes set at whatever height I'm currently displaying and climb will quickly stop. Not good. So, (3) I add 1,000 feet (twice) to the current altitude setting and I've got a couple thousand feet to get the gear up, flaps in and set the altitude to a high enough number that I won't suddenly stop climbing.
 
If there's a more elegant way to do this, I'm all ears, but I've found this works quite well for me. It might be slightly unrealistic, but, while holding the joystick with my right hand *and* trying to manipulate the mouse to click appropriately on the autopilot....
 
I haven't had the guts yet to remove all the buttons/keys from the "standard" FSX setup and rely exclusively on FSUIPC (or anything else), but I do have a keyboard encoder built into a home-brew switchbox and the great majority of the switches are programmed as if they strokes keystrokes, thanks to FSUIPC.
 
Last and certainly not least, I learned some good info on your post. Still have a lot to learn but sometimes I just give up and go flying! I've learned a lot from a lot of other people - love forums!
 
Art - N4PJ
Leesburg, FL

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Tom Allensworth,
    Founder of AVSIM Online


  • Flight Simulation's Premier Resource!

    AVSIM is a free service to the flight simulation community. AVSIM is staffed completely by volunteers and all funds donated to AVSIM go directly back to supporting the community. Your donation here helps to pay our bandwidth costs, emergency funding, and other general costs that crop up from time to time. Thank you for your support!

    Click here for more information and to see all donations year to date.
×
×
  • Create New...