-
Notifications
You must be signed in to change notification settings - Fork 0
PID Controller and Error
One concept you will hear me talk about a lot in this tutorial is the idea of error. Error is going to refer to how far left or right the car is relative to the middle of the track. In the simulation in this repo, the cars have 5 trackers on the front of the car that will be looking for where the car is on the track. We will talk about how they do this later in this tutorial. For now, keep in mind that if the car is not in the middle of the track or is swaying to one side of the track, the car will have a higher error. If the car is in the middle of the track, the car will have no error. Our goal is to keep the error as close to 0 as we can. Below is a representation of what our car will see in regards to the error. Our car is represented by 5 zeros in a line where the 0 one the left is on the very left front of the car and the right 0 is on the very right front of the car. A 1 in this case will represent the road under that portion of the car. There are some cases where the line below the car is in-between two sensors and so both of them are registering a line being under them; this is represented by two 1's next to each other on our line of sensors below. We will talk more in detail on how the car collects this data later on, but for now, lets focus on the overall principle. Keep this visual as a reference for when we talk about our PID controller in detail.
0 0 0 0 1 ==> Error = 4
0 0 0 1 1 ==> Error = 3
0 0 0 1 0 ==> Error = 2
0 0 1 1 0 ==> Error = 1
0 0 1 0 0 ==> Error = 0
0 1 1 0 0 ==> Error = -1
0 1 0 0 0 ==> Error = -2
1 1 0 0 0 ==> Error = -3
1 0 0 0 0 ==> Error = -4
Now we will see some math below but don't fret! It is fairly intuitive once you understand the concepts. Our goal is provide the car with turning and driving that results in smooth turns and avoids oscillation as much as possible.
Below is the formula for our PID controller. We will break it down into its 3 parts, proportional, derivative, and integral parts. Each one of these parts has a constant which will decide the magnitude of change each portion of the PID controller can have on the end decisions. We will talk later how to decide these constants but for just know they will be decided by the user.
The P or proportion portion of the model is going to be our main decision maker here. The logic behind the P controller is simply that if the car is to the left of the track, it should turn right. If the car is to the right of the track, it should turn left. By how much the car turns is the most important part for too much reaction and the car may sling itself off the other side of the track, too little and the car may not be able to turn enough on turns.
Now, if you haven't taken much calculus, the word "Integral" may scare you a bit. However, what we really would like is the sum of all of the errors that our car has experienced and add them up. The point of this is to help sway the car back to the center if the car is experiencing a lean or drift on the track it is running on. If the track is a giant circle, the car will likely always have an error on one side of the car and the I portion will be able to find out this and provide a correction by adding a little more to the turning. In the end, this portion says if you are always turning left, you may want to turn a little more left to keep the error at 0 as much as possible.
The derivative is our reasonable decision maker among the rest of the parts of our PID controller. Its job is to make sure no decision is too rash. If the car is very far to the left of the track, the P controller may want to swing hard to the right, but all that will do is swing the car quite a bit to the right. That's where this portion comes in. It takes the last error, and gets the delta, or difference, between the two and larger the difference is, the more it will lessen the amount the car will be turning.
Data collection is vital for a self-driving car to drive. Without it, the AI would have no idea what is a good or bad decision. We will be collecting data on our RC car using 5 IR sensors on the very front of the car. These IR sensors are very good at one thing and one thing only, bouncing light off something and telling the computer if the light came back or not. The ones we will be using later in the hardware assembly will send a 0 if it does not receive the light it transmitted and a 1 if it did receive it back. Now how do we see the line that is running down the middle of the track using this? For this, we use colors. Colors reflect light in many different ways and as you may know, the color black absorbs mostly all of the visible light spectrum instead of reflecting it. Now, the IR sensors do not use visible light for they use Infrared Radiation or IR as their light source. Lucky for us, most black surfaces absorb this light as well. That means that we will be able to determine the a black road from a lighter color line, or a light colored road with a black line. Yes, that means that five 1 or 0's will be the only data our model receives and it is all it has to work with. However, those five data points go a long way.
Now that we are off driving, lets talk PID constants. On the simulation and the interface, there are sliders that will allow you to adjust your PID constants from a range of 1-100.
To begin tuning, turn all of the sliders down to zero except for our P value; turn that slider to 50. We are doing this so we can start dead in the middle and work our way up or down from there. Because the PID controller is a feedback loop, we make a change, see a result, and then make another change and so on. What you are looking for here though, is the P value that you found results in the car staying on the track the longest, and one that does not cause the car to majorly oscillate from side to side. Start with making sure the car stays on the track the entire lap then try to lessen it to reduce oscillation. Using only a P value without the I and D will cause the car to oscillate no matter what so don't worry if it is oscillating.
Unintuitively, we are going to tune our D value before the I. This value is a little more difficult to tune for it is a balancing act of not turning enough and needing to be able to make a decision rash enough to make sure the car stays on the track. Start this value at 0 and slowly work its way up until you see the car reducing the very sharp turning even when the car isn't being flung off the road. This value will be significantly smaller than the P value for if your D value is too large, then it may cancel out the P's decision all together causing the car to not turn at all.
Adjusting this one will result you in the least significant visual result, however, is still very important for a perfectly tuned PID controller. In this case though, the car can drive itself quite well with this value staying at 0. To tune it, start with a very low value and slowly creep its way up until you see the car make correctional decisions. Again, the I portion wants to make sure that if the car is always a little far to the right, that it pushes it a little more to the left to compensate.
Speed is arguably the most important value to adjust during the tuning process. The interface allows you to adjust the minimum and maximum speeds. They have a range from 0-200 and if your maximum speed is below your minimum speed, it will automatically make the speed the minimum speed at all times. The car provides more power to the rear motors if the car is turning due to the car having to take on incoming friction with the wheels and their orientation. So the higher the error, the more power the motors will be exerting, resulting in more speed around curves.
For this, you first want to look at the surface in which the car will be traveling on. If it is a hard surface like a tile or concrete floor, the minimum speed can be much lower than if the surface was foam or carpet. This is because on the harder surfaces, the car has an easier time overcoming inertia at the lower power levels. However, on a foam surface, you need more power all of the time due to the surface constantly providing friction. I recommend starting the max speed at 100 and the minimum speed at 50 and working both closer and closer until you reach an well working speed range. You may need to move the max or min speed up independently in order to find this optimal range.