Jump to content
The simFlight Network Forums

Custom Axis Range using Lua


Recommended Posts

Hi Everyone,

I Would just like a crosscheck on some code I have written to turn a brightness knob in the VRS superbug.

The code works quite well, I am just not real efficient at programming & am wondering if anyone can comment on a more efficient way of doing the script.

This script calls on my main LUA script that I wrote for the VRS superbug found in the User Contribution Area & screams around in a loop looking for an axis change.

Something I would like to know is will this loop running at full speed have an impact on frames or Flightsim ?

It doesnt seem to but, thought I would ask and this is only for a single brightness knob at this stage, I am planning quite a few more knobs.

If I put any pauses in the loop then the action is not smooth when turning the pot.

I would really appreciate any comments.

Cheers Glenn.


-- read initial values for axis and create variables
Value1 = ipc.axis ("E", "S")
Value2 = ipc.axis ("E", "S")
-- Main loop looking for axis movement
while 1 do
-- read axis value for comparison
Value1 = ipc.axis ("E", "S")
-- test for axis change in either direction, change must be greater than 10 to be recognised
if Value1 - Value2 > 10 or Value2 - Value1 > 10 then
-- There are 20 Values for the knob position, here I have created 20 ranges & testing for each range
if Value1> -1 and Value1<51 then ipc.runlua("SuperScript_VRS", 120)
elseif Value1 > 51 and Value1 < 102 then ipc.runlua("SuperScript_VRS", 121)
elseif Value1 > 102 and Value1 < 153 then ipc.runlua("SuperScript_VRS", 122)
elseif Value1 > 153 and Value1 < 204 then ipc.runlua("SuperScript_VRS", 123)
elseif Value1 > 204 and Value1 < 255 then ipc.runlua("SuperScript_VRS", 124)
elseif Value1 > 255 and Value1 < 306 then ipc.runlua("SuperScript_VRS", 125)
elseif Value1 > 306 and Value1 < 357 then ipc.runlua("SuperScript_VRS", 126)
elseif Value1 > 357 and Value1 < 408 then ipc.runlua("SuperScript_VRS", 127)
elseif Value1 > 408 and Value1 < 459 then ipc.runlua("SuperScript_VRS", 128)
elseif Value1 > 459 and Value1 < 510 then ipc.runlua("SuperScript_VRS", 129)
elseif Value1 > 510 and Value1 < 561 then ipc.runlua("SuperScript_VRS", 130)
elseif Value1 > 561 and Value1 < 612 then ipc.runlua("SuperScript_VRS", 131)
elseif Value1 > 612 and Value1 < 663 then ipc.runlua("SuperScript_VRS", 132)
elseif Value1 > 663 and Value1 < 714 then ipc.runlua("SuperScript_VRS", 133)
elseif Value1 > 714 and Value1 < 765 then ipc.runlua("SuperScript_VRS", 134)
elseif Value1 > 765 and Value1 < 816 then ipc.runlua("SuperScript_VRS", 135)
elseif Value1 > 816 and Value1 < 867 then ipc.runlua("SuperScript_VRS", 136)
elseif Value1 > 867 and Value1 < 918 then ipc.runlua("SuperScript_VRS", 137)
elseif Value1 > 918 and Value1 < 969 then ipc.runlua("SuperScript_VRS", 138)
elseif Value1 > 969 and Value1 < 1024 then ipc.runlua("SuperScript_VRS", 139)
end
end
-- read axis value for comparison
Value2 = ipc.axis ("E", "S")
-- Displaying values to screen for diagnostics here, normally this next line will be commented out
ipc.display ("Value1="..Value1.."\nValue2="..Value2)
end
[/CODE]

Link to comment
Share on other sites

If I put any pauses in the loop then the action is not smooth when turning the pot.

You would be better off using events in any case.

Is the axis your are reading not assignable in FSUIPC's axis assignments? If so then assign it to "luavalue <name of lua>". If you want changes greater than 10 to be recognised then change the default Delta on the assignment page to 10 instead of 256 or whatever it is currently.

The wrap your brightness code into a function like


function brightness(value)
...
end
[/CODE]

and add

[CODE]
event.param("brightness")

[/CODE]

