ABS
koolitude
Posts: 37
From the Wheel_Motion.bs2 extracted from: http://www.parallax.com/dl/sw/Encoder.zip
I dont understand why taking the absolute value requires Leftshifting 8 bits then Rightshifting 8 bits.
Thanks for your help.[noparse][[/noparse]code]
... Move: Dir(LEFT) = Arg.BIT7 'Direction of motion given by sign bit of Arg. Dir(RIGHT) = Dir(LEFT) 'Both directions are the same. [b]Arg = ABS(Arg << 8) >> 8 'Take the absolute value of Arg to get distance. [/b] 'Compute length of first leg of tack. Dist(LEFT) = Arg * (16 - (ABS(DirErr) / (BRDSPER >> 4))) / 16 IF (Dist(LEFT)) THEN 'Is it greater than zero? Dist(RIGHT) = Dist(LEFT) ' Yes: Right wheel goes the same distance. GOSUB DoMove ' Execute the move Arg = Arg - Dist(LEFT) ' Subtract the distance moved from Arg. IF (Arg) THEN ' Anything left? Dist(LEFT) = DirErr.BIT15 ' Yes: Correct direction by the sign of DirErr. Dist(RIGHT) = Dist(LEFT) ^ 1 ' Only one wheel moves forward. GOSUB DoMove ' Execute the move. ENDIF ENDIF IF (Arg) THEN 'Now back to Arg. Is it greater than zero? Dist(RIGHT) = Arg ' Yes: Set distances from it. Dist(LEFT) = Arg GOSUB DoMove ' Execute this leg of the move. ENDIF RETURN ...
I dont understand why taking the absolute value requires Leftshifting 8 bits then Rightshifting 8 bits.
Thanks for your help.[noparse][[/noparse]code]
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Indeed I tried to do the statement explicitly... say Arg=-52.... I did (Arg<<8), the I got 0... then I tried ABS(Arg<<8), I got 0 as well... but when I tried ABS(Arg<<8) >>8... I really got 52. (please have a look of the attached capture)
Thank you very much for your help!
Post Edited (koolitude) : 3/3/2007 8:51:17 PM GMT
ABS(Arg << 8) >> 8
it does so using 16 bit arithmetic. So Arg<<8 is not shifting Arg itself as an eight bit variable, rather, it is putting Arg into a 16 bit working register, shifting that left, taking the ABS, shifting back by 8, and finally storing the 16 bit value back into Arg. The low 8 bits.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com