LUA script question

Help with any software/desktop related issues. e.g. companion9x, eePe, etc
Post Reply
Raythegreat
Posts: 284
Joined: Sat Apr 16, 2016 11:33 pm
Country: -

LUA script question

Post by Raythegreat »

Hi,

I attached a simple LUA program that I use on my Taranis plus with firmware and companion 2.2.2.

This script displays the values in real time of the elevator stick on the LCD screen and output some voices I recorded to say 90%, 80%, 70%.
The program is just a test. I tried on both sim and Taranis and the behavior is the same.
Good job simulator programmers :-)

The display of the elevator on the screen works well but the playFile("xxxx.wav") don't work as expected.
I can hear the 70%, 80%, 90% voice coming out but they are not synchronized at all with the elevator value displayed and there is a huge delay
try.lua
Lua file
(609 Bytes) Downloaded 288 times
.
It seems there is a timing problem somewhere and a buffer that stores the wave sounds.

Please see program and advise.
Thanks
RTG

rdeanchurch
Posts: 750
Joined: Tue Dec 27, 2011 11:22 pm
Country: United States
Location: Carson City, Nv

Re: LUA script question

Post by rdeanchurch »

I think you are dropping in a new playFile every approximately 30 milliseconds. Much faster than the .wav can play.

Change the 900 playFile to a print statement and watch the debug output in the simulator full up with printed output.

I'm sure you know that the elev goes from -1024 to +1024.
I would have used
elevator = (getValue('ele')+1024)/20.48 to get 0 to 100 as an elevator value.
and then 70, 80 and 90 as limits.
then put in a second or 2...5 delay after every playFile.
Dean
OldDmbThms: 1. Takeoff, 2. Crash, 3. Repair, GOTO 1
Raythegreat
Posts: 284
Joined: Sat Apr 16, 2016 11:33 pm
Country: -

Re: LUA script question

Post by Raythegreat »

Hi rdeanchurch,

Thanks for your response.

Yes of course I know about the -1024, +1024, here, I was just trying to do something very simple here to debug the playFile issue.
I know this issue is a timing problem and the playFile takes too much time to execute for the loop.

So I'm trying to find another way to make it work. I did not find it yet. I want to ultimately do this for Lipo % sound out.
The lcd.drawText works fine in real time if there are in the loop alone, no playFile.

It seems there is a memory storage somewhere for the playFile. When I run the program with only one playfile, if the elevator goes for a certain amount of time over 90%, there will be a sound output of 90% several times even if the elevator has gone back to 0 for 30 seconds. The lcd.drawText is displayed at 0 but the playFile for 90% continues to sound out for several seconds. A large buffer is stored somewhere. Can understand it.

Just try my program and use any sound file instead to replace 90perc.wav. you'll see and hear the problem. :-)

RTG.
User avatar
Kilrah
Posts: 11107
Joined: Sat Feb 18, 2012 6:56 pm
Country: Switzerland

Re: LUA script question

Post by Kilrah »

You must read the current time and only call a playFile if the correct amount of time has elapsed since last time you called one. At this point you're filling the play queue in a fraction of a second.
User avatar
MikeB
9x Developer
Posts: 17979
Joined: Tue Dec 27, 2011 1:24 pm
Country: -
Location: Poole, Dorset, UK

Re: LUA script question

Post by MikeB »

I think the voice output (like ersky9x) simply queues the filename. This is needed as some things (e.g. numbers) are actually played by using 2 or 3 files in sequence. playFile simply adds the filename to the queue. If you execute playFile several times, then it is queued several times, each one being played.

To avoid this, you need to only execute playFile once for any given percentage.

Mike
erskyTx/er9x developer
The difficult we do immediately,
The impossible takes a little longer!

Raythegreat
Posts: 284
Joined: Sat Apr 16, 2016 11:33 pm
Country: -

Re: LUA script question

Post by Raythegreat »

Thank you Mike,

Just got your reply as I was responding to Kilrah.

RTG
Raythegreat
Posts: 284
Joined: Sat Apr 16, 2016 11:33 pm
Country: -

Re: LUA script question

Post by Raythegreat »

Thanks Kilrah,

I believe my first answer to you did not get out.
I'm sure the read time will work.

Thanks guys.
RTG
rdeanchurch
Posts: 750
Joined: Tue Dec 27, 2011 11:22 pm
Country: United States
Location: Carson City, Nv

Re: LUA script question

Post by rdeanchurch »

I did try it.
There has to be a buffer or stack to queue up the sounds to be played. If you overflow it you will lose some of the queued files.
But the buffer/stack will still be played for whatever is in the queue.
Since it takes much longer the 30+- MILLISECONDS to play the sound file, the sounds will keep being put in the queue and played from the queue until it is empty. So 90 % will play long after the elevator is moved back to 0.

lcd.draw text takes very little time compared to playing a sound file, so it might not be put in queue at all, but executed, essentially, in-line.

... put in a second or 2...5 delay after every playFile, before you call playFile again. Long enough for the sound to be played, then the playFile queue will be empty (of your playFiles...remember other special functions, alarms and lua scripts can also put something in the queue) when the elv moves back to 0.
Dean
OldDmbThms: 1. Takeoff, 2. Crash, 3. Repair, GOTO 1
Raythegreat
Posts: 284
Joined: Sat Apr 16, 2016 11:33 pm
Country: -

Re: LUA script question

Post by Raythegreat »

Thank you rdeanchurch,

It is good to know that there are queues. This changes the whole concept of using misc founctions.

It is also good to know that there is a delay also, not only queues.

I did this:

timer1 = getTime()
playFile("90perc.wav")
timer2 = getTime()

display timer2 - timer1

You guessed it, answer is 0. It takes lower than 10ms to go throught and the playFile is executed later.

Thanks to all
RTG
murphyj123
Posts: 1
Joined: Tue Apr 20, 2021 7:59 pm
Country: United States

Re: LUA script question

Post by murphyj123 »

Hi, Learning LUA in OpenTX and I have a question. I have several inputs to a LUA script running on my Horus. I use one of the inputs to assign a switch to a function. The value of the selected switch comes in as an input parameter to my run function. It's a mixer script. This all does what I want, but I would like to add the functionality to allow the input to be unassigned meaning that on the custom script page the the user has selected --- instead of a switch. I would like to somehow detect that the input is unassigned but can't think of a way. If unassigned, I always get the input '0', which is of course legitimate for a selected switch too. I have tried getFieldInfo("input5") which gives me id=5, name=input5, desc=Input5 no matter what I set for that input. These are the inputs I have declared. Happens to be the Flaps SW I am trying to determine which I believe is input5.
local inputs = {
{ "Thr Ch", SOURCE },
{ "ThrLk SW", SOURCE },
{ "RSSI SW", SOURCE },
{ "Timer SW", SOURCE },
{ "Flaps SW", SOURCE },
{ "Gear SW", SOURCE },
}

Any Thoughts?
John

Post Reply

Return to “Software”