Board index FlightGear Development Nasal

How would I go about controlling an airplane with nasal?

Nasal is the scripting language of FlightGear.

How would I go about controlling an airplane with nasal?

Postby akh2103 » Tue Sep 17, 2013 9:38 pm

I am new to flight gear. I am interested in modifying flight gear for a school project. I know that the nasal language is very powerful. Is it possible to control (specifically change the pitch, roll and yaw) of an airplane using nasal? If so, what portion of the code should I start to examine? Thanks so much.
akh2103
 
Posts: 7
Joined: Sat Sep 14, 2013 10:10 pm

Re: How would I go about controlling an airplane with nasal?

Postby Hooray » Wed Sep 18, 2013 5:29 am

Hi & welcome !
Regarding your question, please see below, and make sure to follow the various links included:

Subject: Possibility to run a fully automatized mission ?

Hooray wrote:
Is it possible to make a complete automatized mission, from engine start to engine shutdown ?


Yes, it is possible "to make" such a mission - but you will literally have to MAKE it by writing a script to outline all required steps for your aircraft.
Curt did that a while back for the f14b, which did a fully automated carrier approach using just Nasal scripting:

http://diydrones.com/profiles/blogs/uas ... simulation
http://www.mail-archive.com/flightgear- ... 33987.html
http://www.flightgear.org/forums/viewto ... =4&t=13615


http://www.flightgear.org/tours/carrier-ops/

Another example is the "tanker.nas" script in $FG_ROOT which implements a simple scripted AI tanker for AAR purposes: search.php?st=0&sk=t&sd=d&sr=posts&keywords=tanker.nas
http://www.mail-archive.com/search?q=ta ... eforge.net

And then we have the fox2.nas script which implements a fox2 AI missile using Nasal: search.php?st=0&sk=t&sd=d&sr=posts&keywords=fox2.nas

The "bombable" addon is completely implemented in Nasal and created multiple virtual pilots for dogfighting purposes: http://wiki.flightgear.org/Bombable

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: 11354
Joined: Tue Mar 25, 2008 8:40 am

Re: How would I go about controlling an airplane with nasal?

Postby akh2103 » Thu Sep 19, 2013 2:56 am

Thanks! I see that in the code for the f-14b (located in aircraft/f-14b/nasal/fox2.nas) -- lines 5 and 6 get the pitch and roll of the aircraft from the property tree.

Code: Select all
var OurRoll        = props.globals.getNode("orientation/roll-deg");
var OurPitch       = props.globals.getNode("orientation/pitch-deg");


Is it possible to set properties on the property tree at runtime with nasal? Something sort of like...

Code: Select all
while (flying)
      props.globals.setNode("orientation/roll-deg") = props.globals.getNode("orientation/roll-deg") + 3   // add three degrees to roll per iteration
      props.globals.setNode("orientation/pitch-deg") = props.globals.getNode("orientation/pitch-deg") + 3  //add three degrees to pitch per iteration
end while

Thanks for your help. I'm happy to add info from this thread to the wiki once I understand what's going on.
akh2103
 
Posts: 7
Joined: Sat Sep 14, 2013 10:10 pm

Re: How would I go about controlling an airplane with nasal?

Postby Hooray » Thu Sep 19, 2013 5:49 am

I'd suggest to get started by looking at the tanker.nas script instead - fox2.nas, the f14b demo and the bombable script are much more sophisticated, while tanker.nas is really pretty simple and straightforward: https://gitorious.org/fg/fgdata/source/ ... tanker.nas

Regarding your question, you can usually directly use setprop() here: http://wiki.flightgear.org/List_of_Nasa ... prop.28.29
The props.nas APIs are OOP and can greatly simplify working with property tree data, but are not really required.

Thanks for your help. I'm happy to add info from this thread to the wiki once I understand what's going on.

That would be great - but you'll probably want to look at tanker.nas for now, and look up the various APIs used there (see the wiki).
The other examples are a little more involved.

