Hello Shams,
I have only had a preliminary look at your code & circuit, and have not used the Xmega so some of it is foreign to me. I assume you have no problem increasing/decreasing the output voltage if it does not meet your target. I see you are sampling the output volts at 'phase' and reading it at 'involt' . If 'involt' is too low, you need to increase the op volts and vice versa. i assume you can do this? The integer I call 'corr' below will probably need to added somewhere into your control loop - i havn't figured out how that works.
[u:95c578dccf]PI control.[/u:95c578dccf]
PI control, stands for proportional, integral. It is s system of controlling a desired output, knowing what the input error is.
There are 2 correction variables, the P variable and the I variable.
Lets make some assumptions. We will call error the difference between your target ( say 240 VAC) and the actual voltage at a point in time ( say 229) in which case error = 11.
Lets say that you can adjust your output volts with a single or an integer called 'corr' - short for correction. When corr = 0, your output doesn't change. When corr is +ve, the output decreases. ( the error is +ve, the output is too high so we need to reduce output). When corr = -ve, output volts goes up.
[u:95c578dccf]P Variable. (integer or single)[/u:95c578dccf]
To calculate the P variable you need to know what the error is. Say it is 11 volts as above. We can immediately calculate the P variable, with a Pgain of 1 initially.
P = error*Pgain = 11.
corr = P + I
so you can see that P is a correction that is absolutely proportional to the size of the error.
So every second you would measure the error and calculate the corr and apply this to your controller to shift the output in the direction you want. Get this running by itself until it is stable. If the correction is not big enough and doesn't move the output far enough when you deliberately introduce an error, increase the Pgain. to 1.5 or 2 or what ever it takes to get the output moving when an error occurs. ( you can test this with a pot on your involt line, or in place of one of the resistors in the op amp output divider) You will also need to have a play with the sampling speed. i have only used 1 sec as a start point.
As the error gets smaller the P value also gets smaller and therefor the corr will also get smaller and you will eventually arrive at a point where the output is just short of the target (called the offset). Less on - P alone cannot do the job. We need a I value to fix this.
So far our corr =P+I but we don't have an I value yet.
[u:95c578dccf]I Variable.( integer or single)[/u:95c578dccf]
The i value is a correction ( also an integer) based on 'how long' an error has been present. For simplicity lets say that we measure the error every second and every time we detect err>0 we add 1 to the I value.. I starts off at zero.
So every second that there is a +ve error we pump up the I value by 1 count and add it to corr. If the error is negative we take a count off the I value. So the 'longer' there is an error, the bigger the magnitude of the I value becomes. ( it will be a +ve number if the error is +ve and a -ve number if the error is -ve)
Because corr = P+I , every second I increases by 1 count so with time, P will start decreasing as the error decreases but I will keep slowly increasing until eventually we have no error, and therefor no P value left, but we are left with an I value that is exactly correct to maintain the output at the correct value. Its important to grasp this concept.
Incidently, to test your I calculations, you can turn oof the P calculator and just have the I calculator running, and you will slowly see the I value slowly changing until there is no error, and a residual and correct I value. Its real easy to see this happening if you set a slow sample time and a small pump value. You can actually see the I value pumping up or down on your LCD.
To fine tune the I value we need to decide whether to add 1 count every second or 5 counts or 10 counts etc and whether to do it every second or every 100ms or 10ms etc. This is called tuning your loop, and you can see that the P and I values can be calculated absolutely separately and are simply added together to get the final correction.
↧