Go to Post This is one area where you want an old hand in the room to show you how to work on this thing. .... If you survive it, electricity hurts really bad. - ebarker [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 11-01-2016, 19:35
mark219 mark219 is offline
Programmer/Driver
AKA: Mark Szewczuk
FRC #0219 (Team Impact)
Team Role: Programmer
 
Join Date: Jan 2016
Rookie Year: 2015
Location: United States
Posts: 12
mark219 is an unknown quantity at this point
Application of PID in code/real world

I'm a little bit of a newbie at Java coding with FRC. I want to use PID as a part of my drive train and other precision parts of the robot, however I'm not sure of its application. I know that PID stands for proportion, integral, derivative, and have a basic understanding of integrals and derivatives (just started learning about integrals in calculus). Essentially, all I know about PID is that it is a way to minimize error, but the question I am asking is how does it work? Can you show an example of code? Is its only use for autonomous mode?
Reply With Quote
  #2   Spotlight this post!  
Unread 12-01-2016, 00:24
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,088
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Application of PID in code/real world

Quote:
Originally Posted by mark219 View Post
the question I am asking is how does it work?
https://www.youtube.com/watch?v=UOuR...n0gJUQ&index=2


Reply With Quote
  #3   Spotlight this post!  
Unread 12-01-2016, 09:48
Tparbotmail Tparbotmail is offline
Registered User
FRC #3944
 
Join Date: Jan 2015
Location: Az
Posts: 69
Tparbotmail is an unknown quantity at this point
How do you find this video on YouTube?
Reply With Quote
  #4   Spotlight this post!  
Unread 12-01-2016, 23:31
Justin Buist Justin Buist is offline
Registered User
FRC #4003 (TriSonics)
Team Role: Mentor
 
Join Date: Feb 2015
Rookie Year: 2015
Location: Allendale, MI
Posts: 22
Justin Buist is an unknown quantity at this point
Re: Application of PID in code/real world

Quote:
Originally Posted by mark219 View Post
Essentially, all I know about PID is that it is a way to minimize error, but the question I am asking is how does it work? Can you show an example of code? Is its only use for autonomous mode?
There are uses for PID in teleop; it's just not an auton thing. In this game I could see mashing a button to put the robot into a crawler mode where you could put a speed limit on a tank tread when crawling over some of the obstacles. Going over the rumble wall you may want to reduce power to one side when it's entirely in the air so that when it does make contact it doesn't spin the robot to one side, but you would also want to it to come back up to speed quickly once it's under load.

The code is incredibly simple and so is the math. Tuning your PID values is not. Here's the crux of simple PID calculation code:
Code:
double error = target-current;
double errorChange = error-lastError;
double correction = Kp*error + Ki*errorSum + Kd*errorChange;
lastError = error;
return correction;
Figuring out what the values for Kp, Ki, and Kd are is the tricky bit. Let's try an example. Say you're driving a car and you only open your eyes every second or so and make a rash decision about how to jerk the wheel about. This is your Kp value. Assume your steering wheel works at a 1:1 ratio to your tires here. If you open your eyes and find you're driving 5 degrees left of the line you want you'd jerk the wheel 10 degrees right to get you back on track. When you open your eyes again you might be just slightly right of the line, say 3 degrees, so you jerk 6 left, and you kinda keep doing this until you oscillate around the line. That would be how a robot would respond if you give it a Kp value of 2.0. It would double the response to the immediate error. I'm not saying Kp of 2.0 is ever a good idea it's just an example. But first you have to find a Kp value that gives you that subtle and sane oscillation around your target. Once that's done you work on Ki which, as an integral, represents your general inability to keep on track. Once dialed in that dampens the oscillation in that it reduces the effect Kp has on the correction. If you keep correcting solely on Kp but your'e 10% off each time you dampen it down. Finally you define Kd to fix what you can't compensate for with Kp and Ki. Kd is generally going to be 0 unless you want to get really freaking precise about something.

Hope that helps.
Reply With Quote
  #5   Spotlight this post!  
Unread 13-01-2016, 21:52
mark219 mark219 is offline
Programmer/Driver
AKA: Mark Szewczuk
FRC #0219 (Team Impact)
Team Role: Programmer
 
Join Date: Jan 2016
Rookie Year: 2015
Location: United States
Posts: 12
mark219 is an unknown quantity at this point
Re: Application of PID in code/real world

Quote:
Originally Posted by Justin Buist View Post
There are uses for PID in teleop; it's just not an auton thing. In this game I could see mashing a button to put the robot into a crawler mode where you could put a speed limit on a tank tread when crawling over some of the obstacles. Going over the rumble wall you may want to reduce power to one side when it's entirely in the air so that when it does make contact it doesn't spin the robot to one side, but you would also want to it to come back up to speed quickly once it's under load.

The code is incredibly simple and so is the math. Tuning your PID values is not. Here's the crux of simple PID calculation code:
Code:
double error = target-current;
double errorChange = error-lastError;
double correction = Kp*error + Ki*errorSum + Kd*errorChange;
lastError = error;
return correction;
Figuring out what the values for Kp, Ki, and Kd are is the tricky bit. Let's try an example. Say you're driving a car and you only open your eyes every second or so and make a rash decision about how to jerk the wheel about. This is your Kp value. Assume your steering wheel works at a 1:1 ratio to your tires here. If you open your eyes and find you're driving 5 degrees left of the line you want you'd jerk the wheel 10 degrees right to get you back on track. When you open your eyes again you might be just slightly right of the line, say 3 degrees, so you jerk 6 left, and you kinda keep doing this until you oscillate around the line. That would be how a robot would respond if you give it a Kp value of 2.0. It would double the response to the immediate error. I'm not saying Kp of 2.0 is ever a good idea it's just an example. But first you have to find a Kp value that gives you that subtle and sane oscillation around your target. Once that's done you work on Ki which, as an integral, represents your general inability to keep on track. Once dialed in that dampens the oscillation in that it reduces the effect Kp has on the correction. If you keep correcting solely on Kp but your'e 10% off each time you dampen it down. Finally you define Kd to fix what you can't compensate for with Kp and Ki. Kd is generally going to be 0 unless you want to get really freaking precise about something.

Hope that helps.
Thank you,
This actually does help me quite a bit in terms of visualizing an implementation of PID. I am more of a model based learner, so reading text off of wikipedia trying to learn it doesn't work for me. The video that Ether posted above also helped in learning what PID does/is. Towards the end it has a slide about tuning Kp, Ki, and Kd, however it states that the way that these are found are not really 100% precise but will generally work for most implementations of PID. (For reference: )
Just curious, is this how you guys also tune your Kp/Ki/Kd values, or do you just estimate based off of guess and check. Also, wouldn't guess and check error also have to be tweaked differently for different surfaces (i.e. testing a robot drive PID system on school hallway floor v.s. testing robot drive PID system on competition carpeting)?
Reply With Quote
  #6   Spotlight this post!  
Unread 13-01-2016, 22:00
plnyyanks's Avatar
plnyyanks plnyyanks is offline
Data wins arguments.
AKA: Phil Lopreiato
FRC #1124 (The ÜberBots), FRC #2900 (The Mighty Penguins)
Team Role: College Student
 
Join Date: Apr 2010
Rookie Year: 2010
Location: NYC/Washington, DC
Posts: 1,114
plnyyanks has a reputation beyond reputeplnyyanks has a reputation beyond reputeplnyyanks has a reputation beyond reputeplnyyanks has a reputation beyond reputeplnyyanks has a reputation beyond reputeplnyyanks has a reputation beyond reputeplnyyanks has a reputation beyond reputeplnyyanks has a reputation beyond reputeplnyyanks has a reputation beyond reputeplnyyanks has a reputation beyond reputeplnyyanks has a reputation beyond repute
Re: Application of PID in code/real world

I go over some good papers/math explanation of how PID works in this post, take a look and read some of the linked resources. My details are LabVIEW specific, but there's some good java examples on ScreenSteps, and also as a subsystem.

Quote:
Originally Posted by plnyyanks View Post
There are many, many threads on CD and the general internet about PID control. Try searching around.

Here's some good resources from NI:

Quote:
Originally Posted by plnyyanks View Post
Additionally, if you search through NI's website, they have some great resources about PID theory (and their examples are in LV). You can also view the complete help page for the PID VI here.
And here are some more resources:

Quote:
Originally Posted by plnyyanks View Post
The PID VI is entirely mathematical - it doesn't care what the inputs are, all it does it brings the error of the system (Setpoint - Process Variable) down to zero. It will work exactly the same with encoder rates and distances, provided that the SP and PV are scaled the same way (and the constants are tuned properly). I'd be more than happy to explain how some of the math works, if you'd like.

As for your specific system issues, it just looks like the constants aren't tuned properly. Read a little bit about PID Theory (also: wikipedia) and take a look at some tips on designing a good controller (the rest of that page is helpful too). You may also want to start with the Ziegler-Nichols method for tuning (start with all zero, bring P up until it oscillates, add D to dampen and add I to correct for steady state error.

Just another note - if I remember, the LabVIEW implementation of the algorithm doesn't use Kp, Ki, and Kd as straight constants. It actually uses time constants for I and D (so it has Kp, Ti, and Td). You can see details of the NI implementation here. You can verify this in the help popup (ctrl-H) for the VI. Plan your constants accordingly.
And this page looks like it has some cool simulation resources, and some overviews about the theory behind the controller.


As for the best way to tune, Ziegler-Nichols is a decent start, but there will inevitably be lots of guesswork involved unless you take the time to really characterize your system (not usually practice in FRC timeframes). Below is a previous post I made about how I always tuned PID controllers.

Quote:
Originally Posted by plnyyanks View Post
Really, the best way to tune PID constants in Guess and Check - after a little practice, you get pretty good at guessing the correct constants. This is pretty much the method we use on 1124, and we haven't really found the need to migrate away from it.

Quote:
Originally Posted by Wikipedia
If the system must remain online, one tuning method is to first set Ki and Kd values to zero. Increase the Kp until the output of the loop oscillates, then the Kp should be set to approximately half of that value for a "quarter amplitude decay" type response. Then increase Ki until any offset is corrected in sufficient time for the process. However, too much Ki will cause instability. Finally, increase Kd, if required, until the loop is acceptably quick to reach its reference after a load disturbance. However, too much Kd will cause excessive response and overshoot. 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 Kp setting significantly less than half that of the Kp setting causing oscillation.
In our robots, we've found that an I parameter is rarely needed - we usually get what we want with only PD (or sometimes just P) controllers. It's really dependant on your application and the type of feedback you want. There's no really easy way to say it: it's a long, time consuming process.

Some more links to read into

To do this in LabVIEW, you have your PID VI, and one of its inputs is a cluster of three doubles, which are the P,I, and D constants that you tune to your liking. The best way to tune quickly is to probably run your code through a computer (using the Run button on Robot Main.vi - not building/running as startup) and modify that parameter cluster until you get satisfactory results. Just remember to reinitialize your PID VI (it's another control - wire that input to a button on your front panel or something like that) to "apply" your changes. NI also has a good whitepaper about working with PID in LV. It's a good read - check it out.
__________________
Phil Lopreiato - "It's a hardware problem"
Team 1124 (2010 - 2013), Team 1418 (2014), Team 2900 (2016)
FRC Notebook The Blue Alliance for Android

Last edited by plnyyanks : 13-01-2016 at 22:08. Reason: Add subsystem link
Reply With Quote
  #7   Spotlight this post!  
Unread 13-01-2016, 22:07
mark219 mark219 is offline
Programmer/Driver
AKA: Mark Szewczuk
FRC #0219 (Team Impact)
Team Role: Programmer
 
Join Date: Jan 2016
Rookie Year: 2015
Location: United States
Posts: 12
mark219 is an unknown quantity at this point
Re: Application of PID in code/real world

Quote:
Originally Posted by plnyyanks View Post
I go over some good papers/math explanation of how PID works in this post, take a look and read some of the linked resources. My details are LabVIEW specific, but there's some good java examples on ScreenSteps





As for the best way to tune, Ziegler-Nichols is a decent start, but there will inevitably be lots of guesswork involved unless you take the time to really characterize your system (not usually practice in FRC timeframes). Below is a previous post I made about how I always tuned PID controllers.
Thanks, I will definitely read more into it and will respond/private message you if I have any further questions!
Reply With Quote
  #8   Spotlight this post!  
Unread 15-01-2016, 12:43
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,088
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Application of PID in code/real world

Quote:
Originally Posted by Justin Buist View Post
Here's the crux of simple PID calculation code:
Code:
double error = target-current;
double errorChange = error-lastError;
double correction = Kp*error + Ki*errorSum + Kd*errorChange;
lastError = error;
return correction;
There's a line of code missing: errorSum += error


Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 02:10.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi