View previous topic :: View next topic |
Author |
Message |
JAM2014
Joined: 24 Apr 2014 Posts: 138
|
PID controller PWM duty cycle.... |
Posted: Tue Oct 09, 2018 12:44 pm |
|
|
Hi All,
I'm working on a PID controller project that will control the temperature in a thermal chamber. I'm at the point now where I'm ready to actually start controlling the chamber heaters, so I need to think about converting my PID output to a PWM duty cycle. I believe I want the duty cycle to vary from 0% to 100% depending on the magnitude of the PID output, so it seems that I need to calculate the maximum PID output in order to calculate a duty cycle fraction. This will determine the duty cycle value. But, it seems that if my kp, ki, and kd terms change during tuning, this must be a real time calculation?
PIC is a 18F44k22, and I'm running the PWM in 10 bit mode at 1KHz.
Am I thinking about this correctly?
Jack |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19256
|
|
Posted: Tue Oct 09, 2018 1:03 pm |
|
|
Basically yes, except don't think about fractions. Think of parts of an integer.
You can use an int32, effectively as a 'scaled integer', with the low 16bits being the 'fractional part'. Scale your factors to result in a number that is (say) 1024*65536 for the 'maximum', then just put the high word of this into the PWM. |
|
|
guy
Joined: 21 Oct 2005 Posts: 291
|
|
Posted: Thu Oct 18, 2018 8:52 pm |
|
|
If I'm not mistaken, note that the PWM duty cycle cannot reach 100% (only 1023/1024). To get 100% you would need to either
a. disable the PWM module and output_high
b. invert the PWM output if using the ECCP module |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19256
|
|
Posted: Fri Oct 19, 2018 12:58 am |
|
|
You are mistaken.
If you put out a value equal to the maximum or higher into the pwm duty, the output goes permanently on. |
|
|
guy
Joined: 21 Oct 2005 Posts: 291
|
|
Posted: Fri Oct 19, 2018 1:53 am |
|
|
I'm not helping much am I?
This was my experience from some PICs I worked with. Maybe things changed?
So please explain Ttelmah - how can a 10-bit register (1024 combinations) cover 1025 combinations? Assuming 0 gives 0/1024 duty cycle, 1 gives 1/1024, etc. so 1023 will output 1023/1024...?
Theoretically 1024 should output 100% but it is not a valid value for a 10-bit register. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19256
|
|
Posted: Fri Oct 19, 2018 4:48 am |
|
|
First of all, this all depends on if you are using all 1024 cycles.
The point is that if you use less than this the PWM is specified so that if the duty cycle exceeds the period it will go permanently high. Use PR2=254, and 1020 cycles, and you can get 100%.
Then it does depend on the chip. The older chips which have the split duty cycle register (8bits +2), can only give 1023/1024, if you use the maximum PR2 value. However the ones that have a separate duty cycle register do support going to full on. Several of the newer PIC18's, and the PIC24's.
There is also an issue if you are using 8bit mode, since here there are two 'unchanged' bits. In this mode you either have a little bit 'on' at zero, or 'off' at maximum. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9134 Location: Greensville,Ontario
|
|
Posted: Fri Oct 19, 2018 5:14 am |
|
|
10 bits is 1024 combinations, of which 0b0000000000 is a valid entry, so 1023 is the max and 1023/1023 is 1, so 100% 'on'. Same as in 8 bit mode where 255/255=1 and is 100% 'on'.
It may be that if the compiler see a value greater than the max allowed for the mode, that it stores the max. IE in 8 bit mode 255 is the max...your program tries to use 257. The compiler is smart and stores 255.
Decades ago when using the 16F877 for a realtime helicopter, I coded max and min values to be sure my 'math' didn't exceed legal PWM values.
Back to the original post though, you will find that 'temperature control' is a very lazy function, IE slow.... You'll probably fine you can just have preset values, say 10, 20, 30% 'levels' and have very good control over the temperature. Modern stoves do that, using 8 bit mode. It's quick, easy, saves memory, allows for cheaper controller but still has acceptable performance.
While you don't say how big the 'thermal chamber' is, you'll need multiple temperature sensors and a fan or two. BTDT !
Jay |
|
|
|