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...
  • НаукаНаука

Комментарии • 81

  • @tamim24
    @tamim24 5 лет назад +12

    somebody get this man some wheel encoders

  • @human.earthling
    @human.earthling 7 лет назад +1

    Very nice display and dynamic control for P, I, D constants! Thank you

  • @send2murph
    @send2murph Год назад +2

    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!

    • @upgrdman
      @upgrdman  Год назад +2

      Glad it was helpful!

  • @dominicraico1167
    @dominicraico1167 5 лет назад +1

    best informational video i have seen on the subject. thank you so much

  • @alfonshomac
    @alfonshomac 7 лет назад +3

    dude you have a series!... I'm in!. great videos!

  • @jeanbenoit6468
    @jeanbenoit6468 7 лет назад +19

    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 !!!

  • @powling8361
    @powling8361 5 лет назад +2

    Great visualisation of my Problem :)

  • @osamahfarhan
    @osamahfarhan 6 лет назад +2

    That's great video.. Thank you very much..

  • @ObsequiousV4
    @ObsequiousV4 6 месяцев назад +1

    Awesome video thank you!

  • @Daviddarwich
    @Daviddarwich 6 лет назад +1

    Very well done Thank you

  • @brakesitr
    @brakesitr 7 лет назад

    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...

  • @privatefactory1173
    @privatefactory1173 5 лет назад +3

    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.

    • @queenidog1
      @queenidog1 Год назад

      sounds like a great idea. patent it!

  • @viniciusnoyoutube
    @viniciusnoyoutube 5 лет назад +1

    Excellent!

  • @jeffreymelanson9230
    @jeffreymelanson9230 6 лет назад +1

    Brilliant.

  • @47lokeshkumar74
    @47lokeshkumar74 Год назад +1

    Nice video.

  • @abderrahmenbelounis7784
    @abderrahmenbelounis7784 7 лет назад

    fantastic job (y)

  • @mikelemon5109
    @mikelemon5109 7 лет назад +1

    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.

  • @iwasz
    @iwasz 6 лет назад +13

    I'm watching this for nth time, and my robot still wobbles like sh*t :(

  • @aidatifola
    @aidatifola 6 лет назад +1

    hello and great work! -one question on the integral variable. how it gets accumulated if you assign it to 0 just before?

    • @upgrdman
      @upgrdman  6 лет назад

      Thanks. It's a "static" variable, so the 0 is only assigned during start up, not during every function call.

  • @TutocarloCV
    @TutocarloCV 6 лет назад

    nice

  • @mrkattm
    @mrkattm 6 лет назад

    I would love to see your state space equations for this, any chance you can post them, how did you model the motors?

    • @upgrdman
      @upgrdman  6 лет назад +1

      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.

  • @edencheung8468
    @edencheung8468 5 лет назад +1

    very very very helpful. I fully understand it (im in middle school and 13 years old) and is using PID to do robotics stuff!

  • @johnnicinco
    @johnnicinco 7 лет назад

    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?

    • @upgrdman
      @upgrdman  7 лет назад

      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.

  • @AbishaiSingh
    @AbishaiSingh 6 лет назад

    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?

    • @upgrdman
      @upgrdman  6 лет назад

      Having some weight up high actually makes it easier to balance.

  • @montinhoman
    @montinhoman 7 лет назад

    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.

    • @upgrdman
      @upgrdman  7 лет назад

      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.

    • @montinhoman
      @montinhoman 7 лет назад

      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.

  • @numberformat
    @numberformat 6 лет назад +2

    True hero. Real life Tony Stark.

  • @davidtitley4130
    @davidtitley4130 2 месяца назад

    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.

  • @andreluizeng
    @andreluizeng 3 года назад

    Hi have you compensated the motor's deadzone ? how you did it? adding a constant value to the pwm ?

    • @upgrdman
      @upgrdman  3 года назад

      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.

    • @andreluizeng
      @andreluizeng 3 года назад +1

      @@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.

  • @kanabalize
    @kanabalize 7 лет назад

    wow this is great. may i know if the GUI is on windows or Linux?

    • @upgrdman
      @upgrdman  7 лет назад

      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.

  • @miniproject-isem5381
    @miniproject-isem5381 7 лет назад

    My very first question is how you able to set those values of Kp,Ki and Kd ; please explain!?

    • @miniwarrior7
      @miniwarrior7 7 лет назад

      Mini Project-I Sem 5 the entire video explains just that

    • @upgrdman
      @upgrdman  7 лет назад

      Hi. They are set with the three potentiometers on the remote control.

  • @inhwankim640
    @inhwankim640 6 лет назад

    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?

    • @upgrdman
      @upgrdman  6 лет назад

      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.

    • @inhwankim640
      @inhwankim640 6 лет назад

      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.

    • @upgrdman
      @upgrdman  6 лет назад

      Thanks. Yes, I'm using one of these Bluetooth UARTs: amzn.to/2jm1HIw

  • @merseyviking
    @merseyviking 7 лет назад

    So if you did have rotary encoders, how would that affect the set point?

    • @upgrdman
      @upgrdman  7 лет назад

      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.

    • @miniwarrior7
      @miniwarrior7 7 лет назад

      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?

  • @Artur960426
    @Artur960426 5 лет назад

    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.

    • @upgrdman
      @upgrdman  5 лет назад

      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.

    • @Artur960426
      @Artur960426 5 лет назад

      @@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 ;)

  • @aurelioramos8463
    @aurelioramos8463 4 года назад

    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.

    • @upgrdman
      @upgrdman  4 года назад

      You're right. I stopped when it got "good enough," but yes, there is plenty of room for improvement.

  • @iOLlVER
    @iOLlVER 6 лет назад

    Can you quickly tell some differences between Kalman filtering and Madgwicks Sensor Fusion?

    • @upgrdman
      @upgrdman  6 лет назад +1

      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.

    • @iOLlVER
      @iOLlVER 6 лет назад

      great, thank you!

  • @srikantjenakumar
    @srikantjenakumar 6 лет назад

    Which software are you using to visualize the PID Data? Please share the name here.

    • @upgrdman
      @upgrdman  6 лет назад

      It's a program I made, called "Telemetry Viewer." I cover it in depth in several of my other videos.

  • @mrarmymanful
    @mrarmymanful 7 лет назад

    Hi, what's your loop update rate?

    • @upgrdman
      @upgrdman  7 лет назад

      I don't remember the exact rate, but it was around 70-100Hz.

    • @mrarmymanful
      @mrarmymanful 7 лет назад

      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.

  • @shawncrocker7037
    @shawncrocker7037 6 лет назад

    I thought time between the previous error and current error was part of the derivitive equation?

    • @philsson
      @philsson 5 лет назад

      It is. But if the loop always runs at the same interval it can be cancelled out as part of the derivative gain.

  • @jamescullins2709
    @jamescullins2709 3 года назад

    what software is the graphs done on?

    • @upgrdman
      @upgrdman  3 года назад +1

      It's "Telemetry Viewer", a tool that I made:
      www.farrellf.com/TelemetryViewer/

    • @jamescullins2709
      @jamescullins2709 3 года назад

      @@upgrdman Thanks

  • @alibasri6729
    @alibasri6729 2 года назад

    please. is the PID a software control system or hardware????????

    • @upgrdman
      @upgrdman  2 года назад

      I did it in firmware, but a PID control loop can be done either way.

  • @mohamedkhaled-qc7kb
    @mohamedkhaled-qc7kb 6 лет назад

    I want software that show in the monitor for PID control

    • @upgrdman
      @upgrdman  6 лет назад

      Check out my other videos, I cover the software in my "telemetry" and "data visualization" videos.

  • @ammarmahmoud2224
    @ammarmahmoud2224 7 лет назад

    try concentrating the center of mass on the wheels' axis to increase stability

    • @upgrdman
      @upgrdman  7 лет назад +8

      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.

  • @guillermomejiacisneros4304
    @guillermomejiacisneros4304 2 года назад

    Hi, could you share your code?

    • @upgrdman
      @upgrdman  2 года назад

      There's a link in the video description.

  • @psanyi0042
    @psanyi0042 7 лет назад

    Im not a java programmer, but i think you set the previous error to 0 every time you run the function.

    • @upgrdman
      @upgrdman  7 лет назад

      The firmware is written in C, and that variable is static so it's only initialized to 0 at reset.

    • @psanyi0042
      @psanyi0042 7 лет назад

      I see, C and C# is different in this.

  • @ustaoybek
    @ustaoybek Год назад

    Hello 👋