Page 1 of 1

Substraction error

Posted: 24 Oct 2010 19:11
by Pesticida
Hello,
I just test a simple code.
But it's giving a wrong result !

Code: Select all


signed int err;
signed int pitchOffset;
signed int pitch;


void main() {
pitchOffset = -36;
pitch = -37;

  do
  {

     err=pitch - pitchOffset;



       }while (1);
       }
the result is err=65535 why?

Thanks
is very urgent!

regards Pesti

Re: Substraction error

Posted: 24 Oct 2010 22:18
by KaranSoin
how are you checking the result ? LCD display, debugger ? The result is actually correct (-1 is represented as 65535 in 2's compliment), but is somehow being represented as unsigned int instead of signed int. However you are trying to display the value, one of the functions in there accepts an unsigned int value and is treating the -1 as 65535


Regards

Re: Substraction error

Posted: 25 Oct 2010 07:38
by Pesticida
Hello,

I Display this over Uart with InttoStr Function.

I test the same code with the trial version of mikroc pro and the result is ok!

Thanks
Regards Pesti

Re: Substraction error

Posted: 25 Oct 2010 08:56
by KaranSoin
Thats quite strange, I cant see anything obviously wrong with this. Post ur code if possible, may be it will reveal something.


cheers

Re: Substraction error

Posted: 25 Oct 2010 12:33
by Pesticida

Code: Select all

float pitch_est=0;
float Offset_Pitch = 0;
float wy = 0.0;
float yy = 0.0;
float esumy ;
float erry  ;
float I = 0.01 ;
float Kp = 0.2 ;
float DY = 1 ;
float ealty = 0.0;
float y=0;

wy = pitch_est;
yy = Offset_Pitch ;

....
/loop every 10 mS
erry = yy - wy;  // aktuelle Regelabweichung bestimmen

if ((y <220) && (y > 0))  
{
      // (Anti-Windup)
esumy = esumy + erry ;      
}
y = RC_Throttle + (Kp * erry) + (I * 0.02 * esumy) + (DY * ((erry - ealty)) / 0.02) ; // Reglergleichung
ealty = erry  ;               
if (y > 220) y = 220 ;             
if (y < 1) y=0;
}

when wy and yy are negative i got always the wrong result ,for ex. wy = -35 and yy = -35 the result should be 0 but i got 35.

Re: Substraction error

Posted: 25 Oct 2010 19:53
by Pesticida
Nobody can Help me ? :-(

Re: Substraction error

Posted: 25 Oct 2010 20:03
by zan
I'm not using mikroC, but maybe your problem is IntToStr. I think you need to use FloatToStr.

Re: Substraction error

Posted: 25 Oct 2010 20:12
by Pesticida
Hello,

The Problem ist that i got the wrong value on the Debugger too!

Maybe is a MIkroc Bug?!

On the Mikroc Pro Demo i got the right value.

Re: Substraction error

Posted: 25 Oct 2010 21:02
by KaranSoin
quite possible its a MikroC bug, upgrade to a newer if you dont have it already.



Regards

Re: Substraction error

Posted: 26 Oct 2010 13:54
by tihomir.losic
Hello,

please, look at screen shot... I didn't reproduce your issue.
For test is used mikroC PRO for PIC v4.15, and PIC16F887:
oduzimanje.png
oduzimanje.png (117.78 KiB) Viewed 3755 times
Best regards,

Losic Tihomir

Re: Substraction error

Posted: 26 Oct 2010 16:42
by Pesticida
Yes I know on mikro c pro is working but on mikro c 8.2 i got always the wrong Result.
mikroc8_2.jpg
mikroc8_2.jpg (126.74 KiB) Viewed 3752 times

Re: Substraction error

Posted: 26 Oct 2010 18:13
by KaranSoin
The Result is actually correct. -1, in MICROCHIP 32 bit float format, is represented as 7F80 0000 which is equates to 2139095040 in decimal. If you single click on the value, a small button will appear, which opens up a "Edit Value" dialog box. There is a good chance that you have "Dec" representation selected instead of "Float". I am using version 3.2 so may be a slightly different picture.


Regards