Board index FlightGear Support Interfacing

DC3 gear down Nasal puzzler - * solved *  Topic is solved

Connecting two computers, using generic protocol, connecting with Matlab?

DC3 gear down Nasal puzzler - * solved *

Postby Volador » Mon Apr 26, 2021 11:18 pm

Any NASAL experts out there? Any clue how you'd splice these gear commands for the DC3 into a listener?

Code: Select all
controls.gearDown = func(v) {
    if (v < 0) {
      setprop("controls/gear/gear-down-lock", 0);
      setprop("controls/gear/gear-down-cmd", 0);
      settimer(func{setprop("controls/gear/gear-down-lock", 1);}, 6);
    } elsif (v > 0) {
      setprop("controls/gear/gear-down-lock", 0);
      setprop("controls/gear/gear-down-cmd", 1);
      settimer(func{setprop("controls/gear/gear-down-lock", 1);}, 6);
    }
}

var gearDown = func(v) {
    if (v < 0) {
      setprop("/controls/gear/gear-down", 0);
    } elsif (v > 0) {
      setprop("/controls/gear/gear-down", 1);
    }
}


I tried the following based on previous advice but it didn't work. The property input/arduino/gear-down does however update to 1 or 0 depending on the external switch on the interface.

Code: Select all
setlistener("input/arduino/gear-down", func(state)
   {
   if(state.getBoolValue())
        {
        setprop("controls/gear/gear-down-lock", 0);
        setprop("controls/gear/gear-down-cmd", 1);
        settimer(func{setprop("controls/gear/gear-down-lock", 1);}, 6);
        }
      else
        {
        setprop("controls/gear/gear-down-lock", 0);
        setprop("controls/gear/gear-down-cmd", 0);
        settimer(func{setprop("controls/gear/gear-down-lock", 1);}, 6);
        }     
   
   },1,0);


Volador
Last edited by Volador on Sat May 01, 2021 8:38 am, edited 2 times in total.
User avatar
Volador
 
Posts: 1140
Joined: Tue Sep 01, 2020 4:58 pm
Callsign: Volador, G-VLDR
Version: 2020.4
OS: Windows 10, 64 bit

Re: DC3 gear down Nasal puzzler

Postby wlbragg » Tue Apr 27, 2021 1:46 am

How about something like...

Code: Select all
setlistener("input/arduino/gear-down", func(node)
   {
   
   controls.gearDown(node.getBoolValue());
 #OR
  gearDown(node.getBoolValue());

   },1,0);


You may need the nasal space qualifier for the gearDown Function. ???.GearDown(node.getBoolValue());
Although what you did looked to be really close to what you would need as well. What exactly happened with your first attempted solution?
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7586
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: DC3 gear down Nasal puzzler

Postby Volador » Tue Apr 27, 2021 11:13 am

Hi Wlbragg, thanks for the reply. To answer your question, all that happened was property input/arduino/geardown changed but nothing else. I've just tried your suggestion but I'm a bit in the dark when it comes to Nasal - I'm cutting and pasting these sections out of the dc-3-keyboard.xml hoping to make it work:

Code: Select all
  <key n="103">
    <name>g</name>
    <desc>Landing Gear Up</desc>
    <binding>
      <command>nasal</command>
      <script>
        setprop("controls/gear/gear-down-lock", 0);
        setprop("controls/gear/gear-down-cmd", 0);
        settimer(func{setprop("controls/gear/gear-down-lock", 1);}, 6);
      </script>
    </binding>
  </key>

  <key n="71">
    <name>G</name>
    <desc>Landing Gear Down</desc>
    <binding>
      <command>nasal</command>
      <script>
        setprop("controls/gear/gear-down-lock", 0);
        setprop("controls/gear/gear-down-cmd", 1);
        settimer(func{setprop("controls/gear/gear-down-lock", 1);}, 6);
      </script>
    </binding>
  </key>


I tried the following based on your suggestion but remember I'm not an expert - the following didn't work by the way...

Code: Select all
GearDown(node.getBoolValue());

setlistener("input/arduino/gear-down", func(node)
  {
  controls.gearDown(node.getBoolValue());
  if(state.getBoolValue())
      {
      setprop("controls/gear/gear-down-lock", 0);
      setprop("controls/gear/gear-down-cmd", 1);
      settimer(func{setprop("controls/gear/gear-down-lock", 1);}, 6);
      }
  else
      {
      setprop("controls/gear/gear-down-lock", 0);
      setprop("controls/gear/gear-down-cmd", 0);
      settimer(func{setprop("controls/gear/gear-down-lock", 1);}, 6);
      }     
   
   },1,0);
User avatar
Volador
 
Posts: 1140
Joined: Tue Sep 01, 2020 4:58 pm
Callsign: Volador, G-VLDR
Version: 2020.4
OS: Windows 10, 64 bit

Re: DC3 gear down Nasal puzzler

Postby benih » Tue Apr 27, 2021 1:01 pm

Does the listener get called? drop in some print("blabla") statements to see if it executes.
It might be that the listened-to property is tied, so the listener won't fire.
Nasal errors usually show up in the console.

