I dont know if you have found the desired gains (Kp, Ki, Kd). But here are some things you can do and understand:
Ki, is the integral term:
The purpose of Ki is to remove steady state error. For example, in the event of disturbance, with only Kp and Kd gains, the robot will oscillate back "close" to desired reference (vertical position) but will never be 100% vertical. Increasing Kp might reduce this error, however, it might also made the robot unstable. For this reason, Ki is usually used instead of increasing Kp. However, too much Ki may result to overshoot and too much oscillation.
From diagram you shown, your system is clearly a cascaded control system
- It is often best to let the gains of the outer PID control (PID
angle) constant to some value first.
- After that, set Kd and Ki of the inner PID controls (motor left and motor right) to zero first.
- Then increase Kp of inner PID until the robot become unstable (oscillate). At this stage, it doesnt really matter if the robot is not yet vertical. It just have to have enough Kp to be able to reach the vertical point from an angle offset before falling down.
- Set (Kt = 1.2 * Kp / A) and Kd = (3 * Kp * A / 40), where "A" is the oscillation period you desired, im guessing you want to set A to be as low as possible, without getting the motor to be saturated.
- Repeat step 2 to 4 for the outer PID control (PID angle).
This tuning method is called the Ziegler–Nichols cascaded tuning method.
That being said, you still might need to make small adjustment of the gains to fit your requirement. From the description you gave of it initially stable, but then start to oscillate, it sounds like you need to increase Kd to stabilized it and minimize overshoot (making it more smooth steady movement)
The "proper" method of gain tuning in PID:
There isn't a proper method, it depends on your circumstances. But usually, trial and error is not a viable option. Therefore, you would want to actually build a mathematical model and calculate the kinematics of the system. From there you can get an differential equation, where you can calculate exactly the Kp, Ki, Kd you need based on a time specification you required (rise time, settling time, amount of overshoot, etc).
The other way is loop shaping. This is where you can estimate at what frequency the disturbance and sensor measurement noise happened at. From there, you shape the loop in bode plot to the shape you desire so that it has sufficient gain and phase margin. Then, you divide it by the plant transfer function to get the control transfer function. Converting to time domain, you get a control system that is often more robust to disturbance and noise rejection.