Fun, Learning, Friendship and Mutual Respect START  HERE


Unregistered
Go Back   HeliFreak > R/C Electronics Support > OpenTX Radios


OpenTX Radios OpenTX Radios Discussion and Support Forum


Like Tree4Likes
  • 1 Post By xoexoe
  • 1 Post By xoexoe
  • 1 Post By ridge-runner
  • 1 Post By xoexoe
Reply
 
LinkBack Thread Tools Display Modes
Old 01-01-2020, 02:33 AM   #1 (permalink)
Registered Users
 
Posts: 2,053
 

Join Date: Feb 2013
Location: Perth, Western Australia (the most isolated capital city on earth!)
Default Is it possible to increment a timer (not just set it)

Because I'm not yet using TX-FBL integration/telemetry for my helis, I'm still flying on battery times. I have a switch set up to allow me to change Timer1, 6:30, 4:30, 3:30 for various batteries in various states.

I'd like to be able to increment the value in Timer1 (say, add 15 secs) - whether in flight (i.e. TH off) or before take-off (TH on).

Is this possible? How?

TIA,
Paolo
__________________
(building) XLPower 700, ALIGN 550L (beater), Prτtos MAX V2 Leggero 700 & Evoluzione 800 (RCHN #803 - joined just before they shut up shop!)
PaoloFCantoni is offline        Reply With Quote Quick reply to this message
Sponsored Links
Advertisement
 
Old 01-01-2020, 11:24 PM   #2 (permalink)
Registered Users
 
Posts: 6,234
 

Join Date: Sep 2018
Location: Snohomish, WA
Default

Paolo I don't see anything like that in the Special Functions list of options. Just Reset and Set Timer. Nothing to Add to a timer. It does seem like a feature that would be nice to have. There's a way to suggest features for future firmware updates. I think it's through their GitHub repository IIRC.

Another option is to create your own Lua script that does it. I took a look at the Lua reference for OpenTX and did see two functions, called model.getTimer() and model.setTimer(), that could be used to do exactly what you want. You get the timer value, create a new variable that's incremented by a few seconds, then set the timer with the new value. Take a look at https://opentx.gitbooks.io/opentx-2-.../setTimer.html

A few of the OpenTX guys here have done Lua scripting and they might be willing to help. I personally haven't done scripting yet in OpenTX, but I could see myself procrastinating for a few hours in the next week in an attempt to learn.
__________________
Brian — Oxy 2 & 4 Max — S2 & CP X — 150X — iK2N/Brain2/Spirit — Horus X10S — OpenTX — AccuRC —

Last edited by xoexoe; 01-01-2020 at 11:34 PM..
xoexoe is online now        Reply With Quote Quick reply to this message
Old 01-02-2020, 12:37 AM   #3 (permalink)
Registered Users
 
Posts: 6,234
 

Join Date: Sep 2018
Location: Snohomish, WA
Default

There's no time to procrastinate like the present.

I ended up getting something very bad and rudimentary working using Companion. It's a function script. (Is that the best type?) But it keeps incrementing the timer by 15 seconds as long as the switch is active. (It does this MANY times per second if the switch is active.) Is there a simple way to get the code to run only once per click?

And for some reason variables I initialize and define in my init() function (such as local amountToAdd=15 ) aren't defined in the run function. I had to define them again in the run function. Maybe it's a bug in Companion 2.2?? I bet it's just me. I haven't tried it on an actual TX yet.

If I get the script working in the next week or so I can share the code here. Or hopefully someone else can share if they get one working.
PaoloFCantoni likes this.
__________________
Brian — Oxy 2 & 4 Max — S2 & CP X — 150X — iK2N/Brain2/Spirit — Horus X10S — OpenTX — AccuRC —
xoexoe is online now        Reply With Quote Quick reply to this message
Old 01-02-2020, 01:26 AM   #4 (permalink)
Registered Users
 
Posts: 2,053
Thread Starter Thread Starter
 

Join Date: Feb 2013
Location: Perth, Western Australia (the most isolated capital city on earth!)
Default

Quote:
Originally Posted by xoexoe View Post
Paolo I don't see anything like that in the Special Functions list of options. Just Reset and Set Timer. Nothing to Add to a timer. It does seem like a feature that would be nice to have. There's a way to suggest features for future firmware updates. I think it's through their GitHub repository IIRC.

[SNIP]
Done!

Looking forward to the LUA script.

Paolo
__________________
(building) XLPower 700, ALIGN 550L (beater), Prτtos MAX V2 Leggero 700 & Evoluzione 800 (RCHN #803 - joined just before they shut up shop!)
PaoloFCantoni is offline        Reply With Quote Quick reply to this message
Old 01-02-2020, 10:11 AM   #5 (permalink)
Registered Users
 
Posts: 1,329
 

Join Date: Jul 2012
Location: Austin, UT USA
Default Function Script (edge) trigger switch

Quote:
Originally Posted by xoexoe View Post
... It's a function script. (Is that the best type?) But it keeps incrementing the timer by 15 seconds as long as the switch is active. (It does this MANY times per second if the switch is active.) Is there a simple way to get the code to run only once per click? ...
Yes. I use function scripts in my TaraniJuke app to increment the song in a playlist and to jump to the next song. Here is the script that increments the playlist which uses a delay to filter out all but the first instance. And the switch used to trigger the SPECIAL FUNCTION SCRIPT needs to be a quick pulse that can be achieved using an EDGE logical switch. Here's the code that demonstrates the use of the system timer to achieve the filtering effect...

Code:
-- TARANIJUKE File: SCRIPTS\FUNCTIONS\BgMus+.lua Rev:20170814_1000
-- OpenTX 2.0.12+/2.1.7+/2.2.0+ LUA special function script.
-- (c) 2015-2017 Jeffrey Michael Roberson.
-- MIT License: http://www.opensource.org/licenses/mit-license.php

-- Increment through TaraniJuke playlist via special function LUA script.
local t_next = 0                                    -- Earliest time allowed for next run.
local function run_BgMusicInc()                     -- Main "run" function of special function LUA script.
    local t                                         -- Value returned by system getTime().
    if tjg then                                     -- Only proceed if model mix script running.
        t = getTime()                               -- Current time (integer 1/100 sec per count).
        if t > t_next then                          -- If time window open...
            t_next = t + 33                         -- Limit run time frequency (Scroll at about 3 Hz).
            if tjggo() then                         -- If all special funcs good...
                tjg.npl = tjg.npl + 1               -- Increment to next file in playlist.
                if tjg.npl > #(tjg.pl) then tjg.npl = 1 end -- Wrap around as needed.
            end
        end
    end
end
return { run=run_BgMusicInc }                       -- End SCRIPTS\FUNCTIONS\BgMus+.lua.
ridge-runner is offline        Reply With Quote Quick reply to this message
Old 01-02-2020, 01:53 PM   #6 (permalink)
Registered Users
 
Posts: 6,234
 

Join Date: Sep 2018
Location: Snohomish, WA
Default

Ridge Runner thank you very much. Edge logical switches seem like the ingredient I was missing.

Would you mind answering some questions about your code?

1. Where/when exactly does your first line that reads "local t_next=0" get run? It's not in the init() function. Actually you don't have such a function (which is optional I know) as described in https://opentx.gitbooks.io/opentx-2-...n_scripts.html ... Your line is just sort of hanging out there in no-man's land. My understanding is that the init() function gets called exactly once when the model loads, and the run() function gets called whenever the SF is activated. I assumed that writing code at the same hierarchical level of init() and run() wouldn't do anything.

2. For the Edge-type Logical Switch did you just do "0.0" and "instant" so that there is a single ON pulse, meaning you'd have to keep clicking your switch to get your track to advance?

3. But if #2 is correct, I don't quite understand why you have a 1/3 sec delay built in.... Your delay seems like you are allowing the physical switch to be held on so that the logical switch is true for an extended time. Or maybe you are preventing multiple separate clicks that are spaced less than 1/3 sec apart?
__________________
Brian — Oxy 2 & 4 Max — S2 & CP X — 150X — iK2N/Brain2/Spirit — Horus X10S — OpenTX — AccuRC —
xoexoe is online now        Reply With Quote Quick reply to this message
Old 01-02-2020, 06:54 PM   #7 (permalink)
Registered Users
 
Posts: 6,234
 

Join Date: Sep 2018
Location: Snohomish, WA
Default

Hey Paolo this Function Script I made actually works in Companion! But I haven't tested it on an actual transmitter. Give it a shot?

Name this file something like addTim.lua

I believe it must be 6 characters or shorter and must end in .lua

You should put the lua file in the OpenTX folder: SD Structure/SCRIPTS/FUNCTIONS

You need to already have enabled the .lua (and maybe the .luac ?) build options in your Companion/TX setup.

As Ridge said you need to trigger the Special Function using an Edge Logical Switch. Thanks Ridge for the important pointers!

I have sort-of instructions built into the code itself below.


Code:
--
-- INSTRUCTIONS
-- Step 1: Change the user-defined options below to suit your needs
-- Step 2: This Function Script should be called using a Logical Switch of type Edge.
-- For the Edge logical switch, try using: Edge, <switch of your choice>, 0.0, (instant)


-- USER-DEFINED OPTIONS --
local TIMER_INDEX = 0		-- "0" is Timer1, "1" is Timer2, "2" is Timer3. Default is 0
local numSecondsToAdd = 15	-- num seconds to add to selected timer. Default is 15.
-- END USER-DEFINED OPTIONS --

local t_next = 0  												-- Earliest time at which next run_func() is allowed.

local timerToAdjust = {}  										-- will hold reference to the active timer.
local  newTimeValue  											-- integer in seconds for what the new timer value should be
local newTimerParams = {} 										-- Need to create a table to pass to the timer.

local function run_func()
	local t 													-- will hold system time, used to check if enough time has elapsed.
	timerToAdjust = model.getTimer(TIMER_INDEX) 	
	
	if timerToAdjust ~= nil then  								-- do stuff only if the timer exists
		t = getTime() 											--stores current time
		if t > t_next then -- make sure enough time has elapsed
			t_next = t + 33 								 -- next allowed time will be 1/3 of a second later
			newTimeValue = timerToAdjust.value + numSecondsToAdd 	-- New value for the timer.
			newTimerParams["value"] = newTimeValue 					-- Create the table that will be passed to the timer
			model.setTimer(TIMER_INDEX, newTimerParams)
		end
	end
end

return { run=run_func}
Trevael likes this.
__________________
Brian — Oxy 2 & 4 Max — S2 & CP X — 150X — iK2N/Brain2/Spirit — Horus X10S — OpenTX — AccuRC —
xoexoe is online now        Reply With Quote Quick reply to this message
Old 01-02-2020, 09:54 PM   #8 (permalink)
Registered Users
 
Posts: 2,053
Thread Starter Thread Starter
 

Join Date: Feb 2013
Location: Perth, Western Australia (the most isolated capital city on earth!)
Default

Thanks, Brian!

I'm off on leave for a week or so and travelling away, so I may not get to it immediately. Also, I've never used/set up an LUA script so, wish me luck...

I'll try it in Companion first so I understand how it works, then I'll load it up.

Paolo
__________________
(building) XLPower 700, ALIGN 550L (beater), Prτtos MAX V2 Leggero 700 & Evoluzione 800 (RCHN #803 - joined just before they shut up shop!)
PaoloFCantoni is offline        Reply With Quote Quick reply to this message
Old 01-02-2020, 11:23 PM   #9 (permalink)
Registered Users
 
Posts: 1,329
 

Join Date: Jul 2012
Location: Austin, UT USA
Default Lua STATIC and GLOBAL vars

Happy to answer - I just got some free time...

Quote:
Originally Posted by xoexoe View Post
... 1. Where/when exactly does your first line that reads "local t_next=0" get run? It's not in the init() function. Actually you don't have such a function (which is optional I know) as described in https://opentx.gitbooks.io/opentx-2-...n_scripts.html ... Your line is just sort of hanging out there in no-man's land. My understanding is that the init() function gets called exactly once when the model loads, and the run() function gets called whenever the SF is activated. I assumed that writing code at the same hierarchical level of init() and run() wouldn't do anything. ...
Any variable declared "local" outside script functions (in the outermost scope), is a STATIC variable that persists between calls, and is only visible to the local script. I use this to store the absolute time for the time window to open back up - the run function ignores any/all calls until then. I'm not certain when it is run the first time - (I don't think you can have an init() function in a FUNCTION SCRIPT). I'm guessing it gets run the first time it is activated/script loaded. Don't know!

Any (NOT "local") variable declared outside the functions in a mixer script is a GLOBAL variable that persistes between calls and is visible to all scripts, including both FUNCTION and TELEMETRY scripts. Note: This is true on a TARANIS only - HORUS models handle GLOBAL variables differently and each widget exists in its own separate sandbox invisible to all the other running widget scripts.

My Jukebox player keeps a global data table (tjg - "TaraniJuke Global") where the playlist is stored. This table is declared as a global table variable in a playlist MODEL/MIXER script and comes into existence when the MODEL/MIXER script is first activated (where the init() function is called) and lives forever until the model is changed, or the radio is turned off. The TELEMETRY and FUNCTIONs scripts call the tjggo() GLOBAL function also stored in the model/mixer/playlist script.

Note that this global var behavior stuff is not well documented and my description is based on my own empirical evidence (some of what I've said might have changed - I don't have a Horus, but have played with one in the sim).
Quote:
Originally Posted by xoexoe View Post
... 2. For the Edge-type Logical Switch did you just do "0.0" and "instant" so that there is a single ON pulse, meaning you'd have to keep clicking your switch to get your track to advance? ...
Yes, instantaneous EDGE at time zero e.g. "Edge(!L03, [0:instant])".
Quote:
Originally Posted by xoexoe View Post
... 3. But if #2 is correct, I don't quite understand why you have a 1/3 sec delay built in.... Your delay seems like you are allowing the physical switch to be held on so that the logical switch is true for an extended time. Or maybe you are preventing multiple separate clicks that are spaced less than 1/3 sec apart?
You would think that using this method would result in needing only one call to a FUNCTION SCRIPT when it triggers, but my testing showed the function script getting multiple calls anyway - that is why I had to resort to using the time filter.

BTW - I saw your new in-depth description of the "many-ways-to-build-a-heli-model" thread but I have not yet read it. I'll provide some feedback once I do. As I said before, it appears you have very quicky caught up to speed on the power and flexibility of OpenTX - Good Job!
xoexoe likes this.

Last edited by ridge-runner; 01-12-2020 at 12:15 PM.. Reason: minor typo
ridge-runner is offline        Reply With Quote Quick reply to this message
Old 01-02-2020, 11:58 PM   #10 (permalink)
Registered Users
 
Posts: 6,234
 

Join Date: Sep 2018
Location: Snohomish, WA
Default

Thanks RR. That all makes sense to me. Appreciate it!

You can have init in Function scripts. I tried it and it does get called when loading up the model. But your jukebox controller and this timer adjuster are simple enough to not need it it seems.



Now I need to go through all the available functions in OpenTX’s Lua environment to see what kind of fun I can have.

By the way Ridge, your coding is very good. Great comments & documentation at top. Mine needs work.

Last edited by xoexoe; 01-03-2020 at 01:38 AM..
xoexoe is online now        Reply With Quote Quick reply to this message
Old 01-12-2020, 02:22 AM   #11 (permalink)
Registered Users
 
Posts: 6,234
 

Join Date: Sep 2018
Location: Snohomish, WA
Default

FYI Paolo, I did upload it to my X10S. Worked perfectly.
PaoloFCantoni likes this.
__________________
Brian — Oxy 2 & 4 Max — S2 & CP X — 150X — iK2N/Brain2/Spirit — Horus X10S — OpenTX — AccuRC —
xoexoe is online now        Reply With Quote Quick reply to this message
Old 02-08-2020, 05:10 PM   #12 (permalink)
Registered Users
 
Posts: 2,053
Thread Starter Thread Starter
 

Join Date: Feb 2013
Location: Perth, Western Australia (the most isolated capital city on earth!)
Default

Quote:
Originally Posted by xoexoe View Post
FYI Paolo, I did upload it to my X10S. Worked perfectly.
Thanks, guys,

Good to know! Unfortunately, life and some crashes and accidental manslaughter of an FBL unit have intervened.

However, I hope to get to it soon.

Paolo
__________________
(building) XLPower 700, ALIGN 550L (beater), Prτtos MAX V2 Leggero 700 & Evoluzione 800 (RCHN #803 - joined just before they shut up shop!)
PaoloFCantoni is offline        Reply With Quote Quick reply to this message
Old 02-08-2020, 06:10 PM   #13 (permalink)
Registered Users
 
Posts: 6,234
 

Join Date: Sep 2018
Location: Snohomish, WA
Default Is it possible to increment a timer (not just set it)

I saw on your GitHub request that it was milestoned for version 2.3.6 or something like that. Looks like they’ll add it eventually to the firmware.

Hope you get found not guilty of FBL manslaughter.
xoexoe is online now        Reply With Quote Quick reply to this message
Old 02-08-2020, 09:22 PM   #14 (permalink)
Registered Users
 
Posts: 2,053
Thread Starter Thread Starter
 

Join Date: Feb 2013
Location: Perth, Western Australia (the most isolated capital city on earth!)
Default

Quote:
Originally Posted by xoexoe View Post
I saw on your GitHub request that it was milestoned for version 2.3.6 or something like that. Looks like they’ll add it eventually to the firmware.

Hope you get found not guilty of FBL manslaughter.
Unfortunataly not, €200 fine (@1.66x AU$)

Still, I got free shipping!

Paolo
__________________
(building) XLPower 700, ALIGN 550L (beater), Prτtos MAX V2 Leggero 700 & Evoluzione 800 (RCHN #803 - joined just before they shut up shop!)
PaoloFCantoni is offline        Reply With Quote Quick reply to this message
Reply




Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the HeliFreak forums, you must first register.
Please enter your desired user name, your REAL and WORKING email address and other required details in the form below.
User Name:
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself. Use a real email address or you will not be granted access to the site. Thank you.
Email Address:
Location
Where do you live? ie: Country, State, City or General Geographic Location please.
Name and Lastname
Enter name and last name here. (This information is not shown to the general public. Optional)
Helicopter #1
Enter Helicopter #1 type and equipment.
Helicopter #2
Enter Helicopter #2 type and equipment.
Helicopter #3
Enter Helicopter #3 type and equipment.
Helicopter #4
Enter Helicopter #4 type and equipment.

Log-in


Thread Tools
Display Modes

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




Copyright © Website Acquisitions Inc. All rights reserved.
vBulletin Security provided by vBSecurity v2.2.2 (Pro) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.

SEO by vBSEO 3.6.1