Jump to content
The simFlight Network Forums

Rotary HDG auto increase step


Recommended Posts

 

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

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

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

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...

bdd3118d70728fab1a3ae2a209487f8e.png

 

Thanks again....

Link to comment
Share on other sites

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 by John Dowson
Added missing line of code
Link to comment
Share on other sites

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 by John Dowson
Added missing line of code
Link to comment
Share on other sites

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.