PID (Proportional-Integral-Derivative) control is a feedback loop mechanism that uses three components—proportional (error × Kp), integral (accumulated error over time), and derivative (rate of error change)—to calculate motor outputs for achieving precise distance and angular movements in micromouse robots. In micromouse applications, only proportional and derivative terms are typically used because the maze walls are stationary targets; the proportional term provides the driving force toward the goal while the derivative term acts as damping to prevent overshooting and oscillations. The system achieves optimal performance when critically damped, reaching the target in the shortest time without oscillation. For distance control, the error is the difference between desired and current encoder counts, while for rotation, the error is derived from the difference in encoder counts between left and right wheels.
Micromouse PID Control: Lecture 3 Tutorial for Maze Navigation
Added:all right everyone so today we'll be going over P control what that is and how we use it in our rout designs so yep so what is p what are the components of this controller and how do we use it on the R so just really quick uh so PID is a controller but what does that mean so it can take in an input such as environmental input about the robot's position or its angular position and then turn that into a desired output to achieve some sort of goal that you wanted to achieve and then uh there are multiple components into calculating how to perform certain actions to achieve this goal and then we use it in order to in micromouse we use it in order to go a certain distance or achieve a certain angular turn and we'll go over on how we implemented on the rra so what is p to begin with so we have here in this diagram uh the current location of the uh brat and then this is our Target location so it's currently facing this angle so some there's some sort of angular error because we want to face directly to the right and then there's also some sort of distance error because it should be over here but it's currently over there so that's so based on these inputs of like okay we are at this position and we at this we're facing this angle we can then calculate okay we have to take some sort of we have to take some certain action to then get to this Target location and so what are our inputs and what are our outputs so our inputs are listed right over there so there are the IR sensors and encoders so we haven't gone over IR sensors yet you know that we'll go over that in the next lecture so don't worry U but essentially we just use IR encoder sorry IR sensors to detect the walls in the Maze and of course you guys know what encoders are because encoders are just used to measure the or the the degrees spun by the wheel of the motors and we can take in those inputs to calculate our outputs which are our left and right motor speeds and our Target is to go through the maze without running into walls but we have even smaller sub goals which we will Define in this presentation are there any questions so far about like the overall the structure of what we're going to go over all right so PID uh is essentially just a feedback loop that is constantly running until you achieve your goal um and achieving your goal means there's zero error between your current state and the state that you want to be at so say like I'm at position xal 0 m and I want to be at position x 15 m a zero error would mean I'm at that 15 M Mark uh but we'll go over that again in more detail so it is a continuous Loop of reading what the sensors uh are telling you so like what is your current state passing that into your control algorithm which then gives you an output which in our case is the motor speeds for the left and right Motors so how does p ID work so there are three separate components that compose of P ID proportional integral derivative these are terms uh these terms are defined right here um and we sum these all up to in order to get our output so what are these terms is quickly going over them so they are literally just what they are named over here so proportional the proportional term is where we have our error multiplied by just some constant so it's just like KX um and it's not like X2 or X Cub no it's just a linear proportional term and um so our error let me just go over really quick we'll go we'll Define this again properly later on but our error term is just our goal our goal State minus our current state so the goal is let's say is 15 meters away from me and I'm currently zero so this is 15 minus 0 so the error would be 15 does that make sense okay so the proportional term just multiplies the error by a constant the integral term Aggregates that error over time so we're we're constantly adding this error term to a to a variable and accumulating this error and keeping track of that and then we have this derivative term where we're multiplying or sorry where we're keeping track of the rate of change of the error so how fast is the error decreasing or how fast is the error increasing and then we add so we pass in our error into these three uh control subcontrollers we add all of their outputs together and we get our output control signal which we can then pass into the motors which is our motor speed any questions about this diagram nope okay so uh our so again so here's our error term uh which is how far we are from our targets and these these K constants are just so you don't want to just take the integral the proportional uh and the Der derivative of the error term you want to be able to weight these terms relative to each other to give them relative importance U because some terms should be larger or more uh sign significant than the other terms when Computing this sum um so therefore we have these uh constants that allow you to weight these terms accordingly to either amplify their value or um attenuate their value or make them smaller all right and then again all these terms combined sum together then create our control output signal uh any questions really quick about what just C for to the of like how it works yeah is it just a simple s yeah so you take all the um constants multiply by the derivative of the error the integral and the error itself and then you just add it together and the output will be like what pwm you want to set your M to any other questions okay so let's just see this uh these terms in action so here so the the square or the dot that's moving is our robot or our rat in this example and so we are only going to focus on the proportional and the derivative term so again the proportional is where we take just some constant and multiply that with our error the derivative term is just where we keep track of our rate of change of the error and we're just ignoring our interal term completely we so imagine we just set so you can just imagine we set our Ki to zero so that way we ignore it okay so with no damping the system overshoots and oscillates as you can see right here so what is uh what does no damping mean so our derivative term is the damping term and that's basically what allows the robot to slow down as it approaches its goal so you can think of the proportional term the proportional term will only reach zero when you actually get to the Target right because when you're not yet at the Target the proportional term will always be non zero because there will always be some error so therefore it will only reach zero when you hit the target but then because of momentum it's then going to overshoot and then then cause more error is going to come back then set the speed to zero but then because of momentum again it's going to overshoot I just's keep on doing this oscillating motion so now if we have too much damping it's going to take way too long to reach our Target so as you can see right here um it's going but then at the very end it kind of takes a little too long to reach the goal and the system won't do anything it is no driving force so the driving force is our proportional term so the the proportional term again is a constant divided by our error term and that's what actually makes the system move in the direction of decreasing error whereas the damping term or the derivative term prevents it from overshooting and causing more error does that make sense everyone um and just a Goodwill thumb is to know that the proportional term again just is just what gets the robot to move in the direction of decreasing error while the derivative term kind of works against it to prevent it from overshooting and we'll go over like mathematically how these terms actually like uh like signage wise like positive negative how these terms act against each other to make sure that you actually achieve this ideal scenario of critically damp which I'll cover right now so this so where we have um this red square so it's it's approaching and it reaches the goal the fastest without any overshoot and this is ideally what we want zero error in the shortest amount of time possible and this for this we call it critically damped and um so again so this is no damping this is uh underdamped so underdamped is where the damping term isn't large enough so it slightly overshoots but then it comes right back to the goal just good enough but it takes a little bit for it to to to align to zero critically damped is just perfectly damped where it would come to the fast amount of time but then over damped is where it's just way too this too much damping so it takes too long to get to the goal all right does that make sense to everyone anything like you that um not really does anyone have any other questions okay so what about the integral term so we don't use the integral term uh in our scenario because we so integral term is typically used for when the error is or the rather the goal is changing over time so for example um so in in our scenario in micral the goal will always be a set distance away like if we want to go forward one cell that cell isn't going to move away from us that cell will always be stationary and in front of us so therefore that that goal is not changing however say like I'm trying to keep behind a car like say a car is moving and I want to stay behind that car 10 me away um then in that scenario the car the the goal is constantly changing because the car is constantly changing does that make sense so in that kind of scenario we want to use the integral term so that way we can keep up 10 me behind a moving Target or moving goal or in our case or in the case I just described a moving car and so yeah so the error term sorry the so it just keeps track of error change over time and because in micromouse we don't have a moving Target such as like a moving cell or a moving maze you don't need to have this integral term and as you can see right here this is what I this describes what I I just uh this shows what I just described um so if we only have the proportional term it's always just going to lag behind the target uh and if we have the proportional and the derivative term it's going to reach a certain distance behind the target but then not actually achieve it but then once we actually implement the integral sorry the integral term is going to reach the Target and stay with the target um throughout uh you don't have to worry about this too much just the takeaway from these past two slides is only use the integral term if your goal term or your if your goal is moving away from you or constantly changing but in micromouse it isn't because our cells and our is not moving away from you okay any questions about these slides uh how this works um I can go I can go over anything you guys want need to go over again all righty cool so now we talk about using for rotation so not only we want to move forward and backward to achieve not only we want to move forward and backward we also have to turn of course you know we have to navigate through the maze that requires turning so whereas our error term in whereas our error term previously was a distance our error term is now an angle so how can you measure angle in micromouse well we can actually use our encoders um which measure the how far each wheel has spun and if there's a difference between those encoder counts then there's an angular offset because if one wheel has travel forur the the other that means the microm mouse is not pointing straight and you can calculate that angle or error and um as you can see right here uh this to this is where actually let just go for this sorry uh okay um so this is where the the system is over damed so it's taking too long for it to achieve its goal this is where there there's only a proportional term so it's just oscillating around its Target this is where it's uh I believe this is not where oh sorry this is also where it's kind of underdamped so it's kind of still oscillating around the goal but it eventually achieves it and this is where it's critically damp so it achieves the goal without any oscillations so just to go over all the different types of damp uh terms again so there is underdamped or there there's no damping at all which is where it just will just keep keep on Ting around the goal then there's under damed uh where it will oscillate a few times before finally reaching the goal with zero error and this critically damped where it takes no oscillations at all and we'll achieve the goal on the first try does that make sense to everyone all right cool it is a difficult topic so please ask any questions if there are any because it will because coding this is certainly very hard and if you don't understand the conceptual side of it then coding is be a whole whole another level I'm not you guys but just just me like making sure all righty okay now part we'll go over okay so now let's talk about how we actually um use PID specifically in the mouse so we have we just like arunan just talked a bunch about what P ID is and like how it works but how do we actually use it in micr Mouse so we use it in two main ways one is when moving forward and the second is when turning I know amazing right so uh essentially for going forward the way that we use PID is that we use it as a distance controller so whenever we want to go forward one cell we tell or we set the goal distance to be the difference between the encoder counts that it takes for to move forward one cell so that the M the mouse will then try to reach that encoder count goal and like go forward one cell and of course um the reason that we need to do this is because like there's like slight variations in not only like the maze but also the mouse itself so every single time you run um the mouse it's going to have like a slightly different voltage level and like the motors will perform slightly differently so we need P to do this because without it it can like have varying results whereas with P it'll always try to get the correct number of encoder counts which is like constant throughout like all the mice and the second way that we use p in micromouse is for turning so whenever we need to turn we only need it to turn exactly 90° no more no less so what we do is like we set the encoder um count goals in the mouse and again it'll try to achieve uh the goals for each wheel and like essentially do the turn uh yeah so with P you get really accurate turns and you can also go like really straight without going into a wall uh does anyone have any questions about how we actually use it awesome so now let's talk about actually implementing it in the mouse so uh since we're this is like the overall equation that we're going to be using um in code it's not going to look like this but it'll it'll have like this same idea as you'll notice this is the proportional term that's the derivative term and we don't really use the integral term because all the walls are like they're at the same place they're not moving so e to the E of T is the error function which is just like something that you set every time you want the mouse to move or turn and uh for the distance controller this would essentially just be the or sorry EFT is the error function which means like as the mouse is going through the Maze and trying to achieve its goal it's the difference between the goal encoder counts and the current encoder counts so when you're going straight for the distance controller it would be the income counts for each motor to the desired distance minus the current amount of encoder counts so for example let's say I want my mouse to move forward 100 encoder counts you would just set the go to be 100 and at at the beginning the error is going to be 100 because it's the the desired distance which is 100 minus the current distance which is zero and as it gets closer and closer to the goal the error term will start to decrease and the derivative term can be calculated uh by using like the change in error values so like there's really no way for us to calculate derivative real time using the things that you guys learn in classes like math 32A and 32b because we don't have like the entire graph right we're trying to find the derivative at the current point in time without knowing the rest of the graph so essentially what we do is we just use the difference in the error values so the way that you achieve this is like you just store error values in the mouse so like you can store the previous one the past 10 and average them it's really up to you you can see it in the assignments but you um you get the derivative term by uh finding the difference between the current error value and like the prior error values and one important thing uh cha just note is is uh unlike as Parts mentioned unlike in math classes where you take you have a function or a derivative over a continuous line or a continuous graph you in these scenarios you're only checking the state of the encoders or the state of the error uh at discrete points of time like for example maybe like let's just say like one microsc apart right so you only have discrete points you don't have a continuous graph which is why you have to do the derivative in terms of current error minus the previous error instead of just taking in instead of having some sort of like function that you calculate for the derivative and then to continue on um you might notice that we're only finding the difference in error not the difference in time and like although you could find the difference in time you don't really need to because um every time you calculate the the error term or like the derivative of the error term it's always going to be in like set time intervals like the difference in error is always going to be like every One mic or like every 10 micros right so it doesn't really change so you could just straight up find the difference between the current and previous error values and the change in time can be accounted for in uh the derivative constant KD and this works because the micro the microcontroller operates on a constant clock speed which just means that like the time between every sample is always the same and uh just like a clarification KP and KD the two constants are going to be different for everyone's Mouse you're going to have to determine these Yourself by just like testing the mouse over and over again to see what works best does anyone have any questions about this slide okay uh now let's do a little thought experiment where we actually go through all the different stages that the mouse goes through um calculating uh what it needs to do with its Motors so let's say say that you start on that side where runin is and the goal is this line um yeah so at the beginning does anyone have any idea what sign the error will have when the mouse is over there and the goal is over here we have the a signage denoted down here you can see it okay I actually didn't see that but anyways so when it's over there and it's it still needs to reach its goal it will have positive error which just means that um like the KP term over here will contribute to the actual outsid so over there where runin is at the beginning the error term or the derivative of the error term is zero because it actually hasn't moved yet so like the difference in error that it has right now is always just going to be zero but the KP term is affected so this is going to contribute a positive uh pwm to the motors causing it to move forward so so as the mouse starts moving closer and closer to the goal the air starts decreasing which means that this term uh the proportional term starts having less of an effect on the mouse however at the same time the error is decreasing which means that the derivative term has a larger effect on the mouse this is because the error value over here is smaller than the error value at the beginning which means that current error minus final error or current error minus pre previous error is a negative number and because it's a negative number it starts to slow down the mouse as you can see it's acting against the proportional term because the proportional term essentially just wants to move the mouse towards the goal no matter where it is where the derivative term wants it to slow down so that it will reach the goal and stop instead of overshooting and oscillating back and forth over and over again any questions about this part how it works I think also just uh maybe this on a side note also just think of this as more of like instead of like this side being positive that side being being negative error just also think this think more like an axis so like these are like positive values those are negative values and um yeah think more like a number lineis that might also help okay moving on so from as the mouse gets closer and closer to the goal let's assume that we don't have like a perfectly damed system right so what happens is that it'll probably overshoot a little bit and when it overshoots it's past the goal which means that the encoder counts that it has currently are more than the go encoder counts which means that an error the error is a negative number which is why you can see it over over here so when the error is negative you want the mouse to move backwards right and the proportional term does just that the proportional term will be negative which means that the mouse will move backwards towards the goal as you can see no matter what you do the proportional term will always move it towards the goal and similarly as it starts going back to the goal the derivative term will now be positive because uh like the signs for both errors are flipped so it'll be like a small positive number which means that it'll start slowing it down in this direction against the proportional are any questions about this slide it guesss a little confusing once you get past the goal so are there any questions yep well I don't know if it's about this slide but like so you said that we're not using integral terms in micromass because the target's not changing so since the target's not changing here what would the integral ter you so that would just so imagine that the target was moving over time so that so in that case the error will keep on accumulating so what that integral term does is once it accumulates to a large value it will force the the rat or the micromouse in our example to go towards the goal so basically as it agates over time it collects a bigger value right and therefore it will force it it will force the microm regardless of the proportional derivative term to achieve that final goal if that does that make sense yeah but like now that the goal isn't moving so what does the integral term do when the goal is not mov oh it does nothing we don't we don't use the integral term yeah so we just effectively set the Ki constant so the the constant that weights the integral term to zero so the integral term does absolutely nothing okay I'm second were you asking like what it would do if we had it yeah oh so if we did have the integral term it would it would have like a very small effect on the PW mode the mouse but like nothing significant because there error is decreasing and it does reach zero very quickly so like like Aran said it doesn't like have time to accumulate so it'll have like almost no effect on the pwm itself any other questions you your hand ra yeah yeah um you so we're going to like convert the distance into en yes oh I just quickly one more time just I guess I'll go SL over time just in case um because it's a little a little like difficult sometimes so so if you if you go past your goal that means that the error will be negative because you have say your goal is 100 in coder counts but then you went to 150 by accident so 100- 150 is negative thus a negative error term and then the derivative constant so since you're still moving in that direction you're accumulating a greater uh negative error so a greater negative error minus a lesser negative error um is still negative which will then want to slow down the microm mouse as it goes in the negative Direction okay okay and then uh like we explained it'll start going backwards towards the goal and eventually it will reach the goal it might oscillate back and forth a little bit but eventually it will like stop itself e the goal so now let's talk about the PD well P ID but like without the I so PD controllers on the mouse itself so for the distance control controller um it's used to either move forward or it's only used to move forward and okay the goal in with the distance controller so like when you're going forward in the cells would just be the number of encoder counts to the destination and you would set this goal for each motor so just like a quick clarification you're going to be running p on or PD control on both the motors at the same time because like sometimes you're going to need to or for all the times you're going to need to make sure that like either the motors are moving together or are moving separately in opposite directions and the error term is going to be the gold number of encoder counts minus the current number of encoder counts and finally the output is just going to be like a pwm to both of the motors and then for the angle angle controller so this is when you want to turn the use is just going to be to turn and also to drive straight so so when you're driving straight and you're using the distance controller the angle controller doesn't really play or doesn't really do anything except for it make sure that the mouse is actually going um like it's not moving off of the straight line path because as the motor spin if one encoder starts having a much larger value than the other then the PD controller will like try to make sure or then the angle controller will try to make sure that the difference between the encoder counts is always zero so the goal again is just a difference in the encoder accounts so for example if you want to turn it could be like 100 on one motor but like negative 100 on the other motor so that it turns properly the error is going to be just the the go difference minus the current difference in encoder accounts and finally the output is just going to be adding pwm to one motor and subtracting it from from the other any questions about this nice um oh and like I said when you're driving straight the angle goal is always going to be zero so let's go over some P tips and strategies so when you're um when you're actually like setting your PID values what you want to do is that you have you don't want to just only have the P output as the motors pwm you want to start with like some small amount so that the mouse can actually move and then add or subtract the output from the P to the motor's current PW so that like it can actually still move because a lot sometimes what will happen is that when you're really close to the go the output from p is so small that it won't actually move the mouse so you just need to make sure that you have like some constant speed that the motors are running at and then add or subtract from that when you're uh implementing p in code um again when you're moving straight set the angle go to zero so that it doesn't Veer off of its current path and okay so this is the next bullet is for when you're actually trying to determine what KP and KD are so start with both KP and KD at zero and slowly increase the proportional constant like little by little until you can like see that the mouse is going straight but like it it could be oscillating a little bit at its goal so what this means is like let's say when you're going straight and like your goal is like Arin I would get to Arin and like oscillate back and forth very little and that's like what you want to do so you just set KD to zero and keep increasing KP little by little until you oscillate and then after you're done with that then you start increasing KD uh little by little as well until the the oscillations go in and the important thing to know about KD is even though it it it mathematically how KD Is defined and calculated by your P by your PD controller it will work against your proportional term but you but you don't have to make your KD term negative your K all your terms should be positive and the the signage will be accounted for when calculating each subterm within your PD controller all right um another thing to uh keep in mind is that the motors will behave different friend based off of the battery voltage and like this isn't the problem for the most part except like if your battery is really really low then it might not move at all and basically what we're trying to say is just like keep make sure that your batteries are uh charged when you're um using your mouse and also you should keep track of what content you've actually tried like in a notebook or like on Google Docs or sheets so that you know that you don't really need to try them again as long as um everything else is the same uh so yeah just as a recap oh whoops uh kid is a control system that controls the output or that gives you an output based off of a goal that you give it uh KP and KD are what you guys need to tune on your micromouse so that uh your system is as close to critically damed as possible what this means is that it'll reach its goal fast but it won't oscillate when it reaches its goal and also um you can use multiple controllers simultaneously uh for example you're going to be using the angle control and the distance controller at the same time for when you're doing angle or for when you're going straight and for when you're turning and also you're going to be using one for the right motor and one for the left motor and the reason why you want to have um the reason why you want to have them both work together is because when you're going straight you don't want to have an angle offset and when you're turning you don't want to have a distance offset uh so yeah does anyone have any questions yeah will you guys be explaining later how to like take the IR sensor input into account for p control yeah so uh the next lecture is on Monday and it's about how to use our sensors and like we can talk about how you can use it in p as well but the idea is essentially the same you just use IR sensors read IR sensor readings instead of encoder accounts when you're doing p but do assignment 3A you don't need to use IR sensors at all they're they're just completely based on the encode accounts any other questions great so couple of actually you want to take yeah sure so we'll be dropping uh two new assignments tonight so 3A and 3B so 3A will'll go over implementing the PD controller in your code so that way you can achieve your desired output so that way you can uh have your rat uh achieve consistent outputs rather than arbitrarily moving distances um and not achieving them consistently or accurately and then 3B will be an individual assignment as always where you will uh start actually learning how to do how to do PCB design using the schematics you have uh created so far in 1B and 2B and we have another lecture on uh Monday uh sorry for having it like in quick such quick session we were just kind of forced to uh because we want to make sure we have the rack competition done uh by the end of this quarter um so but don't worry we'll give you guys plenty of time plenty of time to get these assignments done because after next lecture there are no more lectures for the rest of the quarter so you we will just be dedicating all our resources to getting your ass getting your assignments done hosting work sessions and give you guys all the help you guys need we're just having the the lecture again early just so we can like put put everything out there so that we guess can just work on it at your own pace before the end of the quarter before the end of R competition or before R comption occurs okay so couple of reminders like arunan said lecture 4 is this Monday in the Tesla room so it's not going to be here it's on the same floor I think it's that way I think so yeah yeah so it's just like next to the elevator over there um assignment if you haven't finished assignment 2 a and 2 or 1 a and 1B do those as soon as possible before getting started and like talk to us if you need any help on them or just come to the lab and also as a quick side note uh it has this thing called the general board where you can be grouped up in small groups with a bunch of officers and like they can just like guide you help you with things like internships or classes but they but then you also do a lot of fun things with the officers like cooking or going on adventures in Westwood or I don't know playing sports or just like video games whatever you want to do um I did General board last year I thought it was a blast I met some of my best friends this year from General W last year so if you guys um haven't heard of it I would highly recommend joining uh the applications are due I think this weekend any questions yeah you know where to apply for the general yeah so I think uh if you check announcements there should be um an announcement from Jackie about the application being Yeah question for 3B is that only the 3v 3 regulator I feel like we're missing a 5 Vol I'm sorry for what uh for assignment 3B is that only the 3v regulator yeah yes or what did you mean by the 5 Vol regulator cuz I just assumed that we're running our Motors off of 5 volts we need that the motors are powered directly by the batteries yes any other questions y when's the next time the lab's going to be open for like working Monday yeah la will be closed tomorrow because the Veterans Day so it'll be open on Monday but if you guys need need need to open the lab over the weekend just be at me and I'll I'll go to the LA and open it yeah all right thanks yeah I I really want you guys to get your assignments done so if if needed just let me know please and I'll let you guys know if uh if I can come and open in any other questions all right um yeah I think that's it from us today and I do plan on going over uh you you guys can leave now but I do plan on doing just going over some like doubts that people had um from like assignment 2A especially uh so if you guys want to stay for that you guys can stay but unless you guys watch you guys recing yeah keep it recording you saw the recording oh
Up Next

Encoder-Based PD Controller Design for Micromouse Robots
@MicroMouse
6.7K views•2022-12-06

MathWorks Virtual RoboSub Simulation Environment | Webinar 2026
@RoboNationInc
209 views•2026-03-10

Micromouse IR Sensor Circuits: Emitter, Receiver, and ADC
@UCLAIEEE
737 views•2025-01-15

Micromouse: The Fastest Maze-Solving Robots on Earth
@veritasium
23.5M views•2023-05-24
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Robotics























![ZIEGLER NICHOLS ✅ SEGUNDO Método [Control PID] Parte 2](https://i.ytimg.com/vi_webp/AAaWNNuqpuY/maxresdefault.webp)





![DRONE con ARDUINO | PROYECTO | ARDUINO desde CERO [Explicado paso a paso] - Control de estabilidad](https://i.ytimg.com/vi/SlGnRwxsJzc/maxresdefault.jpg)








