The linter gives me the above error when I try to take the abs() of a double in this code on lines 4 and 5:
double PIDdo(PID *thing){
updateSensors();
thing->error = thing->target - thing->sensor;
if(abs(thing->error) < 200){
if(abs(thing->error) < 8){
//thing->integral = 0;
}
else{
thing->integral += thing->error;
}
}
else{
thing->integral = 0;
}
//may need integral = 0 stuff
thing->derivative = thing->error - thing->previous_error;
thing->previous_error = thing->error;
return (thing->Kp*thing->error + thing->Ki*thing->integral + thing->Kd*thing->derivative);
}
I tried using
fabs()
, but that seems to be only for C++…
What can I do to fix this?
Barin
2
[s]I think you should be using the
.
operator instead of
->
, as
thing
is already a pointer from your function parameter.[/s]
AFAIK the
.
operator cannot be applied to pointers directly and instead must be used like this:
(*pointer).foo
which
pointer->foo
is actually the shortcut of.
PROS actually notices this and the linter gives this error when the
.
operator is used in this circumstance:
request for member 'integral' in 'thing', which is of pointer type 'PID*' (maybe you meant to use '->' ?)
Actually, adding
#include "math.h"
seems to make
fabs()
exist. I don’t have the ability to see if it actually works right now. Is this a safe thing to do?
This should be fine assuming it compiles +links.
fabs
is correct. If it doesn’t work, remember that its easy enough to implement an abs() function of your own using conditionals or bit inversion.
one example:
double fabs(double num) { //crappy fabs()
if(num < 0.0) return num *= -1;
else return num;
}
Or you can try something more on the edge similar to* this
: c - How is this implementation of fabs() working? - Stack Overflow
Just don’t expect your function library to be compatible with V5 once that comes out if you do that.
Indeed,
abs()
is your problem. I don’t believe C provides the overload for double in
abs(int)
, only C++ does this. Also, @sazrocks is correct that the
->
is not the issue at all here.
- not this because cortex is not x86
edit (confirmed): C abs() function | C Arithmetic functions | Fresh2Refresh