Shop OBEX P1 Docs P2 Docs Learn Events
The two's compliment.... aaargh — Parallax Forums

The two's compliment.... aaargh

fzrobotfzrobot Posts: 7
edited 2007-02-26 21:17 in Learn with BlocklyProp
Hi all, quick question, hopefully it can get answered here. As an overview, I am using the Hitachi H48C for calculating the tilt angle with the BS-2.

My problem is regarding basic coding.

For the sake of the argument... the H48C returns a quantity, which I have named gForce, and it is always positive (in my case). What I would like to know is:

'Let's say I declare a constant value "equilibrium" that I use for comparing "gForce"

equilibrium = 500

'Then, I would like to subtract gForce - equilibrium
'So I need to know if gForce is less or greater than equilibrium
'The first case, is straight forward, because gForce >= equilibrium
'I can just subtract and do my PID calculations:

IF (gForce >= equilibrium) THEN
error(present) = gForce - equilibrium
p = Kp*error(present)
error(sum) = error(present)+error(old)+error(older)
i = Ki*error(sum)
error(delta) = error(present)-error(old)
d = Kd*error(delta)
pid = p+i+d

'The problem arises for the other case, i.e., gForce < equilibrium
'After several tries, this is the code that works (i.e., I get an appropriate result),
'that is, a negative value for "pid"

ELSE
error(present) = equilibrium - gForce
p = Kp*error(present)
error(sum) = -error(present)+error(old)+error(older)
i = Ki*error(sum)
error(delta) = -error(present)-error(old)
d = Kd*error(delta)
pid = -(p+i+d)
pid = -pid
error(present) = -error(present)
ENDIF

'Yes I know, this looks nasty, but it works.

So, I come to you, oh mighty gurus in seek of help for writing better code, and learning how to work with the negative numbers in a positive integer environment. My question is, how can I work with the negative number case in the example above.

At the end, I want to do arithmetic with the "pid" term... So I need to know the difference when it's positive or negative, and hopefully not have to write the IF-ELSE conditional statement.

Any insight is helpful.

Cheers,

-Cisco

Comments

  • edited 2007-02-26 18:48
    Hello fzrobot,

    The BASIC Stamp's twos compliment should work fine for what you're doing: addition, subtraction and multiplication. The two problem areas tend to be magnitude comparisons and division, neither of which are needed here.

    Take a look at PidMathExample.bs2 in PID Control Intro with the BASIC Stamp. All the adding and subtracting come out in the wash; they are controlled by the sign of the error calculation.

    Andy

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Andy Lindsay

    Education Department
    Parallax, Inc.
  • fzrobotfzrobot Posts: 7
    edited 2007-02-26 21:17
    Hi Andy,

    Thanks for the prompt reply. I've had quite a bit of exposure to Control Systems problems (school/work), though that did not prevent me from enjoying the reading (first I skimped through it, but then I stopped to read the whole thing to provide some feedback); the explanations are concise and the text is well written. Perfect for an introduction to Feedback Control Systems implementation.

    I did not know the problems with the BS2 and negative number division. That is, I was only aware that it was a problem if the divisor < 0.

    As it turns out I was doing some division of negative numbers (only in the dividend). In particular, I was "adjusting" my PID gain as follows:

    In the IF case:

    pid = (p+i+d)/100

    which did not have any issues (after I debugged the code, line-by-line). But, on the else case

    pid = -(p+i+d)/1000

    I was having some issues. After your reply I decided to rescale the error differently, rather than the PID gain.

    I'm curious though, would there had been a solution with my former approach as (hopefully clearly) described above.

    Cheers,

    -Cisco
Sign In or Register to comment.