Board index FlightGear Support Interfacing

in-sim 'button press' using .xml protocol booleans  Topic is solved

Connecting two computers, using generic protocol, connecting with Matlab?

Re: in-sim 'button press' using .xml protocol booleans

Postby wlbragg » Tue Jan 05, 2021 6:19 pm

Great, yes, that last example was meant to guide you to how the listener for nasal or fgcommand execution is handled. Basically it is identical to what you already do except you action the property you adjust in a listener.
I too have to see it to wrap my head around it, thus why I posted the protocol stuff. I wasn't sure you had that portion locked down and I needed to include it to tie the listener portion to the protocol ans stream portion. Hopefully that gives you what you need.
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7587
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: in-sim 'button press' using .xml protocol booleans

Postby wkitty42 » Tue Jan 05, 2021 9:38 pm

wlbragg wrote in Tue Jan 05, 2021 4:05 pm:
right, the generic/XML protocol is property driven, so you would indeed also be using listeners to trigger commands

That is not firing or triggering an fgcommand directly from the serial communication stream. That is changing a property on the FG side that is wrapped in a listener and that is what fires/triggers/executes the fgcommand.

that is exactly what i was trying to say... trigger a property from the comms stream and it triggers the fgcommand in the listener... same diff to me ¯\_(-_-)_/¯

certainly not argument worthy and i'm not one for that kinda stuff anyway ;)
"You get more air close to the ground," said Angalo. "I read that in a book. You get lots of air low down, and not much when you go up."
"Why not?" said Gurder.
"Dunno. It's frightened of heights, I guess."
User avatar
wkitty42
 
Posts: 9146
Joined: Fri Feb 20, 2015 4:46 pm
Location: central NC, USA
Callsign: wk42
Version: git next
OS: Kubuntu 20.04

Re: in-sim 'button press' using .xml protocol booleans  

Postby Volador » Sun Jan 10, 2021 4:25 pm

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_Nasal
The ,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 :)
Last edited by Volador on Sat Jul 24, 2021 4:10 pm, edited 2 times in total.
User avatar
Volador
 
Posts: 1140
Joined: Tue Sep 01, 2020 4:58 pm
Callsign: Volador, G-VLDR
Version: 2020.4
OS: Windows 10, 64 bit

Re: in-sim 'button press' using .xml protocol booleans

Postby wlbragg » Sun Jan 10, 2021 5:06 pm

I couldn't have explained that any better, and in fact didn't. That is exactly what I showed you in the examples, but without the detailed instruction. Well done! Now to take it one step farther, you would bind all those pieces together under an fgcommand.

See http://wiki.flightgear.org/Howto:Add_ne ... FlightGear

And specifically see "Subsystem specific fgcommands" for how to bind the nasal portion to an fgcommand.

I would give you an example of this part except I haven't actually done it before. Maybe Hooray has an example handy.
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7587
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: in-sim 'button press' using .xml protocol booleans

Postby Hooray » Sun Jan 10, 2021 5:49 pm

nice walkt-through, you might want to consider adding this to the wiki.
Apart from that, like I mentioned previously, you can automate a fair amount of work here by opening the corresponding XML files via the read_properties() API and procedurally looking up/adding the corresponding bindings.

That way, a single function can procedurally add all bindings by traversing the binding of the cockpit in question - basically, the first step is:

- open the corresponding file io.using read_properties
- get the top-level hash
- and then for each binding set up a listener
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: in-sim 'button press' using .xml protocol booleans

Postby Volador » Sun Jan 10, 2021 6:07 pm

Hi Hooray, thank you, I like to break things down for those who need an explanation. I will look into adding it to the wiki, it took a lot of research and a very kind and patient anonymous user on the PM's to get that point. I kind of get what you mean in theory but I have no idea how to actually do that without an explanation (again just the basics) eg what's a 'read_properties() API'? hence the reason for the original post asking about worked examples. Are you saying there is an even easier way?
User avatar
Volador
 
Posts: 1140
Joined: Tue Sep 01, 2020 4:58 pm
Callsign: Volador, G-VLDR
Version: 2020.4
OS: Windows 10, 64 bit

Re: in-sim 'button press' using .xml protocol booleans

Postby Hooray » Sun Jan 10, 2021 6:14 pm

The process where you described how to locate a file and locate the corresponding binding in that file to hook it up to a listeners can be automated to a fair degree by opening a file from Nasal code and looking for the binding there - i.e. the point is primarily about scaling: if you're hoping to add more than ~10-15 bindings, it would be easier/less work to do this in a semi-automated fashion
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Previous

Return to Interfacing

Who is online

Users browsing this forum: No registered users and 2 guests