Jump to content
The simFlight Network Forums

Recommended Posts

Posted

I have written a routing function in which i take the lat/lon from fs and then calculate the heading (in degrees, unsigned) needed to fly over a target lat/lon. I print out the heading in the degrees and it is always correct. However, when i try to convert the heading back into the FS format and write it to the autopilot heading preset, the heading in the autopilot display is a different number. Here is my code for the conversion:

double heading;

//after heading calculation...

heading = (heading*360)/65536;

mastLock = 1;

headLock = 1;

FSUIPC_Open(SIM_ANY, &dwResult);

FSUIPC_Write(0x07BC, 4, &mastLock, &dwResult);

FSUIPC_Write(0x07C8, 4, &headLock, &dwResult);

FSUIPC_Write(0x07CC, 2, &heading, &dwResult);

FSUIPC_Process(&dwResult);

Everything works fine except for the heading when it appears in the autopilot display. Your help would be greatly appreciated.

Posted

However, when i try to convert the heading back into the FS format and write it to the autopilot heading preset, the heading in the autopilot display is a different number. Here is my code for the conversion:

This is because

double heading;

defines a double floating point value, 8 bytes in length, whereas:

FSUIPC_Write(0x07CC, 2, &heading, &dwResult);

is trying to write this to a 16-bit (two byte) "short". All you are succeeding in doing is writing the lowest 2 bytes of the 8 byte double, which will be pretty meaningless.

You need to copy the double floating point heading to a fixed point short integer first, like this:

short sHdg = (short) (heading + 0.5);

FSUIPC_Write(0x07CC, 2, &sHdg, &dwResult);

I've added 0.5 to round the value, for a little more accuracy :wink:

Regards,

Pete

Posted

pete,

i tried casting it like you suggested but it just results in the short value being zero. wouldnt it always be zero because when converting to fs format you are dividing by 65536 which would make a decimal which would be cut off?

thanks

Posted

i figured out what was wrong 30secs after my last post. here's the corrected code just in case anyone has the same prob in the future:

heading *= 65536/360;

sHdg = heading + 0.5;

this works fine! thanks pete for getting me thinking.

Posted
i tried casting it like you suggested but it just results in the short value being zero. wouldnt it always be zero because when converting to fs format you are dividing by 65536 which would make a decimal which would be cut off?

Actually you are multiplying by 65536, but yes, the result would be the same if you did the computation AFTER casting it to a "short". Do all the computation first -- the bit of code I showed was supposed to be as it was, no intervening changes :wink:

Regards,

Pete

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.