tag:blogger.com,1999:blog-17122426843969911312018-03-05T12:09:26.872-08:00QuickSATInteresting StuffDaniel Pyonnoreply@blogger.comBlogger3125tag:blogger.com,1999:blog-1712242684396991131.post-70540677738711778402018-02-27T21:10:00.001-08:002018-02-27T21:18:18.249-08:00Reversing Text AlgorithmThis algorithm simply reverses an input integer by using modulo ten. I came across this while reading a computer science book. <script src="https://gist.github.com/danielpyon/fcec26a8a5a8b120de5eb576a42b643e.js"></script> Alternatively, <script src="https://gist.github.com/danielpyon/3d2700022b314749b9342f280c4eb125.js"></script>Daniel Pyonhttps://plus.google.com/102396731596231344451noreply@blogger.comtag:blogger.com,1999:blog-1712242684396991131.post-32411834055624615822018-01-27T19:44:00.001-08:002018-02-27T21:09:59.831-08:00Introduction to PID Algorithms<h2>Introduction:</h2>Often times in robotics, you want to have your robot reach a desired sensor point, whether it is driving to a certain distance, or keeping an arm at a certain height. Let's assume that you want your robot to autonomously drive N meters. To accomplish this, one may run motors for a specific time in order to reach that desired place. However, there is too much variation on things like how much battery the robot has at the time, or the state of the motors, or the surface of contact with the drive, momentum and inertia, and more. Usually, if you have tried this method, you might have noticed that the robot goes over the desired distance (called "overshooting"). So, now let's assume you have attached an encoder to the chassis to measure the amount of degrees that have occurred since the beginning of movement. In the code, the robot moves forward until the encoder reads the goal sensor value. This method is better than not using any sensor, yet still causes overshooting because of the aforementioned physical limitations. The solution? A feedback loop that constantly changes the power of the drive motors.<br /><br /><h2>P, I and D</h2><div>There are 3 components to a PID controller. P, I and D respectively, stand for Proportional, Integral, and Derivative. In basic calculus, integral is just the area under a curve of some function, and derivative is the slope at a specific point on the graph of a function. The function that we work with in PID is called the <i>error value,</i> that, every iteration loop in the algorithm, updates itself to the new error value. The error value is a function that takes time as an input, and returns the setpoint minus the current sensor value, given by:<br /> <img src="https://latex.codecogs.com/gif.latex?e%28t%29%20%3D%20s-c" />.<br />With that established, let's dive into the proportional term (P). Proportional term is just taking the error value, and multiplying it by some constant (to weight it properly with the other three terms; this is how PID works fundamentally), so it would look something like this:<br /> <img src="https://latex.codecogs.com/gif.latex?kP%5Ccdot%20e%28t%29" />,<br />where kP is a nonzero floating point number.<br />Intuitively, as the P term increases, the further the robot has to move to get to the point, and as the P term decreases, the closer it is to the setpoint. You could technically just use a P controller without any I or D, but it would oscillate from overshooting to undershooting the goal sensor value, because we did not add any fine tuning with integrals or derivatives.<br />Now, lets get into the integral term (I). Integral is the area of the graph of the error function of t versus e(t). Mathematically,<br /><img src="https://latex.codecogs.com/gif.latex?kI%5Ccdot%20%5Cint_%7B0%7D%5E%7Bt%7Ddt%5Ccdot%20e%28t%29" /><br />where kI is another non zero constant. Intuitively, the integral term increases as the past errors made sum up to a significant amount.<br />Finally, lets see the derivative term. Again, it is multiplied by some kD constant, but this time it calculates the current slope of the error graph. The expression is as such:<br /><img src="https://latex.codecogs.com/gif.latex?kD%5Ccdot%20%5Cfrac%7B%5Cmathrm%7Bd%5C%2C%20e%28t%29%7D%20%7D%7B%5Cmathrm%7Bd%7D%20t%7D" />.<br />Intuitively, the derivative term calculates the <i>future</i> possible error values! The slope can predict what the motor needed maybe three seconds into the future.<br /><br /><h2>Putting It All Together:</h2></div>Now that we have each individual term, we can establish a single function that sums up our very simple algorithm:<br /><img src="https://latex.codecogs.com/gif.latex?u%28t%29%20%3D%20kP%5Ccdot%20e%28t%29&plus;kI%5Ccdot%20%5Cint_%7B0%7D%5E%7Bt%7Ddt%5C%2C%20e%28t%29%20&plus;%20kD%5Cfrac%7B%5Cmathrm%7Bd%5C%2C%20e%28t%29%7D%20%7D%7B%5Cmathrm%7Bd%7D%20t%7D" />.<br />So our actual code will have to calculate the error value, and integral and derivative terms, and constantly set that value to the motor.<br />Finally, one of the hardest things of PID is tuning the constants so that your robot properly reaches it's setpoint. This may take hours, but there are some well known techniques to help you tune (like the Ziegler-Nichols Method).Daniel Pyonhttps://plus.google.com/102396731596231344451noreply@blogger.comtag:blogger.com,1999:blog-1712242684396991131.post-15128275338234308102017-10-01T22:26:00.001-07:002018-02-27T21:19:01.902-08:00ArrayList vs LinkedListThis is a simple performance test of arraylists vs linkedlists in the JCF. Linked lists' elements always have the previous element's position in memory's reference, and the next as well. Insertion yields O(1) time, while accessing yields O(n) time. <script src="https://gist.github.com/danielpyon/d146900caacf9c5361e3ca0606d12011.js"></script><span style="color: #cccccc;"></span>Daniel Pyonhttps://plus.google.com/102396731596231344451noreply@blogger.com