at the end. This will then allow FSUIPC to automatically call that function with the axis value when it changes. At other times the Lua plug-in is suspended. You don't need to test "value" for changes -- if it hasn't changed the function won't be called.

You do need of course to make sure the plug-in is running first -- e.g via an [Auto] action or from ipcReady.lua.

This worries me a bit:

-- There are 20 Values for the knob position, here I have created 20 ranges & testing for each range

If you only deal with 20 values each with a range of 51 from the axis, why are you testing for changes in 10s? Surely a change of 51 should be needed?

Instead of:

[CODE] if Value1> -1 and Value1<51 then ipc.runlua("SuperScript_VRS", 120)
elseif Value1 > 51 and Value1 < 102 then ipc.runlua("SuperScript_VRS", 121)
elseif Value1 > 102 and Value1 < 153 then ipc.runlua("SuperScript_VRS", 122)
elseif Value1 > 153 and Value1 < 204 then ipc.runlua("SuperScript_VRS", 123)
elseif Value1 > 204 and Value1 < 255 then ipc.runlua("SuperScript_VRS", 124)
elseif Value1 > 255 and Value1 < 306 then ipc.runlua("SuperScript_VRS", 125)
elseif Value1 > 306 and Value1 < 357 then ipc.runlua("SuperScript_VRS", 126)
elseif Value1 > 357 and Value1 < 408 then ipc.runlua("SuperScript_VRS", 127)
elseif Value1 > 408 and Value1 < 459 then ipc.runlua("SuperScript_VRS", 128)
elseif Value1 > 459 and Value1 < 510 then ipc.runlua("SuperScript_VRS", 129)
elseif Value1 > 510 and Value1 < 561 then ipc.runlua("SuperScript_VRS", 130)
elseif Value1 > 561 and Value1 < 612 then ipc.runlua("SuperScript_VRS", 131)
elseif Value1 > 612 and Value1 < 663 then ipc.runlua("SuperScript_VRS", 132)
elseif Value1 > 663 and Value1 < 714 then ipc.runlua("SuperScript_VRS", 133)
elseif Value1 > 714 and Value1 < 765 then ipc.runlua("SuperScript_VRS", 134)
elseif Value1 > 765 and Value1 < 816 then ipc.runlua("SuperScript_VRS", 135)
elseif Value1 > 816 and Value1 < 867 then ipc.runlua("SuperScript_VRS", 136)
elseif Value1 > 867 and Value1 < 918 then ipc.runlua("SuperScript_VRS", 137)
elseif Value1 > 918 and Value1 < 969 then ipc.runlua("SuperScript_VRS", 138)
elseif Value1 > 969 and Value1 < 1024 then ipc.runlua("SuperScript_VRS", 139)
end[/CODE]

why not just:

[CODE]
if value > 1019 then value = 1019 end
ipc.runlua("SuperScript_VRS", 120 + (value /51))

[/CODE]

making the entire program just

[CODE]
function brightness(value)
if value > 1019 then value = 1019 end
ipc.runlua("SuperScript_VRS", 120 + (value /51))
end
event.param("brightness")[/CODE]

Finally, using runlua like that is really destroying the beauty of the solution. It would be far more efficient to put whatever code "SuperScript_VRS" is doing directly into this plug-in. It's only using a bit of memory, then, but running it afresh every time is reading a file, compiling the Lua, as well as executing it.

Regards

Pete

Link to comment
Share on other sites

Thanks for your Reply Pete.

You would be better off using events in any case.

Yes I thought events would be the better way to do it and I could see an event for buttons but not an event for axis in the manual.

So I decided to try and detect the axis change using my long winded code.

Is it possible to have an event for an Axis ?

Is the axis your are reading not assignable in FSUIPC's axis assignments? If so then assign it to "luavalue <name of lua>".

Yes, The axis is just a normal Joystick Axis and I did try assigning that way, however the knob that I am working with is on a panel only addressable via L:Vars and has 20 discrete values accross it's range, if I only assign 10 which is the maximum in the axis assignment panel I lose 50% resolution of the knob, sure it would still work though.

If you only deal with 20 values each with a range of 51 from the axis, why are you testing for changes in 10s? Surely a change of 51 should be needed?

