aerostar17 Posted June 9, 2019 Report Share Posted June 9, 2019 Trying to make the HDG rotary increase step from - 1 deg per click to 10 deg per click - automatically when turning the rotary after a set number of deg's have occured in a given time. Here is a sample of the LUA actions.lua script from LINDA website for the Aerosoft Airbus A320 that I've been trying to alter with no success (I am not a programmer). Can anyone help please...... This the original for HDG plus , there is also one for HDG plusfast but you need to manually trigger which one to select _________________________________________________________ function AB_HDG_plus () AB_hdgtrk = ipc.readLvar("AB_AP_HDGTRK") AB_drift = round(ipc.readLvar("AB_AP_HDGTRK")) if AB_hdgtrk == 0 then LVarSet = "AB_AP_HDG_Select" else LVarSet = "AB_AP_TRK_set" end LVarGet = round(ipc.readLvar(LVarSet)) AddVar = LVarGet + 1 if AddVar > 359 then AddVar = 0 end DspHDG(AddVar) ipc.writeLvar(LVarSet, AddVar) end Here is one of my inept attempts at altering it..... _______________________________________ function AB_HDG_plus () AB_hdgtrk = ipc.readLvar("AB_AP_HDGTRK") AB_drift = ipc.readLvar("AB_AP_HDGTRK") if AB_hdgtrk == 0 then LVarSet = "AB_AP_HDG_Select" else LVarSet = "AB_AP_TRK_set" end LVarGet = ipc.readLvar(LVarSet) if (LvarGet - val1) > 20 then AddVar = LVarGet + 10 else AddVar = LVarGet + 1 if AddVar > 359 then AddVar = 0 end --DspHDG(AddVar) ipc.writeLvar(LVarSet, AddVar) end val1 = LVarGet sleep(200) end event.flag(1, "AB_HDG_plus") _________________________________ My general idea is when the rotary turns, a reading is taken of the HDG degrees at that time then ie 200ms later a reading is taken again and if the difference is over ie 15 deg it sets the rotary to step 10deg per click instead of 1deg step per click. As soon as the sample is less than 15deg diff then it steps back to 1deg per click. This should be easy for someone with a bit of LUA knowledge to do (I think) Thanks for any help Link to comment Share on other sites More sharing options...
aerostar17 Posted June 10, 2019 Author Report Share Posted June 10, 2019 .... my latest attempt using "event.timer" but any iterations and many different tries later it still doesn't work Can't figure out how the event.timer is used in this... among other things Link to comment Share on other sites More sharing options...
John Dowson Posted June 11, 2019 Report Share Posted June 11, 2019 event.timer calls a function after the interval specified, it doesn't return anything. In your case, it will call a function called 'deg1', which isn't defined. What you need to do is to save the current time whenever you increment. Then, the next time your function is called, you can compare the previously saved 'current time', and adjust your reading delta accordingly. John Link to comment Share on other sites More sharing options...
John Dowson Posted June 11, 2019 Report Share Posted June 11, 2019 To get you going, here's a short lua script that will adjust a delta value depending upon the time since last activated - it requires the lua socket package which you will need to download and install if using FSUIPC5: require "socket" lastTime = 0; function myTimer(flag) currentTime = socket.gettime()*1000 ipc.log("Current time is " .. currentTime .. ", lasTime is " .. lastTime) difference = currentTime - lastTime if (difference > 200 and difference < 1000) then delta = 10 else delta = 1 end ipc.log("Delta is " .. delta .. " (difference=" .. difference .. ")") lastTime = currentTime end event.flag(1, "myTimer") You can use the logic in this script in the first script you posted (dump the second) - should be reasonably straightforward (hopefully!). John Link to comment Share on other sites More sharing options...
aerostar17 Posted June 12, 2019 Author Report Share Posted June 12, 2019 Hi John, thanks very much for replying, I have been trying to implement your code into the existing since you posted but after trying different ways I still haven't got it to run... I'm a bit confused if this should run seperately and if your delta difference is named delta or I rename it to AddVar, and also with a few other things. I did manage to find and install the Socket.lua addon. function AB_HDG_plus () AB_hdgtrk = ipc.readLvar("AB_AP_HDGTRK") AB_drift = ipc.readLvar("AB_AP_HDGTRK") if AB_hdgtrk == 0 then LVarSet = "AB_AP_HDG_Select" else LVarSet = "AB_AP_TRK_set" end LVarGet = ipc.readLvar(LVarSet) require ("socket") lastTime = 0; --ipc.log("Current degree is " .. currentdeg .. ", lastdegree is " .. lastdeg) --diffdeg = currentdeg - lastdeg currentTime = socket.gettime()*1000 ipc.log("Current time is " .. currentTime .. ", lasTime is " .. lastTime) difference = currentTime - lastTime if (difference > 200 and difference < 1000) then AddVar = LVarGet + 10 else AddVar = LVarGet + 1 end if AddVar > 359 then AddVar = 0 end ipc.log("Delta is " .. AddVar.. " (difference=" .. difference .. ")") lastTime = currentTime end --DspHDG(AddVar) ipc.writeLvar(LVarSet, AddVar) event.flag(1, "AB_HDG_plus") Latest log of this ... ********* LUA: "AB_HDG_speed" Log [from FSUIPC version 5.151] ********* 9323640 System time = 12/06/2019 10:42:09, Simulator time = 11:43:13 (10:43Z) 9323640 LUA: beginning "F:\P3Dv4\Modules\AB_HDG_speed.lua" 9323640 LUA: F:\P3Dv4\Modules\AB_HDG_speed.lua:28 9323640 LUA: Global: ipcPARAM = 0 9323640 LUA: F:\P3Dv4\Modules\AB_HDG_speed.lua:1 9323640 LUA: F:\P3Dv4\Modules\AB_HDG_speed.lua:30 9323640 LUA: F:\P3Dv4\Modules\AB_HDG_speed.lua:32 9323656 LUA: Waiting for an event in "F:\P3Dv4\Modules\AB_HDG_speed.lua" 11448781 >>> Thread forced exit (ipc.exit or os.exit) <<< 11448781 System time = 12/06/2019 11:17:34, Simulator time = 12:18:26 (11:18Z) ********* LUA execution terminated: Log Closed ********* ********* LUA: "AB_HDG_speed" Log [from FSUIPC version 5.151] ********* 11475422 System time = 12/06/2019 11:18:00, Simulator time = 12:18:43 (11:18Z) 11475422 LUA: beginning "F:\P3Dv4\Modules\AB_HDG_speed.lua" 11475422 LUA: F:\P3Dv4\Modules\AB_HDG_speed.lua:27 11475422 LUA: Global: ipcPARAM = 0 11475422 LUA: F:\P3Dv4\Modules\AB_HDG_speed.lua:1 11475422 LUA: F:\P3Dv4\Modules\AB_HDG_speed.lua:29 11475422 LUA: F:\P3Dv4\Modules\AB_HDG_speed.lua:31 11475422 LUA: Waiting for an event in "F:\P3Dv4\Modules\AB_HDG_speed.lua" ... a snap with the line numbers... Thanks again.... Link to comment Share on other sites More sharing options...
John Dowson Posted June 12, 2019 Report Share Posted June 12, 2019 (edited) You should review basic lua programming! Merging the code I gave into your function gives this: require "socket" lastTime = 0; function AB_HDG_plus () currentTime = socket.gettime()*1000 difference = currentTime - lastTime lastTime = currentTime if (difference > 200 and difference < 2000) then delta = 10 else delta = 1 end AB_hdgtrk = ipc.readLvar("AB_AP_HDGTRK") AB_drift = round(ipc.readLvar("AB_AP_HDGTRK")) if AB_hdgtrk == 0 then LVarSet = "AB_AP_HDG_Select" else LVarSet = "AB_AP_TRK_set" end LVarGet = round(ipc.readLvar(LVarSet)) AddVar = LVarGet + delta if AddVar > 359 then AddVar = 0 end DspHDG(AddVar) ipc.writeLvar(LVarSet, AddVar) end event.flag(1, "AB_HDG_plus") John Edited June 12, 2019 by John Dowson Added missing line of code Link to comment Share on other sites More sharing options...
John Dowson Posted June 12, 2019 Report Share Posted June 12, 2019 (edited) I've also just noticed that your are reading the lvar AB_AP_HDGTRK twice, into variables AB_hdgtrk and AB_drift. You never use the latter. Also the function DspHDG isn't defined, so the script can be simplified to: require "socket" lastTime = 0; function AB_HDG_plus () currentTime = socket.gettime()*1000 difference = currentTime - lastTime lastTime = currentTime if (difference > 200 and difference < 2000) then delta = 10 else delta = 1 end AB_hdgtrk = ipc.readLvar("AB_AP_HDGTRK") if AB_hdgtrk == 0 then LVarSet = "AB_AP_HDG_Select" else LVarSet = "AB_AP_TRK_set" end LVarGet = round(ipc.readLvar(LVarSet)) AddVar = LVarGet + delta if AddVar > 359 then AddVar = 0 end ipc.writeLvar(LVarSet, AddVar) end event.flag(1, "AB_HDG_plus") Edited June 12, 2019 by John Dowson Added missing line of code Link to comment Share on other sites More sharing options...
John Dowson Posted June 12, 2019 Report Share Posted June 12, 2019 Hi again, just noticed your actual requirements: On 6/9/2019 at 6:18 PM, aerostar17 said: My general idea is when the rotary turns, a reading is taken of the HDG degrees at that time then ie 200ms later a reading is taken again and if the difference is over ie 15 deg it sets the rotary to step 10deg per click instead of 1deg step per click. As soon as the sample is less than 15deg diff then it steps back to 1deg per click. The function provided above only changes the step rate based upon the speed of input - anything less than 200ms (or over 2s) will set the step/delta to 1, and if the time since last input > 200ms (and < 2s) then it will set the step/delta to 10. If you also want to add logic to adjust the step/delta rate depending upon HDG difference, then you will need to add this yourself. John Link to comment Share on other sites More sharing options...
aerostar17 Posted June 12, 2019 Author Report Share Posted June 12, 2019 Thanks again John, will try and get a better understanding of programming, though I am wondering if I have the necessary brainpower... it's amazing how you programmers manage to put it together... Just a quick question, if I may... can I use the ipc.log method to save the HDG for comparison ? cheers Link to comment Share on other sites More sharing options...
John Dowson Posted June 12, 2019 Report Share Posted June 12, 2019 No problem. You can add ipc.log statements anywhere you wish - it just logs the message in the output. John Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now