Board index FlightGear Development Nasal

Help needed

Nasal is the scripting language of FlightGear.

Help needed

Postby mhab » Sat Mar 21, 2015 11:21 pm

Hello

Maybe somebody can shed some light on this.

The following code is from EC130 Nasal scripting.
I had noticed some recurring Nasal error and it seems I found a fix, as it didn't show up since that fix.
See the lines marked with "<<<<<<<<<<<<<<<<<<" in the code part

Code: Select all
var oilpressurebar = func{

  oilpres_bar = props.globals.getNode("/engines/engine/oil-pressure-bar", 1);

  var rpm = props.globals.getNode("/engines/engine/rpm").getValue() or 0;
  var oilpres_low = props.globals.getNode("/engines/engine/oil-pressure-low").getValue() or 0;
  var oilpres_norm = props.globals.getNode("/engines/engine/oil-pressure-norm").getValue() or 0;

  # fix mhab                                          <<<<<<<<<<<<<<<<<<
  if ( !(oilpres_low >= 0) ) oilpres_low=0;           <<<<<<<<<<<<<<<<<<
  if ( !(oilpres_norm >= 0) ) oilpres_norm=0;         <<<<<<<<<<<<<<<<<<

  if ((rpm > 0) and (rpm < 23000)){
    interpolate ("/engines/engine/oil-pressure-bar", oilpres_low, 1.5);
  }elsif (rpm > 23000) {
    interpolate ("/engines/engine/oil-pressure-bar", oilpres_norm, 2);
  }

  settimer(oilpressurebar, 0.1);
}

oilpressurebar();


My question is now, how can it happen that a codeline as below can produce NaN values ?

Code: Select all
  var oilpres_low = props.globals.getNode("/engines/engine/oil-pressure-low").getValue() or 0;

Is this a Nasal error ? Ori is it somewhere rooted in FG kernel ?

P.S.: I noticed the problem in FG 3.0. I think it was there before 3.0 too, but I am not sure about that.

Thanks for any help

Mike-DE
mhab
 
Posts: 418
Joined: Thu Apr 18, 2013 11:59 pm
Callsign: D-MIKE
Version: 2020.3.4
OS: Win10

Re: Help needed

Postby Philosopher » Sat Mar 21, 2015 11:42 pm

Try using .getValue(path) not .getNode(path).getValue() (unless you are concerned about compatibility with pretty old versions) because it will not fail when the node doesn't exist.
Philosopher
 
Posts: 1593
Joined: Sun Aug 12, 2012 7:29 pm

Re: Help needed

Postby Richard » Sun Mar 22, 2015 9:47 am

mhab wrote in Sat Mar 21, 2015 11:21 pm:The following code is from EC130 Nasal scripting.
I had noticed some recurring Nasal error and it seems I found a fix, as it didn't show up since that fix.


Firstly if it's my property (i.e. in my aircraft) I'd make sure that it is initialized

Code: Select all
setprop("/engines/engine/oil-pressure-norm", 0);

var oilpressurebar = func{
..
}


For properties that may be empty or invalid that I don't control normally I'd do

Code: Select all
  var oilpres_norm = getprop("/engines/engine/oil-pressure-norm");

  if ( oilpres_norm == nil) oilpres_norm=0;
Richard
 
Posts: 810
Joined: Sun Nov 02, 2014 11:17 pm
Version: Git
OS: Win10

Re: Help needed

Postby Marius_A » Sun Mar 22, 2015 3:13 pm

My question is now, how can it happen that a codeline as below can produce NaN values ?

Code: Select all
var oilpres_low = props.globals.getNode("/engines/engine/oil-pressure-low").getValue() or 0;



This can be written as follows:
Code: Select all
var node = props.globals.getNode("/engines/engine/oil-pressure-low");
var oilpres_low = node.getValue() or 0;

If property "/engines/engine/oil-pressure-low" does not exist, props.globals.getNode("/engines/engine/oil-pressure-low") will return nil. The code would be equal to:
Code: Select all
var node = nil;
var oilpres_low = node.getValue() or 0;

This code will produce error, because node does not have getValue() function (node equals nil).
Marius_A
 
Posts: 92
Joined: Wed Dec 04, 2013 3:20 pm

Re: Help needed

Postby mhab » Mon Mar 23, 2015 12:08 pm

Hello

Thanks for these helpful hints.
I've tried to improve some code (remember: I am working on code not done by me originally) and
again ran into a very nasty issue which deadens my machine for a cople of minutes before everything is maxxed out and I can regain control.

