# What PID values should I keep?

Robotics Asked by Sulaiman Ayub on January 6, 2022

I have built quadcopter but the problem is of balancing. It doesn’t goes up. I am using PID technique for balancing. But I am not finding the suitable values for PID tuning. I am using mpu6050 as a sensor. I get the accelerometer values of x and y axis and find the error from them. That is lets say if accel on x is not zero then it error cause it should be zero if balanced. I am using +-2g sensitivity scale of accelerometer. The motors I am using are DJI 920 kva. What values for kp, ki, and kd should I set? I cant set them while in flight cause it completely out of balance.

This is the design. Completely homemade. I have modified it a little after this photo. An accelerometer is at 2g so at balance z will be 32768/2.

short PID()
{
short error,v;
error = desired-current;
//error/=390;
integ += error;
der = error - perror;
x = error;
x2 = integ;
x3 = der;
x* = kp;
x2* = ki;
x3* = kd;
v = kpi;x/=100;
v = kii;x2/=1000;
v = kdi;x3/=1000;
x = x+x2+x3;
//x/=390;
perror = error;
return x;
}


There are also few more questions, should I scale error or PID output? Because, the error is ranging from 0 to 16380 at 2g setting, so I am scaling it from 0 to 42. So should I divide error or PID by some value?

Okay, so developed a flight controller for this project http://kevinsheridan.info/highly-autonomous-aerial-reconnaissance-robot-proof-of-concept/

I spent 4 months trying to figure out "the right pid values". However in the end it turned out that I was implementing pid incorrectly.

Here is how to implement it. If you are trying to get the quad to stabilize itself at an angle you need to use TWO Pid controllers per axis.

Requested angle & Angle -> PID1 -> rate + requested rate into PID2-> motors

Basically use a pid for rate and for angle feeding into eachother

Answered by Trexter on January 6, 2022

I asked for your code because I've found people tend to implement PID controllers incorrectly. It doesn't really matter what your gains are, if you're not calculating the error terms correctly and/or you're not applying the gains correctly, you'll probably never get the system to work right.

That said, as Arlakt mentioned, your best bet for PID control with no system model is to try different gains. There are several methods for tuning the PID controller, but all of these methods rely on you to calculate the error terms correctly and apply the gains correctly.

## :EDIT:

You are not calculating your error terms correctly.

You have:

error = desired-current;
//error/=390;
integ += error;
der = error - perror;


Your integ error term is not an integral and your der error term is not a derivative because you have not incorporated a sample time into your calculation. You need:

integ += error*sampleTime;
der = (error - perror)/sampleTime;


Later, you have:

v=kpi;x/=100;
v=kii;x2/=1000;
v=kdi;x3/=1000;


Which I am assuming is interpreted as:

v=kpi;
x/=100;
v=kii;
x2/=1000;
v=kdi;
x3/=1000;


I do not know why you are dividing your error terms by 100 or 1000, but this is incorrect. I also do not know what v is; it is not a term used anywhere else in the function and is not returned by the function. kpi,kii,kdi are also not defined.

In any event, your entire PID controller should be:

short PID()
{
short error;
error = desired-current;
integ += error*sampleTime;
der = (error - perror)/sampleTime;
x=error;
x2=integ;
x3=der;
x*=kp;
x2*=ki;
x3*=kd;
x=x+x2+x3;
perror = error;
return x;
}


See this post I wrote yesterday on nearly identical flaws in a PID controller. In short, get sampleTime with either a delay/pause or by setting/using a timer.

Answered by Chuck on January 6, 2022

I'm sorry but it is absolutely impossible to tell you what values you should put based on the information you gave. Actually to give a real answer we would need a complete model of the system (or an approximation at least...). There are some generic models for quadcopters but parameters depend on your own system.

If you want to really understand the command of a quadcopter I recommend to read some articles like this one (I found it very quickly, maybe some others are better, but it explains how to control a quadcopter with a PID). Or at least watch this video to understand what is a PID controler.

However, if you don't want to find a model for your system, the best way to find PID parameters is empirically. You just need to know what are the consequences of each coefficient, and try to adapt them according to your system's behaviour. There is a very quick explanation for each coeff :

• P : is mainly for response speed. Too low = correction will be to slow. Too high : correction will be very fast and oversized, and your system will probably oscillate.
• I : is mainly used to cancel static error. Too low : your response will never reach the order. Too high : oscillations.
• D : is... more complex to understand. It will impact your response variation. Look at the video to have a good explanation.

Good luck !

Answered by user10198 on January 6, 2022

## Related Questions

### Which type of actuator will be suitable for a very strong robot arm?

5  Asked on January 6, 2022

### Why do quadcopters use brushless motors?

3  Asked on January 6, 2022 by robz

### How to track robot position?

2  Asked on January 6, 2022 by michael-coss

### How to make a robot move using Arduino other than timing to predefined locations?

2  Asked on January 6, 2022 by olivier-how

### What PID values should I keep?

3  Asked on January 6, 2022 by sulaiman-ayub

### How to combine odometry information with time-shifted information from IMU?

3  Asked on January 6, 2022 by jstanle1

### Inverse Kinematics Computation — why are alpha angle values not included?

1  Asked on January 6, 2022 by hey

### Why ODE for optimal control theory?

2  Asked on January 6, 2022 by bicepjai

### What exactly does a motor driver do? Why do we need an additional power supply for motors?

3  Asked on January 6, 2022 by guubi

### How to call remote ROS node on mobile robot through laptop using wifi?

3  Asked on January 6, 2022 by kaviranga

### How to set the output in a differential amplifier?

1  Asked on January 6, 2022 by siddiqui_902003

### The biases in the state vector of Extended Kalman Filter(EKF)

1  Asked on January 6, 2022

### Why to seal a motor shaft, or the whole motor, for underwater ROV?

2  Asked on January 6, 2022 by rotor-rotor

### digital servo won’t seek to angle if delta between current position and target is more than ~15 degrees

1  Asked on January 6, 2022 by gever-tulley

### Robot arm matlab Simulink simulation error

1  Asked on January 4, 2022

### Building a stationary robot. ROS the right tool for me?

4  Asked on January 4, 2022 by frimann-bjornsson

### Calculating the distance of a point from point cloud data

2  Asked on January 2, 2022 by paul-shuvo

### Simulator for NEMA17 (stepper motor) and A4988 (stepper motor driver) along with Arduino

1  Asked on December 28, 2021 by glassanimals

### Given this old pin diagram and numbers on this old encoder, what would be a suitable modern encoder to use?

1  Asked on December 24, 2021 by nick-bowyer

### How to do a relative rotation of a cuboid such that the longest edge is upright?

0  Asked on December 24, 2021