View Single Post
  #4   Spotlight this post!  
Unread 05-08-2005, 05:15 PM
billbo911's Avatar
billbo911 billbo911 is offline
I prefer you give a perfect effort.
AKA: That's "Mr. Bill"
FRC #2073 (EagleForce)
Team Role: Mentor
 
Join Date: Mar 2005
Rookie Year: 2005
Location: Elk Grove, Ca.
Posts: 2,345
billbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond repute
Re: Concept of PID explained

This is my Rookie year with FIRST, and let me tell you, I love it!
Coding software has never been one of the things that I have had to do in my professional life, but it is something I have come to realize is very important to do and/or understand to some extent if we are going to have our robots work in ways beyond the default code is set up to do. So, with that in mind, I have spent a couple month this year, after our local regional, beginning to learn. "So", you might ask, "what does this have to do with PID systems?"

One of the first things I want to do with our future bots is to use position control to operate and arm or axis instead of just using the joystick to adjust speed and then stop when the driver felt, or saw, that they were in the correct position. The coding I came up with to do this is quite simple, remember, this was my first attempt to write anything. It follows some of the basic PID architecture described in previous posts. I find that actually seeing the code can sometimes help others to learn. Besides, I know many of you will have input as to why it will or will not work and, I'm hoping, ways to improve it. So here goes:



unsigned int pospot1;
unsigned char PosFdbk1;
char PosError3;
char DrvCmd3;

/************************************************** **************************************************
* This section compares the value of the feed-back/Position pot attached to RC Analog input 1 *
* and drives pwm03 until the value of the feedback pot matches the value of the Port 3 Y axis. *
* to within a value of +- 5, which is narrower than the DeadBand of the Victor it's self. *
* This routine will also limit the Max Forward and Reverse Speeds to that of the Victor Max/Min *
* values. Lastly, it maintains a minimum speed as the position error approaches 0, which will *
* also allow small movements more easily. Additional "Else If"s can bee added to modify mid speeds.*
************************************************** **************************************************/


pospot1 = Get_Analog_Value(rc_ana_in01) ;/* read in the analog value of the position pot*/
PosFdbk1 = (unsigned char)(pospot1 >> 2); /* assign an 8 bit value to the variable PosFdbk1 */
PosError3 = (p3_y - PosFdbk1);/* create the uncorrected drive value */

if (PosError3 > 127) /*Limits drive to Max Forward Speed of Victor and Motor*/
{
PosError3 = 127; /* Lower this value to reduce max forward speed */
}
else if (PosError3 < -127) /*Limits drive to Max reverse peed of Victor and Motor*/
{
PosError3 = -127; /* Increase this value to reduce max reverse speed */
}
else if (PosError3 > 5 && PosError3 < 20) /* Small forward movement window */
{
PosError3 = 20; /* Change this number to modify Min Froward speed */
}
else if (PosError3 < -5 && PosError3 > -20) /* Small reverse movement window */
{
PosError3 = -20; /* Change this number to modify Min reverse speed */
}
else if (PosError3 > 21 && PosError3 < 40) /* medium forward movement window */
{
PosError3 = 40; /* Change this number to modify Medium Froward speed */
}
else if (PosError3 < -21 && PosError3 > -40) /* medium reverse movement window */
{
PosError3 = -40; /* Change this number to modify Medium reverse speed */
}
DrvCmd3 = (127 + PosError3);/* Apply the corrected drive value to neutral */
pwm03 = DrvCmd3;/* drive the motor */


The middle section of this code limits the drive command to 0-254. It also adds some speed corrections in the low speed range to maintain a certain minimum speed until the axis is quite close to the actual value desired. I did this because the Victors have approximately a +- 10 deadband window that may be a little larger than desired for positioning a system. The values of the intermediate steps can be modified to accommodate various masses and friction variables encountered with differing system designs etc.

Please feel free to comment on, correct, improve or otherwise suggest anything you would like. I make no claim to correctness or accuracy. Again, this is my first attempt. I suspect it will be improved upon quite a bit in the future.