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 PWM Valve

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



Joined: 04 Feb 2009
Posts: 83
Location: PA

View user's profile Send private message Send e-mail

PID PWM Valve
PostPosted: Wed Apr 15, 2009 8:42 pm     Reply with quote

Hello all!

I would like to see if anyone can help me with a project I am currently working on to control a proportional solenoid valve using a PID controller.
The valve is 0 to 30V.
I would like to understand how the PWM duty cycle works a little better.
from what I understand, the duty cycle is 0 to 100%, but do the values that I send for the duty cycle depend on whether I am using int's or longs, etc?

Or do I need to just convert the numbers to values between 0 and 100?

Basically I would like to adjust my PID gains appropriately in order to send the correct duty cycle for controlling pressure in a line. I have a reference pressure of 100psi and would like to maintain this pressure, limiting overshoot.

Right now I cannot post the whole code since it is almost 6000 lines long, but I would like to understand PWM a little better, and whether or not I need to scale my gains way down in order to get the correct duty cycles.

THank you as always and Best regards,

deperkin
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 15, 2009 11:31 pm     Reply with quote

Quote:

I would like to understand how the PWM duty cycle works a little better.

Post your PIC.

These threads discuss PWM thoroughly. They have also links
to further discussions:
http://www.ccsinfo.com/forum/viewtopic.php?t=33003
http://www.ccsinfo.com/forum/viewtopic.php?t=17729
http://www.ccsinfo.com/forum/viewtopic.php?t=24055
http://www.ccsinfo.com/forum/viewtopic.php?t=37206
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Apr 16, 2009 4:01 am     Reply with quote

An important parameter, that has to choosen first, is the intended PWM frequency. This setting defines also the scaling of PWM duty cycle. Typically, the same range is given to the PID controller's manipulated value output. In case of an integer PID design, you should provide some additional (fractional) resolution in the integrator right of the LMN output's LSB.
deperkin



Joined: 04 Feb 2009
Posts: 83
Location: PA

View user's profile Send private message Send e-mail

PIC
PostPosted: Fri Apr 17, 2009 7:23 am     Reply with quote

I am using a PIC18F2331.

Here is what I have setup so far for the PWM:
Code:

int Kp = ;           //needs to be chosen... (I will find these soon)
int Ki = ;            //needs to be chosen...
int Kd = ;           //needs to be chosen...
float PulseFast = 100; //max value for PWM
float PulseSlow = 10;  //min value for PWM
...
setup_timer_2(T2_DIV_BY_16,127,1);
setup_ccp1(CCP_PWM);
...
void FlowValve_Adj(){                //proportional solenoid flow control
float Adj,Pe,Ie,De,Einit;
//**********get PID values*************
   EInit = EPrev;                    //get previous error for derivative control
   P0 = Read_Pressure();
   delay_us(20);
   Pe = Get_Error(P0);
   Ie = ETotal;
   De = Get_Error(P0) - Einit;
//*************************************
   Adj = Get_Adj(Pe,Ie,De);
   Adj = NormPulse(Adj);             //normalize the PID values to a PWM pulse
//NEED TO OUTPUT TO PROPORTIONAL SOLENOID THE ADJ VALUE
// I.E. PULSEWIDTH(MYCURRENTFLOW +- ADJUSTED VALUE
}

void FlowValve_Close(){
   output_low(FLOWVALVE);        //Fully close the proportional solenoid valve
}

float Get_Adj(float Pe,float Ie,float De){     // Get adjusted value for flow-rate
   float total;                                //PID adjustment value for control
   total = Pe*Kp + Ie*Ki + De*Kd;
   //************************************************************************
   //DECREASE FLOW (-) WHEN BELOW Pref...INCREASE FLOW(+) WHEN ABOVE Pref!!!!
   //************************************************************************
   return total;
}

float Get_Error(float P){           // check value of pressure versus reference
   float Error;
   Error = Pref - P;                //SHOULD THIS BE SIGNED ???
   if(Error<5){                     //if error is very close to reference
     ETotal = 0;                    //set Total Error to zero
   }
   else{
     ETotal = ETotal + Error;       //used for integral control
   }
   EPrev = Error;                   //used for derivative control
   return Error;
}

//NormPulse returns a value Pulse for PWM duty cycles (between 0% and 100%).
float NormPulse(float Pulse){
   if(Pulse>PulseFast){       // Get rid of values that are outside of the range
      return PulseFast;
      }
   else if(Pulse<PulseSlow){
      return PulseSlow;
      }
   else {
      return Pulse;
      }
}

This is not all of my code, since I am dealing with over 7000 lines I did not want to add all of it.

I really just need to define a region for the duty cycle.

Thank you all for you great help.

BR
Deperkin
deperkin



Joined: 04 Feb 2009
Posts: 83
Location: PA

View user's profile Send private message Send e-mail

PWM
PostPosted: Fri Apr 17, 2009 7:30 am     Reply with quote

I guess what I am really not understanding, to clarify, is that as of yet I am not sure what my maximum values will be in my error, particularly in my total error used for the integral control.
Without knowing these values how would I be able to scale for an appropriate duty cycle ??

