negative number calculation
Philip Verspreeuwen
Posts: 9
Hello,
I have the next problem:
I'm using a gyroscope and accelerometer input. Both are a pulse width modulation signal. To see the actual difference, I substract the read value of the reference value (which is the neutral signal for both sensors). This has as result that when the sensors are turned one way, I become a value between 0 and 575. When turned the other way I get a value between 0 and -575. When using this negative value in a calculation, the BS2px sees this value as a positive value much higher then the actual value... Can anybody tell me which kind af variable I need to use? For the moment I'm using a "word", but apparently this is only able to have a positive value.
thanks,
Philip
I have the next problem:
I'm using a gyroscope and accelerometer input. Both are a pulse width modulation signal. To see the actual difference, I substract the read value of the reference value (which is the neutral signal for both sensors). This has as result that when the sensors are turned one way, I become a value between 0 and 575. When turned the other way I get a value between 0 and -575. When using this negative value in a calculation, the BS2px sees this value as a positive value much higher then the actual value... Can anybody tell me which kind af variable I need to use? For the moment I'm using a "word", but apparently this is only able to have a positive value.
thanks,
Philip
Comments
Some math operations, such as / and // do not work on signed values.
I do this (not tested here, but gives you the idea)
b = value.bit15 ' get bit 15, 1 = negative
y = abs( value) / 20
if b= 1 then y = y * -1 ' re-apply sign
-Martin
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
StampPlot - GUI and Plotting, and XBee Wireless Adapters
Southern Illinois University Carbondale, Electronic Systems Technologies
Cheers,
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tom Sisk
http://www.siskconsult.com
·
DEBUG SDEC value
Arithmetic works fine on those twos complement values for addition, subtraction and multiplication. If your algorithm needs division, you will need to use some tricks, which basically means saving the sign, doing the division using the absolute value, and then restoring the sign.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com