And maybe you could try to make the referenced setprop-attributes absolute by prepending a slash...
User avatar
benih
 
Posts: 1689
Joined: Tue Aug 15, 2017 10:34 am
Callsign: D-EBHX
Version: next
OS: Debian Linux 64bit

Re: DC3 gear down Nasal puzzler

Postby Volador » Tue Apr 27, 2021 1:45 pm

Hi Beni, I'm really don't have a deep understanding of your suggestion but here's what I tried ....and yes I'm trying to get it ready for our big flight tomorrow night :)

Code: Select all
setlistener("input/arduino/gear-down", func(state)
  {
  if(state.getBoolValue())
      {
        print("blabla")
      setprop("controls/gear/gear-down-lock", 0);
      setprop("controls/gear/gear-down-cmd", 1);
      settimer(func{setprop("controls/gear/gear-down-lock", 1);}, 6);
      }
  else
      {
        print("blabla")
      setprop("controls/gear/gear-down-lock", 0);
      setprop("controls/gear/gear-down-cmd", 0);
      settimer(func{setprop("controls/gear/gear-down-lock", 1);}, 6);
      }         
   },1,0);


no blabla prints on screen ...but I don't even know if my syntax is right.
I also looked in the joysick xml as that has a custom button that triggers controls.gearToggle() (and that works - but it will just toggle - I need to read the position of the switch which is 0 or 1) so there could be a really easy solution (I'm just not that great at code)
User avatar
Volador
 
Posts: 1140
Joined: Tue Sep 01, 2020 4:58 pm
Callsign: Volador, G-VLDR
Version: 2020.4
OS: Windows 10, 64 bit

Re: DC3 gear down Nasal puzzler

Postby Volador » Tue Apr 27, 2021 4:07 pm

SOLVED: Solution very kindly figured out by @Polly

Paste this into dc3.nas around line 12 after the var declarations

Code: Select all
# ===== ====== added to allow property input/arduino/gear-down to control gear ====== ======
setprop("input/arduino/gear-down", 1);

setlistener("/input/arduino/gear-down", func(state)
  {
  print("Hear Gear");
  if(state.getBoolValue())
      {
      print("Hear Gear Down");
      setprop("controls/gear/gear-down-lock", 0);
      setprop("controls/gear/gear-down-cmd", 1);
      settimer(func{setprop("controls/gear/gear-down-lock", 1);}, 6);
      }
  else
      {
        print("Hear Gear Up");
      setprop("controls/gear/gear-down-lock", 0);
      setprop("controls/gear/gear-down-cmd", 0);
      settimer(func{setprop("controls/gear/gear-down-lock", 1);}, 6);
      }         
   },1,0);
# ====== ======= end of section added for arduino control ====== ======


EDIT - this did work *but* on spawing on the ground the gear would retract - even if the four gear related properties were forcibly set. Wlbraggs solution is closest so far but it only puts the gear down
Last edited by Volador on Thu Apr 29, 2021 4:59 pm, edited 2 times in total.
User avatar
Volador
 
Posts: 1140
Joined: Tue Sep 01, 2020 4:58 pm
Callsign: Volador, G-VLDR
Version: 2020.4
OS: Windows 10, 64 bit

Re: DC3 gear down Nasal puzzler

Postby benih » Tue Apr 27, 2021 10:18 pm

Cool!
User avatar
benih
 
Posts: 1689
Joined: Tue Aug 15, 2017 10:34 am
Callsign: D-EBHX
Version: next
OS: Debian Linux 64bit

Re: DC3 gear down Nasal puzzler

Postby Volador » Wed Apr 28, 2021 5:21 pm

I spoke too soon - the gear is not solved yet, will update if a solution is found
User avatar
Volador
 
Posts: 1140
Joined: Tue Sep 01, 2020 4:58 pm
Callsign: Volador, G-VLDR
Version: 2020.4
OS: Windows 10, 64 bit

Re: DC3 gear down Nasal puzzler

Postby Volador » Wed Apr 28, 2021 5:57 pm

wlbragg wrote in Tue Apr 27, 2021 1:46 am:How about something like...

Code: Select all
setlistener("input/arduino/gear-down", func(node)
   {
   
   controls.gearDown(node.getBoolValue());
 #OR
  gearDown(node.getBoolValue());

   },1,0);


You may need the nasal space qualifier for the gearDown Function. ???.GearDown(node.getBoolValue());
Although what you did looked to be really close to what you would need as well. What exactly happened with your first attempted solution?



Hi Wlbragg, I tried again with your suggestion and it worked but only in one direction - it only put the gear down
Last edited by Volador on Thu Apr 29, 2021 4:49 pm, edited 1 time in total.
User avatar
Volador
 
Posts: 1140
Joined: Tue Sep 01, 2020 4:58 pm
Callsign: Volador, G-VLDR
Version: 2020.4
OS: Windows 10, 64 bit

Re: DC3 gear down Nasal puzzler

Postby Volador » Thu Apr 29, 2021 2:00 pm

@wlbragg -to help my understanding of what you did, what's the difference between func(node) and func(state), and why would it be one and not the other in this case? - this seem to be the key for anything that switches polarity rather than just being pressed once

Update: the code you provided only puts the gear down.
User avatar
Volador
 
Posts: 1140
Joined: Tue Sep 01, 2020 4:58 pm
Callsign: Volador, G-VLDR
Version: 2020.4
OS: Windows 10, 64 bit

Re: DC3 gear down Nasal puzzler - *** still a puzzler

Postby wlbragg » Thu Apr 29, 2021 7:59 pm

Hi Volador,

what's the difference between func(node) and func(state)

I never looked at the listener function to definitively make this statement, but using my general knowledge of programing languages and syntax, I'd say the state VS node had nothing to do with it. Normally that variable or container could be named anything, with the exception of maybe a "reserved" word. So long as it is not changed in the body of the function.

You can test that by changing "node" to "myvar" and seeing if it still works. Keep in mind you have to use "myvar" throughout the body of the listener function.

My main point was that you shouldn't need to copy the body of the function that changes the gear values, you should be able to call that nasal function directly.

But I wasn't taking into account that you might not have a handle on the entire programming flow to implement it.
In other words...
If you change a variable on the Arduino and pass that variable or property back to FlightGear, you have to do it in a manner that catches that the property changed and execute that change.

So, you do that in one of two ways, a listener, that is watching that value and when it sees it has changed, it executes the code in the body of the listener, which only needs to be a call to the already existing function. You don't need to replicate the "already existing function", you only need to execute it by calling it and passing any of the needed parameters.

Or you create your own nasal loop that is executing constantly and catch the property change there and when the change happens you execute ne nasal gear function and pass the change to it.

The listener is probably the easier method unless you already have a infinite loop running.

your suggestion and it worked but only in one direction


this did work *but* on spawing on the ground the gear would retract


Sorry, I ran out of time to help you address these two statements, I'll try to address this later tonight.
Last edited by wlbragg on Thu Apr 29, 2021 10:06 pm, edited 2 times in total.
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7586
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: DC3 gear down Nasal puzzler - *** still a puzzler

Postby wlbragg » Thu Apr 29, 2021 8:22 pm

this did work *but* on spawing on the ground the gear would retract


I have a second to answer this, the listener function ends with this
Code: Select all
 },1,0);

