 # How is the PID constants Kp KI Kd calculated?

zhomeslice: charliesixpack, Do you have the Lagrangian equations as a C code you could share? I struggle reading lego nxt

You don't need to solve the Lagrangian. I have already done that. The Lagrangian was used to describe the robot body . That is, to get the output (tilt angle, theta) in terms of the input (wheel angle, beta). You control the tilt angle by changing the wheel angle. The ratio of change in tilt angle with change in wheel angle is the "gain" of the robot body. You multiply the gain by the input to get the output.

NXT code and arduino code are very similar to C.

charliesixpack: NXT code and arduino code are very similar to C.

[pedant] Arduino code IS C/C++ [/pedant]

@gabrielislost, I am only learning/studying this also.

...R

gabrielislost: Dear zhomeslice and Robin2 thanks for your reply, I am really a beginner at this as i am trying to do this self balancing robot project and i am trying to use PID to control the stepper motors of the robot, i am still trying to understand the code as i only know abit about arduino, please help haha. And is it normal for my values of the mpu to go back and forth when changing angles? and is will defining debug solve that problem? Hope to hear from you guys again thanks:) Sincerely Gabriel

Which MPU are you using? Post some of the output to show me what you are seeing with the "back and forth when changing angles?" I am using the MPU-6050 to get the angles for my bot.

This topic has been very helpful, because I also am trying to understand PID Now, I believe I understand that the desired setpoint for a self-balancing robot is upright (90 degrees), and the input is the current angle that the robot is at. I plan on doing a line-follower robot soon, and I’m somewhat confused about the desired setpoint and input of the robot. I get that the desired setpoint would be the robot centered above the line(following the line), and the input is what the IR sensors read. Though, what confuses me is since the set point and input in the code are stored as numbers (as what I’ve understood), what would be the values for the desired set point and input for a line follower robot?

Any help would be great. SMM2: This topic has been very helpful, because I also am trying to understand PID :)

Now, I believe I understand that the desired setpoint for a self-balancing robot is upright (90 degrees), and the input is the current angle that the robot is at.

I have my balancing bot maintain a setpoint of zero and use a signed int to see if it is falling +- X° from vertical. to get this I do have n offset adjustment to correct for the gyro not being mounted perfectly level

I plan on doing a line-follower robot soon, and I'm somewhat confused about the desired setpoint and input of the robot. I get that the desired setpoint would be the robot centered above the line(following the line), and the input is what the IR sensors read. Though, what confuses me is since the set point and input in the code are stored as numbers (as what I've understood), what would be the values for the desired set point and input for a line follower robot?

Any help would be great. :)

You are working with Yaw on the line following robot instead of pitch. Now your sensor will be receiving a value representing the line now most line following robots only follow the edge of the line and keep the sensor only on the left or right side of the line. Your setpoint should be about 50% of the value from black to white. You will need to have a way to tell if you hop over the line on accident to keep your bot from loosing the line.

Lets say if your reading range is 0 ~ 100 and the ideal line position is 50. My first recommendation is to subtract 50 from the reading to get a new range of -50 ~ 50 with an ideal line potion of 0. This will solve several problems that you will face when the PID loops Starts.

Now lets look How PID works. Proportional is like a balancing scale if your Kp is set to 1 then a change of 10 on the input the output will change by 10. Now since the ideal line position is 0 you could start following the line with just proportional control. Integral slowly adds or subtracts from the output (you could say bending) the balancing scale bar to achieve setpoint. Integral is not needed because 0 is the ideal setpoint and having the default value for integral of Zero is ideal. Derivative allows for a quick reaction to change when the reading changes too fast (rate of change) the derivative will counter the change to prevent overshooting. Derivative will be a huge benefit when capturing the line. Too much Derivative your bot will be jittery too little and you bot will be sluggish when the line turns quickly.

Your PID setting could be something line this Kp=10 Ki=0 Kd=0.3 I would start with Kp first leaving Ki and Kd at 0. After Kp can follow a line without Ki or Kd you should start next with Kd and start with .1 or even smaller if .1 causes Shaking. again Ki isn't necessary because you have shifted the reading by -50 to make the ideal line following setpoint of Zero.

Z

I'm still new to this PID concept but I'm am slowly starting to understand it more and more.

I have a few questions though... First, I'm still not understanding why the integral term is not needed, could you explain why further more? Next, why would this happen?

Too much Derivative your bot will be jittery too little and you bot will be sluggish when the line turns quickly.

Also how would it do this?

the derivative will counter the change to prevent overshooting

Last question...

