Jump to content
The simFlight Network Forums

A little help with LUA please


Recommended Posts

Many years ago Pete Dowson gave me his lua file for monitoring various values in FSX and then P3D.

I've added to it but one small problem remains and I can't solve it. Here's the code...

--Display Pitch Angle...
    pit=ipc.readUD(0x0578)
    mypit = pit*360/(65536*65536)
    if mypit <= 20 then --descending
      mypit = math.floor(-mypit *10) / 10
      cond="c1"
    else --ascending
      mypit = pit*360/(65536*65536)
      mypit = 360 - math.floor(mypit *10) / 10
      cond="c2"
    end

A value is displayed but sometimes shows decimal places to 8 or more places. I'd like just 1 please.

What is wrong with it? Any help gratefully appreciated. 😁

Link to comment
Share on other sites

Hi Ray,

not sure what value you are trying to display in that code extract (mypit?). but if you want to format a lua number as a string, you can use the lua string.format function to convert it first, e.g. for one decimal place, use

    local mypitString  = string.format("%.1f", mypit)

See https://www.codecademy.com/resources/docs/lua/strings/format

Cheers,

John

  • Thanks 1
Link to comment
Share on other sites

Hi John,

Thanks for your quick reply. “mypit” just refers to the quick ‘n dirty name I gave to the Pitch Angle of the aircraft. It’s especially critical for Concorde as it’s quite possible to stall even with full power plus reheat. 🤣

I’m looking to display a number to one decimal place such as 8.9. Would a string still be the most appropriate?

 

Link to comment
Share on other sites

2 hours ago, Ray Proudfoot said:

I’m looking to display a number to one decimal place such as 8.9. Would a string still be the most appropriate?

That depends what you mean by "display", and whatever you are passing the value to display expects. So, if you were logging it, ipc.log expects a string, so you would use, for example
    ipc.log("mypit = " .. string.format("%.1f", mypit))

If you want to keep it as a number, you can use:
    mypit = tonumber(string.format("%.1f", mypit))

Link to comment
Share on other sites

It’s displayed in a small window on a WideFS PC. I have other sim values shows such as sim local time, sim UT time, number of Ai aircraft on the ground and airborne.

It’s updated once per second. Your second example looks like what I need. I’ll use that and report back tomorrow. Thanks John. 👍

Link to comment
Share on other sites

1 hour ago, Ray Proudfoot said:

This is when the aircraft is climbing so I would expect a value of 10-15 degrees.

Then your calculations are wrong. The description for offset 0x0578 reads:

Quote

Pitch, *360/(65536*65536) for degrees. 0=level, –ve=pitch up, +ve=pitch down

Your code:

--Display Pitch Angle...
    pit=ipc.readUD(0x0578)
    mypit = pit*360/(65536*65536)                 -- 1
    if mypit <= 20 then --descending              -- 2
      mypit = math.floor(-mypit *10) / 10
      cond="c1"
    else --ascending
      mypit = pit*360/(65536*65536)               -- 3
      mypit = 360 - math.floor(mypit *10) / 10    -- 4
      cond="c2"
    end

You calculate the degree angle correctly (1).
But then you have a conditional on 'mypit < 20 then -- descending' (2) - why?  Descending is +ve pitch (pitch down), so should be 'mpit >= 0', no? 
For ascending/pitch up, the value is negative. But you are again converting to degrees (3) where this has been done already (1).
You are then subtracting this from 360, which I also don't understand.