Thanks again!

Deperkin
deperkin



Joined: 04 Feb 2009
Posts: 83
Location: PA

View user's profile Send private message Send e-mail

FvM
PostPosted: Sat Apr 18, 2009 10:02 pm     Reply with quote

what do you mean by LVM outputs least bit???

i know i will most likely need to scale the PID values, but is is tough at this point to see what error i will forsee, since i am actually not starting at the reference point, but well below the reference.

I am used to starting with zero error using PID, but in my case i have to start with an error of almost 100.

thanks again and you guys are great!
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Apr 19, 2009 12:58 am     Reply with quote

My remark regarding PID internal resolution is dedicated to fixed point PID designs. Cause you utilize float, there should be no problem of this kind.

In the discussion of maximum error signal, scaling of controller output (= PWM duty cycle in this case), I suggest a different approach. It's normal, that a controller output reaches limit values, cause most actors have electrical or mechanical limitations. But the integrator action should be stopped at this point. Otherwise you get large overshoots. With some actors, there may be a problem to set the controller numerical limit according to the actual actor end position.

A proportional valve actor must be clearly able to shut down the flow completely, the maximum output may be adjusted application dependant, but if the valve's orifice is choosen correctly, the controller output should go up to nominal solenoid current. This sets the duty cycle range.

The PWM frequency has a special meaning for proportional valve control. If choosen correctly, it can reduce mechanical hysteresis by forcing small vibrations of the valve. This results in PWM frequencies of a few 100 Hz, the optimal value is type dependant of course.

Another important parameter is the controller sampling frequency. The suitable range strongly depends on the control process (plant). For a pressure regulator, I would expect a few Hz up to several 100 Hz.
deperkin



Joined: 04 Feb 2009
Posts: 83
Location: PA

View user's profile Send private message Send e-mail

thanks!
PostPosted: Sun Apr 19, 2009 6:08 am     Reply with quote

Thank you again for your help, you are well knowledged here.

i am not too worried (at this point) with the frequency, but only the resultant RMS voltage coming from the PWM.

I was thinking for simplicity i could use:
setup_timer_2(T2_DIV_BY_16,100,1);

which would give me a freq of about 1238 Hz., but allowing me to look at the duty cycle as a percentage from 0 to 100... (correct)?

I could probably postscale this to lower the frequency if you think this would help me with hysteresis error... this will be worth trying if i have time (i only have two weeks to finish my project)...

i am really dealing with very low flowrates and i want to avoid any chattering of the valve.

we are boiling water to maintain a pressure...
my concern is that i will have initially large errors resulting in large duty cycles (thus opening my valve completely)... the way our system is set up is: the larger flowrate will lower the output temperature and keep the pressure lower, in effect keeping the error higher. What i would like to do is with a larger error make the duty cycle lower, thus trickling in the water and raising the temperature = lowering the error by raising the pressure.

am i making any sense??

Thank you all again!
Ttelmah
Guest







PostPosted: Sun Apr 19, 2009 7:35 am     Reply with quote

First, remember that the value you feed into the PWM, can be a 'short' (8bit) integer, or a 'long' (16bit) integer. The hardware PWM, allows values up to the timer count _plus one_ to be used. If the value used is a 16bit integer, two extra bits of scaling become available, using the individual cycles of the master clock, instead of the processor clock.
So:
If using 8bit integers to feed the PWM, setup_timer_2(T2_DIV_BY_16,99,1);
gives legitimate 8bit values from '0', to '100'.

However if using 16bit integers, setup_timer_2(T2_DIV_BY_16,24,1);
gives legitimate values from 0L to 100L, and gives you a possibly more friendly output rate.

Your frequency figure, suggests you are using 8MHz. For most PWM applications, unless there is significant output filtering present, you want to aim for a resulting output, that is above audio. Using a prescaler of /4, would take your frequency up out of audio. Might be worth considering....

Best Wishes
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Apr 19, 2009 8:13 am     Reply with quote

Quote:
my concern is that i will have initially large errors resulting in large duty cycles (thus opening my valve completely)... the way our system is set up is: the larger flowrate will lower the output temperature and keep the pressure lower, in effect keeping the error higher. What i would like to do is with a larger error make the duty cycle lower, thus trickling in the water and raising the temperature = lowering the error by raising the pressure.

am i making any sense??


This operation clearly goes beyond the capabilities of a PID controller by involving a region with inversion of the controller gain. A more practical approach is to to limit the feed water flow to a value, that still maintains the evaporator temperature and avoids an inversion of control process gain.

Technical steam generators are usually controlling the steam pressure by varying the heat supply and keeping a constant water level, thus avoiding the said instable operation range.
deperkin



Joined: 04 Feb 2009
Posts: 83
Location: PA

View user's profile Send private message Send e-mail

Thanks
PostPosted: Sun Apr 19, 2009 9:42 pm     Reply with quote

Thank you again for all the good advice and help on this.

I will post again and let you know how it is working... time to do some testing!

regards,

deperkin
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