// Integral term integral += error * (double) (timeChange * .000001); ITerm = ki * integral;

I understand that the ITerm is calculated by multiplying ki by integral, but what I'm not getting is how the integral term is found. It says integral equals integral plus the error, but what is the value of integral?

I also found this link which I found very helpful, if you want to take a look at it:http://samvrit.tk/tutorials/pid-control-arduino-line-follower-robot/

This was actually the way I was thinking of doing it.

I'm sorry for all the questions...

SMM2: I'm still new to this PID concept but I'm am slowly starting to understand it more and more.

I have a few questions though... First, I'm still not understanding why the integral term is not needed, could you explain why further more?

Integral is additive it adjusts the output to allow you to land setpoint. And the example we had below we adjusted the zero 200 by -50 creating the ideal line following position or setpoint to be zero. In a basic sensor for driving a straight line both the left and the right will be turning exactly the same there'll be no offset to turn and so that would also be zero. Because Integral produces an offset to achieve setpoint. And no offset is needed. Integral is not necessary. Integral works great when faced with a constantly changing environment. something like room temperature you need to have the valve for heat open more when it's cold outside and less when it's warmer outside. Integral adds to or subtracts from the output over time to make adjustments to achieve setpoint

Next, why would this happen?Also how would it do this?Last question... I understand that the ITerm is calculated by multiplying ki by integral, but what I'm not getting is how the integral term is found.

Integral is calculated using the error from setpoint and adding it to the integral variable. Integral starts off at zero and is added to or subtracted from itself overtime to achieve a value representing the ideal output to maintain that setpoint. integral variable is added to the proportional and derivative variables create the output

It says integral equals integral plus the error, but what is the value of integral?

I also found this link which I found very helpful, if you want to take a look at it:http://samvrit.tk/tutorials/pid-control-arduino-line-follower-robot/

This was actually the way I was thinking of doing it.

I'm sorry for all the questions...

No problems :) Z

Thanks for your help

Could you help me with my second question?

Derivative is sometimes the hardest one to understand. I've seen two equations to calculate derivative one looks at change in input the second looks at change in error. Both do about the same thing. When a change occurs it counters the change by temporally adding a value. For example when you're line crossing robot is deviating from the line the signal will change that change in signal will feed the derivative with a quick boost faster than the change feeds the proportional. This helps "kick" the motor driving it back onto the line. another example is room temperature. if you open the door cold air flies into the derivative sees that cold air and kick the heater valve open providing more heat into the room proportional will do by itself.

Thanks, that was a great explanation and it helped a lot with my understanding of the derivative term.

May I ask another question.

As far as I can see the PID calculations combine into this line

``````double output = PTerm + ITerm + DTerm ;
``````

and if the error is zero and has been zero for some time the output will be zero.

I guess for the balancing robot that would be correct because it could balance statically without requiring any power.

But for an electric motor that is required to run at a specified speed or an oven required to hold a temperature there must be a positive output even when the error is zero. Where/how does that come into the calculations?

...R

I'm assuming an error of zero that results in an output of zero means there will be no change in the output.

You can see here that the motors will maintain the same speed:

``````int left_motor_speed = initial_motor_speed-PID_value;
int right_motor_speed = initial_motor_speed+PID_value;
``````

Note that "PID_value" is the resulting output, which in this case is 0.

Where are the lines of code you quoted in Reply #32?

...R

Thanks. I was relating my comment in Reply #30 to the balancing-robot code that @zhomeslice kindly provided in Reply #8

This subject is complex enough without trying to absorb the code for another project. And @zhomeslice has also posted some nice video evidence of the impact of changing the k values.

…R

SMM2: You can see here that the motors will maintain the same speed:

``````int left_motor_speed = initial_motor_speed-PID_value;
int right_motor_speed = initial_motor_speed+PID_value;
``````

Note that "PID_value" is the resulting output, which in this case is 0.

I'm assuming an error of zero that results in an output of zero means there will be no change in the output.

If I'm reading your comments right I would say Yes 0 = setpoint the line sensor is on the line at setpoint Integral is set to 0 for this examples sake The output would be zero

PID is on setopint with the Kp = 10 Ki = 0 Kd = 0.4 as an example since setpoint is zero at the ideal line position

``````PID_value = Output; // PID returns 0

initial_motor_speed = 100 // assuming a number between 0 and 255

int left_motor_speed = constrain(initial_motor_speed-PID_value,0,255);
int right_motor_speed = constrain(initial_motor_speed+PID_value,0,255);

// both motors would be going at the same speed
``````