Board index FlightGear Development Nasal

Function not running

Nasal is the scripting language of FlightGear.

Function not running

Postby cyyz_spotter » Thu Nov 26, 2020 1:43 am

I have a function here which calculates v-speeds,
Code: Select all
  # Create initial announced variables at startup of the sim
    V1 = 1.0;
    VR = 2.0;
    V2 = 3.0;

 
 # The actual function
setlistener("sim/signals/fdm-initialized", func
{
settimer(vspeeds, 1);
});

 var vspeeds = func {
 
        # Create/populate variables at each function cycle
        # Retrieve total aircraft weight and convert to kg.     
    WT = getprop("yasim/gross-weight-lbs")*0.0004535;
    flaps = getprop("controls/flight/flaps");

 
        # Calculate V-speeds with flaps 10
    if (flaps == 0.2) {
       V1 = (0.3*(WT-200.0))+100;
       VR = (0.3*(WT-200.0))+115;
       V2 = (0.3*(WT-200.0))+135;
    }
 
        # Calculate V-speeds with flaps 20
    elsif (flaps == 0.4) {
       V1 = (0.3*(WT-200.0))+95;
       VR = (0.3*(WT-200.0))+110;
       V2 = (0.3*(WT-200.0))+130;
    }

    # Calculate V-speeds with flaps 30
    elsif (flaps == 0.6) {
       V1 = (0.3*(WT-200.0))+90;
       VR = (0.3*(WT-200.0))+105;
       V2 = (0.3*(WT-200.0))+125;
    }

    # Calculate V-speeds with flaps 40
    elsif (flaps == 0.8) {
       V1 = (0.3*(WT-200.0))+85;
       VR = (0.3*(WT-200.0))+100;
       V2 = (0.3*(WT-200.0))+120;
    }
 
        # Export the calculated V-speeds to the property-tree, for further use
    setprop("/instrumentation/adc/reference/V1",V1);
    setprop("/instrumentation/adc/reference/Vr",VR);
    setprop("/instrumentation/adc/reference/V2",V2);
 
        # Repeat the function each second
    # settimer(vspeeds, 1);
 }
 
 # Only start the function when the FDM is initialized, to prevent the problem of not-yet-created properties.
 # setlistener("/sim/signals/fdm-initialized", vspeeds);


I set the initial variables to 1, 2, and 3 for debugging purposes and they stayed the same in the property browser so I know that the function is not running, but I don't get why. There are no parsing errors or anything, and I have called the function in my aircraft set.xml under the nasal tags
cyyz_spotter
 
Posts: 25
Joined: Fri Jul 10, 2020 4:57 pm
Location: Canada
Callsign: C-FWES
Version: 2020.3
OS: Windows 10

Re: Function not running

Postby WoodSTokk » Thu Nov 26, 2020 4:53 am