Those flags change the way the listener behaves. Whether it executes at startup or not and whether it executes only when the listened to property changes or every cycle.
Look up listeners in the wiki to see how it works.
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7586
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: DC3 gear down Nasal puzzler - *** still a puzzler  

Postby wlbragg » Thu Apr 29, 2021 8:25 pm

Code: Select all
setlistener("input/arduino/gear-down", func(node)
   {
   
   controls.gearDown(node.getBoolValue());
 #OR
  gearDown(node.getBoolValue());

   },1,0);

I tried again with your suggestion and it worked but only in one direction - it only put the gear down


It should work both ways but it does depend on what the value of input/arduino/gear-down is.
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7586
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: DC3 gear down Nasal puzzler - *** still a puzzler

Postby Volador » Thu Apr 29, 2021 8:48 pm

Hi @Wlbragg The state of input/arduino/gear-down changes with the physical switch as it should, 1 for down 0 for up but the code you kindly sent only puts the gear down once it's up (using the G on the keyboard). There's another section of code in the dc3.nas that could be messing with things though. As well as controls.gearDown = func(v) there is a var gearDown = func(v) like this:
Code: Select all
controls.gearDown = func(v) {
    if (v < 0) {
      setprop("controls/gear/gear-down-lock", 0);
      setprop("controls/gear/gear-down-cmd", 0);
      settimer(func{setprop("controls/gear/gear-down-lock", 1);}, 6);
    } elsif (v > 0) {
      setprop("controls/gear/gear-down-lock", 0);
     setprop("controls/gear/gear-down-cmd", 1);
      settimer(func{setprop("controls/gear/gear-down-lock", 1);}, 6);
    }
}

var gearDown = func(v) {
    if (v < 0) {
      setprop("/controls/gear/gear-down", 0);
    } elsif (v > 0) {
      setprop("/controls/gear/gear-down", 1);
    }
}
User avatar
Volador
 
Posts: 1140
Joined: Tue Sep 01, 2020 4:58 pm
Callsign: Volador, G-VLDR
Version: 2020.4
OS: Windows 10, 64 bit

Re: DC3 gear down Nasal puzzler - *** still a puzzler

Postby WoodSTokk » Fri Apr 30, 2021 1:47 am

The problem is that 'controls.gearDown()' need a value of 1 or -1.
Your code
Code: Select all
controls.gearDown(node.getBoolValue());

call the function with the value 1 if the node is true or 0 if the node is false.
But 0 have no effect.
Try this instead:
Code: Select all
setlistener("input/arduino/gear-down", func(v)
    {
        if (v.getBoolValue()) {
            controls.gearDown(1);
        } else {
            controls.gearDown(-1);
        }
    },1,0);


PS: not tested, straight out of my head
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

Next

Return to Interfacing

Who is online

Users browsing this forum: No registered users and 3 guests