How to Visually Tune PID Control Loops
HTML-код
- Опубликовано: 16 дек 2016
- This video shows how I tuned the PID control loop of a balancing robot. The behavior of the robot is shown, and perhaps more importantly, I also show live graphs of the error, set point, proportional component, integral component and derivative component. Finally the source code is shown and explained.
The video starts with a brief demo of how the robot performs with a tuned PID control loop. The balancing robot does not have any wheel encoders or other position feedback, so the only control loop inputs are the pitch of the robot and the throttle input from the user. Because of that, the robot can drift a little if it picks up speed, but even so, with a properly tuned PID control loop it performs fairly well.
I then show a proportional-only control loop and we see how it performs when the gain is too low or too high.
Next I add the integral part, forming a PI control loop. Again, I show how it looks when the gain is set too low or too high.
Then I add the derivative part, forming the PID control loop. The derivative part helps to dampen oscillations and really brings everything together. Like before, I also show how it behaves when the gain is too low or too high.
Finally I show the source code written for an STM32F0 microcontroller, and explain how it works.
The source code is available here:
github.com/farrellf/Balancing...
This video is part of a series showing how to build a balancing robot:
Part 1: Modify RC Servos for Continuous Rotation and External H-Bridge Control
• Modify RC Servos for C...
Part 2: Building a Robot Chassis with Brass Square Tube, a Dremel, and Solder
• Building a Robot Chass...
Part 3: First Steps with a GY-86 10DOF Sensor: MPU6050, HMC5883L and MS5611
• First Steps with a GY-...
Part 4: 6DOF & 9DOF Sensor Fusion with Madgwick's Filter, MPU6050, HMC5883L (GY-86 Module)
• 6DOF & 9DOF Sensor Fus...
Part 5: How to Use CC2500 PA LNA 2.4GHz Wireless RF Modules
• How to Use CC2500 PA L...
Part 6: GPU-Accelerated Data Logging and Telemetry
• GPU-Accelerated Arduin...
Part 7: [THIS VIDEO] How to Tune PID Control Loops Visually
• How to Visually Tune P... Наука
somebody get this man some wheel encoders
Very nice display and dynamic control for P, I, D constants! Thank you
I've been looking at balancing robot code on the internet for some time now, and this is by far the most exceptional example I have come across. Thanks for sharing this great project, and thanks for also sharing all of the extremely well written source code. Well done!
Glad it was helpful!
best informational video i have seen on the subject. thank you so much
dude you have a series!... I'm in!. great videos!
You are my savior....I was desperate to find one day somebody with enough knowledge of the subject and with the desire to really help others understand it...thank you very much !!!
Great visualisation of my Problem :)
That's great video.. Thank you very much..
Awesome video thank you!
Very well done Thank you
Boom!!! Great video man and you help out a lot. Now, if we could watch that graph as we are flying. Sh%t will get real...
Great video, thanks for sharing the code , thumbs up! I am trying to construct a very similar system but which does is some way the opposite( not to maintain an equilibrium\balance at a set target but to stay away from it) . A safety mechanism that prevents a motorcycle from flipping over when wheeling, when it reaches a critical angle, system kicks in and a servo motor pushes a brake pedal and brings the motorcycle back to "safe" angle range.
sounds like a great idea. patent it!
Excellent!
Brilliant.
Nice video.
fantastic job (y)
I'm sure there is an auto PID system tuner library out there after all what visually is visible is also visible for the sensors.
I'm watching this for nth time, and my robot still wobbles like sh*t :(
hello and great work! -one question on the integral variable. how it gets accumulated if you assign it to 0 just before?
Thanks. It's a "static" variable, so the 0 is only assigned during start up, not during every function call.
nice
I would love to see your state space equations for this, any chance you can post them, how did you model the motors?
I intentionally did no math or engineering for this control loop. The goal was to show how to do it intuitively, to hopefully make it understandable for a wider audience.
very very very helpful. I fully understand it (im in middle school and 13 years old) and is using PID to do robotics stuff!
Very good. What is the minimum number of readings that you must have per second? In other words could it work with only one reading per second?
A PID control loop can be used with any sample rate, but for a balancing robot I wouldn't want to go much slower than 50 Hz.
Nice work there, just have a question. Why is the battery positioned at the top of the robot, isn't it harder for the controller to balance?
Having some weight up high actually makes it easier to balance.
Hi, where did you get that joystick (the gimbal part)?
Also, you might wanna try to control the cossine of the pitch instead of the pitch itself ( i.e. error = cos(pitch) - cos(set_point) ). The torque needed to counteract the falling of the robot should be proportional to the cossine of the pitch. I haven't tried that, but I think that it might help as it would make the system linear.
Thanks for the tip, I'll try that. The gimbal was pulled from a RC hobby radio. You can find used radios for cheap on eBay, and its often straightforward to remove their gimbals and reuse them. Or buy a cheap new radio from somewhere like Hobby King.
Thanks. Yeah, a full radio on hobby king is half the price of a single
gimbal on digikey...But I thought there might be a cheap source for them
on aliexpress or something, but could not find any.
True hero. Real life Tony Stark.
How would you add an additional device that only turn on when the wheels are moving, so if the robot was still the additional device object also stays off but does not have the same voltage.
Hi have you compensated the motor's deadzone ? how you did it? adding a constant value to the pwm ?
The motors don't have much of a deadzone. If you are referring to slop in the gears, the servos I modified have very little slop. I didn't need to compensate for it.
@@upgrdman thanks for your answer, what I actually meant with the dead zone is the the value your PWM in the motors starts making it run. For example, in my case, from 0 to 100, the robot start moving when PWM is around 30.
wow this is great. may i know if the GUI is on windows or Linux?
Thanks. It's running on Windows in the video, but it should run on Linux and Mac OS as well. I wrote it in Java, and the serial port and OpenGL libraries I used are supposedly cross-platform. I've only run it on Windows 10 and 7 but I wouldn't expect any problems on other platforms. For more info on the program, see the "Part 6" link in the video description.
My very first question is how you able to set those values of Kp,Ki and Kd ; please explain!?
Mini Project-I Sem 5 the entire video explains just that
Hi. They are set with the three potentiometers on the remote control.
I have a question about those initial values of Kp, Ki, and Kd. How did the 12000 come out? In this video, you explained with the equation Max error * Kp = Max PWM. So Max error = 1000 (Max PWM) / 12000 (Kp) = 0.083 Radian = about 4.7 Degrees. I guess you set the max pitch angle to about 5 degrees, right? I am confused because you explained that you set max pitch angle to 30 degrees. Also, could you explain more specific about the initial values of Ki and Kd?
It's been a while and I don't remember exactly. I think I just held the robot at an angle that looked like it would require full throttle to recover from, and found that multiplying that pitch number by 12000 would result in a PWM setting of around 1000. So that would probably be around 4.7 degrees like you mentioned. The 30 degrees number is where my code says "we can't recover, turn off the motors, there's no point in even trying any more." This prevents the motors from staying on when the robot has fallen over. As for Ki and Kd, their initial values were 0 and I just tried increasing them until I got decent results. After finding a decent value, go back and tweak the other two, since there is some interaction between them.
Thanks for the reply. It clears my understanding. Btw, your telemetry viewer is awesome. Did you receive the telemetry data via RF module? I am trying to receive serial data in wireless with Bluetooth but having some issues in using BT.
Thanks. Yes, I'm using one of these Bluetooth UARTs: amzn.to/2jm1HIw
So if you did have rotary encoders, how would that affect the set point?
With rotary encoders the robot could know if it is actually stationary. Right now it can only know it's angle. For example, if my floor was slanted, the robot would just glide down the slant (but stay vertical) instead of coming to a stop. Also, if I use encoders I could keep track of distance traveled and position the robot accurately.
upgrdman but even if you know wheel position that wouldn't matter because you still need to monitor your angle to keep it upright. So even with a encoder it will still walk back and fourth?
How do you manage to avoid drifting to one side, accelerating and eventually falling to that side, while balancing without encoders?
It looks like you have perfect mass distribution between front and back (which can't really be in real world).
My robot keeps stabilized only on surfaces with high friction (like carpet), but on smooth floor it keeps drifting to one side and falls.
I would really be grateful for any hint on that.
BTW, great job with your robot.
Thanks. On very smooth surfaces, and sloped surfaces, it can drift like you describe. I was tempted to try using the accelerometer readings to compensate for that type of drift, but never got around to it. I suspect wheel encoders or some other means of velocity/position sensing would be required for best results.
@@upgrdman Hey, thanks for response.
I just realized, that I control PWM (velocity) and not angle like in your case (I also do not have encoders - only angle position). Isn't that the case, why my robot keeps falling immediately to little bit heavier side?
BTW, Merry Christmas ;)
May I suggest something slightly different: Apply a transformation to the total PID output that attempts to "flatten" the rotational acceleration. In other words, work out a function that computes the static torque required to support the weight at each angle. Measure the function that maps throttle input to torque (maybe this is hard to do without complicated hardware, so a good proxy might be to measure current to the motor unloaded (or with any load that does not lock it up) for each throttle position and draw that plot). Then tune a PID algorithm that feeds the input to a control function that only requires normalized torque (and automatically remaps it to throttle based on current instantaneous angle)
Once you flatten out the non-linearity of controlling the inverted pendulum, you can tune a PID (or any other linear controller of your choosing) on top of that and I believe you will have far smoother control.
You're right. I stopped when it got "good enough," but yes, there is plenty of room for improvement.
Can you quickly tell some differences between Kalman filtering and Madgwicks Sensor Fusion?
The biggest difference is that Madgwick's filter is easier to use because it has just one coefficient that needs to be adjusted. A Kalman filter is the classic way of doing things, and it works well, but it requires a lot more tweaking to get things performing well.
great, thank you!
Which software are you using to visualize the PID Data? Please share the name here.
It's a program I made, called "Telemetry Viewer." I cover it in depth in several of my other videos.
Hi, what's your loop update rate?
I don't remember the exact rate, but it was around 70-100Hz.
Oh interesting. And how fast are those servos in terms of RPM? I have an issue where my bot is barely able to maintain vertical position. It may be because my bot is fairly short and does not have enough weight up top.
I thought time between the previous error and current error was part of the derivitive equation?
It is. But if the loop always runs at the same interval it can be cancelled out as part of the derivative gain.
what software is the graphs done on?
It's "Telemetry Viewer", a tool that I made:
www.farrellf.com/TelemetryViewer/
@@upgrdman Thanks
please. is the PID a software control system or hardware????????
I did it in firmware, but a PID control loop can be done either way.
I want software that show in the monitor for PID control
Check out my other videos, I cover the software in my "telemetry" and "data visualization" videos.
try concentrating the center of mass on the wheels' axis to increase stability
That would actually make things more difficult. Think of balancing a pen on your finger. A longer pen (or whatever) is easier to balance. Having the CG higher up makes the system easier to work with.
Hi, could you share your code?
There's a link in the video description.
Im not a java programmer, but i think you set the previous error to 0 every time you run the function.
The firmware is written in C, and that variable is static so it's only initialized to 0 at reset.
I see, C and C# is different in this.
Hello 👋