Also, it will be important if you are going to control the main aircraft, or some form of AI aircraft - because properties may differ then ...
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: 11354
Joined: Tue Mar 25, 2008 8:40 am

Re: How would I go about controlling an airplane with nasal?

Postby akh2103 » Thu Sep 19, 2013 8:18 pm

I took a look at the tanker script. I also took a look at the link you provided showing how to set properties in the property tree using nasal. Both of those things clear up some of my questions. Thanks!

Now I'm confused about how nasal links up with the main flight gear code base. To use an analogy, I understand how javascript works within the context of HTML, CSS and a web browser. What's the equivalent for NASAL? Where can I go (either within the documentation or the codebase) to understand how the main flight gear codebase interacts with NASAL?

I am interested in controlling a main aircraft -- not in AI for an external airplane.
akh2103
 
Posts: 7
Joined: Sat Sep 14, 2013 10:10 pm

Re: How would I go about controlling an airplane with nasal?

Postby Hooray » Fri Sep 20, 2013 7:07 am

that's pretty simple and straightforward, you can simply look at the autopilot dialog of your aircraft (e.g. c172p, b1900d or 777) - sometimes, looking at the cockpit panel may also make sense (MCP) - otherwise, you can then simply use the autopilot system to control the aircraft, which also includes support for flying complete routes via the built-in route manager, for details, see:

Image
Image
http://wiki.flightgear.org/Autopilot

These are just XML files (GUI dialogs in $FG_ROOT/gui/dialogs or somewhere in $FG_AIRCRAFT - if customized) - so you can simply open those files and look up the properties for the various autopilot modes that you want to use (heading hold, wings level, altitude hold etc).

Equally, the route manager is well-documented:
http://wiki.flightgear.org/Route_Manager
http://wiki.flightgear.org/Route_manager_internals

So, basically, you really only need to set properties via setprop/prop.nas to control the route manager and/or autopilot, which will in turn allow you to control the main aircraft - this is much more straightforward than controlling AI traffic, because all these hooks aren't currently exposed yet.

Now I'm confused about how nasal links up with the main flight gear code base. To use an analogy, I understand how javascript works within the context of HTML, CSS and a web browser. What's the equivalent for NASAL? Where can I go (either within the documentation or the codebase) to understand how the main flight gear codebase interacts with NASAL?


For starters, I'd suggest to read through these:
http://wiki.flightgear.org/What_is_Nasal
http://wiki.flightgear.org/Nasal_for_C% ... rogrammers

You'll find a few analogies in those articles - but basically, Nasal scripting works exactly like JavaScript in your browser: it's an embedded extension language, that is integrated into FlightGear as a conventional FlightGear subsystem - during each frame, the Nasal interpreter gets its time slice to process scripts - which is why badly written scripts may slow down the sim and affect frame rate/frame spacing - in addition, Nasal supports dozens of FG-specific extension functions to do various FG-related things - which allows callbacks to be registered in the form of property listeners (which get invoked whenever a property is written to) or via timers (that may expire and trigger a callback). In addition, there's a standard library of Nasal library functions and a plethora of FlightGear-specific modules in $FG_ROOT/Nasal that greatly simplify coding stuff from scratch.

To learn more about timers, see: http://wiki.flightgear.org/List_of_Nasa ... imer.28.29
http://wiki.flightgear.org/List_of_Nasa ... 2.11.2B.29

To learn more about listeners, see: http://wiki.flightgear.org/Using_listen ... with_Nasal

You can start playing around with Nasal by using the so called "Nasal Console": http://wiki.flightgear.org/Nasal_Console
Image

Where can I go (either within the documentation or the codebase) to understand how the main flight gear codebase interacts with NASAL?

the nitty-gritty details are in $FG_SRC/Scripting: https://gitorious.org/fg/flightgear/sou ... salSys.cxx

To understand the subsystem (SGSubsystem) design of FG (which is how Nasal is integrated), see: http://wiki.flightgear.org/Howto:Create_new_subsystems
You'll want to be especially nice to Philosopher, because he can basically walk you through the whole thing within 5-10 minutes :D