Here is what I got on console window (and somehow managed to copy before everything crashed)
Code: Select all
Nasal getValue: property /velocities[0]/vertical-speed-fps[0] is NaN
Nasal runtime error: nil used in numeric context
  at C:/Users/michael/Documents/FlightGear/Aircraft/ec130/Nasal/ec130.nas, line 2284
Nasal getprop: property /velocities[0]/airspeed-kt[0] is NaN
Nasal runtime error: nil used in numeric context
  at C:/Users/michael/Documents/FlightGear/Aircraft/ec130/Nasal/ec130.nas, line 1119
Nasal getValue: property /orientation[0]/roll-deg[0] is NaN
Nasal runtime error: nil used in numeric context
  at C:/Users/michael/Documents/FlightGear/Aircraft/ec130/Systems/kcs55.nas, line 119
  called from: C:/Users/michael/Documents/FlightGear/Aircraft/ec130/Systems/kcs55.nas, line 428
  called from: C:/Users/michael/Documents/FlightGear/Aircraft/ec130/Systems/kcs55.nas, line 433
Nasal getprop: property /accelerations[0]/pilot-g[0] is NaN
AL Error (sound manager): Invalid Value at update


The related codepart in ec13.nas is this (relevant lines marked by <<<<<<<<<<<<:
Code: Select all
# main() ============================================================
var delta_time = props.globals.getNode("/sim/time/delta-sec", 1);
var hi_heading = props.globals.getNode("/instrumentation/heading-indicator/indicated-heading-deg", 1);
var vertspeed = props.globals.initNode("/velocities/vertical-speed-fps");                <<<<<<<<<<<<<<<
var gross_weight_lb = props.globals.initNode("/yasim/gross-weight-lbs");
var gross_weight_kg = props.globals.initNode("/sim/model/gross-weight-kg");
props.globals.getNode("/instrumentation/adf/rotation-deg", 1).alias(hi_heading);

var main_loop = func {
  props.globals.removeChild("autopilot");
  if (replay)
    setprop("/position/gear-agl-m", getprop("/position/altitude-agl-ft") * 0.3 - 1.2);
  vert_speed_fpm.setDoubleValue(vertspeed.getValue() * 60);                              <<<<<<<<<<<<<<<
  gross_weight_kg.setDoubleValue(gross_weight_lb.getValue() * LB2KG);

  var dt = delta_time.getValue();
  update_torque(dt);
  update_stall(dt);
  update_slide();

  update_absorber();
  fuel.update(dt);
  engines.update(dt);
  vibration.update(dt);

  settimer(main_loop, 0);
}


My questions:
1) From what I read initnode() should initialize a property ?!
2) Do I have any chance to fix this on aircraft level ? as see next ...
3) This property is not under my control, I think this should be under Yasim's control (EC130 works with YASIM)
so if it's lost, sounds like a bug to me
AND: this happens some time during flight and not reproducible and not so often but still too often to ignore it

Thanks for any helpful hints
Mike-DE
mhab
 
Posts: 418
Joined: Thu Apr 18, 2013 11:59 pm
Callsign: D-MIKE
Version: 2020.3.4
OS: Win10

Re: Help needed

Postby mhab » Thu Mar 26, 2015 9:40 am

Hello

Quite disturbing what I find ...
Code: Select all
  oilpres_bar = props.globals.getNode("/engines/engine/oil-pressure-bar", 1);

  var rpm = props.globals.getValue("/engines/engine/rpm") or 0;
  var oilpres_low = props.globals.getValue("/engines/engine/oil-pressure-low") or 0;   <<<<<<<<<
  var oilpres_norm = props.globals.getValue("/engines/engine/oil-pressure-norm") or 0; <<<<<<<<<<<

  # fix mhab
  if ( !(oilpres_low >= 0) ) oilpres_low=0;      <<<<<<<<<<<<<<
  if ( !(oilpres_norm >= 0) ) oilpres_norm=0;   <<<<<<<<<<<

Even after changing the code to getValue(...) I still need the lines marked with #fix mhab
Seems props.globals.getValue("/engines/engine/oil-pressure-low") or 0; doesn't return 0 if the proerty is NaN

This looks like lots of work to do in the ec130 Nasal ...
Mike-DE
mhab
 
Posts: 418
Joined: Thu Apr 18, 2013 11:59 pm
Callsign: D-MIKE
Version: 2020.3.4
OS: Win10


Return to Nasal

Who is online

Users browsing this forum: No registered users and 2 guests