your properties are of type float and you compare it with
Code: Select all
if (flaps == 0.2) {

The problem is, a computer can mostly not constitute a float number so exactly. This is known as precision error.
Try as example this one:
Code: Select all
if (flaps > 0.19 && flaps < 0.21) {
WoodSTokk
 
Posts: 1077
Joined: Tue Oct 17, 2017 3:30 pm
Location: Milky Way/Sol/Earth/Europe
Callsign: SX-W57
IRC name: WoodSTokk
Version: 2020.4.0
OS: Debian Bullseye

Re: Function not running

Postby cyyz_spotter » Thu Nov 26, 2020 6:48 pm

Still no luck. I might just try making two seperate nasal files though since this plane never takes off on flaps 1 or 2, its either 3 or 4.
cyyz_spotter
 
Posts: 25
Joined: Fri Jul 10, 2020 4:57 pm
Location: Canada
Callsign: C-FWES
Version: 2020.3
OS: Windows 10

Re: Function not running

Postby Fritz » Thu Nov 26, 2020 7:03 pm

Are you sure that controls/flight/flaps gives these values? Isn't it 0, 0.25, 0.5, 0.75, and 1?
Fritz
 
Posts: 283
Joined: Tue Apr 26, 2016 11:04 pm
Location: Bavaria, Germany, near ETSL
Version: 2018.3.6
OS: Windows 7 Prof.

Re: Function not running

Postby cyyz_spotter » Thu Nov 26, 2020 9:14 pm

On my aircraft (A220) the steps are flaps 1(0.2), flaps 2(0.4), flaps 3(0.6), flaps 4(0.8) and flaps 5/full(1)
cyyz_spotter
 
Posts: 25
Joined: Fri Jul 10, 2020 4:57 pm
Location: Canada
Callsign: C-FWES
Version: 2020.3
OS: Windows 10

Re: Function not running

Postby legoboyvdlp » Thu Nov 26, 2020 10:15 pm

What will help is to do a

Code: Select all
debug.dump(flaps);


at the start of vspeeds();

This confirms 2 things:
1. That the function is running
2. That the value is what you think it is
User avatar
legoboyvdlp
 
Posts: 7981
Joined: Sat Jul 26, 2014 2:28 am
Location: Northern Ireland
Callsign: G-LEGO
Version: next
OS: Windows 10 HP

Re: Function not running

Postby gordonshamway23 » Thu Dec 03, 2020 10:27 pm

I might be wrong, but arent't you missing some "var" keywords.
And also what are your vspeeds for flaps 0 and flaps 5?
The following is a guess what you want to achieve, a bit shorter and handling all cases:

PS: corrected settimer to maketimer :)

Code: Select all
# Create initial announced variables at startup of the sim

var vspeeds = func {

   var V1 = 1.0; # can be local variables quess
   var VR = 2.0;
   var V2 = 3.0;

    # Create/populate variables at each function cycle
    # Retrieve total aircraft weight and convert to kg.     
    var WT = getprop("yasim/gross-weight-lbs")*0.0004535;
    var flaps = getprop("controls/flight/flaps");#value between 0.0 and 1.0

    var flapStep=math.round(flaps*5.0); # integer value between 0 and 5, assuming 5 different flap positions
   
    # Calculate V-speeds
    V1 = (0.3*(WT-200.0))+105-flapStep*5;
    VR = (0.3*(WT-200.0))+120-flapStep*5;
    V2 = (0.3*(WT-200.0))+140-flapStep*5;
 
 
    # Export the calculated V-speeds to the property-tree, for further use
    setprop("/instrumentation/adc/reference/V1",V1);
    setprop("/instrumentation/adc/reference/Vr",VR);
    setprop("/instrumentation/adc/reference/V2",V2);
 
    # Repeat the function each second
    # settimer(vspeeds, 1);
 }


var loopTimer = maketimer(1.0 , vspeeds ); #updating every second
 
 # Only start the function when the FDM is initialized, to prevent the problem of not-yet-created properties.
setlistener("sim/signals/fdm-initialized", func
{
   loopTimer.start();
});
 
Last edited by gordonshamway23 on Thu Dec 03, 2020 10:46 pm, edited 2 times in total.
gordonshamway23
 
Posts: 24
Joined: Sun Nov 22, 2020 8:15 pm

Re: Function not running

Postby Hooray » Thu Dec 03, 2020 10:35 pm

note that it is generally recommended to favor maketimer() over settimer() for a plethora of reasons, unless you are aware of the intricacies and are perfectly satisfied with them
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: Function not running

Postby jsb » Tue Jan 05, 2021 12:48 pm

You might be better off with property rules for such calculations. See http://wiki.flightgear.org/Autopilot_co ... _reference
They are not only useful for AP but anything that needs "not too complicated" math and according to what I heard from some core developers they perform (scale) much better than nasal timers even running at framerate.
Just my 2ct :)
jsb
 
Posts: 285
Joined: Sat Oct 25, 2014 9:17 pm
Location: Hamburg, Germany
Callsign: D-JSB
Version: next
OS: Win7/Linux

Re: Function not running

Postby wlbragg » Tue Jan 05, 2021 4:26 pm

Interesting, I had something like that happen as well. I don't know why I couldn't read the flaps value in the manner I wanted. My work around was something like this. This probably doesn't help in your case, but maybe?

Code: Select all
    var old_flap_position = 0;
    var current_flap_position = getprop("/surface-positions/flap-pos-norm");

    current_flap_position = getprop("/surface-positions/flap-pos-norm");
    if (current_flap_position != old_flap_position) {
        old_flap_position = current_flap_position;

       # flaps changed position

    }


I was trying to catch the time between the resting position values and flat out couldn't do it any other way, ie; trying to catch those values directly using =, < or >.

I wasn't smart enough to notice the float precision issue and don't know if that might have been my issue as well. I don't think it was as I was comparing a float or double "position-norm" with the same I think. I used the values 0.000 0.333, 0.666, 1.000 as comparison values if I remember correctly.
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7588
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070


Return to Nasal

Who is online

Users browsing this forum: No registered users and 1 guest