View Full Version : Tuning PID Code
Rick TYler
15-02-2006, 00:46
Help us, Kevin, you are our only hope! Or someone else...
Our programming guy (robotaddict -- my son as it turns out) has written single-stick code based heavily on Kevin's PID code routines. It's been a long, difficult road for a student who had little C experience at the beginning of the season, but the code seems to work pretty well. The wild oscillations are gone, and the robot is drivable. But there are some problems. I'm hoping some of you with real-world PID experience can give John -- er, I mean robotaddict -- some guidance of final debugging. We also use these routines for aiming and autonomous, so it is important that he fix them. John and the programming mentors have tweaked the P,I, and D settings a LOT, but the perfect blend of settings has been elusive.
1. The controls are pretty sensitive. Turning is especially twitchy.
2. Can someone describe the PID tuning process in even more detail than Kevin does in the comments in his code. Careful descriptions of what the robot is doing in relation to each of the control settings would be especially appreciated.
3. The robot nearly drives in a straight line, but gently drifts to one side in a long (say 30-40 feet) drive.
Thank you SO MUCH in advance.
I am facing this challenge too, except PID code is controlling the position and elevation of our turret, plus its steering and autonomous mode.
There is no one in my area who has responded with any PID experience except those that use it for temperature control.
This leaves me with many questions about the best way to do PID control, like how long should the I values stick around? For something like controlling the spin (yaw) of a turret, should I even use I, or switch to PD?
Kevin's PID code routinesAre you talking about the NAV code from 2005?
Eagerly waiting,
Robinson
Rick TYler
15-02-2006, 01:13
Are you talking about the NAV code from 2005?
Yep, that's it. John, er "robotaddict," wrote his own which was extremely complex and never worked very well (unless you wanted your robot to go back and forth rapidly), and then developed "version 2" based on Kevin's code. It almost worked great. KEVIN!! (Or other guru)
developed "version 2" based on Kevin's code.Are you willing to post it?
The actual PID algorithm is not very complex, just the tuning process.
I found this on the Wikipedia for PID Controller:
There are several methods for tuning a PID loop. The choice of method will depend largely on whether or not the loop can be taken "offline" for tuning, and the response speed of the system. If the system can be taken offline, the best tuning method often involves subjecting the system to a step change in input, measuring the output as a function of time, and using this response to determine the control parameters.
If the system must remain online, one tuning method is to first set the I and D values to zero. Increase the P until the output of the loop oscillates. Then increase I until oscillation stops. Finally, increase D until the loop is acceptably quick to reach its reference. A fast PID loop tuning usually overshoots slightly to reach the setpoint more quickly; however, some systems cannot accept overshoot.
Effects of increasing parameters
Parameter Rise Time Overshoot Settling Time S.S. Error
P Decrease Increase Small Change Decrease
I Decrease Increase Increase Eliminate
D Small Chang Decrease Decrease Small ChangeAnother tuning method is formally known as the "Ziegler-Nichols method". It starts in the same way as the method described before: first set the I and D gains to zero and then increase the P gain until the output of the loop starts to oscillate. Write down the critical gain (Kc) and the oscillation period of the output (Pc). Then adjust the P, I and D controls as the table shows:
Ziegler-Nichols method
Control P Tr Td
P 0.5 * Kc - -
PI 0.45 * Kc Pc / 1.2 -
PID 0.6 * Kc Pc / 2 Pc/8
Everyone wish everyone else luck, (God knows at least I need it)
Robinson
Also, while I am at it, note this post (and the rest of the thread, really)
PD is pretty much the most common way to go about velocity control.
- Set Kd to 0.
- Increase Kp slowly until the robot starts to oscillate around its setpoint. Watch the oscillations, are they dying down, or staying consistent?
- - + If yes, then set Kp to about 30% of its current value.
- - + If no, then Kp is set way too high, for some reason. Reduce it by 60%, and watch for the oscillations again. When the oscillations are reasonable (staying about constant, or dying away, and aren't too violent), then move to the next step.
- Increase Kd slowly until the robot "snaps" to its setpoint.
- If the robot "snaps" eventually, but there is some error between where it snaps, and the desired setpoint (and you can't live with this error), then you have to add in a bit of Ki.
- Ki is usally ends up being 1/Kd. I don't like Ki for velocity control purposes, but you can fiddle with it if you want.from Problems Using PID for Velocity (http://www.chiefdelphi.com/forums/showthread.php?t=42795)
Send us more luck,
Robinson
Eldarion
15-02-2006, 03:23
I am facing this challenge too, except PID code is controlling the position and elevation of our turret, plus its steering and autonomous mode.
There is no one in my area who has responded with any PID experience except those that use it for temperature control.
This leaves me with many questions about the best way to do PID control, like how long should the I values stick around? For something like controlling the spin (yaw) of a turret, should I even use I, or switch to PD?
Are you talking about the NAV code from 2005?
Eagerly waiting,
Robinson
The integral term is used for long-term steady-state error compensation.
Unless your robot will sit in the middle of the field for the entire match, I don't think it's really necessary. :)
There will be much larger errors induced by motion than the (usually small) steady-state error.
The integral term is used for long-term steady-state error compensation.
Unless your robot will sit in the middle of the field for the entire match, I don't think it's really necessary. :)
There will be much larger errors induced by motion than the (usually small) steady-state error.So just PD is the way to go with the turret? What about steering (i.e. if both joysticks (we do tank drive) are set to the same value and the robot should be going straight, but the gyro tells me my angular rate is > 0 deg/s). I would like to compensate with some form of closed loop control, is PID/PD/P/PI/whatever the way to do this?
Thanks in advance,
Robinson
Chris_Elston
15-02-2006, 08:12
I won't go in the method of tuning a PID as others have done, but here are some tips to get you up in running FAST.
1. Use three POTs on a board labeling them P,I,D. Connect them to Analog ports. Say 14,15,16 respectfully. Make a function to read the pots, and then multiply the values out. Like K_P = analog14 * 0.0001. Of course K_P needs to be a float.
Your analog will be 0-1024 so you need to make your K_P, K_I, K_D values smaller, normally less than 1
K_P = analog14 * 0.0001
K_I = analog15 * 0.0001
K_D = analog16 * 0.0001
If you need a bigger "tweak" just shrink the multiplier
K_P = analog14 * 0.001
K_I = analog15 * 0.001
K_D = analog16 * 0.001
You won't be able to view K_P in your "Print to Screen" so just display your analog value, so you can multiply or divide back to get your "real" K_P,K_I,orK_D value to "hard code" it back into the system so you can REMOVE the PID trimmer pot board.
2. Wow...use LABVIEW to tune your PID system. We tuned our ball velocity PID by connecting the ball motor to PWM1, then mapped our velocity variable to PWM2. Start Labview Dashboard. Both applications, the connections one, and the view application. Of course there ARE NO MOTORS connected to PWM2, we are just transporting the information to Labview because we don't know how to edit Labview. Out of the box, PWM1 and PWM2 was configured in a graph......;-)
See these handy video tutorials if you haven't done it yet.
http://www.lasarobotics.org/Forum/viewtopic.php?t=366
Now you can view your PWM1 which is your motor OUTPUT, and your pretending that PWM2 is your velocity output so you can just use Labview PWM2 to view, graph and log your PID tuning. You'll see very quickly how effective this tool is.
In combination of the two tools, making a PID tuning POT trimmer board, and displaying the tune on Labview, you'll have your PID tuning done in a matter of minutes.
Hopfully this helps.
-
Chaychay
26-01-2007, 11:59
Mr. Elston, first off, what an amazing idea. Our team had nightmares tuning last year, we didn't get the values right until Atlanta! (albeit, we didn't have much testing time before hand anyway)
Use three POTs on a board labeling them P,I,D. Connect them to Analog ports. Say 14,15,16 respectfully.
-
I was wondering if this would work equally as well if the POTs were connected to the OI kind of like joysticks. I ask because I want to be able to tune the drive PID system and give it to our driver to see how he likes it. Based on this feedback I can retune and he can test again. Or I can teach him what they P, I, D means, and just get him to tune it to his liking. I don't see any obvious reason why this wouldn't work, but I might be missing something. Thanks very much for this time saving idea!!! :cool:
Alan Anderson
26-01-2007, 12:25
I was wondering if this would work equally as well if the POTs were connected to the OI kind of like joysticks.
We did exactly that last year. A student built a "tuning box" with three pots and three pushbuttons (there wasn't really room to make it four and four) that connects to a joystick port. We reserved that port for debug and calibration use.
We used the box to set the PID constants for our turret, drivebase position seek, and direction tracking; and for setting the motor speed and timing for the catapult winder mechanism we started with. We will probably be doing the same thing for our PID parameters this year, with the additional feature of using menus and commands through the program port to save the data to EEPROM the way Kevin Watson's camera software works.
Chris_Elston
26-01-2007, 12:36
Yes you can.
You'll need 3 100K pots.
See this document:
http://www.ifirobotics.com/docs/legacy/fr-2004-oi-ref-guide-2004-2-17.pdf
PDF Page #5
Each port provides four analog inputs. These inputs are typically connected to joysticks and potentiometers, providing an analog input ranging from 0 to 254 (in software) that is transmitted to the Robot Controller. In the event that an analog input is less than 0.05V, a value of 127 will be generated. This ensures that when a joystick is unplugged, the corresponding output on the Robot Controller will not go to a full reverse condition. Each port provides +5V Aux, used for wiring to potentiometers or other sensors. The wiring diagram for a potentiometer is shown below. Always use 100kΩ potentiometers.
You'll need to "borrow" a joystick port. And then scale your PID to something different like 0-254 instead of 0-1024 like you would see on the analog port of the RC.
WARNING....UNPLUG....PID pots when in competition.....LOL....
WARNING #2....Make sure that your values default to 0 when radio drops...because by default they go to 127.
I like the pots onboard the robot the best, when robot was up on blocks. Though the below picture looks "kewl" from the OI, RC tuning from the analog was my favorite....
Chaychay
26-01-2007, 13:45
Mr. Anderson & Mr. Elston,
Thank you for confirming this. This is going to save so much time for us :), we're hoping on getting this tuning board built today.
Thanks again for the help =)
Chaychay
28-01-2007, 15:29
Ok, so our team looked around the lab for 100 K ohm potentiometers, and we could not find any. So after some thought we decided to slice open the KOP Joystick from last year (it was semi-busted anyway) and extract the 2 potentiometers from there.
To our surpise, these pots are 120K ohms, can we still use them for the PID trimming board? Seems to me that if they were used for the KOP joystick, there is no reason why they would pose a threat.
Thanks for your help, I just want to make sure because that OI is mighty expensive to repair/replace if we blew it...:p
Andrew Blair
28-01-2007, 16:10
As a side note, I have read that the easiest way to tune a full PID controller is to increase your P constant until there is a constant steady-state error, then increase your I constant until the steady-state error is gone and you have minor oscillation. Finally, increase the D constant to soak up any oscillation.
Hope this helps! We'll have some tuning to do pretty soon ourselves hopefully...
Kevin Watson
28-01-2007, 17:51
Ok, so our team looked around the lab for 100 K ohm potentiometers, and we could not find any. So after some thought we decided to slice open the KOP Joystick from last year (it was semi-busted anyway) and extract the 2 potentiometers from there.
To our surpise, these pots are 120K ohms, can we still use them for the PID trimming board? Seems to me that if they were used for the KOP joystick, there is no reason why they would pose a threat.
Thanks for your help, I just want to make sure because that OI is mighty expensive to repair/replace if we blew it...:pAnything above ~5K ohms will work well.
-Kevin
I'm using a couple of three position rotary switches and an 8 bit dipswitch for my PID tuning needs.
Jared Russell
30-01-2007, 00:06
I just thought I'd share this tidbit...
We were working on some PID code for the drive (using a gyro for feedback) and it was kind of thrown together as a hack. We were just trying to figure out if the logic was alright, no code errors, directions were good, etc. Well, low and behold, the first gain parameters we tried got it EXACTLY right. Tweaking just a little around this point resulted in oscillation and/or slower rise time. This has never happened to me before.
Chaychay
30-01-2007, 00:46
Abwehr, please buy a lottery ticket :p
I'm pretty sure that getting the scaling paramters right on first attempt is very very very rare (at least I would think so, based on how much trouble we had last year)
But good job! I hope ours works as smoothly :)
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.