Jump to content
The simFlight Network Forums

Recommended Posts

Posted

Hi. I am tring to scale an offset (eleavtor trim) offset 0bc0. the vb pgm works fine, I can read the offset, I now need to scale it down to a value of 0- 255 so I can send this out the serial port to a micro controler. that will then drive a meter 0 to 5 vdc. I see in epicinfo doc. about scaling but do not know how to do this in vb. The value I read is -16383 to 16383. I don't know the math to convert that type number. any help. Ron.

vb code snippet

if fsuipc_read (&hbc0, 2, varptr (eletrim), dwresult) then

if fsuipc_process (dwresult) then

pitchtrim.text = (eletrim)

end if

end

Posted
Hi. I am tring to scale an offset (eleavtor trim) offset 0bc0. the vb pgm works fine, I can read the offset, I now need to scale it down to a value of 0- 255 so I can send this out the serial port to a micro controler. that will then drive a meter 0 to 5 vdc. I see in epicinfo doc. about scaling but do not know how to do this in vb. The value I read is -16383 to 16383. I don't know the math to convert that type number. any help. Ron.

vb code snippet

if fsuipc_read (&hbc0, 2, varptr (eletrim), dwresult) then

if fsuipc_process (dwresult) then

pitchtrim.text = (eletrim)

end if

end

What do you mean scale an offset???

First and foremost...I apologize if I cannot answer you, but it's been years since I tinkered with FS stuff....version 5.0 was the last time I flew anything...but I will try and answer or assist your specific question in regards to programming.

Anyways,

Scale an offset...well you can't...cuz an offset is an offset...you change the number any and you'll change the data your after too...

The value returned at offset

0BC0

is an short integer with a range of 32768 or like you said (+-)16383. You cannot do an explicit cast to a

BYTE

which holds the values 0-255 or (+-)128...cuz you will likely always result in loss of data.

So what I would suggest is calculate the percentage of the first and convert that into the byte code your trying to send over the serial port...

For instance:

a value of 5243...

5243 / 32768 = 0.16

0.16 * 100 = 16% elevator trim

Note: The values stored at offset: 0BC0 appears to be signed by the looks of things, so you will need to convert the value into an unsigned value before plugging that into the equation above...

Now you calculate the value (If I fail to use proper math terminology...it's cuz...i'm not a math guy...i'm a geek...computers do my work for me :oops: ) required for the serial port based on the percentage above :)

So... 16% of 255 (A serial port I believe can send/recv only a single byte at a time which is a 0-255 unsigned)

255 * 0.16 = 40.8

Remember an integer doesn't allow for any precision, so whole numbers are only allowed...so in reality...

40.8 will likely become 40

Not sure what the hell the above process is called...I think it's interpolation or extrapolation or something...but who cares...as long as you understand it and it works :)

Hope this helps 8)

Cheers :)

Posted

I can read the offset, I now need to scale it down to a value of 0- 255 so I can send this out the serial port to a micro controler. that will then drive a meter 0 to 5 vdc. I see in epicinfo doc. about scaling but do not know how to do this in vb. The value I read is -16383 to 16383. I don't know the math to convert that type number.

Convert N from a range of -16384 - +16383 to N2 with a range 0 - 255.

N is a 16-bit integer, N2 is a BYTE (8 bit).

Use M as an intermediae 32-bit integer

1. Get everything on the same "base" (i.e. starting from 0):

M = N (make sure you have enough room -- i.e. 32 bits)

M = N + 16384 (now M runs from 0 to 32767)

2. Scale it down from 32767 max to 255 max 00 i.e 32768 different vales to 256 different values:

N2 = M * (256 / 32768)

or simpler N2 = M / 128

Now this rounds "off". More accurately you should round to "nearest", which would be by:

N2 = (M + 64) / 128

You can do this using shifts instead of division:

N2 = (M + 64) >> 7

(where >> means "shift right by").

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.