Jump to content
The simFlight Network Forums

Reading Axis Value in Lua


Recommended Posts

Pete,

I am just wondering if it is possible to read the value of an axis in Lua. I currently have my parking brake assigned to an axis; in one position it's on, in the other it's off. What I would like to do is write a Lua plug-in that reads the position of the axis, and sets the park brake to match the hardware. For example, if the axis value is in the negative range (which means the brake should be on), and the on-screen setting does not match, then write the appropriate value, and vice-versa. I know how to write the value (ipc.control(65752)), just need a little help in reading the axis value. I have identified the axis of interest as Joystick 0, Axis R.

Jim

Link to comment
Share on other sites

I am just wondering if it is possible to read the value of an axis in Lua.

Yes. Several ways. The easiest is probably simply to assign the axis to the Lua program, like assigning any other "FS" control (the Lua controls are listed in the "Send to FS" drop down). The axis value is passed over as the "ipcPARAM" variable for use in that plug-in, as described in the short document "FSUIPC Lua Plug-Ins".

That way is a little inefficient if the axis is frequently changing, as each time a new value is detected the same Lua plug-in is loaded, compiled and executed. For a parking brake axis this shouldn't be a real problem, though. Note that you can still get the control sent too (via dual assignment in the same place for an Axis or "Set" control, or via the right-hand assignments by range).

Another way is to assign to one of the Offset controls (a separate option in the Axis Assignments tab), getting the axis value written to a user offset, one of those free for users in the range 66C0 to 66FF. You'd have a Lua plug-in already running, probably loaded via the [Auto] options, or by an ipc.macro call in ipcReady.lua.

That plug-in could either sit forever in a loop, checking on the value in the chosen offset (and sleeping a lot), and taking the appropriate action, or you could instead be a bit more sophisticated and use the event.offset function to have any change to the chosen offset automatically call a Lua function in the same plug-in. No loops then -- FSUIPC keeps the plug-in in memory ready to call the listed event function..

If you use that latter way then the actual parking brake function itself would also best be performed by the plug-in.

There are several other ways, variations on the above.

Regards

Pete

Link to comment
Share on other sites

Pete,

Here is what I ended up with. It works fine, however I would be interested in using the "event.offset", just not sure how to set it up.

while 1 do

PB=ipc.readSW(0x0BC8)

Axis=ipc.readSD(0x66C1)

if (Axis>0) and (PB>-1) then

ipc.writeSW(0x0BC8,0)

end

if (Axis<0) and (PB>-1) then

ipc.writeSW(0x0BC8,-1)

ipc.sleep(10)

end

end

I assigned the axis to offset 66C1.

Jim

Link to comment
Share on other sites

I would be interested in using the "event.offset", just not sure how to set it up.

I haven't got time today to write it for you, but it goes roughly like this:

function checkmystuff(off, val)
-- Here do what you want with the offset and value -- you don't need to read the offset, the value is provided as the second parameter, i.e. val here
-- no need to loop at all, you'll be called when the offset changes again
end

event.offset(0x66D1, "SD", "checkmystuff")

You'd start this running somehow, either by Macro call in an ipcReady.lua file or by adding it to the [Auto] section in the INI file. Whatever you are doing to run yours.

BTW, In your program you should put that "ipc.sleep(10) call in the outside loop, just before the last "end", else its only sleeping on that last "if" condition.

Regards

Pete

Link to comment
Share on other sites

Pete,

Thanks for the info. Still no luck figuring out the event.offset, however I am pleased that I got it working anyway, given that I have nil in programming experience. Placing the last "ipc.sleep(10)" call in the outside loop also solved another problem that I as having. I had copied the Lua plugin to do a similar thing with my cut off switches, which I also have assigned to axis, however when I attempted to load it together with the on for the ParkBrake axis, FS9 would freeze up and become unresponsive. After moving the "icp.sleep(10)" to outside the loop, the problem disappeared.

On another note, I am really interested in using your plug in that allows the user to assign three commands to a single button. I have somewhat successful incorporated it into my setup, however I am having a problem using it with more then a single button. How can I add more button assignments to the one Lua plug-in? I assume that one does not need to write a separate Lua plug-in for each button, but rather have the one plug-in able to control several buttons. If you could just provide me with the required modifications, when you get the time, that I would have to make to your existing Lua plug-in file, I would certainly be grateful.

Jim

Link to comment
Share on other sites

Still no luck figuring out the event.offset

What was wrong with what I gave you above? You only had to plug your code in where it says so! What don't you understand? The code you needed was in my message.

On another note, I am really interested in using your plug in that allows the user to assign three commands to a single button. I have somewhat successful incorporated it into my setup, however I am having a problem using it with more then a single button. How can I add more button assignments to the one Lua plug-in?

That would get pretty complicated unless you can be sure that you'd never use any two such buttons so close that the timings overlap. If you wanted it completely general it would be far easier to have a separate little Lua module for each button. FSUIPC allows up to 255 altogether.

There might be a way of doing it using a different method, but it would take me a while to think it through. how many buttons are you thinking of using it for?

Pete

Link to comment
Share on other sites

Pete,

I successfully got the Lua plug for the axis working using event.offset.

To my second request, I was thinking of using as many as 12 buttons in total (CH Throttle Quadrant). If I have to write a Lua plug-in for each, that would be fine. Having said that, I tried last evening to run two of the "triple use" Lua plug-ins using [Auto] in my fsuipc.ini file. I simply modified your code, changing the commands "ipc.control(#####)" with "ipc.macro("macroname"). Individually, each Lua plug-in works no problem. However, when I try both files together, they work for maybe one or two button presses, then FS9.1 freezes up and I have to "End Program" via right click in the task bar.

Some info that you may require: I am running FS9.1 on a Vista Home 64bit machine, am using FSUIPC 3.966, and am using the AutoAssignLetters=Yes in the .ini file, however in the Lua "triple use" files, I have use "J=number" instead of "J=letter". I tried switching to letters in the Lua plug-in; however, it no longer functioned at all.

Jim

Link to comment
Share on other sites

Individually, each Lua plug-in works no problem. However, when I try both files together, they work for maybe one or two button presses, then FS9.1 freezes up and I have to "End Program" via right click in the task bar.

Hmmm. Shouldn't hang. Do you mean pressing both buttons together? They might interfere with each other though. I'll have to investigate that -- it won't be today though. Perhaps you could Zip up the Lua programs and send them to me at petedowson@btconnect.com , so I can use exactly what you use.

in the Lua "triple use" files, I have use "J=number" instead of "J=letter". I tried switching to letters in the Lua plug-in; however, it no longer functioned at all.

Ah, yes. The Lua code accesses the memory locations for each joystick, and isn't aware of the Letter assignments. I never thought of that . If possible I'll make it accept those too, in a later version. Thanks,

Pete

Link to comment
Share on other sites

I press one button, and then a second button. I even wait a second or two between button presses; It still hangs.

I've found it -- it's a bug in the "event.cancel" function which only manifests itself when two or more plug-ins are using it.

I've fixed it in versions 4.586 and 3.969, now available in the Updates Announcement. I think you will be able to handle your 12 buttons easily now.

I did think of a way to handle all 12 in one plug-in, but it is quite complex, involving arrays and indexing, and, quite honestly, I think it would be less efficient in terms of performance and demands on the PC than having the 12 little simpler plug-ins. After all, those are merely taking up a little memory space, only running when triggered by the appropriate button. That would not be the case the the other way of doing it which would involve monitoring all the buttons closely enough to detect short press-release cycles.

Have fun!

Regards

Pete

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.