Basically, you'll find both, Philosopher and myself, incredibly helpful if you should start contributing your findings to the wiki, so that our docs are improved subsequently - we can help you fill in any gaps and missing stuff/details
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: 11354
Joined: Tue Mar 25, 2008 8:40 am

Re: How would I go about controlling an airplane with nasal?

Postby Philosopher » Fri Sep 20, 2013 1:08 pm

Two things I'd like to say: first, as Hooray suggests, you will mainly use the autopilot to control the aircraft (since it has a real FDM it's kinda hard to control), and we would definitely recommend one with a developed autopilot (the 777s are really excellent with everything and has a good autopilot with all features you want). I don't fly in aircraft a lot, and like only three times with an autopilot, so I can't recommend any other aircraft with good autopilots. But one option with the 777 is that you could potentially use Nasal to dynamically edit routes that the aircraft just flies – not much work there. Another approach would be manually adjusting the autopilot parameters (like heading hold, bank angle, and stuff like that) through Nasal feedback algorithms for aircraft that don't have a.route manager. You could also couple this with the XML autopilot (now called "property rule") system to have real PIDs, easy lowpass, etc.

The other thing is that there are two aspects to Nasal: that of interaction with itself and with the property tree. For your purposes, the property tree is going to be your communication with the wider FlightGear code. Nasal also can use its own mechanisms to interact with different things (file system, route manager, fgcommands, ...). In interest of the web browser analogy, XML in FlightGear is like HTML. Specifically it is responsible for setting up much of the property tree that FlightGear's subsystems act off of. David Megginson (creator of FG's property tree) once said that "XML should provide the nouns, Nasal should be the verbs" (or something, that isn't a direct quote) – so PropertyList XML is really good at providing data that Nasal can work off of. For like the fox2.nas script, this means that ideally all of the specific data about the missile would be taken from Nasal and stored in an XML file (which would be loaded by the Nasal file and the data would be copied over).
Thanks,
Philosopher
(inactive but lurking occasionally...)
Philosopher
 
Posts: 1590
Joined: Sun Aug 12, 2012 6:29 pm
Location: Stuck in my head...
Callsign: AFTI
Version: Git
OS: Mac OS X 10.7.5

Re: How would I go about controlling an airplane with nasal?

Postby Philosopher » Fri Sep 20, 2013 1:18 pm

Sorry guys, I've been having airline-loads of trouble posting with the buggy software introduced by using updates on my old iOS version, but regarding the first point, Mr. Olson posted on his blog this article about circle holding which will be of interest: http://gallinazo.flightgear.org/uas/spi ... r-control/. If he isn't too busy, you could ask him about some more of the details or ask where to learn about the algorithms he used. I don't have time to reread it, but I think he used like the rascal or other small aircraft and a Nasal algorithm consisting of (I'm just guessing here) bank hold (to set up an approximate radius and to turn in a circle) and bank angle correction (to get the right radius, to stay on track and correct errors, etc.). He also mentions a lot of control mixing in the article required to maintain the fine balance of throttle/speed, bank angle/turn radius, and pitching/altitude, so it was probably more sophisticated than that.
Thanks,
Philosopher
(inactive but lurking occasionally...)
Philosopher
 
Posts: 1590
Joined: Sun Aug 12, 2012 6:29 pm
Location: Stuck in my head...
Callsign: AFTI
Version: Git
OS: Mac OS X 10.7.5

Re: How would I go about controlling an airplane with nasal?

Postby Hooray » Fri Sep 20, 2013 6:38 pm

Actually, the algorithmic side of things should be fairly straightforward in this case, because it's all hidden in the depths of the AP/RM systems, so from a high-level standpoint, it's really nothing else than using setprop() to switch between AP/RM modes and then setting the corresponding parameters (altitude, speeds, heading, course etc).

You could even fly a holding pattern automatically like this, without ever having to do any fancy maths.
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: 11354
Joined: Tue Mar 25, 2008 8:40 am


Return to Nasal

Who is online

Users browsing this forum: No registered users and 2 guests