How to Write a Simple PID Line Follow Algorithm

Поделиться
HTML-код
  • Опубликовано: 6 июл 2024
  • I made a couple goofs regarding the speed of my motors. I forgot that the motor driver library I was using ranges from 0 to 100 for motor speed and I did my calculations from 0 to 255 so it took longer than normal to find a working P value.
    This isn't a true PID in that it doesn't include an I term and it doesn't account for the amount of time between each reading. In a simple PID these don't matter all that much, as long as your readings are consistent in timing.
    The blog article for this build can be found here: robotresearchlab.com/2019/03/1...
  • НаукаНаука

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

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

    Your efforts, your work,can be seen in us.

  • @steveh2433
    @steveh2433 9 месяцев назад

    Great Video😀Has made understanding PID programming really easy

  • @rock1223
    @rock1223 4 года назад +1

    Thanks for all PID videos Sir 😍

  • @ynsche851
    @ynsche851 Месяц назад

    Best Tutorial so far! good job sir.

  • @gowrishankar7011
    @gowrishankar7011 10 месяцев назад

    Great video good explanation.. I have a doubt y we need d term?

  • @nhatpham5797
    @nhatpham5797 4 месяца назад +1

    hello, thank you for your lecture, can I ask you about this PID algorithm but with the motor running backward? I still can't do it

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

    i thought pid was though but when i say your video i just understood it very well waiting for the next video on pid and also want a video with arduino and l298n motor driver.the same algorothim

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

    Hello thank you for your video
    I would like to know if this algorithm will work with a feedback mechanism too, like an encoder for example?
    Also if my robot is meant to go up a slope, should I include the I term for better stability or is it not necessary?

    • @RobotResearchLab
      @RobotResearchLab  3 года назад +2

      In this particular video I'm using one of the motor libraries I developed. 100 in this case means percent of power so when I say 100 I mean full power.
      Typically power ranges from 0 to 255 which I thought might be confusing so I developed my library using 0 to 100 for the power range.

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

      Yes, this algorithm will work for pretty much any case where you have an output and a goal setpoint, you just have to come up with the right way to convert the error into an adjustment value. For encoders, what I have done in the past is set the goal to some specific distance for each encoder (like say, the goal is to be 1 meter from where I currently am) and the PID would return the current distance from the start point. Then you can compare the current position with the goal.
      The other way you can do this is to have a setpoint be a speed (meters/second). You can take a very quick read of the encoders to see what speed is being recorded currently, then you can adjust the motors to maintain a specific speed.
      The most complicated way to do this would be combine both, you set a distance and a speed, the PID monitors the speed and you just simply keep checking the distance traveled since the start of the movement until you hit the desired distance.
      As far as needing an I term for an incline, it depends on a lot of variables. If your motors are weak and need more power to get up the incline the I term might be needed to get up the incline. But, it's also possible that you would be on the center of the line and so PID returns an adjustment closer to zero, in this case the only way to know if your robot is moving up the hill would be to use encoders.
      This is probably a lot to process in one response so let me know if you have any questions on this response. Good questions though.

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

    Hello, sir. May i ask, what is the problem with my line following robot because the left gear motor is not running and the right one is running. My components are Arduino Uno, L298N, and the QTR-8RC SENSOR ARRAY. Also, i use 12V battery.

  • @Mr0neShotAway
    @Mr0neShotAway 4 года назад +1

    These videos are great! Could you please make a video showing your approach with very sharp/acute and 90 deg angles? Also, how to sense color with multiple color sensors with a single Arduino/mC. Thanks a lot for the information!

    • @RobotResearchLab
      @RobotResearchLab  4 года назад +2

      Thanks, I'll add it to my list, unfortunately I'm long overdue for a video but just haven't had the time. When you say 90 degree turn are you talking about only one turn or like a T or cross where there are multiple directions at 90 degrees?

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

      @@RobotResearchLab What i mean is this: robotics.stackexchange.com/questions/9728/how-to-check-for-a-sharp-angle-with-a-line-follower

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

      @@Mr0neShotAway Sorry, I didn't get notified of this comment for some reason. Off the cuff I would say you could try to build some intelligence into your program so if it loses the line it remembers which sensor last saw it so it knows to keep turning in that direction until getting back to the line. Although, I may not be understanding your issue exactly as it's difficult without videos of the problem. You could also try to use the QTR library, just set it up with a 2 sensor array and the rest should work the same.
      Also, I just created a video on programming a color sensor that I'll be posting soon but your comment mentions "multiple sensors with a single Arduino". The video only covers using one sensor, are you talking about using multiple color sensors on the same pin? If not then you could use the same code to run one and just modify it to read a second one (maybe that's what your needing help with, copying and modifying code for one sensor to work with more than one?)

  • @akarshtechshooter8193
    @akarshtechshooter8193 4 года назад +3

    can we implement PID for sharp turns, chess boxes, discontinues lines.
    Is PID restricted for curves smooth performance ???

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

      PID will work for sharp turns however, you won't know what it's reaction will be because it's going to be dependent on the readings from the sensor. So, if you have a left and right, it may go right sometimes and it may go left other times. If you want to make a decision at a turn or intersection then you would do something like what I do in this video ruclips.net/video/R1UMd1FXDwU/видео.html
      Let me know if this helps.

  • @alltop172
    @alltop172 6 месяцев назад

    1) What i have to do to replace the motor driver with L298.
    2) If i want to use TB6612FNG then will it work with the same library of TB67H420TBG or i have to make anything change. and what's that?
    please help sir🙏

  • @shafiqalom8408
    @shafiqalom8408 4 года назад +1

    Awesome...♥️
    What should I change in the code, 1) if White line onto back background only or (black+white) line with opposite background comes randomly? How to change Calibration then?
    2) If one side of line follower track remain dark 🌃 weather but other side remain in sunlight ⛅ then how to calibrate line follower?Can you make such type of line follower video?

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

      These are great ideas for new videos, I'll add them to my list. Just to give you some immediate feedback though.
      For issue number 1, one solution would be to monitor the track using the sensors individual readings. So something like "if more than 4 of 8 sensors see black, assume we have a black background with white tape but if more than 4 of 8 sensors see white, assume we have a white background with black tape" Then depending on the situation, you can pass a variable into the readLine function to tell it if you're following a black or white line. With the new library there are two different functions 'readLineBlack' and 'readLineWhite' so just use whichever you need to based on your if statement.
      For issue number 2, one solution might be (not sure this would be enough) to run two calibration phases. Run a calibration in the sunny area and one in the dark area, the calibration won't get reset between the two phases so you don't need to worry about that. Ideally you want to calibrate your sensors in every environment that your robot will see so while it might work just calibrating in the sunny or the dark area, it would be best to calibrate once in each area. This could be as simple as just running the calibration, then putting a ten second pause until another calibration loop to give you time to move to the other section of the track. A better solution might be to use a button though. Also, and probably more importantly, you should try to shield your sensor from any sunlight, even putting a "wall" of electrical tape around your sensor is better than letting the sun interfere with your readings. The sun has a lot of IR light so it can wash out your sensor values so much that you'll never get good enough readings to read the line.
      I hope that helps you out until I can get time to come up with some videos on this, thanks for watching.

  • @ninhtrieu9731
    @ninhtrieu9731 Год назад +1

    Can I use TB6612fng to control? The line I sensor in the middle receives a white line, the other 2 on each side receive a black line. I use TCRT5000, thank you very much.

    • @RobotResearchLab
      @RobotResearchLab  11 месяцев назад

      Sorry for the late reply, you could use the TB6612fng motor driver to control the motors. Almost any motor driver would work as long as it handles at least two motors.

  • @godgajet6763
    @godgajet6763 8 месяцев назад

    how can i do this with 5 tcrt array?

  • @joegabriel8021
    @joegabriel8021 Год назад +1

    hi can u suggest the code when l293d motor shield is used

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

      I use a library I wrote. I prefer this library because it will work no matter which motor driver you use, and if you change motor drivers, you only have to change one line of code, the rest is the same.
      With that said, here's the library, there is no example for the L293 chip but if you look on the first page in the README it shows how I change the DRV8835 code to work on L293 so there is code pasted right in the README.
      github.com/mcc-robotics/Dynamic_Motor_Driver
      Let me know if you have issues.

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

    can i download these two library that you used on arduino app

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

      You can't download them from the Library Manager in the IDE but you can download them from github and then add them using the Library Manager under "Add Zip File"
      The motor driver library can be found here github.com/mcc-robotics/Dynamic_Motor_Driver
      The QTR Sensors library can be found here github.com/gberl001/qtr-sensors-arduino

  • @usuariogenerico2
    @usuariogenerico2 4 года назад +2

    Why didn't you use the Integral term? And also, why your KD is zero?
    Thank you very much for this explanation!!

    • @RobotResearchLab
      @RobotResearchLab  4 года назад +1

      I didn't use the Integral term because it's generally not needed for a simple line follow and it adds a lot of complexity because you have to pay attention to overflow and other aspects.
      I set KD to zero when I tune the PD algorithm because it's more difficult to tune P and D together. By setting it to zero it multiplies away the D calculation. Once I get a good line follow with only P, I will then start adding a D value and adjusting it. If I tried to set both P and D at the same time, I would never know for certain if the problem was P or D so always set one at a time. Then you can adjust them slightly later once they've all been added.
      Thanks for watching!

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

      @@RobotResearchLab undestood! Thanks

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

      ​@@RobotResearchLab Sorry to ask you again, but, do you think that, if I implement a full PID controller on my line follower, it will have a good raise on stability?

    • @RobotResearchLab
      @RobotResearchLab  4 года назад +1

      @@usuariogenerico2 I'm not sure what you mean but "raise on stability" but if you are just doing a line follower on a simple track it's possible a full PID would be better but again, the I term isn't necessary for simpler applications. It helps if one motor is significantly weaker than another, if there are hills, if the surface changes causing more or less traction. Basically the I term helps with external influences.

  • @keshavshanmukhanathane3500
    @keshavshanmukhanathane3500 10 месяцев назад +1

    One doubt!!! Is this path dependent or path independent? Can this code be run for other lines?

    • @RobotResearchLab
      @RobotResearchLab  10 месяцев назад

      This is path-independent. All the code is doing is trying to stay on a line. Therefore, if there are tees or intersections, it may make a left turn one time, and another time it might make a right turn. With intersections, it generally goes straight since the line is going to give you a center-weighted value. It depends on how the robot approaches the tee or intersection at that specific time.

  • @jovangarcia2935
    @jovangarcia2935 3 года назад +2

    hello sir. can i use qtr-md-05rc sensor array for this project? thank you!

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

      Sorry for the delay, yes you can use any QTR sensor array you want. You just need to modify the code so it knows how many sensors you have. And you'll have to recalculate your "goal" and "pid" values.
      For example, you'll need to change NUM_SENSORS to 5, and adjust the array of pins they're attached to. Then for the goal, I can't recall if I covered the math in the video but you have 5 sensors so your readings will be between 0 and 4000, so to have a goal of center, you'd want to use a goal value of 2000 (which will be under the third sensor). Then for your initial P value, you'll want it a bit larger so that 4000*P equals your max motor speed... so if you're using a library with motor speeds 0-100 that would be about 100/4000=~0.025 or if your motor speeds are 0-255 that would be 255/4000=~.0063

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

      @@RobotResearchLab i've watched your motor driver video. im using l298n motor driver. what would i do? ill just replace the object with l298?
      btw thanks for the reply and help!

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

      @@jovangarcia2935 That's exactly right, just replace the object declaration with your motor driver type. So, if you're using the L298 you would use this line
      L298 driver(3, 4, 5, 6);
      Where 3 and 4 are pins for AIN 1 and 2, 5 and 6 are for BIN 1 and 2. So just change those to whatever pins you are using, be sure they are PWM pins. It also assumes that you pull the enable pin high. There are two ways to hook up an L298 and the way the library expects it is to always have the enable line high, then provide a PWM for the AIN1, AIN2, BIN1, and BIN2 pins. The other way to hook it up requires 6 pins so I did it this way to require less pins but still get forward and backward motion.
      Also, I got your message from my contact page, thanks, I am glad I'm able to help. Unfortunately I don't have much time these days to post videos but I almost always have time to respond to comments so I try to continue helping in that way. Thanks for your support!

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

      @@RobotResearchLab Thank you sir!

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

    Can I use 6v n20 motor for this??

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

      You would need to use a different motor driver for 6V motors, technically you could run 6V motors at 12V but they will likely burn out much faster than normal. So, you'd need to change the 12V regulator to a 6V regulator that handles the right current draw, then you'd have to have a motor driver that supports 6V motors for the amount of current you need to draw.
      I chose the 12V because they draw less current for the same power since the voltage is twice that of 6V. It's harder (and typically more expensive) to find components for higher current motors.

  • @ascrolpatil6154
    @ascrolpatil6154 4 года назад +2

    HELLO SIR . How to use counter in a pid code and how to stop a robot at the end of the track ?

    • @RobotResearchLab
      @RobotResearchLab  4 года назад +1

      Working on it, hopefully it will be up soon, thanks for the suggestion.

    • @ascrolpatil6154
      @ascrolpatil6154 4 года назад +1

      @@RobotResearchLab Also it is possible to share the code of maze solver Robot using pid ?

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

      @@ascrolpatil6154 I will add it to my list, the line counting video should be up soon so keep an eye out.

    • @RobotResearchLab
      @RobotResearchLab  4 года назад +1

      As requested... a video on counting lines, let me know if it helped you or if I'm missing something. Thanks ruclips.net/video/R1UMd1FXDwU/видео.html

    • @ascrolpatil6154
      @ascrolpatil6154 4 года назад +1

      @@RobotResearchLab
      Yeh Thankyou a lot..
      I will try it

  • @harshitsrivastava5559
    @harshitsrivastava5559 4 года назад +1

    Can I use arduino for the same code??

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

      Yes, the same code will work for an Arduino, but you'll have to hook it up to the correct pins for the Arduino board you choose. Like making sure you choose the correct pins for PWM outputs (for the motors). Also, be aware that the QTR-8RC uses 8 digital pins which is quite a few pins from the Uno and Nano boards, and if you use QTR-8A you won't have enough analog pins for all 8 sensors. It can still work but you'll only be able to use 6 sensors (I would leave the outside sensors disconnected). For using Arduino, I would recommend the Mega board as it has enough analog and digital pins but it's a little bulky and expensive for what you get but if you must use Arduino that's what I would choose.

  • @hadwart
    @hadwart Год назад +1

    sir you have my 1 subscribe. Your video absolutely saves me ass lol

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

    instead of using constrain can we just write this?-
    if((MAX_SPEED+adjustment)>255)
    {driver.setMotorAPower(MAX_SPEED);}
    else
    {driver.setMotorAPower(MAX_SPEED+adjustment);}
    if((MAX_SPEED-adjustment)

    • @RobotResearchLab
      @RobotResearchLab  2 года назад +2

      The code you wrote is basically the same as the Arduino source code for constrain so yes. You have a typo in your first if statement, it should set the motor power to 255, not MAX_SPEED.
      Here's the actual source from the Arduino core library, it might look confusing if you aren't familiar with ternary operators. Constrain() just makes the code look simpler since it only uses one line of code but in the background it is the same.
      #define constrain(amt,low,high) ((amt)(high)?(high):(amt)))
      A lot of the Arduino functions are like this, they are just ways of simplifying code making it easier for people to learn how to code. Good catch. If you're ever interested in learning how other functions in Arduino work, you can use the search field in the github repository here github.com/arduino/ArduinoCore-avr

    • @Thinkdifferentsagnic
      @Thinkdifferentsagnic 2 года назад +1

      @@RobotResearchLab Thank You Sir

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

    hi..i have a question. in your code you wrote "/*
    * Initialize our driver with the pins (motorA1, motorA2, motorB1, motorB2) and we're assuming:
    * Motor A = Left motor
    * Motor B = Right motor
    * The pins here would map to AIN1, AIN2, BIN1, BIN2 respectively
    */
    L298 driver(3, 4, 5, 6);"
    you are using two motors here but you haven't defined any pin for PWM/ENA or ENB...so how do you actually control the speed?

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

      Sorry, I didn't see this comment until just now. There are two main ways to program an L298 driver, you can use 4 PWM and 2 digital or 4 digital and 2 PWM. If I recall, the way I programmed the L298 in the library assumed that you would have the enable lines pulled high and the 4 pins for the two motors would all be PWM. What this does is it has the motors always enabled since they're pulled high with a wire, and then the speed and direction is controlled by the INA1, INA2, etc lines. For example, to go one direction you would send a PWM signal to INA1 and drive INA2 low, then to go the other your would do the opposite. To brake you would send the same PWM signal to both though I believe I found through research that it was not that simple so a true "brake" cannot be performed on the L298 so you should only expect a "coast".
      The other way to program this would be to use digital pins on INA1 and INA2 to control direction (setting one high and one low) and then use PWM on the Enable lines ENA to control the speed. I believe when I wrote the library I wanted the 298 to be easily interchangeable with the DRV8835 since both were commonly used by students in my class. That is why I wrote it in a way that used four PWM pins rather than 4 digital and 2 PWM (because the DRV8835 used 4 PWM pins)

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

    thanks, i will recommend you to my friends!!!

    • @RobotResearchLab
      @RobotResearchLab  4 года назад +1

      Antonio luiz thank you for the support, I’m taking a break during the pandemic but rest assured I’ll be creating content again soon.

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

    Hi, can you make a PID code based only on analogWrite for the motors? I use a L298n mini driver and a QTR-8RC Poolu line sensor

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

      Sure can, I'll add it to my list, it's been a while since I uploaded a video so I'm hoping to get one done soon. Thanks for the feedback

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

      @@RobotResearchLab Do you have some time for make it sooner? My competition is tomorrow

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

      @@errazvanoficial53 That's a little too soon ha ha. It's going to depend on how you hooked up your L298 but I can try to help out in a comment here. The library I'm using will work for the L298 series motor drivers, just use the exact same code but instead of creating a TB67H420G object create an L298 object and adjust the pin values as needed. I actually wrote up an example on the README in the library, check it out here and scroll down to read it github.com/mcc-robotics/Dynamic_Motor_Driver
      If you want to go ahead with using analogWrite() then:
      OPTION 1: If you have Enable A connected to PWM, then IN1 and IN2 connected to digital, writing IN1 HIGH and IN2 LOW with analogWrite to the Enable pin will power the motor in one direction. Then if you flip the signals so IN1 is LOW and IN2 is HIGH you'll go the other direction.
      OPTION 2: If you have Enable A connected to digital, then IN1 and IN2 each to PWM you set Enable A to HIGH so the motor is always enabled, then using analogWrite a positive value to IN1 and analogWrite(IN2, 0) will go one direction and set analogWrite(IN1, 0) and a positive analogWrite to IN2 and it will go the other direction.
      Hope some of that helps, happy coding.

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

      Just a quick follow up, the proper format would be the following:
      L298 driver(in1, in2, enableA, in3, in4, enableB)
      Where in1, in2, in3, and in4 are digital pins and a PWM pin is assigned to EnableA and EnableB
      Again, I hope this helps.

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

      @@RobotResearchLab that was helpful lol Xd

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

    where is the code ?

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

      There's a link in the description... apparently my site is down but I'll work on getting that fixed
      robotresearchlab.com/2019/03/13/build-a-custom-pid-line-following-robot-from-scratch/

  • @JohnJohn-gy2st
    @JohnJohn-gy2st Год назад

    Hello 600