Shop OBEX P1 Docs P2 Docs Learn Events
ABS — Parallax Forums

ABS

koolitudekoolitude Posts: 37
edited 2007-03-03 21:01 in BASIC Stamp
From the Wheel_Motion.bs2 extracted from: http://www.parallax.com/dl/sw/Encoder.zip

...
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 AllenTracy Allen Posts: 6,664
    edited 2007-03-03 19:17
    From the first line of the program, it says Arg.bit7 is the sine bit of Arg. So<<8 makes bit 15 the sign bit, where it has to be for ABS to work. Then>>8 restores the positive Arg to 8 bits. A clever way to do it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • koolitudekoolitude Posts: 37
    edited 2007-03-03 20:10
    But Arg was declared as a Byte instead of a Word... Leftshifting it for 8 bits, wont make all the bits disappear?

    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)

    attachment.php?attachmentid=45712

    Thank you very much for your help!

    Post Edited (koolitude) : 3/3/2007 8:51:17 PM GMT
    370 x 334 - 64K
    arg.jpg 64.4K
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-03-03 21:01
    Okay, I see your point of confusion. When the Stamp evaluates any expression, like
    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
Sign In or Register to comment.