SOLUTION
Worked example for pressing the FLCH button (or any cockpit control) in the 7478i to trigger it from an arduino with generic protocol .xml over serial.
Discalimer: Reading the wiki and forum only helped me a little so I've had a lot of help to establish this process from an anonymous person who knows a lot more than me, if you've got technical/coding questions there's a good chance I won't know the answers. The following assumes you're familiar with sending data from your arduino to property values in the property tree and using xml generic protocol to receive data from the arduino.
OK, Geardown boolean is easy from the arduino, either write 0 or 1 to the gear/geardown property right? But what about pressing the FLCH button? This actually triggers lots of height and thrust calculations and changes some properties over time, so it's not just a case of changing a boolean - but it could be...
Solution: We create a new property for the FLCH-button (or as many buttons as we like) in the property tree called eg:
inputs/arduino/ap/flch-but
We 'listen' to this new property and trigger the code associated when it changes from 0 to 1 - which we send from the arduino and our push button on our interface. The code we trigger is buried somewhere we just need to find it in order to point to it.
How do we do all that?
Adding a new property to the 747 property tree:
The file called 747-8-main.xml contains a tag for <nasal>. Just before the closing tag </nasal> we're going to add our own reference to a new property tree definition file (that we'll create in the next step).
This follows line 569 in this example
<arduino-control>
<file>Aircraft/747-8i/Nasal/arduino-747.nas</file>
</arduino-control>
Save your changes to 747-8-main.xml
Next step, we create the new file called arduino-747.nas and save it into /fgfs/fgaddon/Aircraft/747-8i/Nasal (or wherever the equivalent aircraft/nasal folder location is on your system). Inside this new arduino-747.nas file we're going to add a listener and a new branch on the tree called:
input/arduino/ap/flch-but
and a reference to the FLCH code we have to trigger.
- Code: Select all
# simple listener script
setlistener("input/arduino/ap/flch-but", func(state)
{
if(state.getBoolValue())
{
# this is the code that is triggered on FLCH press
Boeing747.afds.input(1,8);
}
else
{ }
},1,0);
Save this arduino-747.nas file in the nasal folder for the aircraft.
For the full syntax of setlistener() have a look at
http://wiki.flightgear.org/Using_listen ... with_NasalThe ,0,1); refers to <startup> and <runtime> which are handy to know about depending on what cockpit button you're wanting to press.
The key piece of code here is Boeing747.afds.input(1,8);
But how do we discover this for this particular FLCH button? We need to hunt down the FLCH button.
Cockpit / Models for the aircraft (7478i in this case) are the best place to look because clicking on something on the screen is the primary interface;
Using an editor like "Visual Studio Code" (on windows) will allow you to instantly scan through loads of files (eg, open folder /fgfs/fgaddon/Aircraft/747-8i) to search for words (strings) that appear in any file within that folder - saves a heap of time.
So, opening and searching in folder /fgaddon/Aircraft/747-8i for 'FLCH' reveals quite a few hits.
Check on Cockpit or the Model files first because they *have* to specify clickable areas.
Open /fgfs/fgaddon/Aircraft/747-8i/Models/Cockpit/glareshield.xml
in an editor (like "Visual Studio Code") and, again, search on 'FLCH'; find at line #1095 the <pick> animation for FLCH items calling for the action to be a nasal command:
controls.click(4);Boeing747.afds.input(1,8);
Try going to fgdata/Nasal/controls.nas to see what click(4) does:
fgdata/Nasal/controls.nas Hmm, think this may be the button click sound, so let's continue.
The other command: Boeing747.afds.input(1,8); is easier: the Boeing747 prefix come from 747-8-main.xml at line #526 the AFDS.nas file and is inside the <Boeing747> tags.
Looking in 747-8/Nasal AFDS.nas get to line #129 input(mode, button) so if we choose we can follow the code to see what's being done. There's actually not much need to go that far into the code, we've already seen that calling Boeing747.afds.input(1,8); in your listener should produce exactly the same effect as clicking 'FLCH' in sim.
For other on screen buttons or functions simply start from the clickable animation hotspots usually in the Models subfolder.
For each function you want to trigger, place it in in your new arduino.nas file like the example above eg you could add the VS button using the same process.
- Code: Select all
setlistener("input/arduino/ap/vs-but", func(state)
{
if(state.getBoolValue())
{
# VS Vertical speed button
Boeing747.afds.input(1,2);
}
else {}
},1,0);
Open up FG and display the property tree, you should see your new tree structure inputs/arduino/ap/flch-but - change the value and it should work the same as pressing FLCH.
Point to note: When writing a 1 from the arduino to 'press' this FLCH button you'll need to write a 0 first, so the arduino sends 0 then 1 in sequence (easy to code and fast to transmit). I did this like this where SendData() is the void that transmits (prints) all my other property values in a stream.
Section of arduino code:
- Code: Select all
FlchBut.poll(); //change the state
if(FlchBut.pushed())
{
flchbut = false;
SendData();
flchbut = true;
SendData();
}
Thanks again to one particular person behind the scenes (you know who you are) who explained this process to me. I hope it helps you if you're working on an interface and want to press any button in the cockpit and let FG do the rest.
Volador