Do you want to show +ve pitch for ascending, and -ve pitch for descending (i.e. opposite of what offset 0x0578 holds? Or do you always want to show +ve values for both., in which case you would need to subtract the -ve pitch value from 360. 

Link to comment
Share on other sites

Hi John,

I wrote this code several years ago and it appeared to deliver what I wanted but with the exception of multiple decimal places being displayed under certain conditions.

What those conditions are I can’t determine but think it was only when the aircraft was descending.

To answer your question on why mpit changes when <=20 I can’t honestly say. Too long ago.

During the descent today the pitch value remained positive. Around 1.2. I would have expected it to be a negative number.

43 minutes ago, John Dowson said:

Do you want to show +ve pitch for ascending, and -ve pitch for descending (i.e. opposite of what offset 0x0578 holds? Or do you always want to show +ve values for both

I’m not sure. Could you give an example of both and I’ll use whichever seems best.

Thanks.

Link to comment
Share on other sites

2 hours ago, Ray Proudfoot said:

I wrote this code several years ago and it appeared to deliver what I wanted but with the exception of multiple decimal places being displayed under certain conditions.

I showed you how to restrict to 1 decimal place. Thats all the code I showed you does - it does not change the values. So, if the values are wrong, they have always been this way.

2 hours ago, Ray Proudfoot said:

During the descent today the pitch value remained positive. Around 1.2. I would have expected it to be a negative number.

What pitch value? The value in offset 0x0578 will be positive on pitch down, With your code, it will depend on the pitch angle, as you have a conditional on the is being less than 20 for some reason, as I said...

2 hours ago, Ray Proudfoot said:

I’m not sure. Could you give an example of both and I’ll use whichever seems best.

?

When you take-off (pitch-up) so you expect to see a positive or negative pitch? Offset 0x0578 will show negative.
When landing (pitch-down), do you expect to see a positive or negative pitch?  Offset 0x0578 will show positive.

Why don't you just try this:

--Display Pitch Angle...
    pit=ipc.readUD(0x0578) -- 0=level, –ve=pitchup, +ve=pitch down
    mypit = -pit*360/(65536*65536)  -- convert to degrees, also reverse sign so -ve descending/pitch down, +ve ascending/pitch up
    mypit = tonumber(string.format("%.1f", math.floor(mypit *10) / 10))) -- ensure value is to 1 decimal place
    if mypit < 0 then --descending
      cond="c1"
    else --ascending
      cond="c2"
    end

I have just adjusted as per the comments I have previously made...

Try adjusting yourself if that is not what you want...

John

Link to comment
Share on other sites

Hi John,

I pasted your code in and tried a flight. During the climb it was showing a value like -350 which appeared to equate to 10° nose up.

To answer your question, yes, climbing should show a positive pitch value. Nose down during descent will show a negative value.

During descent it showed -3.5 which appears correct.

I looked at your code but couldn’t figure out what to change. So I reverted to my original code and both climb and descent values were correct despite your suspicions about my code.

Incidentally I don’t use c1 or c2 elsewhere so I’m not sure they’re doing anything.

I can live with what I have as the numerous decimal places are only an occasional nuisance. Thanks for your help nevertheless. 😁

Link to comment
Share on other sites

19 hours ago, Ray Proudfoot said:

During the climb it was showing a value like -350 which appeared to equate to 10° nose up.

That is strange...maybe I should have checked this in P3d (I checked in MSFS as that is what I had running).

19 hours ago, Ray Proudfoot said:

I looked at your code but couldn’t figure out what to change. So I reverted to my original code and both climb and descent values were correct despite your suspicions about my code.

I can't see how your code can work because of this:
    

if mypit <= 20 then --descending

Why is 19 descending, -19 descending, but 21 ascending?

Maybe try something like this:

-Display Pitch Angle...
    pit=ipc.readUD(0x0578) -- 0=level, –ve=pitchup, +ve=pitch down
    mypit = -pit*360/(65536*65536)  -- convert to degrees, also reverse sign so -ve descending/pitch down, +ve ascending/pitch up
    if mypit < -180 then 
      mypit = 360 + mypit
    end
    mypit = tonumber(string.format("%.1f", math.floor(mypit *10) / 10)) -- ensure value is to 1 decimal place

I can fire-up P3D to check there but not today...

 

John

Edited by John Dowson
script corrected
Link to comment
Share on other sites

2 hours ago, Ray Proudfoot said:

No idea about that IF condition. I’ll remove it and see what happens.

If you just remove that, it will brake. You have to correct it. But, you say this is working. If your original code WAS working (which I doubt), then just converting mypit to have 1 decimal place should not change anything, except to display the value with 1 d.p. But you saiy this didn't work.

2 hours ago, Ray Proudfoot said:

I’ll try the above tomorrow and report back. 👍

Ok.

 

Link to comment
Share on other sites

Hi Ray,

I just tested this with a simple lua:

function PitchDisplay(offset, pit)
	-- Ray's code
	local mypit = pit*360/(65536*65536)
	if mypit <= 20 then --descending
		mypit = math.floor(-mypit *10) / 10
	else --ascending
		mypit = pit*360/(65536*65536)
		mypit = 360 - math.floor(mypit *10) / 10
	end
	local mypit1 = tonumber(string.format("%.1f", mypit))
	
	-- My Code
	local jdPit = -pit*360/(65536*65536)  -- convert to degrees, also reverse sign so -ve descending/pitch down, +ve ascending/pitch up
	if jdPit < -180 then 
		jdPit = 360 + jdPit
	end
	jdPit = tonumber(string.format("%.1f", math.floor(jdPit *10) / 10)) -- ensure value is to 1 decimal place
	
	displayString = "Ray's original pitch =" .. mypit .. ", Ray's 1dp pitch = " .. mypit1 .. ", my Pitch = " .. jdPit
	ipc.log(displayString)
	ipc.display(displayString)
end

event.offset(0x0578, "SD", "PitchDisplay")

When running this I got mostly the same number printed each time, the only difference being when the pitch falls below -20:

Quote

   955062 LUA.5: Ray's original pitch =-8.8, Ray's 1dp pitch = -8.8, my Pitch = -8.8
   955094 LUA.5: Ray's original pitch =-9.3, Ray's 1dp pitch = -9.3, my Pitch = -9.3
   955141 LUA.5: Ray's original pitch =-10, Ray's 1dp pitch = -10, my Pitch = -10
   955172 LUA.5: Ray's original pitch =-10.6, Ray's 1dp pitch = -10.6, my Pitch = -10.6
   955219 LUA.5: Ray's original pitch =-11.3, Ray's 1dp pitch = -11.3, my Pitch = -11.3
   955250 LUA.5: Ray's original pitch =-11.9, Ray's 1dp pitch = -11.9, my Pitch = -11.9
   955281 LUA.5: Ray's original pitch =-12.5, Ray's 1dp pitch = -12.5, my Pitch = -12.5
   955328 LUA.5: Ray's original pitch =-13.2, Ray's 1dp pitch = -13.2, my Pitch = -13.2
   955359 LUA.5: Ray's original pitch =-13.8, Ray's 1dp pitch = -13.8, my Pitch = -13.8
   955406 LUA.5: Ray's original pitch =-14.3, Ray's 1dp pitch = -14.3, my Pitch = -14.3
   955437 LUA.5: Ray's original pitch =-15, Ray's 1dp pitch = -15, my Pitch = -15
   955469 LUA.5: Ray's original pitch =-15.6, Ray's 1dp pitch = -15.6, my Pitch = -15.6
   955516 LUA.5: Ray's original pitch =-16.2, Ray's 1dp pitch = -16.2, my Pitch = -16.2
   955547 LUA.5: Ray's original pitch =-16.8, Ray's 1dp pitch = -16.8, my Pitch = -16.8
   955594 LUA.5: Ray's original pitch =-17.4, Ray's 1dp pitch = -17.4, my Pitch = -17.4
   955625 LUA.5: Ray's original pitch =-17.9, Ray's 1dp pitch = -17.9, my Pitch = -17.9
   955656 LUA.5: Ray's original pitch =-18.5, Ray's 1dp pitch = -18.5, my Pitch = -18.5
   955703 LUA.5: Ray's original pitch =-19.1, Ray's 1dp pitch = -19.1, my Pitch = -19.1
   955734 LUA.5: Ray's original pitch =-19.7, Ray's 1dp pitch = -19.7, my Pitch = -19.7
   955781 LUA.5: Ray's original pitch =339.9, Ray's 1dp pitch = 339.9, my Pitch = -20.2
   955812 LUA.5: Ray's original pitch =339.4, Ray's 1dp pitch = 339.4, my Pitch = -20.7
   955844 LUA.5: Ray's original pitch =338.9, Ray's 1dp pitch = 338.9, my Pitch = -21.2
   955891 LUA.5: Ray's original pitch =338.4, Ray's 1dp pitch = 338.4, my Pitch = -21.7
   955922 LUA.5: Ray's original pitch =337.9, Ray's 1dp pitch = 337.9, my Pitch = -22.2
   955953 LUA.5: Ray's original pitch =337.4, Ray's 1dp pitch = 337.4, my Pitch = -22.7
 

So it seems your original code displays to 1dp anyway!

Cheers,

John

Link to comment
Share on other sites

Hi John,

How strange. I've never seen values like 337 etc. If my aircraft pitched up that much I'd be virtually dead especially with a B737 or Concorde.

As I said it was only on occasions the lua code would result in 8 decimal places or more. I'll try and replicate the situation and report back.

It's like an itch you need to scratch isn't it? 😁

Link to comment
Share on other sites

5 minutes ago, Ray Proudfoot said:

How strange. I've never seen values like 337 etc. If my aircraft pitched up that much I'd be virtually dead especially with a B737 or Concorde.

That was pitch-down - I went to the extremes to test! The maximum pitch-up I tested was around 50 degrees:

Quote

   948422 LUA.5: Ray's original pitch =50.3, Ray's 1dp pitch = 50.3, my Pitch = 50.3

 

8 minutes ago, Ray Proudfoot said:

As I said it was only on occasions the lua code would result in 8 decimal places or more.

Hmm, strange - I am not sure why, but the code I showed you should always restrict this to 1dp.

John

Link to comment
Share on other sites

Hi John,

I had two flights today and in neither did the pitch calculations misbehave. I’ll try with a different aircraft tomorrow but given you’re busy with MSFS I won’t take up any more of your time.

Thanks.

Link to comment
Share on other sites

1 minute ago, Ray Proudfoot said:

I had two flights today and in neither did the pitch calculations misbehave.

Well, they won't as you never pitch down for than 20 degrees.

2 minutes ago, Ray Proudfoot said:

but given you’re busy with MSFS I won’t take up any more of your time.

I released the next version of FSUIPC7 last night, so the pressure is off, and I am going to have a few days off (well, not off, but more relaxed!).
But I don't really have anything else to say on this subject that I haven't already. I don't understand why your original code doesn't always display to one dp (it does here), but I am not going to worry about that. Use any of the three options - your original code, original code + conversion to 1dp, or my code. They are all pretty much the same except for the difference I have already pointed out.

Cheers,

John

Link to comment
Share on other sites

53 minutes ago, John Dowson said:

Well, they won't as you never pitch down for than 20 degrees.

But it has occurred with shallower descents. My eyes aren’t deceiving me, honest. 😁

Thanks for your insight John. Have a well deserved break. 👍

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.