Jump to content
The simFlight Network Forums

Recommended Posts

Posted (edited)

Hello there!

 

I'm very new to FSUIPC (order #1342052) and still getting acquainted with this marvellous bit of software. Thank you Pete! So far (as well as the basic stuff like axis assignments) I've managed to program hardware buttons to control some features in A2A aircraft using their published L:Var list. For example, their PA-24 and PA-28 use an S-TEC System 30 autopilot with a 'turn knob' (separate to the HDG knob). I've set up a hardware button to drive this anticlockwise using its L:Var - L:ApTurnKnob. The knob has a range of -50 (full left) to 50 (full right) with 0 of course being the centre position. Therefore it's set as follows...

 

FSUIPC_APknob1.jpg?dl=0

 

This works wonderfully and I use another button to turn the knob clockwise.

 

I also managed to instantaneously centre the knob with a third button using the L:ApTurnKnob=SET action. However, this leads onto my question... I'd love to be able to smoothly centre the knob by pressing a single button using the 'INC' and 'DEC' actions; however, I suspect that a custom Lua function will be necessary for this.

 

The basic logic would be that if the knob's position was <0 then it should INC until reaching zero, and if its position was >0 then it should DEC until it's at zero. I'm sure this is a pretty simple undertaking, and if anyone could suggest a bit of code that would do this, I'd be very grateful. It's a rather trivial little thing I know, but it should help get me started with some simple Lua programming and the basic syntax etc. (I'm completely new to Lua but have worked with Visual Basic a bit.)

 

Thanks,

Nick

 

P.S. I also tried using the parameter of 0 in the example pictured above, expecting this would cause the knob to stop at its centred position but it didn't - it's just kept turning. The closest I could get was using a -1 or 1 parameter. Anyway, this is a slight aside as ideally I'd like to be able to just use a single hardware button to centre the knob.  ;-)

 

(...Edited to improve post title.)

Edited by Nick M
Posted

I have not looked into the details, but it seems to me you could use a Lua program that would be activated by a "return to center" keystroke or button. The Lua program would read the current turn knob value (apparently somewhere between -50 and 50). Based on the value found (positive , 0, or negative), the program would then enter a loop if the value was not 0. Each iteration of the loop would inc or dec the turn value (based on whether the value was neg or positive), and then compare the new turn value to 0. If the new value was not 0, the loop would repeat, etc, until a turn value of 0 was reached e.g., a  while turnknob ~= 0 do  type of loop ** . The loop could include a Lua ipc.sleep(n) function (where n is millisecs of delay) so the value of n would determine the rate at which the turn value of 0 was reached. This all assumes there are Lvars for reading, incrementing, and decrementing the turn knob value.

Al

 

** If the turnknob values are not restricted to integers, then comparing to 0 becomes more involved since as I understand it, Lua uses a floating point format to represent all numbers. A Google search on this should help explain the issues.

Posted

Thanks Al for the suggestion. ;-)  I think I need to have a dig around for some snippets of code that do similar things. I'm not sure if a loop would be overcomplicating things more than needed though, as (presumably) there's a way to just invoke the increment or decrement action via a Lua function until the specified parameter (in this case 0) is met? As far as I'm aware, the turn knob values are just integers so sounds like that should help to simplify things.

 

I'll post the question on the A2A forums when I get round to it as I know there are a few Lua gurus there too.

 

Thanks again,

Nick

Posted

Thanks Al for the suggestion. ;-)  I think I need to have a dig around for some snippets of code that do similar things. I'm not sure if a loop would be overcomplicating things more than needed though, as (presumably) there's a way to just invoke the increment or decrement action via a Lua function until the specified parameter (in this case 0) is met? As far as I'm aware, the turn knob values are just integers so sounds like that should help to simplify things.

 

I'll post the question on the A2A forums when I get round to it as I know there are a few Lua gurus there too.

 

Thanks again,

Nick

Since the control repeats while held, if you don't include a value in the FSUIPC paramater field,  can you control the amount of bank by just how long you hold down the related left or right bank control button? If so, it would seem you could use this technique to set arbitrary degrees of bank, and also to return the a/c to level flight.

Al

Posted

Adding to the above posters suggestion, I often use the lua display function to give me feedback when I am changing various functions with either a button assignment or axis. The display can be triggered by the change in value of an FSX offset, a user-defined offset, or any of the other 'on event' operations.

 

Here is an example of a lua routine that displays the current value of rudder trim:

  function Disp_Rud_Trim(control,Dummy)

	RTrim_in = ipc.readSW(0x0C04)

    Rud_Trim = (round(RTrim_in/163.83))/10

    ipc.display("Rud Trim = "..Rud_Trim, 1)

  end
  
-- rounds the integer
  function round(num)
	num = tonumber(num)
	if num == nil then return 0 end
    if num >= 0 then return math.floor(num+.5)
    else return math.ceil(num-.5) end
  end
  
event.offset(0x0C04,"SW","Disp_Rud_Trim")

In this case, the event that triggers the display is a change in the value at offset 0x0C04. You could also have an L:Var cause the triggering of the display. The duration of the display is user-defined. In the above case, 1 second.

 

Here is an example using an L:Var. In this case I am controling the A2A C182 panel light and also displaying the setting as it changes


  function Panel_Light(control, Knob_in)

  if Knob_Out_Last == nil then Knob_Out_Last = 0 end

  Knob_Out = (round((Knob_in + 16383)/1024))

    if Knob_Out < 0 then Knob_Out = 0 end

  ipc.writeLvar("L:PanelLightKnob", Knob_Out)

    if Knob_Out > Knob_Out_Last + 0.5 or Knob_Out < Knob_Out_Last - 0.5 then

    ipc.display("Panel Light = "..Knob_Out, 1) end

  Knob_Out_Last = Knob_Out

  end

-- rounds the integer
  function round(num)
	num = tonumber(num)
	if num == nil then return 0 end
    if num >= 0 then return math.floor(num+.5)
    else return math.ceil(num-.5) end
  end
  
-- Here the desired panel light setting is written to user-defined offset 0x66D2
-- When that value changes this event statement triggers the code above.
-- The value is in the range of +\- 65535, a signed word from an axis, and
-- scaled above for output to the L:Variable and also displayed for 1 second.

event.offset(0x66D2,"SW","Panel_Light")


All of these options are detailed in the FSUIPC4 for Advanced Users.pdf

 

   Paul

Posted

Thanks chaps!

 

Since the control repeats while held, if you don't include a value in the FSUIPC paramater field,  can you control the amount of bank by just how long you hold down the related left or right bank control button? If so, it would seem you could use this technique to set arbitrary degrees of bank, and also to return the a/c to level flight.

Al

 

Yeah maybe Al. Perhaps this would simply the Lua function needed. Basically I'd just need a function which would check whether the existing value was negative or positive and then increment or decrement accordingly as long as the button was pressed in.

 

 

Here is an example using an L:Var. [...]

 

Paul, thanks very much for these examples: they're exactly what I need to get started. This last Sept weekend here in the UK has seen some glorious weather, so no fiddling with FS or Lua stuff and I'm away with work for a couple of weeks from tomorrow. However, when I get back I'll revisit this topic. Thanks again fellas! ;-) 

 

Nick 

Posted

Just FYI, I have the A2A Cherokee and was able to verify that if you set the control to repeat while held you can in fact control the amount of bank by just how long you hold down the related left or right bank control button, and you can also return the a/c to level flight the same way.  That seems to mirror the situation in the real a/c where you have to turn the knob to either set the amount of bank or return the a/c to level flight.  I do understand, however, you are trying to implement something a little different.

Al

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.