Jump to content
The simFlight Network Forums

fuel weight problem


Recommended Posts

Using default cessna, both tanks 99.9% full yields approx 314lbs.

Problem is that I read 330lbs.

If I set the tanks to 20lbs each I should then read 40lbs yet I read 2700lbs.

I reckon its something to do with signed/unsigned numbers but I cant seem to find where its going wrong. Can anyone see the problem please?

Here is my code for the fuel calculation. To call the function I use

Dim FuelWT As Integer

FuelWT = GetFuelWt()

Function GetFuelWt() As Integer

Dim dwResult As Integer

Dim token As Integer

Dim gallonslbs As Short

Dim RightTankCapacity As Integer

Dim LeftTankCapacity As Integer

Dim RightTankLevel As Integer

Dim LeftTankLevel As Integer

Dim f As Integer

'get lbs per gallon wt

If Not FSUIPC_Read(&HAF4, 2, token, dwResult) Then

lblfuelwt.Text = "error - process code: " + dwResult.ToString

Else

If Not FSUIPC_Process(dwResult) Then

lblfuelwt.Text = "process code: " + dwResult.ToString

End If

FSUIPC_Get(token, gallonslbs)

gallonslbs = gallonslbs / 256

End If

'get capacity of right fuel tank

If Not FSUIPC_Read(&HB98, 4, token, dwResult) Then

lblfuelwt.Text = "error - process code: " + dwResult.ToString

Else

If Not FSUIPC_Process(dwResult) Then

lblfuelwt.Text = "process code: " + dwResult.ToString

End If

FSUIPC_Get(token, RightTankCapacity)

End If

'get capacity of left fuel tank

If Not FSUIPC_Read(&HB80, 4, token, dwResult) Then

lblfuelwt.Text = "error - process code: " + dwResult.ToString

Else

If Not FSUIPC_Process(dwResult) Then

lblfuelwt.Text = "process code: " + dwResult.ToString

End If

FSUIPC_Get(token, LeftTankCapacity)

End If

'get capacity % of fuel in right tank

If Not FSUIPC_Read(&HB94, 4, token, dwResult) Then

lblfuelwt.Text = "error - process code: " + dwResult.ToString

Else

If Not FSUIPC_Process(dwResult) Then

lblfuelwt.Text = "process code: " + dwResult.ToString

End If

FSUIPC_Get(token, RightTankLevel)

RightTankLevel = RightTankLevel * 100 / (128 * 65536)

End If

'get capacity % of fuel in left tank

If Not FSUIPC_Read(&HB7C, 4, token, dwResult) Then

lblfuelwt.Text = "error - process code: " + dwResult.ToString

Else

If Not FSUIPC_Process(dwResult) Then

lblfuelwt.Text = "process code: " + dwResult.ToString

End If

FSUIPC_Get(token, LeftTankLevel)

LeftTankLevel = LeftTankLevel * 100 / (128 * 65536)

End If

'total fuel weight

f = ((RightTankCapacity * gallonslbs) * (100 / RightTankLevel)) + ((LeftTankCapacity * gallonslbs) * (100 / LeftTankLevel))

Return f

End Function

Many thanks

Graham

Link to comment
Share on other sites

Using default cessna, both tanks 99.9% full yields approx 314lbs.

Problem is that I read 330lbs.

If I set the tanks to 20lbs each I should then read 40lbs yet I read 2700lbs.

2700 ????

Here is my code for the fuel calculation.

As an aside, not relevant to your problem, but you should not really be calling FSUIPC_Process individually for each value, as each one invokes a process switch. It is very inefficient. Do all the Reads and Writes you want to do, then the Process, then the Gets and calculations.

One reason for a little inaccuracy could be here:

gallonslbs = gallonslbs / 256

You are assuming a whole number of lbs to the gallon. With a number as small as 6, lopping off the fractional part could cost you anything up to 16% You should keep the original and simply divide by 256 at the end, when you've got the results in units of 256ths of a lb.

f = ((RightTankCapacity * gallonslbs) * (100 / RightTankLevel)) + ((LeftTankCapacity * gallonslbs) * (100 / LeftTankLevel))

Why are you multiplying by 100 in these? You already converted the levels to a % between 0 and 100. If, say, the tanks were half full so these values were 50, you'd end up with a total twice the total possible. You should be using (TankLevel / 100) as the proportional multiplier, not the inverse.

This sort of error isn't related to FS data or interfacing at all. You need to "dry run" your code before compiling it -- i.e. follow it through logically, with pencil and paper and hypothetical figures. You'd soon find simple errors like this then.

Regards,

Pete

Link to comment
Share on other sites

thanks Pete,

I re-arranged my formula thus

f = ((RightTankCapacity * gallonslbs / 100) * RightTankLevel) + ((LeftTankCapacity * gallonslbs / 100) * LeftTankLevel)

and it works ok now.

I worked it out correctly on a calculator using values read from FSInterrogate and the results were correct, however when I came to coding the formula I put things in the wrong order ! (I didnt check the formula as I was sure I had it right)

Should I be creating new threads for each question that I have or should I stick them on the one thread?

