CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

pid control with pwm output

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ferkar



Joined: 14 Jul 2007
Posts: 38

View user's profile Send private message

pid control with pwm output
PostPosted: Wed Dec 12, 2007 3:51 am     Reply with quote

hi everyone,

i would like make a pid controller. i simulated the code that i wrote in a simulation program,İSİS. on the screen i saw everything seemed well, i mean when look at oscillscope the pwm duty ratio varies between 0% to %100 in 10bit configuration. However when i test the code on the real system i realized that there was big dead band, about %50.

now i need some help to understand where the real problem is software or harware.

could anyone tell me where ı should look for digital pid controller flow chart?
thanks indeed...
Ttelmah
Guest







PostPosted: Wed Dec 12, 2007 4:11 am     Reply with quote

Obvious thing to look at, would be something silly with the variables used to control the PWM. Remember that CCS, 'switches mode', and is an 8bit variable is used, feeds this to just the top eight bits of the PWM control, while if a 'long' is used, give the 10bit output.
There are a couple of very good PID examples, in the Microchip site, under 'servo' control. AN523, gives in particular the process logic diagram for a digital PID loop. I posted sections of a much modified PID program here some time ago. The big problem is keeping the loop processing _fast_. Otherwise 'windup' behaviour in the control is liable to happen, with several loops occuring, before a response is seen, resulting in overcorrection. A lot also depends on the resolution of your encoder setup, and how often changes are likely to be seen.
If you can't find something to help, I'll try to trim the parser, and other stuff out of my code, and post it.

Best Wishes
ferkar



Joined: 14 Jul 2007
Posts: 38

View user's profile Send private message

pid
PostPosted: Wed Dec 12, 2007 5:34 am     Reply with quote

thank you interested in,

i check an523 on microchip site but i disappared. in other sources i read so much about pid. i need a programming support. if you want to see my code i could send it to check if any missing or failure lines..

thanks
mmprestine



Joined: 13 Jan 2004
Posts: 29
Location: Green Bay, Wisconsin

View user's profile Send private message

PostPosted: Wed Dec 12, 2007 8:41 am     Reply with quote

here is a good reference

http://www.embedded.com/design/embedded/4211211/PID-without-a-PhD

http://www.embedded.com/ContentEETimes/Documents/Embedded.com/2000/f-wescot.pdf


here is code based on it that works well....

Code:

PID.c
#include <18F452.h>

#device *=16 ADC=10

#use delay(clock=20000000)

#use rs232(baud=38400,xmit=PIN_C6,rcv=PIN_C7)

#include "PID.h"

#INT_TIMER0

TIMER0_isr(){

   drive = UpdatePID(&plantPID,(plantCommand - position), position );

}


void main(){

  setup_adc_ports ( NO_ANALOGS );
  setup_adc ( ADC_OFF );
  setup_psp ( PSP_DISABLED );
  setup_spi ( FALSE );
  setup_wdt ( WDT_OFF );
  setup_timer_0 ( RTCC_INTERNAL|RTCC_DIV_8|RTCC_8_bit );
  enable_INTerrupts ( INT_TIMER0 );
  enable_INTerrupts ( GLOBAL );

  setup_oscillator ( False );

  while( 1 ) {

  }
}

PID.h
typedef struct {

  signed int16 dState,     // Last position input
               iState;     // Integrator state

  signed int16 iMax,
               iMin;       // Maximum and minimum allowable integrator state

  signed int16 iGain,      // integral gain
               pGain,      // proportional gain
               dGain;      // derivative gain
} SPid;

SPid plantPID;

signed int16 plantCommand, position, drive;

//PID controller code
signed int16 UpdatePID(SPid *pid, signed int16 error, signed int16 position)
{
   signed int16 pTerm,
                dTerm,
                iTerm;

   // calculate the proportional term
   pTerm =  pid->pGain * error;   // calculate the proportional term

   // calculate the integral state with appropriate limiting
   pid->iState += error;

   if ( pid->iState > pid->iMax){
       pid->iState = pid->iMax;
   }

   else if ( pid->iState <  pid->iMin){
      pid->iState = pid->iMin;
   }

   iTerm = pid->iGain * pid->iState;  // calculate the integral term

   dTerm = pid->dGain * ( pid->dState - position);

   pid->dState = position;

   return (pTerm + dTerm + iTerm);
}



Last edited by mmprestine on Fri May 10, 2013 7:07 am; edited 2 times in total
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

PostPosted: Sat Sep 05, 2009 3:39 pm     Reply with quote

fascinating. I'm having a difficult time understanding some of your code though. In particular:

Code:
drive = UpdatePID(&plantPID,(plantCommand - position), position );


Where does plantCommand come from / what is it? And according to the UpdatePID function, the central term should be related to the desired control value, not the input but you have it relating to the input. That confuses me. Say for example its a cruise control... the input is the pedal and the desired control value is speed. The error would be actual speed - desired speed, not actual speed - pedal position. So not knowing the context I am confused on that middle term.

Finally, where are the iGain, pGain and dGain values set/determined/calculated? i see them being declared but never initialized.

If I can wrap my head around your code this may be just what I've been looking for... a PID loop that doesn't use any floats!
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Sep 06, 2009 6:44 am     Reply with quote

Obviously, the code is rather a principle example than a functional application. As you already found out, part of the code is missing, e.g. initialization of parameters.

There are however more basic issues. As you are looking for fixed point PID code, you may want to check how scaling of product terms is achieved in the code. Unfortunately, it isn't at all. I can't see, how you get a functional PID code this way.

When you are using e.g. int16 variables and parameters, you have to define a scaling, too. You can have e.g. 4 bits left of the implicite decimal point, this is usually called a fractional number format. The multiply int32 result has to be scaled accordingly. To scale back to your your fractional format, you have to drop 4 MS and 12 LS bits. But saturation rather than overflow should be performed on the MSB side.

As another disadvantage of the PID example, it uses an explicite integrator limit, that typically doesn't correspond to the actual manipulated value's limits. But a poor windup suppression is still better than none.

Regarding your pedal and speed setpoint discussion, it isn't specific to the present PID code in any respect. Basically a car's pedal control doesn't involve a feedback controller, it directly sets the drive power.
mmprestine



Joined: 13 Jan 2004
Posts: 29
Location: Green Bay, Wisconsin

View user's profile Send private message

PostPosted: Tue Sep 08, 2009 7:15 am     Reply with quote

Did you read the article "PID Without a PhD" that was posted with the sample code?
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

PostPosted: Tue Sep 08, 2009 3:56 pm     Reply with quote

Yes, of course...

my problem isn't so much the PID theory as it is getting it to work without floats.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group