PID Help

Hi all,

I've been searching and reading about PID and now approximately it's working in theory but
still got a hard point of understanding how it's working in code. I've looked some videos on
youtube how people stabalize motors, airplanes, quadcopters, robots..etc I searched arduino
PID examples but nothing that I could understand.

Could someone please help me and explain me the basic code of PID on arduino? Lets say for example
I have a Gyro single axe and i wanna stabalize a motor. I need just a tiny little example and
explanation nothing more, please ::slight_smile:

The PID library takes a Setpoint (the desired behavior) and an Input (the current behavior) and returns an Output which you then apply to the control of the thing.

What will the output from the gyroscope be when the motor is stabilized? That value will be your Setpoint. The current output from the gyroscope will be your Input. The Output will be used to control the motor, probably the motor speed using analogWrite() to the motor driver.

Ok, so a gyro sensor as far as I'm aware will output the rotational velocity of an axis.

Often PID tutorials and code will attempt to stabilise the rotational position of an axis - i.e. the angle.

That is certainly something you'll need to be aware of...

Also, where are you in terms of learning?

You don't know calculus/basic dynamics? not essential, but if we know you know it then it might speed up explanations.

You don't know code? if that is the block, then we can explain it and you'll see how it matches up nicely with the theory.

It's deceptively simple in so many cases, and trivially non-trivial - i.e. hard to communicate - in others :o

So I just looked on the PID lib. that i have and this basic example :

#include <PID_v1.h>

#define PIN_INPUT 0  
#define PIN_OUTPUT 3

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
double Kp=2, Ki=5, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

void setup()
{
  //initialize the variables we're linked to
  Input = analogRead(PIN_INPUT);
  Setpoint = 100;

  //turn the PID on
  myPID.SetMode(AUTOMATIC);
}

void loop()
{
  Input = analogRead(PIN_INPUT);
  myPID.Compute();
  analogWrite(PIN_OUTPUT, Output);
}

Let's say my INPUT is the axe of the Gyro and my OUTPUT is the dc motor.
I got setpoint, input and output,
Input - is the input of the gyro x axe,
Output - is the output of the final calculations of the PID
Setpoint - is ?? ? let's say as in the example "100" that means it gonna try to stay at 100 in any kind of
case ?

Let me give and example and you tell me if im right (about the setpoint) :

We have a Gyro and i wanna echilibrate the motor at x=0 of the x axe (the motor and the gyro
are mounted together "like a self balancing robot") so in this case i put the Setpoint at 0 ??
so in case the Gyro value is increasing the motor spins and goes back to 0 (Im i right?).

Now how about the Kp,Ki,Kd values?
I've followed some tutorials and understood they are combined together so they could echilibrate
and have final 0 error in the output as possible but it's hard for me to imagine the code and real
world (action) without understanding yet the code compltlly.

and something last : How I know exactly what values to give Kp,Ki,Kd ? and how they behave each of thouse, I've seen they combined together to minimize the vibrations and smoth the movement of the motor..etc

have you read up and tried to fully understand P function by itself ? the I function by itself and the D function by itself ?

the balancing act of trying to get them all to work is not magic, but it does appear to be.

a note here is that derivative is often not used for any number of reasons. one is that the process absolutely does not need it.

another is that derivative is working against stability and actually making things worse. if the process is very dynamic, inverse derivative may be needed.

I think that once you fully grasp each of the parameters, the whole will make much more sense.

I'd like to know >> do you know basic calculus ?

Pretty much what Dave asks: do you inherently understand what I and D are doing? or does is just sound 'reasonable' to you and you're pushing ahead and not bothering with the details ?

What about basic analog to digital theory ?

I ask this, as most C implementations of PID you see online are pretty dang close to literal translations of the theory.

There are a few good articles on setting up P and I and D.
the thing that most people do is to get P working all by itself. well, working 'well enough' because P is not a full solution. then start with I .

I think that if you read up on PID tuning, written by someone you can understand.
the discussion on how to tune will enlighten you on what the function is doing.

"many an engineer needs a PhD to tune a PID."
the cauldron and Pointy hat are usually kept out of sight of the non-initiated.

There are several PID Control videos on Youtube that may be useful to you. To really understand and have a working knowledge you should know something about differential equations, laplace transforms, and digital filters.

You won't get a working knowledge from a post reply.

Domino60:
Let's say my INPUT is the axe of the Gyro and my OUTPUT is the dc motor.
I got setpoint, input and output,
Input - is the input of the gyro x axe,
Output - is the output of the final calculations of the PID
Setpoint - is ?? ? let's say as in the example "100" that means it gonna try to stay at 100 in any kind of
case ?

Setpoint is the value you want Input to end up at. The gyroscope is a rate gyroscope which says how fast the gyroscope is turning. You have to compensate for the gyroscope's zero offset (the output value when NOT turning). If your Setpoint is 0 your output will try to keep the gyroscope from turning. That won't let you create a self balancing robot since it will only attempt to prevent a change of tilt, but if any tilt happens it will maintain that tilt, and drive until it hits something.

If the Setpoint is 100 (let's assume that is in degrees per second) the output will try to adjust the motor to get the gyroscope spinning at 100 degrees per second (a little over one turn every four seconds).

The Setpoint value is always in the same physical units as the Input value.

Ok, 1st Yeas I've seen a lot of youtube videos about PID one of them is this:

I've been interested in PID few months ago but i never gave the time to learn it so yesterday came in my
mind to use PID to stabilize and have accurate Outputs for my motors, servos..etc I've seen many people
using it to stabilize quadcopters..etc

Yes I'm not really friendly with different kind of formulas but when i need something i learn it.
Someone asked me above if i understood what exactly P, I and D is doing, I've seen this video:

I've looked also this real life (no theory) example 01:40 :

To understand my mind is I understand 100% only if i see fully theoreticaly and real life "experimentation"

This video is what i needed :

But still got some questions on the code:

The code didn't turn up ...

But back to the more general discussion: for an initial tune you can try the Ziegler–Nichols method.

this might help

nilton61:
this might help

nice !

I knew an engineer who was often the smartest person in the room. at a meeting, someone asked him a question, he went into a 2 hour dissertation.

the person asking the question made notes, got the logarithm patented and started a multi-million dollar business on that one logarithm.

he once said that an good engineering does not know all the answers, but he DOES know where to find them !

Thanks for Info guys, I'll keep reading If i don't understand something i'll come back for some questions.
dave-in-nj
If you only new why i need that PID :smiley: I'll give you only a piece of my future puzzle (AI).

meanwhile

dave-in-nj:
I knew an engineer who was often the smartest person in the room. at a meeting, someone asked him a question, he went into a 2 hour dissertation.

the person asking the question made notes, got the logarithm patented and started a multi-million dollar business on that one logarithm.

he once said that an good engineering does not know all the answers, but he DOES know where to find them !

You mean "algorith"? ^^
Anyway your conclusion is right. The role of an engineer is to know what to do with information, how to use them. Not knowing them by heart... Especially with all these technologies thanks to which information can be found easily.