Howto:Processing route manager data in Nasal

From FlightGear wiki
Jump to navigation Jump to search
This article is a stub. You can help the wiki by expanding it.

Last updated: 01/2012

This tutorial will document all the steps involved in creating a new Nasal module that constantly updates itself to process route manager data. Nasal is FlightGear's scripting language.

This tutorial is based on Howto: Create a new Nasal module, you should fully understand that one first.

A system that constantly processes route manager data could for example be used to create an instrument that shows route manager related data, such as waypoints, time/altitude constraints and so on.

Please keep in mind that Nasal functions that are invoked this way are obviously limited by the frame rate of the simulator (i.e. if your frame rate is 50 fps, you cannot run a Nasal function at higher rates). Update rates of a couple hertz are fine however. Keep in mind though, that the time spent in each Nasal function will obviously also have an effect (on the frame rate, too).

  • create a new plain text file under $FG_ROOT/Nasal/MODULE.nas.
  • replace MODULE with the name for your new module (for example, use "test")
  • paste the following code into this file using a text editor (such as notepad on Windows)
 # test.nas (save in $FG_ROOT/Nasal)
 var wp_sampler = func {
   var active_waypoints=0;
   if (!getprop("/autopilot/route-manager/active")) 
     print("Route manager not running !");
   else  
     active_waypoints = props.globals.getNode("/autopilot/route-manager").getChildren("wp");  
   print("running wp_sampler ", size(active_waypoints)," found");
   settimer(wp_sampler, 3); # register a timer, invoke the "wp_sampler" function again after 3 seconds
 }
 
 _setlistener("/sim/signals/nasal-dir-initialized", wp_sampler);

This piece of code defines a new function named "mycode" that just prints a message to the console and checks if the route manager is running. Next, a timer is registered to invoke the mycode function again after 3 seconds have passed.

The _setlistener call at the end of the snippet waits for the Nasal system to be initialized before the "mycode" function gets called.

  • /autopilot/route-manager/route
  • /instrumentation/gps/wp/leg-distance-nm
  • /instrumentation/gps/wp/leg-mag-course-deg