The only reason I was testing in 10's was to stop the code from executing from axis jitter or a dirty potentiometer, I noticed if I tested for a change of only 1 & the axis value was jumping between two values as axis sometimes do, it was entering the loop constantly, which I didn't want.

I ran a test using the value of 51 as you said but then when I turn the potentiometer slowly there is not enough change to trigger the code to enter the loop.

I tried with a value of 5 & it seems quite sensitive enough, If the pot is jumping around greater than values of 5 then it's time for a new pot !!

Finally, using runlua like that is really destroying the beauty of the solution. It would be far more efficient to put whatever code "SuperScript_VRS" is doing directly into this plug-in. It's only using a bit of memory, then, but running it afresh every time is reading a file, compiling the Lua, as well as executing it.

As I had already defined the 20 individual positions in my main script I thought I would just call them up from this script, I have already defined the L:Var & associated values for that L:Var & assigned each one (20 in total) a parrameter value in the main script, so I do not ever have to look them up again.

Thankyou for taking the time, I will see if I can streamline it to be a little more efficient as you have suggested.

Cheers Glenn.

Link to comment
Share on other sites

Is it possible to have an event for an Axis ?

Yes, exactly thev way I explained and showed code for.

Yes, The axis is just a normal Joystick Axis and I did try assigning that way, however the knob that I am working with is on a panel only addressable via L:Vars and has 20 discrete values accross it's range, if I only assign 10 which is the maximum in the axis assignment panel I lose 50% resolution of the knob, sure it would still work though.

Er, you are confusing me now. The limit of 10 is on the number of range assignments on the right-hand side of the axis assignments tab. You should instead assign to the luavalue control on the left. That passes the axis value onto the Lua plug-in. I gave these instructions already, how come you are thinking in terms of 10 ranges?

I must have failed dismally in my previous reply as you appear to have misunderstood everything. Why not simply try the code I provided?

I ran a test using the value of 51 as you said but then when I turn the potentiometer slowly there is not enough change to trigger the code to enter the loop.

But your tersting of the value was in ranges of 51!!!

As I had already defined the 20 individual positions in my main script I thought I would just call them up from this script.

I have already defined the L:Var & associated values for that L:Var & assigned each one (20 in total) a parrameter value in the main script, so I do not ever have to look them up again.

But loading and compiling another Lua is wasteful in time and energy, and all you are saving is a little memory space. Just cut and paste the code directly into the new plug-in. It's not necessary to work it all out again!

Thankyou for taking the time, I will see if I can streamline it to be a little more efficient as you have suggested.

I actually coded it for you. All you had to do was try it! I don't understand your confusion!?

:sad:

Pete

Link to comment
Share on other sites

  • 1 month later...

Hi Pete, I just wanted to come back and finish of this thread and say thankyou for your guidance.

It has actually taken me this long to get my head around event driven scripts, I must have read your answer here 50 times trying to make sense of it all but I think I have finally got a handle on functions & events and how to make it all work, for somebody that has never dealt with anything other than loop type programming and really I have not done a great deal of that either, it's a little hard get the brain into gear.

However I think I have got now, I recently re-wrote an Annunciator script for the VRS Superbug that was originally a massive loop, I could see the problems starting to arise as the loop got bigger and bigger & it also handles Flashing lamps that really just pushed me over the edge, So I sat down and rebuilt the Annuciator script on events and functions after slowly understanding how they work.

Then when I came back here I realised what you were trying to guide me to.

So Here is what I ended up with, it is not an exact copy of what you coded for me.

I didn't come to the forum to get code written for me, I just wanted some guidance and that you did VERY WELL, It's just the answer was over my head at the time.


function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
function contrast(value)
axis = value
level = round (axis/53.84, 0)
ipc.display ("Axis Value = "..axis.."\nLevel Value = "..level)
ipc.sleep(100)
ipc.runlua("SuperScript_VRS", (120 + level))
end
event.param("contrast")
[/CODE]

It basically takes the Axis Value divides it into 20 ranges, rounds off the the calculation at the same time

Throws the values on screen for verification

Has a little sleep, just seems a little bit more stable with that there.

Then calls my main script that has all the LVars etc. that turns the virtual knob.

I do understand about putting the code from the main script into here but it seems to work fine like this so I am happy.

Thankyou once again for your help.

Glenn.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

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