HOWTO create a Line Following Robot using Mindstorms. The easy way of learning how to built your own Line follower Robot using a PID Controller without any of the mathematical expressions that you will find on most tutorials. Code included! The first thing you have thought about after unboxing your LEGO Mindstorms was building the first robot, and just after that you would love to make it follow lines, isn’t it? Line following is one of the most common problems on industrial robots, and it is one of the most useful applications because it allows the robot to move from one point to another to do tasks. There are several ways of making a Line Follower, the one that I am going to explain you about is using the Light sensor. As you know, both Mindstorms and EV3 sets come with a little light sensor that it is able to get a reading of reflected light, apart of seeing colors. On this tutorial I will explain how to do line following with just one sensor. The more sensors you have the better and faster the robot will be able to follow it. Building the line follower robot. So first thing is build yourself a little robot much like Track. You can download the instructions provided by LEGO. It is a simple construction. Or base it on one of the LEGO Education models? Let me explain how we get to the “best” solution with a serie of intermediate steps that will help you understand it better. Building your playground for line following. Ok, the robot is done. How can you design and program a robot to follow a line? Line Follower lesson plan – NXT + ROBOLAB / NXT-G (PDF). Try the Line Follower on the NXT Test Pad. Design your own course with tape on the floor. Various microcontroller project and schematics, Electronics Principles. Building various robot include line follower robot and autonomous bots. A PID Controller For Lego Mindstorms Robots. A PID Controller is a common technique used to control a wide variety of machinery including vehicles, robots and even rockets. The complete mathematical description of a PID. But before start coding, we need the line that the robot will follow. If you happen to have the Mindstorms NXT 2. But if you don’t just do like me. I have used a black tape and with the finger I have sticked it to the floor creating a continuous path that the robot will follow. You don’t need a closed loop ( although it is a good idea to do it in that way ). My floor is done of marble that it is white and brown at times and even with that it works.
So it may work too on yours unless it has even less contrast than mine. Line Following Problem definition. It is quite important to understand the line following problem first. So let’s describe the problem. We have a thick black line on a white surface and we want our robot to move along the line following it in the fastest possible way. Well, first thing that we need to understand is that we don’t want to follow the line ( wtf?! We want to follow the line where there is a 5. So, next step is defining what it is black and what it is white. I hope you have a clear idea of what these two colors are, but unfortunatelly your robot don’t. So the best thing you can do, before starting anything else is calibrate the robot. Light Sensor calibration. Ok, as you know Color sensor can also work as a Light sensor, so we choose the Measure reflected light mode and we are going to store in two variables the white and black colors. The reflected light value is just a number between 0 and 1. So the pseudocode would be. CALIBRATE. print ? We add a Touch sensor to our Robot to record the light value, you can also do it using Brick buttons, as you prefer. Here is the EV3 code that I used for it. So the idea is that you place it on the white surface, press the touch sensor, place it now on the black surface and press the touch sensor again, now we have the white and black readings and can start working. I do it each time I start the robot but you can safely ignore it while light conditions keep stable. Line Following with On/Off Controller. Ok, we have the robot, we have the calibration data. Bad! Let’s think first what we are going to do. Let’s start with the simplest possible way ( and perhaps the worse ) of doing line following. We place the robot on the line, we get a reading if it below the middle ( black- white ) we move to one side and if it is above we move to the other side. Let’s see again the pseudo- code. LINE FOLLOWING. white = 0, black = 0. Read Light Sensor. B set power 5. 0. C set power 2. 5. B set power 2. 5. C set power 5. 0. The idea is pretty simple just make one wheel turns faster than the other. Here it is how it works. Here is the EV3 program( You can download all the source codes on the bottom of the page )Does it works? If the corner is step enough the robot will miss it and as you can see it is missing the straight line and it starts oscillating around it. Why? Because we have only two states, so the robot is either turning left or turning right. So we have left, straight and right.. I have a deep understanding of them but I really can't stand the complex way of explaining simple things.. The bigger Kp is the higher will be the turning when there is an error, but if Kp is too big you may find that the robot overreacts and that it is uncontrollable. You can watch what happens when you change the value of Kp from 0 to 1. So start with 1. So our P- Controller would be like this pseudo code. LINE FOLLOWING. white = 0, black = 0. Read Light Sensor. Turn B+C Motors by correction. If you are not using the EV3 module just move one motor a value + correction and the other motor to value - correction. It is pretty much the same. Here it is the EV3 program( You can download all the source codes on the bottom of the page )Tunning the Kp parameter. Start with Kp=1 and run your robot. If the robot can't follow the turns on the line then increase Kp, if on the other hand your robot turns violently on the line, decrease the Kp value. The bigger the effect the bigger the change need on Kp. Just keep playing.. The P Controller has two problem that can be . Let me explain the overall idea and we move to the pseudo code. Proportional part of the PID Controller. This is exactly the part that have just seen above. Integral part of the PID Controller. The idea behind the integral part is that the error must tend to zero over a period of time. I will avoid you a very ugly mathematical expression explaining this.. Because we want to correct the error, so the only way of the robot being exactly over the midpoint between black and white is that this part of the PID controller is zero too. So our controller would be likecontroller = kp * error + ki * integral. As you can imagine the only way for the integral part to be zero is to cancel the error by applying another negative error. So the robot will move from one side to the other of the midpoint in order to cancel the error and be exactly on the midpoint. Derivative part of the PID Controller. The final part of the PID Controller is the derivative part. If we can think of the integral part as a memory of the controller, the derivative part is like an economist that watch the future and try to guess it based on past errors. So if we know the last error and we know the actual error we can compute what the next error will be. So the derivative part of the PID controller will help the controller to correct the error faster. One problem of the derivative part is that it is highly sensible to the noise in the controller signal ( our light detector ) so if the signal is noisy it may not estimate the correct error. And yes, light sensor is quite noisy : )So the final PID Controller is like.. Keep reading.. Mindstorms EV3 PID Controller Program. So, here it is the EV3 code for the PID controller. And here is the EV3 file.. Basically, you are free to copy, modify, redistribute it given that you respect these freedoms on the changes you made yourself. Now a big problem appears.. So after reading Wikipedia and reading about various approaches, I think the one that best suits our tasks it the heuristics approach. So copying from Wikipedia: . A fast PID loop tuning usually overshoots slightly to reach the setpoint more quickly; however, some systems cannot accept overshoot, in which case an over- damped closed- loop system is required, which will require a K? No problem, I am here to help you. Just set Kd and Ki to 0. Set Kp to 1 and run your robot. Does it follow the line smoothly? No, try decreasing it by a half? Can it still follow the line? No, increase it a bit.. Yes, increase a bit more.. Too unstable, decrease it.. Keep repeating until you are happy. How different Line following approaches compare. One video is more than enough to see it : )Miguel says.. As a final word, you now need to know that each kind of line require a Kp, Ki and Kd params tunning to get the best of the robot. That's all folks. Hope you enjoyed the tutorial as much as I did. Don't miss my post about a self- balancing robot that I built using a gyro sensor I have just got from LEGO. Did you know that I run a Youtube channel with almost daily LEGO reviews and HOWTO? Please subscribe to The Technic Gear LEGO Reviews. Downloads. Download Mindstorms EV3 PID Controller. EV3 Source Code for PID Controller. This source code is available free for you under the GNU General Public License 3 which you can read here. Basically, you are free to copy, modify, redistribute it given that you respect these freedoms on the changes you made yourself.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2016
Categories |