Shop OBEX P1 Docs P2 Docs Learn Events
Negative back to positive angle — Parallax Forums

Negative back to positive angle

ClintClint Posts: 95
edited 2008-07-09 10:54 in General Discussion
I am reading an incremental encoder to measure angle. Based on the encoder, I will INC or DEC a word I call "angle" representing the angle in tenths of degrees (3600 = 360 degrees). What I would like to happen is for the angle to switch from -999·to 2600 when decreasing and from 2600 to -999 when increasing.

I thought I could do this, but it only seems to work for increasing. When decreasing, the number just keeps decreasing. In this example, the value of angle remains -1000

angle VAR Word
 
angle=-1000
 
 IF angle=2601 THEN
  angle=-999
 ELSEIF angle=-1000 THEN
  angle=2600
 ENDIF

Am I doing something wrong?

Thanks for any help

Post Edited (Clint) : 7/1/2008 7:44:08 AM GMT

Comments

  • Sens-a-DatSens-a-Dat Posts: 44
    edited 2008-07-01 11:32
    Clint,

    I do not have the answer for you as to why this is not working, but I have learned in using SX SIM on your code is that the compiler is taking your statement of "ELSEIF angle=-1000 THEN" and making it "ELSEIF angle=1000 THEN". Why the minus value is being made a positive value is something others will need to explain. I have checked the help file and see no numeric limits for the IF..THEN..ELSEIF..ENDIF command.

    The VIEW LIST of the code for "angle=-1000" is:
    182 0067 0C18 MOV angle_LSB,#(-1000 & 255) ; angle=-1000
    0068 002D
    183 0069 0CFC MOV angle_MSB,#(-1000 >> 8)
    006A 002E

    The VIEW LIST for the code of "ELSEIF angle=-1000 THEN" is:
    196 007B 0CE8 CJNE angle_LSB,#1000 & 255,@__ELSE_2
    007C 008D 0743 0010 0A8B
    197 0080 0C03 CJNE angle_MSB,#1000 >> 8,@__ELSE_2
    0081 008E 0743 0010 0A8B

    I know someone will be able to help us better understand this.

    Gary
  • Sens-a-DatSens-a-Dat Posts: 44
    edited 2008-07-01 12:17
    Clint,

    As a workaround, define a CON such as TestLow, giving it a value of -1000. Then use that CON name in the IF..ENDIF area. This makes the program function as desired now, but does not explain my earlier observation.

    TestLow CON -1000
    
    angle VAR Word
    
    START: 
    angle = -1000
    
    MAIN: 
     IF angle = 2601 THEN
         angle = -999
     ELSEIF angle = TestLow THEN
         angle = 2600
     ENDIF
    
    



    Gary
  • ClintClint Posts: 95
    edited 2008-07-02 01:20
    Gary - Thank you very much for giving this your attention. I will give your workaround a try [noparse]:D[/noparse] For a second there, I thought I'd lost my marbles!
  • ClintClint Posts: 95
    edited 2008-07-09 03:22
    Well that worked okay, but then I realized I need a > or < to take care of situations where the angle skips over the limit (say I go from 2600 to 2610). Now it doesn't work again.

    Here's what I'm trying
    upperlimit CON 2600
    lowerlimit CON -999
    angle VAR Word  'angle
    

     IF absangle>upperlimit THEN
      angle=angle-3600
     ELSEIF angle<lowerlimit THEN
      angle=angle+3600
     ENDIF
    

    Am I doing something wrong?
  • BeanBean Posts: 8,129
    edited 2008-07-09 10:54
    Clint,
    · WORD variables can only hold positive values. When you do a comparison to a negative value, the compiler takes the "two's complient" of the negative value, and uses that. This works fine for "=" and "<>", but it won't work for ">" or "<".

    WORD value      2's Compliement value
    -------------------------------------
    32766           32766
    32767           32767
    32768          -32768
    32769          -32767
    32770          -32766
    ...            ...
    65533          -3
    65534          -2
    65535          -1
    

    · So when you say "IF var < -999" you are really saying "IF var < 64538" which isn't what you mean.

    · One solution is to simply offset the value before doing the comparisons so everything is positive.

    upperlimit CON 3600 + 2600
    lowerlimit CON 3600 - 999
    angle VAR Word  'angle
    
    
    
     absangle = absangle + 3600 ' Make absangle postive
     IF absangle > upperlimit THEN
      angle = angle - 3600
     ELSEIF angle < lowerlimit THEN
      angle = angle + 3600
     ENDIF
     absangle = absangle - 3600 ' Put value back to original (IF NEEDED)
    


    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "A government big enough to give you everything you want, is big enough to take away everything you·have."·· Thomas Jefferson

    "It is our choices, Harry, that show what we truly are, far more than our abilities."·Dumbledore from Harry Potter

    www.iElectronicDesigns.com



    Post Edited (Bean (Hitt Consulting)) : 7/9/2008 11:04:42 AM GMT
Sign In or Register to comment.