Board index FlightGear Development Nasal

Elapsed time after each local_weather.nas call  Topic is solved

Nasal is the scripting language of FlightGear.

Elapsed time after each local_weather.nas call

Postby gierschi@flightgear » Tue Jul 19, 2016 3:41 pm

Hey guys,

What is the frequency the nasal scripts are calling during running FG (like local_weather.nas)?

Best regads,
Sebastian
gierschi@flightgear
 
Posts: 58
Joined: Tue Nov 03, 2015 10:40 pm

Re: Elapsed time after each local_weather.nas call

Postby Thorsten » Tue Jul 19, 2016 6:03 pm

Whatever the loop constant says - there's no one single rule.

Often scripts are called at framerate, though it's possible to update them faster (by using a listener to an FDM-rate updated property) or slower (by specifying a non-zero loop constant).
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: Elapsed time after each local_weather.nas call

Postby Hooray » Tue Jul 19, 2016 7:41 pm

It would be a much better idea to tell us exactly why you are asking that question - the concept of time in FlightGear is massively convoluted, overloaded and even broken in some parts - so if this has to do with benchmarking or determining performance bottlenecks, this may be very misleading actually

The Nasal code in the LW/AW system will usually "just" call a few property tree APIs (setprop, getprop, listeners, timers) and otherwise use a handful fgcommands
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: Elapsed time after each local_weather.nas call

Postby gierschi@flightgear » Tue Jul 19, 2016 9:01 pm

I am trying to implement external turbulent wind data into FG using Nasal. This approach seems to me being a more comfortable way of doing it than to manipulate the source code and compile FG each time when I want to check changes in the source code (I know only how to compile all source code files (this last roughly 10 minutes) and not how to check just few c++ changes in one file as fast as possible).

Until now I have read the data with the help of nasal and store everything in a 1 dimensional array (unfortunately it is not possible/so easy to define multidimensional array like 257 gridpoints x 257 gridpoints x130 gridpoints with nasal). Subsequently, I will determine the new position of the aircraft in the implemented turbulent wind field every time the local_weather.nas script is calling and do a trilinear interpolation of the wind to the aircraft position. I recognized that FG uses JSBSim (at least for some aircrafts) to calculate the wind dependent flight attitude. Also the turbulent fluctuations are calculating in this FDM. I know that JSBSim runs with 120Hz. Therfore, I want to realize with Nasal a determination of the wind at the aircraft position in the same time interval JSBSim and therefore the flight dynamic calculations are calling and commit the calculated wind from the nasal script to the right position in the JSBSim model. Of course, i have to prevent JSBSim from using his own turbulent wind data.

I hope, that was understandable
gierschi@flightgear
 
Posts: 58
Joined: Tue Nov 03, 2015 10:40 pm

Re: Elapsed time after each local_weather.nas call

Postby Thorsten » Wed Jul 20, 2016 6:38 am

unfortunately it is not possible/so easy to define multidimensional array like 257 gridpoints x 257 gridpoints x130 gridpoints with nasal


You mean you get into memory issues or that the data structure doesn't work for you (because Nasal sure can do arrays of arrays - or arrays of hashes - or any combination)?

Therfore, I want to realize with Nasal a determination of the wind at the aircraft position in the same time interval JSBSim and therefore the flight dynamic calculations are calling and commit the calculated wind from the nasal script to the right position in the JSBSim.


Basically the only way to achieve this is to tie a listener to a JSBSim property which calls the Nasal update routine - inserting into any place into AW is not going to work because that never runs faster than framerate (and utilizes the JSBSim turbulence field).
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: Elapsed time after each local_weather.nas call

Postby gierschi@flightgear » Wed Jul 20, 2016 1:33 pm

You mean you get into memory issues or that the data structure doesn't work for you (because Nasal sure can do arrays of arrays - or arrays of hashes - or any combination)?


No, i don't get into memory issues. The problem is how the declaration of an array works in Nasal. Let's assume we want to assign a 1D vector which consists of the wind input data in a file (one value per row)

Code: Select all
u_palm  = split("\n", io.readfile("filname of .txt data"));


to a 2D array (Dimension:257x257, that means 257 y values and 257 x values). First we create a 1D array

Code: Select all
var dummy1 = setsize([], dimensionx);


According to my knowledge you can create a 2D structure just by setting

Code: Select all
var 2Dfield = [dummy1]


