Shop OBEX P1 Docs P2 Docs Learn Events
Negative Number in a LOOKUP instruction? — Parallax Forums

Negative Number in a LOOKUP instruction?

ArchiverArchiver Posts: 46,084
edited 2002-03-10 06:01 in General Discussion
Bob-

Your Stamp doesn't handle negative numbers, but subtraction works
fine so long as everything => 0. Try something like this:


RANDOM RandomNumber 'Generate a random number

DieRoll = RandomNumber//6 'Set Direction variable to be between 0 and 5

LOOKUP DieRoll, [noparse][[/noparse]1,0,0,0,1,1], PanRight

LOOKUP DieRoll, [noparse][[/noparse]0,1,1,1,0,0], PanLeft

LOOKUP DieRoll, [noparse][[/noparse]0,1,0,0,0,1], PitchUp

LOOKUP DieRoll, [noparse][[/noparse]0,0,1,0,1,0], PitchDown

FOR X = 1 to 150 'This loop moves the servos to the desired position

PULSOUT PanServo, pancenter + (PanRight * X * 2) - (PanLeft * X * 2) 'Gradually
pan in the appropriate direction

PULSOUT NodServo, nodcenter + (PitchDown * X * 3) - (PitchUp * X * 3) 'Gradually
nod in the appropriate direction

PULSOUT JawServo, jawclosedmax + (X * 2) 'Open the jaw a bit at a time

PAUSE 10

NEXT 'Loop until we've reached our destination



Regards,

