Board index FlightGear Support

Nasal maketimer question - flashing lamps

All general support: help on flying, installation, hardware, getting online etc. There are lots of users and developers to help you out.
Forum rules
In order to help you, we need to know a lot of information. Make sure to include answers to at least the following questions in your initial post.

- what OS (Windows Xp/Vista, Mac etc.) are you running?
- what FlightGear version do you use?
- what graphics card do you have?
- does the problem occur with any aircraft, at any airport?
- where did you download your aircraft/scenery from?
- is there any output printed to the console (black window)?
- copy&paste your commandline (tick the "Show commandline box on the last page of FGRun or the "Others" section on the Mac launcher).

Please report any bugs not specific to an aircraft on the issue tracker.
To run FlightGear on old computers with bad OpenGL support, please take a look at this wiki article.

Note: If you did not get a reponse, even after 7 days, you may want to check out the FlightGear mailing lists to ask your question there.

Nasal maketimer question - flashing lamps

Postby ScottBouch » Fri Jan 07, 2022 11:17 am

Hi all,

I have tried following the guidance on the wiki to use maketimer to flash my anti-collision lamps on an aircraft I'm developing: https://wiki.flightgear.org/Nasal_library#maketimer.28.29

I've not done this before, and need a hand to get it working please.

I have this flasher script within my nasal lighting script that runs every 25ms. Logic-wise, when circuit LC211 voltage is above 22V, the the var flasher-relay does start to alternate in the property viewer, but really fast.

As I have placed the timer inside a function that's called by the main loop timer every 25ms, would I be correct to believe that the timer will get restarted by flasher.start() every 25ms? This could explain the fast nature of the "flashing". However, I experimented by slowing down the program loop timer, but still get the fast flashing, so this may not be the issue. If this is the issue, is this snippet best places in a different nasal script allowed to run at frame rate?

Could it be how I have implemented my if statement setting the flasherRelay var true / false?

I guess some of this could be better answered if I understood if maketimer executes the code between curly brackets just once every time the time has elapsed, or if it continually executes the code while the time is elapsing? If continual, then my if statement can't work and would cause this fast flip-flopping, I implemented it thinking it'd just execute the code once, as the wiki shows a print() occurring once every time period.

(I have cut out irrelevant parts of this code that do other tasks)

Code: Select all

#########################
# Settings & Defaults
var update_period = 0.25;         # Loop Frequency

#########################
## Circuit properties
var lc211V = dcCkt.getNode("lc211-voltage",1);
lc211V.setValue(0);

# Flasher unit output relay
var flasherRelay = dcCkt.getNode("flasher-relay",1);
flasherRelay.setValue(0);

#########################
# Functions

var flaserUnit = func {
# Flasher timer
   var flasher = maketimer(1, func(){
   if (flasherRelay.getValue() == 1)
      {flasherRelay.setValue(0)}
      else
      {flasherRelay.setValue(1)}
   });

   if (lc211V.getValue() > 22)
      {flasher.start()}
      else
      {flasher.stop()}
}

#########################
# Main Loop

var main_loop = func {
   flaserUnit();                              # Call flasher function (for high-flash lamps)
}


# Loop Actuation
   loop_timer = maketimer(update_period, main_loop);
   setlistener("sim/signals/fdm-initialized", func {
         loop_timer.start();
});



Many thanks, Scott.
User avatar
ScottBouch
 
Posts: 183
Joined: Wed Jun 22, 2016 4:14 pm
Location: Midlands, UK
OS: Linux Mint

Re: Nasal maketimer question - flashing lamps

Postby benih » Fri Jan 07, 2022 1:01 pm

Hey Scott,
In your main loop you create and start a new timer every 25ms.
What you want to do is, to initialize the flasher timer outside the flaserUnit() function. Inside the function you can repeatedly call flasher.start() on your timer - it is afaik a noop when it already runs.
User avatar
benih
 
Posts: 1711
Joined: Tue Aug 15, 2017 10:34 am
Callsign: D-EBHX
Version: next
OS: Debian Linux 64bit

Re: Nasal maketimer question - flashing lamps

Postby ScottBouch » Fri Jan 07, 2022 4:27 pm

benih wrote in Fri Jan 07, 2022 1:01 pm: initialize the flasher timer outside the flaserUnit() function. Inside the function you can repeatedly call flasher.start() on your timer - it is afaik a noop when it already runs.


Thanks for that.. I have had a play about with it, but honestly don't know what I'm doing, and am clutching at straws here! (ie: I have just been failing with this)

If you (or anyone) can offer a hand-holding guide as to which elements I place where, that'd really help this along.

Sorry to ask for more help!!
Many thanks, Scott.
User avatar
ScottBouch
 
Posts: 183
Joined: Wed Jun 22, 2016 4:14 pm
Location: Midlands, UK
OS: Linux Mint

Re: Nasal maketimer question - flashing lamps

Postby benih » Sat Jan 08, 2022 12:38 pm

Try about this (typed from my phone and thus not tested).
I moved the flasher timer to global script space, so you essentially now have:
- a timer definition for the actual flashing (one per second)
- a main loop running at 0.25 secs, which calls a check function
- a check function which looks if the flasher timer should be active or not


Code: Select all
#########################
# Settings & Defaults
var update_period = 0.25;         # Loop Frequency

#########################
## Circuit properties
var lc211V = dcCkt.getNode("lc211-voltage",1);
lc211V.setValue(0);

# Flasher unit output relay
var flasherRelay = dcCkt.getNode("flasher-relay",1);
flasherRelay.setValue(0);

#########################
# Functions

# Flasher timer
   var flasher = maketimer(1, func(){
   if (flasherRelay.getValue() == 1) {
          flasherRelay.setValue(0);
      } else {
          flasherRelay.setValue(1);
      }
   });

var flaserUnit = func {
   if (lc211V.getValue() > 22) {
      flasher.start();
   } else {
      flasher.stop();
   }
}

#########################
# Main Loop

var main_loop = func {
   flaserUnit();                              # Call flasher function (for high-flash lamps)
}


# Loop Actuation
   loop_timer = maketimer(update_period, main_loop);
   setlistener("sim/signals/fdm-initialized", func {
         loop_timer.start();
});
User avatar
benih
 
Posts: 1711
Joined: Tue Aug 15, 2017 10:34 am
Callsign: D-EBHX
Version: next
OS: Debian Linux 64bit

Re: Nasal maketimer question - flashing lamps

Postby ScottBouch » Sat Jan 08, 2022 9:02 pm

Brilliant, thanl you for the help with that! I so need to learn more basic programming constructs.

I will try your recommendation early in the week.

Thanks again, Scott
User avatar
ScottBouch
 
Posts: 183
Joined: Wed Jun 22, 2016 4:14 pm
Location: Midlands, UK
OS: Linux Mint

Re: Nasal maketimer question - flashing lamps

Postby ScottBouch » Mon Jan 10, 2022 4:15 pm

Brilliant! That worked like a charm! Many thanks indeed!

I only had to add a line to set the flasherRelay false when the flasher switch is turned off, as the flasher relay would stay true if turned off on a true phase.

Code: Select all
## Anti-collision lamp flasher
# Flasher timer
   var flasher = maketimer(1, func(){
   if (flasherRelay.getValue() == 1)
      {flasherRelay.setValue(0)}
      else
      {flasherRelay.setValue(1)}
   });

var flaserUnit = func {
   if (lc211V.getValue() > 22)
      {flasher.start()}
      else
      {flasher.stop();
      flasherRelay.setValue(0)}
      
# LC212 - Anti-collision lamps (flashing)
if (flasherRelay.getValue() == 1)
   {lc212V.setValue(lc211V.getValue())}
   else
   {lc212V.setValue(0)}
}




Circuit LC212 is now going between LC211 Voltage and 0V, just like the real thing!

Thanks for the pointer - it's actually really pleasing to see I had it about right, just in the wrong location!
Great bit of learning , thanks.
User avatar
ScottBouch
 
Posts: 183
Joined: Wed Jun 22, 2016 4:14 pm
Location: Midlands, UK
OS: Linux Mint

Re: Nasal maketimer question - flashing lamps

Postby benih » Mon Jan 10, 2022 7:27 pm

You are welcome! :)
User avatar
benih
 
Posts: 1711
Joined: Tue Aug 15, 2017 10:34 am
Callsign: D-EBHX
Version: next
OS: Debian Linux 64bit


Return to Support

Who is online

Users browsing this forum: No registered users and 8 guests