Go Down

Topic: cos or sqrt function messing up my double - PID v1 library (Read 246 times) previous topic - next topic

rotilahp

Hey guys,

I have been working on a self balancing robot. Its been great but now Im running into a problem that should just be basic math. I am using the PID v1 library that arduino gives examples for.

Here is what I am trying to do:

Code: [Select]

void loop(){
  MPUdata = MPUloop();  //Gets complimentary angle in degrees, double
  Serial.print("Angle: ");
  Serial.println(MPUdata);

  MPUradian = (MPUdata*71L)/4068L;
  MPUvelocity = sq(2L*9.8L*0.1524L*(1L-cos(MPUradian)));
  Serial.print("Velocity Needed: ");
  Serial.println(MPUvelocity);
  Out = PIDloop(MPUvelocity);
  }


My output from the PIDloop is coming out as zero. What the heck !?!?

Here is the PIDloop just so you can see it:

Code: [Select]

double PIDloop(double In)
{
  Input = In;
  myPID.Compute();
  return Output;
}


Everything is in double, what do I do now lol!!!Need help!!
Thank you!

rotilahp


sterretje

No idea what the compiler does with something like 0.1524L. Why do you specify a float and next tell the compiler to use it as a long?
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

rotilahp

Cause idk what im doing! Im trying to keep it from turning into an int.

sterretje

So you make it a long int instead of int. Something like 0.123 will be treated as a float and not as an int.

My first step would be to remove those capital L and try again.

Not behind a PC so can't check.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

rotilahp

I got rid of the L's, still not working. I got rid of the whole function and the square root. The cos is messing it up for some reason. MPUdata works but MPUvelocity does not.

Code: [Select]


  MPUdata = MPUloop();  //Gets complimentary angle in degrees, double
  Serial.print("Angle: ");
  Serial.println(MPUdata);

  MPUradian = (MPUdata*71)/4068;
  MPUvelocity =cos(MPUradian);
  Serial.print("Velocity Needed: ");
  Serial.println(MPUvelocity);
  
  Out = PIDloop(MPUvelocity);         //Places velocity into PIDloop, setpoint being 0.
  Serial.print("PID Output: ");
  Serial.println(Out);

odometer

Everything is in double, what do I do now lol!!!
Post your entire sketch lol!!!

If the problem is not apparent from the part of the sketch we can see, then it most likely has to do with the part of the sketch we can't see. So, post your entire sketch, and that will help us to help you.

I got rid of the L's, still not working. I got rid of the whole function and the square root. The cos is messing it up for some reason. MPUdata works but MPUvelocity does not.
This is not very helpful.
What do you mean by "works", exactly? What results are you hoping to see?
And what results do you actually see?
What behavior would constitute "working" for you?

johnwasser

What is the value are you passing into the cos() function?
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

rotilahp

So normally, my PID output is a double that is in the range of 0 to 255. But when I throw try to use a cos() on the MPUdata variable, suddenly the PID output is zero no matter what the movement is. Super lame, I think I am going to figure out another way to do cos() or maybe find a math library that isn't complete trash

jremington

The cos() function works exactly as it is intended to work. That is MOST DEFINITELY not your problem.

PieterP

No idea what the compiler does with something like 0.1524L. Why do you specify a float and next tell the compiler to use it as a long?
A floating point literal followed by the L suffix indicates a long double, which is a 128-bit quad-precision floating point type on at least some Linux systems, AFAIK. On other systems, it's the same as double. It probably doesn't do anything on an 8-bit AVR (could it be a 64-bit floating point type? Probably not, but worth checking).

Pieter

odometer

@rotilahp

Post your entire sketch. The whole thing. All of it.

Also, if you are using a library, please post a link to the Web page containing that library. That way, we can be reasonably sure that what we are seeing is the same as what you are using.

AWOL

I've tidied this thread up.
A lot.

Keep it civil, keep it on-topic.

Go Up