In the next step you can assign all u_palm values to the variable 2Dfield (at least for ygridpoint=0 and xgridpoint=[0-256]) doing

Code: Select all
for (var i = 0; i < dimx; i += 1){
              u[0][i] = u_palm[i];
};


The problem already appears for the second dimension. The loop shall look like this:

Code: Select all
var n = 0
for (var j = 0; j < dimy; j += 1){

      for (var i = 0; i < dimx; i += 1){
           u[j][i] = u_palm[n];
           n = n + 1;
       };
};


But j can't be larger than 0 because you have just declare the vector dummy1. So what you have to do is to declare 257 vectors like

Code: Select all
var dummy1 = setsize([], dimensionx);
var dummy1 = setsize([], dimensionx);
var dummy2 = setsize([], dimensionx);
var dummy3 = setsize([], dimensionx);
...


Afterwards, you can set your 2D structure via

Code: Select all
var 2Dfield = [dummy1,dummy2,dummy3,....,dummy257]


Thats not practicable, especially not if the dimension change with a new implemented wind field. Is there any other possibility to declare a 2D, better 3D field so that you can assign the velocities in three for loops like:

Code: Select all
for (var k = 0; k < 130; k += 1){
                     
         for (var j = 0; j < 257; j += 1){
         
              for (var i = 0; i < 257; i += 1){
                   u[k][j][i] = u_palm[n];
                   n = n + 1;
              };
         };
};


If not, i will try to do everything with a 1 D array of a dimension 257 x 257 x 130.

Basically the only way to achieve this is to tie a listener to a JSBSim property which calls the Nasal update routine - inserting into any place into AW is not going to work because that never runs faster than framerate (and utilizes the JSBSim turbulence field).


I do not really understand the concept of listeners but i inform myself at the following pages: http://wiki.flightgear.org/Using_listeners_and_signals_with_Nasal and http://wiki.flightgear.org/Developing_and_debugging_Nasal_code#Managing_timers_and_listeners
The text on this pages says:
Unfortunately, listeners don't work on so-called "tied" properties when the node value isn't set via property methods. [..] Most of the FDM properties are "tied", and a few in other subsystems.
So is it possible to use the setlistener concept with, e.g., the "/fdm/jsbsim/atmosphere/turb-north-fps" property?

AW is not going to work because that never runs faster than framerate


Am i write, that the Nasal scripts aren't calling in a steady time interval (even when you have a steady framerate) like the FDM which runs with 120Hz?
gierschi@flightgear
 
Posts: 58
Joined: Tue Nov 03, 2015 10:40 pm

Re: Elapsed time after each local_weather.nas call

Postby gierschi@flightgear » Wed Jul 20, 2016 1:47 pm

which calls the Nasal update routine


Another idea would be defining a new function in the local_weather.nas script which overwrites the velocitiy properties in JSBSim to the values in the implemented wind field, wouldn't it?
gierschi@flightgear
 
Posts: 58
Joined: Tue Nov 03, 2015 10:40 pm

Re: Elapsed time after each local_weather.nas call

Postby Thorsten » Wed Jul 20, 2016 4:07 pm

Try something like

Code: Select all
var data_3d = [];

for (var i=0; i< n; i=i+1)
 {
  var data_2d = [];
    for (var j=0; j<m;j=j+1)
     {
      data_2d[j] = fill_my_value(i,j);
     }
  append(data_3d, data_2d);
 }

print(data[0][4]);


Another idea would be defining a new function in the local_weather.nas script which overwrites the velocitiy properties in JSBSim to the values in the implemented wind field, wouldn't it?


It'd still run at framerate (and I'd be surprised if it actually overwrites anything JSBSim sets).

Am i write, that the Nasal scripts aren't calling in a steady time interval (even when you have a steady framerate) like the FDM which runs with 120Hz?


At framerate or the nearest possible frame equal to the loop time constant.
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: Elapsed time after each local_weather.nas call

Postby Hooray » Wed Jul 20, 2016 9:40 pm

yeah, like Thorsten says, multi-dimensional arrays (vectors actually) work just fine in Nasal, we're using them all over the place - I suggest you start reading some tutorials and maybe start tinkering a bit with the built-in Nasal console
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: Elapsed time after each local_weather.nas call

Postby gierschi@flightgear » Thu Jul 21, 2016 8:28 am

First of all: Thanks for your reply

@Thorsten:

What do you mean with
Code: Select all
fill_my_value(i,j)
a new defined function?