Steve

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2002-03-09 07:51
    Hi,

    I wrote the code below to move a servo either to the left or right of center
    depending on a die roll. Well, the LOOKUP instructions work fine, but of course
    they put a very large postive number into the variable instead of a -1 (two's
    complement, right?). The servo slams all the way over to the right, instead of
    gradually moving to the left. So what's an easy way to fix this? I've already
    got a workaround by changing the -1 values to a postive value (I used "2"), and
    then testing and branching if the direction variable = 2 to a different formula
    (pancenter - (X*2)). Is there a better way to do this?

    Thanks in advance!
    Bob Pony

    Here's the code:
    RANDOM RandomNumber 'Generate a random number

    DieRoll = RandomNumber//6 'Set Direction variable to be between 0 and 5

    LOOKUP DieRoll, [noparse][[/noparse]1,-1,-1,-1,1,1], PanDirection 'Assign appropriate Pan value
    [noparse][[/noparse]R,UL,DL,L,DR,UR]

    LOOKUP DieRoll, [noparse][[/noparse]0,-1,1,0,1,-1], NodDirection 'Assign appropriate Nod value
    [noparse][[/noparse]R,UL,DL,L,DR,UR]


    FOR X = 1 to 150 'This loop moves the servos to the desired position


    PULSOUT PanServo, pancenter + (PanDirection * X * 2) 'Gradually pan in the
    appropriate direction

    PULSOUT NodServo, nodcenter + (NodDirection * X * 3) 'Gradually nod in the
    appropriate direction

    PULSOUT JawServo, jawclosedmax + (X * 2) 'Open the jaw a bit at a time

    PAUSE 10

    NEXT 'Loop until we've reached our destination



    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-09 13:13
    Here's what I think is happening. I'm guessing PanDirection is a
    byte variable. When left panning is specified, the LOOKUP statement
    yields -1, or 65535 as the 16-bit workspace result, but stores only
    the low order 8 bits in panDirection. When the variable is recalled
    later for the calculation, its 8 bits are padded with 8 leading
    zeroes, giving a 16-bit intermediate value of $00FF, or 255. 255 *
    X * 2 then results in a large, non-negative number which is then
    added to pancenter, producing a rapid right panning instead. This
    is a longer version of my earlier, simplistic remark regarding the
    Stamp's ability to handle negative numbers.

    Please correct as required. I'd like to be sure I understand how
    this works.

    Regards,

    Steve


    > >Well, the LOOKUP instructions work fine, but of course they put a
    > >very large postive number into the variable instead of a -1 (two's
    > >complement, right?). The servo slams all the way over to the right,
    > >instead of gradually moving to the left. So what's an easy way to
    > >fix this?
    > >...
    > >LOOKUP DieRoll, [noparse][[/noparse]1,-1,-1,-1,1,1], PanDirection 'Assign appropriate
    > >Pan value [noparse][[/noparse]R,UL,DL,L,DR,UR]
    > >...
    > >PULSOUT PanServo, pancenter + (PanDirection * X * 2) 'Gradually pan
    > >in the appropriate direction
    >
    > It is true that the -1 in twos complement is represented as 65535.
    > Multiplication works fine under twos complement (division does
    > not!). If you multiply out, say, 65535*5*2, and then discard the
    > upper 16 bits, you get 65526, which is the correct twos complement
    > representation of -10. When added to pancenter, it should give the
    > correct result. I am puzzled why the servo slams against the wrong
    > stop.
    >
    > -- Tracy
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-09 16:37
    Thanks Steve! I like that! A very simple solution. Much appreciated!

    Bob

    Original Message
    From: S Parkis <parkiss@e...>
    To: <basicstamps@yahoogroups.com>
    Sent: Friday, March 08, 2002 2:06 PM
    Subject: Re: [noparse][[/noparse]basicstamps] Negative Number in a LOOKUP instruction?


    > Bob-
    >
    > Your Stamp doesn't handle negative numbers, but subtraction works
    > fine so long as everything => 0. Try something like this:
    >
    >
    > RANDOM RandomNumber 'Generate a random number
    >
    > DieRoll = RandomNumber//6 'Set Direction variable to be between 0 and 5
    >
    > LOOKUP DieRoll, [noparse][[/noparse]1,0,0,0,1,1], PanRight
    >
    > LOOKUP DieRoll, [noparse][[/noparse]0,1,1,1,0,0], PanLeft
    >
    > LOOKUP DieRoll, [noparse][[/noparse]0,1,0,0,0,1], PitchUp
    >
    > LOOKUP DieRoll, [noparse][[/noparse]0,0,1,0,1,0], PitchDown
    >
    > FOR X = 1 to 150 'This loop moves the servos to the desired position
    >
    > PULSOUT PanServo, pancenter + (PanRight * X * 2) - (PanLeft * X * 2)
    'Gradually pan in the appropriate direction
    >
    > PULSOUT NodServo, nodcenter + (PitchDown * X * 3) - (PitchUp * X * 3)
    'Gradually nod in the appropriate direction
    >
    > PULSOUT JawServo, jawclosedmax + (X * 2) 'Open the jaw a bit at a time
    >
    > PAUSE 10
    >
    > NEXT 'Loop until we've reached our destination
    >
    >
    >
    > Regards,
    >
    > Steve
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-09 19:38
    >Well, the LOOKUP instructions work fine, but of course they put a
    >very large postive number into the variable instead of a -1 (two's
    >complement, right?). The servo slams all the way over to the right,
    >instead of gradually moving to the left. So what's an easy way to
    >fix this?
    >...
    >LOOKUP DieRoll, [noparse][[/noparse]1,-1,-1,-1,1,1], PanDirection 'Assign appropriate
    >Pan value [noparse][[/noparse]R,UL,DL,L,DR,UR]
    >...
    >PULSOUT PanServo, pancenter + (PanDirection * X * 2) 'Gradually pan
    >in the appropriate direction

    It is true that the -1 in twos complement is represented as 65535.
    Multiplication works fine under twos complement (division does not!).
    If you multiply out, say, 65535*5*2, and then discard the upper 16
    bits, you get 65526, which is the correct twos complement
    representation of -10. When added to pancenter, it should give the
    correct result. I am puzzled why the servo slams against the wrong
    stop.

    -- Tracy
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-10 06:01
    Steve, you nailed this right on.

    I just changed PanDirection and NodDirection from byte variable to word
    variables and the original code works fine now. Thanks for catching that.
    It makes perfect sense. Fortunately I've got plenty of RAM left, otherwise
    I'd use your earlier suggestion of multiple LOOKUP statements and a modified
    formula.

    Both are good solutions. Thanks so much for the help with this guys!

    Cheers,
    Bob Pony

    Original Message
    From: S Parkis <parkiss@e...>
    To: <basicstamps@yahoogroups.com>
    Sent: Saturday, March 09, 2002 5:13 AM
    Subject: Re: [noparse][[/noparse]basicstamps] Negative Number in a LOOKUP instruction?


    > Here's what I think is happening. I'm guessing PanDirection is a
    > byte variable. When left panning is specified, the LOOKUP statement
    > yields -1, or 65535 as the 16-bit workspace result, but stores only
    > the low order 8 bits in panDirection. When the variable is recalled
    > later for the calculation, its 8 bits are padded with 8 leading
    > zeroes, giving a 16-bit intermediate value of $00FF, or 255. 255 *
    > X * 2 then results in a large, non-negative number which is then
    > added to pancenter, producing a rapid right panning instead. This
    > is a longer version of my earlier, simplistic remark regarding the
    > Stamp's ability to handle negative numbers.
    >
    > Please correct as required. I'd like to be sure I understand how
    > this works.
    >
    > Regards,
    >
    > Steve
    >
    >
    > > >Well, the LOOKUP instructions work fine, but of course they put a
    > > >very large postive number into the variable instead of a -1 (two's
    > > >complement, right?). The servo slams all the way over to the right,
    > > >instead of gradually moving to the left. So what's an easy way to
    > > >fix this?
    > > >...
    > > >LOOKUP DieRoll, [noparse][[/noparse]1,-1,-1,-1,1,1], PanDirection 'Assign appropriate
    > > >Pan value [noparse][[/noparse]R,UL,DL,L,DR,UR]
    > > >...
    > > >PULSOUT PanServo, pancenter + (PanDirection * X * 2) 'Gradually pan
    > > >in the appropriate direction
    > >
    > > It is true that the -1 in twos complement is represented as 65535.
    > > Multiplication works fine under twos complement (division does
    > > not!). If you multiply out, say, 65535*5*2, and then discard the
    > > upper 16 bits, you get 65526, which is the correct twos complement
    > > representation of -10. When added to pancenter, it should give the
    > > correct result. I am puzzled why the servo slams against the wrong
    > > stop.
    > >
    > > -- Tracy
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >
Sign In or Register to comment.