I am currently working on a flight monitor program for the Flight1 Cessna 172R. This will give alarms based on speed, weight, flap settings etc from the manual as I want a sense of responsibility when flying. Aside from FSPassengers there is nothing I can find to monitor to check if the aircraft is being flown as it should. So on that point I will no doubt have more questions for this board soon.

Best regards

Graham

Link to comment
Share on other sites

Should I be creating new threads for each question that I have or should I stick them on the one thread?

Providing others can find the dtails by the subject at the top it doesn't matter. Best to choose a specific heading though -- some folks use things like "FSUIPC?" etc, which doesn't help others much.

Aside from FSPassengers there is nothing I can find to monitor to check if the aircraft is being flown as it should.

FS FlightKeeper is good, and it has lots of extras too, but it isn't free.

Regards,

Pete

Link to comment
Share on other sites

Pete, you informed me to be efficient and do the reads/writes, then the processes instead of doing a separate process for each read/write.

Im changing my code but am not too sure about how to separate the process from the reads. I will create an array to hold the data later on as it is messy having 30+ separate variables for similar things!

Below is what I have started with. Running the below gives me a zerofuelweight of around 19909989090 with zero payloads :). If I run only the zerofuel weight section it runs fine and reports 1990lbs. The way I have incorrectly set the code out adds to the zerofuelweight.

Any advice please on exactly how I can split them up into the sections that you recommend?

Many thanks

Graham

Dim t1, t2, t3, t4, t5, t6, t7, t8, t9, t10 As Integer 'token

Dim r1, r2, r3, r4, r5, r6, r7, r8, r9, r10 As Integer 'result

Dim pay As Double 'temp holder for payload weight

FSUIPC_Read(&H3BFC, 4, t1, r1) 'zero fuel weight

FSUIPC_Read(&H1400, 8, t2, r2) 'payload station 0

FSUIPC_Read(&H1430, 8, t3, r3) 'payload station 1

FSUIPC_Read(&H1460, 8, t4, r4) 'payload station 2

FSUIPC_Read(&H1490, 8, t5, r5) 'payload station 3

FSUIPC_Read(&H14C0, 8, t6, r6) 'payload station 4

If FSUIPC_Process(r1) Then

FSUIPC_Get(t1, ZeroFuelWT)

ZeroFuelWT = ZeroFuelWT / 256

End If

If FSUIPC_Process(r2) Then

FSUIPC_Get(t2, pay)

PayloadWT += pay

End If

If FSUIPC_Process(r3) Then

FSUIPC_Get(t3, pay)

PayloadWT += pay

End If

If FSUIPC_Process(r4) Then

FSUIPC_Get(t4, pay)

PayloadWT += pay

End If

If FSUIPC_Process(r5) Then

FSUIPC_Get(t5, pay)

PayloadWT += pay

End If

If FSUIPC_Process(r6) Then

FSUIPC_Get(t6, pay)

PayloadWT += pay

End If

Link to comment
Share on other sites

Pete, you informed me to be efficient and do the reads/writes, then the processes instead of doing a separate process for each read/write.

No! You do all the reads and writes, as you like. Then only ONE process! The reads and writes (and gets) take almost zero time, the Process calls cause a process switch and take maybe hundreds of milliseconds each! One process call performs ALL your reads and writes in one call!

Any advice please on exactly how I can split them up into the sections that you recommend?

Delete most of the code! For example:

FSUIPC_Read(&H3BFC, 4, t1, r1) 'zero fuel weight

FSUIPC_Read(&H1400, 8, t2, r1) 'payload station 0

FSUIPC_Read(&H1430, 8, t3, r1) 'payload station 1

FSUIPC_Read(&H1460, 8, t4, r1) 'payload station 2

FSUIPC_Read(&H1490, 8, t5, r1) 'payload station 3

FSUIPC_Read(&H14C0, 8, t6, r1) 'payload station 4

If FSUIPC_Process(r1) Then

FSUIPC_Get(t1, ZeroFuelWT)

ZeroFuelWT = ZeroFuelWT / 256

FSUIPC_Get(t2, pay)

PayloadWT += pay

FSUIPC_Get(t3, pay)

PayloadWT += pay

FSUIPC_Get(t4, pay)

PayloadWT += pay

FSUIPC_Get(t5, pay)

PayloadWT += pay

FSUIPC_Get(t6, pay)

PayloadWT += pay

End If

See? The single Process processes all of the queued Reads and Writes. That's its job, to pass ALL of your requests as one batch to FS! The only limit is the total size -- only 31000 or so bytes can be handled in any one Process request, counting the data plus red tape overhead of 16 bytes per read or write. Your list is nowhere near.

Since you never process any of the results, why have a separate variable (r1 ...) for each? Even if you did check it, you should do so immediately, so you still only need one variable!

Of course you could do the sequential Reads and Gets in a loop if you put the t1's to tn's in an array.

Also, there's something wrong with your code in any case. You have

Dim t1, t2, t3, t4, t5, t6, t7, t8, t9, t10 As Integer 'token

but you are only reading an Integer into t1. The others are 64-bit doubles, as you seem to have recognised in the Reads by making the size 8 bytes.

BTW I'm no expert, but doesn't the ZFW, by definition, already include all the payload, merely excluding the Fuel?

Pete

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.