And
Code: Select all
data[0][4]
is also a 2D structure i don't know where it comes from.

In addition, i get the error message:

vector index 0 out of bounds (size: 0)

because data_2d seems to have a size of 0.
Last edited by gierschi@flightgear on Thu Jul 21, 2016 8:46 am, edited 1 time in total.
gierschi@flightgear
 
Posts: 58
Joined: Tue Nov 03, 2015 10:40 pm

Re: Elapsed time after each local_weather.nas call

Postby Thorsten » Thu Jul 21, 2016 9:18 am

fill_my_value(i,j) a new defined function?


Well, obviously I don't know what value you want to assign to a coordinate, so I can't write it into the example. You have to supply a function to fill your value.

is also a 2D structure i don't know where it comes from.


Well, it got produced by the code above (the purpose of which was to illustrate how to combine vectors to vectors of vectors).

In addition, i get the error message:


Yeah, make that

Code: Select all
for (var j=0; j<m;j=j+1)
     {
     var point = fill_my_value(i,j);
     append(data_2d, point);
     }
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: Elapsed time after each local_weather.nas call

Postby gierschi@flightgear » Thu Jul 21, 2016 1:40 pm

Okay thank you. I got it now. At least, the problem with the 3D field is solved:

Code: Select all
var n = 0;

var data_3d = [];

for (var k = 0; k < dimz; k += 1){

    var dummy_2d = [];

    for (var j = 0; j < dimy; j += 1){
       
        dummy_1d = [];
       
        for (var i = 0; i < dimx; i += 1){
                     
             pointer = u_input[n];
             n = n + 1;
             append(dummy_1d, pointer); 
        };
 
    append(dummy_2d,dummy_1d); 
    };
   
append(data_3d, dummy_2d); 
};


maybe i could overwrite the jsbsim velocites with the maketimer function?:

Code: Select all
var update_wind = func(){
setprop("/fdm/jsbsim/atmosphere/turb-north-fps", 5.0); 
}

var timer = maketimer(1/120, update_wind);

timer.start();
gierschi@flightgear
 
Posts: 58
Joined: Tue Nov 03, 2015 10:40 pm

Re: Elapsed time after each local_weather.nas call

Postby Thorsten » Thu Jul 21, 2016 2:32 pm

maybe i could overwrite the jsbsim velocites with the maketimer function?:


As I said, I'd really be surprised if that works.

The first thing you'll discover is that Nasal loops using timers run in the next frame that fits the timer value (I've said that above), i.e. whatever your constant is, it won't run faster than framerate.

And the second thing you're likely to discover is that JSBSim runs as a block, so it will first compute its own winds (and overwrite whatever you set) and then compute aerodynamics. The scenario that JSBSim first computes its own winds, then your Nasal script happens to set something and then JSBSim continues to compute aeroydnamics is really unlikely.

Anyway - we've told you what works and what doesn't - if you want to verify it the hard way yourself, please just go ahead (but please don't ask again why it didn't work!)
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: Elapsed time after each local_weather.nas call

Postby gierschi@flightgear » Thu Jul 21, 2016 4:36 pm

whatever your constant is, it won't run faster than framerate
.

Okay, I still thought that the maketimer function CAN run faster than the framerate although it's written into a Nasal script. Sorry.

Basically the only way to achieve this is to tie a listener to a JSBSim property which calls the Nasal update routine


So, that's the only solution you mentioned. Do you mean something like this?

Code: Select all
setlistener("/fdm/jsbsim/atmosphere/turb-north-fps", Nasal update routine, 5.0); }, 1, 1);


Or do you mean a JSBSim property itself can call the Nasal update routine? But how shall a property call a function?

It's very important to get a solution. That's an significant part of my current work and without finding one I can not go further :( .
gierschi@flightgear
 
Posts: 58
Joined: Tue Nov 03, 2015 10:40 pm

Re: Elapsed time after each local_weather.nas call

Postby Thorsten » Thu Jul 21, 2016 5:58 pm

I don't think there's been a system intentionally designed like this, so at this point you know about as much as I do. You simply have to give it a try, see what properties are at FDM rate and not tied and whether it does run at the intended rate.

I am usually a fan of Nasal solution, but personally I believe modifying the C++ code would be the safer way forward - there's no guarantee any of us could give that Nasal works as imagined for such a use case.
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Next

Return to Nasal

Who is online

Users browsing this forum: No registered users and 5 guests