My love for you never slides around.
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 24-04-2004, 15:25
jdong jdong is offline
Linux Nerd
AKA: John D
#0245 (Adam-Bots)
Team Role: Programmer
 
Join Date: Apr 2004
Location: Rochester, MI
Posts: 98
jdong will become famous soon enoughjdong will become famous soon enough
(Experimental) PID Control System

This code could be used in any feedback loop. I wrote it all in theory, using a simple simulator for testing... Robot still not back from FedEx yet....


Testing is well appreciated. Feel free to use if desired.


--NOTE-- The following source was designed to run on a PC (Actually, Cygwin+KDevelop). Tweak accordingly. You only need the do_discrete_control and its global variables on the robot -- the rest was for PC simulation.

Code:
/*
 *
 *	Proportional-Integral-Derivitive Closed-Loop feedback algorithm.
 *
 *
 *	By John Dong, Team 245 (Adam-Bots)
 *
 *	jdong@wideopenwest.com
 *
 *	www.adamsrobotics.com
 *
 *
 *
 */



#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* These keep track of previous errors */

int error=0, Lerror=0, LLerror=0;
int last_out=0;




/*THIS is the bulk of the program. Pass in actual and wanted values,
 * and get back the optimal PWM value.
 *
 *
 * Constants to tweak are kp, ki, and kd, for each of the 3 bands.
 *
 * Set a constant to ZERO in order to disable the band.
 *
 * 	(You may want to disable the derivative band, as it's just
 * 	 a dampener. May not be worth calculation cost)
 *
 *
 */

int do_discrete_control(int actual, int wanted, int kp, int ki , int kd)
{
  int out; //Store output.
  LLerror=Lerror;
  Lerror=error;
  error=wanted-actual;

  out=last_out+kp*(error-Lerror)+ki*error+kd*(error-2*Lerror+LLerror);

  if(out>127) out=127;
  if(out<-127) out=-127;
  return out;
}


#define KP 		4

#define KI 		5

#define KD		0


int main(int argc, char *argv[])
{
  int i=0;

        int pwm,pos;
	pwm=0; pos=0;
  for(i=0;i<600;i++)
  {
    pwm=do_discrete_control(pos,250,KP,KI,KD);
    pos+=(double)pwm*.1;
    printf("# %d\t\tPOS: %d\t\tPWM:%d\n",i,pos,pwm);
    if(i==500)
    {
    	scanf("%d",&pos);
        i=0;
    }
  }
  getchar();
  return EXIT_SUCCESS;
}
__________________
John Dong
Co-Captain, Webmaster / Programmer
Team 245 -- Rochester Adams Robotics
  #2   Spotlight this post!  
Unread 25-04-2004, 19:20
Guest
 
Posts: n/a
Re: (Experimental) PID Control System

I don't see the integral part of the PID.

Why is Ki multiplied by the current error? There should be a variable keeping track of the running sum of the error. That variable would be multiplied by Ki.
  #3   Spotlight this post!  
Unread 25-04-2004, 20:45
Salik Syed Salik Syed is offline
Registered User
FRC #0701 (RoboVikes)
Team Role: Alumni
 
Join Date: Jan 2003
Rookie Year: 2001
Location: Stanford CA.
Posts: 514
Salik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud of
Send a message via AIM to Salik Syed
Re: (Experimental) PID Control System

