Go to Post Team 71: So good at Strategy they already have figured out next years game before the GDC has even met. - IKE [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Spotlight this post!  
Unread 04-24-2004, 03:25 PM
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
 


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 01-09-2004 09:47 PM
Need a FIRST Robotics control system kershawrobotics General Forum 3 07-07-2003 09:49 AM
control system worth more than $500 archiver 2001 8 06-24-2002 02:00 AM
New Innovation FIRST control system and the dashboard archiver 2000 0 06-23-2002 10:15 PM
Ok, how much is the control system worth? archiver 2001 6 06-23-2002 10:05 PM


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

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