Jump to content
The simFlight Network Forums

Recommended Posts

Posted

Dear all,

hopefully someone can provide some assistance , I'm just starting to wrote code to access FS data via FSUIPC so apologies in advance if the anwser to this is obvious.

I'm trying to access FS2002 data using FSUIPC from an application written using Borland C++ Builder.

I've been able to succesfully access data such as ASI , ALT, VSI, Compass and ADF however I have not been able to produce any meaniful results from HDG. ( i have not tried anything else yet).

I'm using FSUIPC v 3.212

Any advice would be appreciated.

regards

Gilles.

The following is a snippet of my code:

double hdg;

// - code removed for clarity.

FSUIPC_Read(0x580, 4, &hdg, &dwResult);

FSUIPC_Process(&dwResult); // Process the request(s)

// Process Heading

hdg=hdg*360/(65536*65536);

AnsiString sHdg=hdg;

RawData->eDG->Text=sHdg; // Send to Editbox for viewing.

Posted

I'm using FSUIPC v 3.212

That's getting pretty old now. Please try to keep up to date. Current is 3.30.

hdg=hdg*360/(65536*65536);

What is "hdg" defined as? If it is correctly declared as a 32-bit unsigned integer (i.e. a DWORD in Windows terms), then there are two errors here:

1. Multiplying it by 360 will probably make the value too large to fit -- after all, the scaling in FS is deliberately designed to make the largest possible value (just under 360) fit exactly into those 32 bits (ie. as 0xFFFFFFFF or exactly 65536*65536-1. Think about what happens when you multiply the biggest possible number by 360.

2. Dividing a 32-bit value by 65536*65536, which is one more than the biggest number that will fit into 32 bits, will give zero, if it works at all.

You need to copy the value you read into something bigger for this to work -- either a 64 bit integer (if you don't care about fractions of a degree), or, better, a floating point number (then you get fractions too).

If all you want it the nearest degree you could do it something like this:

hdg = (((hdg + 32768) / 65536) * 360) / 65536;

This way you keep the value within the 32-bit capacity of "hdg" at each stage.

I've added 32768 to start with to round up the fractional part if it is .5 or above. However, this may give you 360 instead of 0 from a value like 359.5 or above, so you also need to do this:

if (hdg >= 360) hdg = hdg - 360;

In programming you need to always be aware of the data types you are dealing with. Arithmetic on computer variables isn't just a matter of writing the formulae down.

Regards,

Pete

Posted

Thanks for the quick response Pete,

As you can probably gather I'm not only new to programing applications with FSUIPC but new to programming in general.

You have shed some light on what I'm doing wrong , I'm not allowing enough space in memory to adequate hold the data needed for the calculation.

I will review the my data types and try again.

all the best Gilles.

Posted

You have shed some light on what I'm doing wrong

Sorry, I thought I'd given you three possible solutions too?

I'm not allowing enough space in memory to adequate hold the data needed for the calculation.

I will review the my data types and try again.

If you want an accurate heading, with fractional part, you have to use floating point -- fixed point integers do not include fractions.

I don't know what you want to use the result for. If you only want the nearest whole degree then the slight modifications to your formula which I showed you will do the trick. They just force the calculations to be performed in an order designed to keep the intermediate results within the 32-bit capacity of the "hdg" variable.

Regards,

Pete

Posted

Hi Pete ,

Yes you did give three solutions. I have implemented the second i.e. hdg=(((hdg+32768)/65536)*360)/65536;

The one degree increments are more than adequated for my purposes.

This has work perfectly and more improtantly I fully understand why now , it is obvious now its been pointed out to me. As I am still very green to programming I haven't had to deal with numbers this large before and didn't even think of it.

Thanks for the lesson learn't more in C programming rather than a actual FSUIPC one !

All the best

Gilles

Posted

Gilles,

1. Be sure to add the magnetic variation to your heading. I forgot this once. It's only a minor correction until you get up far north (e.g. Seattle, where it is about 20 degrees...) and things start to go wrong.

2. To go from integers to double you can use casting:

int i;

double d;

...

FSUIPC_Read(address, &i, 4, &dwResult);

FSUIPC_Process(&dwResult);

...

d=(double)i;

Just in case you hadn't discovered that in C yet.

Regards,

Jeroen.

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.