yes...you want to integrate i add up all the values then divide by total time (multiplying each new i by time shouldn't matter because its an "assumed" even loop time...or use interrupts timers or whatever..) ...then multiply by Ki... don't even bother multiplying by .025 or whatever the loop time is ...just by 1 its simpler

this is what I did for our also Experimental PID loop (PI so far actuallY):
Code:
error=realv-targetv;
errorint=(error+errorint/time)/intprop;  //intprop does the same thing as your Ki term
time++;
also it seems that last_OUT isn't being changed anywhere since definition (it always=0)

can you please explain how the deriviatve portion works? ... I am kinda clueless to how it works...and does anything ...basically the theory behind it..and why it helps and what exactly it is
__________________
Team 701

Last edited by Salik Syed : 25-04-2004 at 21:27.
  #4   Spotlight this post!  
Unread 26-04-2004, 07:33
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Question Re: (Experimental) PID Control System

What exactly is PID supposed to do? Is it a fancy way of having whatever get to a certain position?
  #5   Spotlight this post!  
Unread 26-04-2004, 10:05
mtrawls's Avatar
mtrawls mtrawls is offline
I am JVN! (John von Neumann)
#0122 (NASA Knights)
Team Role: Programmer
 
Join Date: Mar 2003
Location: Hampton, VA
Posts: 295
mtrawls is a splendid one to beholdmtrawls is a splendid one to beholdmtrawls is a splendid one to beholdmtrawls is a splendid one to beholdmtrawls is a splendid one to beholdmtrawls is a splendid one to beholdmtrawls is a splendid one to behold
Send a message via AIM to mtrawls
Re: (Experimental) PID Control System

Quote:
Originally Posted by Astronouth7303
What exactly is PID supposed to do? Is it a fancy way of having whatever get to a certain position?
PID stands for Proportional Integral Derivative control. Yes, basically it is a "fancy way" of getting "to a certain position." It's what is called closed loop control -- you have a sensor telling you where you are, you have a setpoint telling you where you want to be, and you try to get the two values to become equal by modifying an output (to a motor, for instance). There's another thread out there on the matter, and google'll provide a lot more info on the matter .... it can get complicated, but most applications related to FIRST should be fairly straightforward. Note that you don't have to include all the different elements of a PID control; for instance, for our Arm this year, a simple P control worked quite well, whereas for another appendage we had to use something a bit more complicated.
  #6   Spotlight this post!  
Unread 26-04-2004, 10:59
Salik Syed Salik Syed is offline
Registered User
FRC #0701 (RoboVikes)
Team Role: Alumni
 
Join Date: Jan 2003
Rookie Year: 2001
Location: Stanford CA.
Posts: 514
Salik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud of
Send a message via AIM to Salik Syed
Re: (Experimental) PID Control System

or getting to certain speed too
__________________
Team 701
  #7   Spotlight this post!  
Unread 26-04-2004, 10:59
Joe Johnson's Avatar Unsung FIRST Hero
Joe Johnson Joe Johnson is offline
Engineer at Medrobotics
AKA: Dr. Joe
FRC #0088 (TJ2)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1996
Location: Raynham, MA
Posts: 2,650
Joe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond repute
Beware of the I...

With regard to the I part of PID, be very careful.

There is a phenomon called "integral wind up" that is not a theoretical problem but a practical implementation problem.

Essentially, you need to "turn off" the integration when the controller output is saturated or limited (for example when your robot is disabled).

We had a nasty bug that we had fits trying to kill. Our robot would come out of a disabled state and do a major twitch that hurt our robot and almost hurt some workers on a number of occassions. We finally killed the bug, but it was very sutble and was quite difficult to discover. It turned out to be a case of integral wind up.

Joe J.

Last edited by Joe Johnson : 26-04-2004 at 14:41.
  #8   Spotlight this post!  
Unread 26-04-2004, 11:38
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: (Experimental) PID Control System

Thank you. We used something to that sense, but we just had direction (if too low go up, if too high go down), that worked pretty well.
  #9   Spotlight this post!  
Unread 27-04-2004, 14:49
jdong jdong is offline
Linux Nerd
AKA: John D
#0245 (Adam-Bots)
Team Role: Programmer
 
Join Date: Apr 2004
Location: Rochester, MI
Posts: 98
jdong will become famous soon enoughjdong will become famous soon enough
Re: (Experimental) PID Control System

oops, sorry, I switched kp and ki when typing in a hurry. kp is ki and ki is kp


Time constants are 'included' in the k's.

I thought that if anyone needed a code snippet for PID, they probably wouldn't know exactly what effect each constant has, and probably would just "guess and check"
__________________
John Dong
Co-Captain, Webmaster / Programmer
Team 245 -- Rochester Adams Robotics
  #10   Spotlight this post!  
Unread 10-06-2004, 21:46
jdong jdong is offline
Linux Nerd
AKA: John D
#0245 (Adam-Bots)
Team Role: Programmer
 
Join Date: Apr 2004
Location: Rochester, MI
Posts: 98
jdong will become famous soon enoughjdong will become famous soon enough
Re: (Experimental) PID Control System

Reguarding the addup -- the loop goes through and adds the current output to previous outputs -- so errors are summed in there. I found this in a whitepaper about PID (I lost the url, so can't post it here). It was the "most optimal and stable" implementation, according to the author.
__________________
John Dong
Co-Captain, Webmaster / Programmer
Team 245 -- Rochester Adams Robotics
  #11   Spotlight this post!  
Unread 15-06-2004, 18:05
10intheCrunch's Avatar
10intheCrunch 10intheCrunch is offline
Who's John V-Neun?
AKA: Alex Baxter
None #0254 (Cheesy Poofs)
Team Role: College Student
 
Join Date: Feb 2004
Rookie Year: 2004
Location: San Jose, CA
Posts: 129
10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough
Send a message via AIM to 10intheCrunch
Re: (Experimental) PID Control System

Integral windup can definately become a problem if you leave your robot on, and something that is connected to your PID loop sensor moves (when the RC is on but the robot is powerless to do anything about it, it will build the integral uselessly, until reactivation when people get hurt). We struggled for a while trying to get the RC to realize when the OI was on or off, but if you can manage to reset everything when the OI comes on (not just resetting the RC), that's a good way to go. You can also limit your integral, as this will help you if you ever get stuck on something during real testing (you won't break your motors). If you don't have a failsafe implemented though, make sure to reset your robot every time before turning your OI back on!
__________________
~Alex Baxter
Programming, Arms operation, Team 254
  #12   Spotlight this post!  
Unread 15-06-2004, 18:11
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: (Experimental) PID Control System

If the OI isn't on, doesn't it NOT execute user code? I'd be more worried about disable.
  #13   Spotlight this post!  
Unread 18-06-2004, 13:42
DKolberg DKolberg is offline
Mentor Iron Giants
AKA: David Kolberg
FRC #5069 (Iron Giants)
Team Role: Mentor
 
Join Date: May 2002
Rookie Year: 2000
Location: South Bend
Posts: 44
DKolberg has a spectacular aura aboutDKolberg has a spectacular aura about
Lightbulb Re: (Experimental) PID Control System

The OI does know what state it is in. It knows when it is in competition mode, disable mode, or autonomous mode. Your control loops should be aware of which state the sytem is in and provide code for transitioning between each state. You can use different controls for each state, but anything with memory like integrators should be delt with as the system changes from state to state. All memory type variables should be initialized to a known state while not being used in a specific state so it is ready to be used when the new state starts.
  #14   Spotlight this post!  
Unread 18-06-2004, 15:37
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: (Experimental) PID Control System

I know that. But if you unplug the OI (the OI is no longer broadcasting), then doesn't the RC just stop the user proc?
  #15   Spotlight this post!  
Unread 18-06-2004, 15:55
DKolberg DKolberg is offline
Mentor Iron Giants
AKA: David Kolberg
FRC #5069 (Iron Giants)
Team Role: Mentor
 
Join Date: May 2002
Rookie Year: 2000
Location: South Bend
Posts: 44
DKolberg has a spectacular aura aboutDKolberg has a spectacular aura about
Re: (Experimental) PID Control System

No, the RC goes into disabled mode.
Closed Thread


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
What do you wish you knew about the new control system? Joe Ross Control System 2 09-01-2004 21:47
Need a FIRST Robotics control system kershawrobotics General Forum 3 07-07-2003 09:49
control system worth more than $500 archiver 2001 8 24-06-2002 02:00
New Innovation FIRST control system and the dashboard archiver 2000 0 23-06-2002 22:15
Ok, how much is the control system worth? archiver 2001 6 23-06-2002 22:05


All times are GMT -5. The time now is 13:08.

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