Friday, March 15, 2019

Second failed attempt at linear model of an LCL filter

In my previous blog post, I had described how trying to approximate a differential of Ldi/dt with a linear model is probably impossible. However, there was a flaw in the first linear model that I attempted between the filter capacitor voltage, the grid current and the grid voltage. I had chosen a case where the grid current were always in quadrature to the grid voltage and the filter capacitor voltage has the same template as the grid voltage.

So, to check out if the flaw would result in any improvement, I used a training model where the filter capacitor voltages were different from the grid voltages both by magnitude and by phase. The result was an equally flawed linear model.

vcf = -0.2091*igrid_ref + 1.1028*vgrid

As is evident, this model is even worse that the previous one. Because of the phase angle variation, the model has chosen a larger multiplying factor for the grid voltage of 1.1028 but has tried to compensate with a larger resistance of 0.2091 which is useless in the face of the multiplying factor to the grid voltage.

So, the conclusion is that trying to approximate a differential in this manner by a linear model using linear regression is quite useless. So, I need to step back and figure out what the major challenge with controlling the current through the grid inductor of a LCL filter is.

The transfer function between the inverter and the final grid current is a 3rd order polynomial. Physically, the challenge is in accurately regulating the filter capacitor voltage so as to regulate the grid current to a desired value. The simplest control that can be achieved in this case is controlling the current immediately at the output of the inverter. In that cases, the transfer function between the inverter current and the inverter voltage (or modulation index) is a first order polynomial.

The problem is that inverter output current and the grid current will differ by a current that flows through the filter capacitor. Again, the problem would boil to estimating the current flowing through the filter capacitor. So, to avoid any form of estimation either of current or of voltages, how do we achieve a closed loop control of the final grid current without any synchronous transformation?

The reason why I avoid synchronous transformation is that it transforms the grid current to dc values. This assumes that there are no harmonics are intended to be injected in the grid currents because if the converter is a harmonic filter and is expected to inject harmonics into the grid, the synchronous transformation will have to be performed for every harmonic.

So with these objectives laid out, I will find better ways to control the inverter with advanced concepts of machine learning.

Monday, March 11, 2019

Failed first attempt at linear model for inverter with LCL filter

In the previous blog post, I had attempted to create a linear model to predict the reference for the filter capacitor voltage such that the current injected into the grid will track it's reference. And when I saw the linear model, I had the feeling, it would probably not work. And it didn't.

The linear model that the LinearRegression module with scikit.linear_model gave me was:

vcf_ref = -0.01157 + 0.04176*igrid_ref + 1.0587*vgrid

Seems elegant and when I tried to fit it to the test set, I got a cluster that fell along a linear line. All this seemed wonderful as an application of machine learning to power electronics. But looking at it from a power electronics perspective, what did I get?

vcf_ref = vdc + Remu*igrid_ref + K*vgrid

Where vdc is a dc bias that apparently the linear model feels is needed. Though there should not be a dc bias needed in an ac system unless there is a dc component due to some severe unbalanced harmonic load, this dc bias is fairly small and can be neglected.

The Remu is actually what the linear model approximated the inductor as. The actual equation is:

vcf = vgrid + igrid*R + L* digrid/dt

The linear model has approximated (igrid*R + L* digrid/dt) by Remu (emulated resistance). And this Remu = 0.04176 which is very small given the fact that an inductor is being modeled by this resistor.

The next major problem is the 1.0587*vgrid. We are scaling the grid voltage by 5.8%. And compared to the other two factors, this is the major factor. So really, all that this linear model does to try to create a reference filter capacitor voltage is to scale up the grid voltage by 5.8%. And this is totally inadequate.

Suppose, the current injected into the grid will have to be in-phase with the grid voltage and this would be a requirement for many distributed energy resources. The filter capacitor voltage would need a phase advance with respect to the grid voltage. Or else, it would not be able to produce a grid current in phase with the grid voltage. Because the current through the inductor would be in quadrature and lagging behind the voltage drop across it.

With the above linear model, the filter capacitor voltage would be scaled up by 5% and so in phase with the grid voltage. The voltage drop across the grid inductor would be in phase with the grid voltage and therefore, the current injected into the grid would be lagging behind the grid voltage by 90 degrees. Which is not what we want.

My hypothesis is that what I need is a more detailed and accurate model for the differential operator. And that may need more complicated techniques that a LinearRegression. But before jumping to that, I would like to exhaust any possible solution that linear models have just to gain knowledge.

In all fairness, the linear model that I got from LinearRegression above was a flawed one. Because what I did to train the model was simplistic. To begin with, I had chosen the equilibrium point to be a modulation signal that was the grid voltage scaled by the dc bus:

m = vgrid/vdc

So, the filter capacitor voltage is very close to the grid voltage in magnitude and most importantly in phase as well. To this modulation signal, I had added a random disturbance.

m + mrand*sin(wt)

Here was the mistake - I was giving the random modulation signal perturbation the same phase as the modulation signal. And so, the filter capacitor voltage voltage only changes in magnitude.

And this is exactly why the linear model we ended up with has the major component that scales up the grid voltage by 5%. Because the linear model saw that in a vast majority of cases, that's all there was to it - the capacitor voltage scaled up by small amount and the grid current in quadrature with the grid voltage. So, the linear model assumed this was the ultimate truth and gave that very convenient equation.

So, my second attempt would be to train the linear model making sure that the filter capacitor voltage differed from the grid voltage by not just magnitude by also in phase.

So, check out these two current snapshots in the previous training case:

In both cases, the current is always 90 degrees lagging behind the grid voltage because filter capacitor voltage is in-phase with the grid voltage. So, the linear model thinks this is the norm, this is always how it is.

In contrast, now I am going to add two components to the random shuffle to the modulation index - an in-phase component and a quadrature component. So, the grid current will have a varying phase angle difference with the grid voltage. Below is an example. With this, I am going to try and retrain the linear model. Probably still won't work, but let's see how it changes.

Saturday, March 9, 2019

First attempt at a linear model for an LCL filter

In the previous blog post I described how I am trying to use machine learning to create a functional relationship between the grid voltage and the filter capacitor voltage with respect to the grid current for a dc-ac converter connected to an ac grid through an LCL filter.

So, if that is a bit of a mouthful, here is what it is in a flow:

Inverter - Inductor - Filter Capacitor - Inductor - Grid

The idea is can we determine how much the voltage across the filter capacitor  should be for a particular value of current injected into the grid? The grid voltage can be measured. So, we have:

1. Measured value of grid voltage
2. Reference of current injected into the grid

We need a reference for the voltage across the filter capacitor. Why? Because we have already succeeded in controlling the voltage across with a state feedback controller based on Linear Quadratic Controller.

I ran a free simulation. What that means is I adjusted the modulation index of the dc-ac converter to random values and checked what would be the values of grid voltage, grid current and filter capacitor. All this is for training a machine learning algorithm. And I showed in the previous blog post, the plot of this.

Now, to the next stage. Take the output file and convert it to a .csv file that Pandas can read. Pandas is a data processing package available in Python as part of the PyData library.

This is an extract of the DataFrame which is what you call a database in Pandas:

Time Ammeter_Lfinv Ammeter_Lfgrid Voltmeter_Cfilter Voltmeter_Vgrid inv1trainer_modsignal pwm_carr pwm_s1logic
0 0.00001 -2.968437 -0.003901 -0.095838 0.640844 2.368476e-17 0.200 0.0
1 0.00002 -5.663500 -0.014377 -0.309820 1.217527 2.368476e-17 0.400 0.0
2 0.00003 -8.951290 -0.037764 -0.734868 1.922236 4.946608e-03 0.602 0.0
3 0.00004 -11.932744 -0.070674 -1.276453 2.562736 4.946608e-03 0.800 0.0
4 0.00005 -14.905593 -0.116192 -1.964789 3.203073 9.874666e-03 1.000 0.0

Just the top 5 rows. The data frame has a total of 199999 rows. So that's quite a bit of data.

With this, we are now going to try to create a linear model. What we are trying to do is predict the voltage across the filter capacitor. This will be our output 'y'. The inputs are the grid current which we call x1 (input 1) and the grid voltage x2 (input 2). What we want is:

y = b0 + b1*x1 + b2*x2

If we can achieve this, we have a way to calculate the reference capacitor voltage for any given grid voltage and a reference grid current.

We use linear regression. Linear regression will build exactly the above model with data that we pass to it in order to generate the co-efficients b1 and b2 and the intercept b0. The theory behind it is fairly mathematically intense and maybe I will read about it in detail some day. But in short, linear regression generates these constants by trying to minimize the residual sum of squares for all the data points that we are using to train this model:

RSS = (y1 - b0 - b1*x1_1 - b2*x2_1)^2 + (y2 - b0 - b1*x1_2 - b2*x2_2)^2 + ... +
(yn - b0 - b1*x1_n - b2*x2_n)^2

When there are n samples. We have a total of 199999 samples. But we don't want to use all of them. We want to split them into a training set and a testing set. So, let's do a 70%-30% split - 70% of the data is for training and the rest we will use for testing.

I am using the LinearRegression package available with scikit-learn. As I am getting started with machine learning, I am going to try to keep this simple. I will have to add complexity as I go on. For now, this is a good start.

This is what I do:

y = df['Voltmeter_Cfilter']
x = df[['Ammeter_Lfgrid', 'Voltmeter_Vgrid']]
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)
inv_model = LinearRegression(), y_train)

The next part is to check how accurate the model is with test data:

predictions = inv_model.predict(x_test)
sns.scatterplot(x=y_test, y=predictions)

And this is a scatter plot of the prediction of the capacitor voltage versus the actual voltage from the simulation. Most of the scatter lot is in a straight line which is a good sign. But the bad news is that there is some deviation and that deviation is of the order of magnitude of 50V. The filter capacitor and the grid are separated by a small inductor of the order of around 1 mH. Such a difference in voltage of 50V could produce huge spikes in the current.

The final equation turns out to be:

vcf = -0.01157 + 0.04176*igrid + 1.0587*vgrid

Even before trying to simulate this, I am pretty sure it won't work. But still this has been a good first step as I try to apply my rudimentary machine learning in power electronics. I hope as time goes by I will be able to apply more accurate models that can actually be of use.

Wednesday, March 6, 2019

Training a linear model to predict reference converter voltage

After the last post where I thought of mapping a function that links the grid current with the voltage across the filter capacitor:
vcf = f(ic)

Or, if I include the grid voltage as well:
vcf = f(ic, vgrid)

For now, let us try to create a linear model for this function f. And let us just use Python's in-built linear model function to achieve this. I will try to build some theory around it a little later if the results seem promising.

So, to create a linear model, the first step is to generate some training and testing data. For this I am running the simulation in open-loop. To begin with, let us consider a perfectly sinusoidal system without harmonics. So, the way I am doing this training is as follows.

The base case will be when we generate a modulation index which is such that the filter capacitor voltage will be approximately equal to the grid voltage.
m = vgrid/vdc

Where vdc is the dc bus voltage and that is needed to make sure the modulation index is between -1 and +1. From this state of equilibrium, I will add a random sinusoid. So, I created a random array:

This will have random floats between 0 and 0.1. I add to the modulation index:
m = vgrid/vdc + random[i]*math.sin(phase_angle)

The counter i will be initialized to 0 but every 0.1s, will be incremented to fetch the next random number. This is applied to PWM. The result is a waveform like this in which I have plotted - the grid voltage, the voltage across the filter capacitor and current injected into the grid. Every 0.1s, the current changes quite often drastically as the modulation signal has changed with the new random value.

A zoom between two changing values:

I am going to let this simulation run to it's limit of 1s. After that will extract the data into a Pandas data frame. And apply machine learning algorithms.

There is a very possibility that I am doing may not be current. We may need help in the control. But I feel this is a great way to learn about a system.

Monday, March 4, 2019

State feedback control of inverter with LCL filter - preliminary investigation

So before I discussed the control of a UPS which a dc-ac converter (single phase for now) with an LC filter at the output. The objective of the controller was to regulate the voltage across the filter capacitor. The state feedback controller was designed using Linear Quadratic Regulator (LQR) and in addition to that a correction was added to the reference to get rid of the steady state error.

The next stage is to control a dc-ac converter with an LCL filter at the output with the objective of the controller being to regulate the current through the final inductor that connects the system to the grid. This is a problem that I have never been able to solve comprehensively and am now going to take another crack at it.

The difference between control of a UPS and the current control of an inverter with LCL filter is the sensitivity of the output with respect to the input. In the case of the UPS, the input is the output of the dc-ac converter which is a voltage in the case of a Voltage Source Converter (VSC). The output of the system is the voltage across the filter capacitor. These ratio of the output to the input is close to unity. And therefore, the controller was much easier to design, the behavior of the controller system much more predictable and the correction of the steady state error was fairly easy.

But, in the case of a dc-ac converter with LCL filter, the input is the converter voltage while the output of the system is the current through the final inductor.  The ratio of the output to the input is very low. Therefore, the controller is quite sensitive. Small changes in the modulation index can result in large changes in the output/grid current.

Suppose the filters were named as follows - Lf the inductor immediately after the converter, Cf the capacitor after Lf, and Lc the inductor that connects the system to the grid. The equation that defines the grid current is:

vf - ic*Rc - Lc*dic/dt - vgrid = 0

The difference in the voltage (vf - vgrid) is what defines the grid current. So, if the state feedback control law is:

m = k1*(if - ifref) + k2*(vf - vfref) + k3*(ic - icref)

The problem is with the second term. The second term will dominate the controller action because of the nature of inputs and quite interestingly the value of k2. I have been using Scilab again to design the state feedback gains and the results are quite interesting.

When the weighting giving to the state variables was Q = diag(0.1, 0.1, 1) which means low weightage to if and vf and high weightage to ic, the state feedback gains were:
k1=3.1852838, k2=4.3873536, k3=7.2589122

Given the magnitudes of vf with respect to if and ic, a value of k2 as above will result in dominating the control output. So, it turns out that the reference for vf becomes critical.

We know the reference for ic as that is the objective of control. It should be possible to do:

vfref = vgrid + Lc*dicref/dt

But you never want to use d/dt in control because it magnifies disturbance. For a sinusoidal fundamental harmonic reference icref, it could be possible to do a decomposition and peform:

vfref_real + j vfref_imag = vgrid + jwLc*(icref_real + icref_imag)

But then, the control cannot be applied to harmonic compensation. So, the question - can we map a function:

vfref = f(icref)

I have only considered using electrical network laws and their approximation so far to do something like the above. But now, with my limited forays into data science and linear regression, this seems like a ripe problem for machine learning. Can we fit a model that is reasonably accurate based on simulations from a vast number of cases? This is what I am going to try to do next - combine machine learning with power electronics!

This a simulation result of what I have achieved so far in terms of current control. As with the previous case - considerable error but most important stable response within a cycle.  A PI controller could take 6-8 cycles to stabilize in contrast. If we solve the problem of steady state error, we could have an incredibly robust controller!

Friday, January 25, 2019

Correcting the steady state error in LQR

So in the previous post I had posted on the state feedback control of a single phase UPS with the state feedback gain designed using Linear Quadratic Regulator (LQR). There were positives and negatives. The positive was that you didn't need synchronous transformation - the control was applied in stationary reference frame. The negative was that there was a steady state error. The main cause for the steady state error was the choice of reference for the inductor current. The reference for inductor current was assumed to be equal to the load current, but this neglects the current drawn by the filter capacitor.

The straightforward way to solve the steady state error issue would be to try to generate a more accurate reference for the inductor current. So, the equation for inductor current would be:

if = Iload + icap

To estimate the filter capacitor current icap, the reference output voltage which is the desired voltage across the filter capacitor can be used:

if = iload + Cf d vo_ref/dt

This can be done and has been done by many many researchers in the past. I too have tried to implement the above. The only tricky part is the d/dt. In control, you do not want to perform differential - never ever. For the simple reason that d/dt magnifies any noise that may be present in the system. So we find ways of generating the signal without d/dt.

Anyway, I have always been thinking - do we need to do this? We know there is an error. So, let's first analyze this error and figure out what it looks like. Assuming the desired reference output voltage is perfectly sinusoidal, this was the tracking performance we saw from the last post.

Next, let's plot the error.

The error between the desired reference output voltage and the actual output voltage is a sinusoidal waveform. Sinusoidal waveforms are nice - periodic, nice shape etc. Most importantly, a sinusoidal waveform is an indication that the reason for the error lies mainly with the choice of the magnitude. Why? If the error was let's say a constant, this would mean the reference and the actual waveforms will be criss-crossing each other. Because, the error is defined as v_ref - v_actual. If the error was some other weird shape, it means it is difficult to predict how the actual voltage tracks the reference. But with a sinusoidal waveform such as the one above, the reference always has a larger magnitude and the actual voltage will differ only by magnitude and not by phase angle. Try drawing some waveforms and check it out for yourself.

So, the next part - the solution. A sinusoidal waveform is nice because it can be defined by several parameters - peak, RMS, frequency etc. The frequency of the sinusoidal waveform will be the same as the system frequency. Well, this might be worth a check. But peak and RMS are related. And RMS is very convenient to calculate.

RMS = Integral ( sqrt(err*err/T) )

Here, T is the time period of the waveform and is also the time interval of integration. Now, there a few ways of doing the above integration. Some (and this includes me before) prefer to do a moving window integration. So, they collect some number of samples - 100, 1000 etc and keep moving the waveform across this window. And they calculate the sum of squares over this window (or array of values). This works. But quite inefficient from the perspective of implementation, if you were to implement in a DSP or FPGA.  You need an array of 1000 elements - not very nice.

A simpler way of doing this integration is to declare a timer for the time period T. Start the sum with 0. And add squares to this sum until the time period expires. At this point, use the sum to calculate RMS and reset the sum and also reset the time period. This is convenient to do using a DSP or FPGA. The result of this is:

And as you can see the RMS has been added to the waveform. It updates every 0.01 seconds. The larger this time period is, the better for accuracy of calculation of the RMS. But is bad for the performance of the controller as the RMS value will be of the previous state until the time interval expires.

So, now what of it? If we know the RMS value (or incidentally the peak value), we can now scale up the reference voltage by that value. The reason, we know this error is going to appear anyway. So, if vref if what we want and verr is the error we get, by manipulating vref to be:

vref' = vref + verr

And applying vref' to the state feedback, the error verr that will anyway result, will now produce a v_actual that is almost equal to v_ref. Check this out:

And you see the difference. After 0.01s, the peak error (RMS*1.414) is added to the desired reference. The result, the actual voltage now catches up with the desired reference that we wanted to begin with. And you see, the tracking is almost perfect, the error is close to zero - tough to see the waveforms apart.

The true test will come when the reference has harmonics - let's add a 5th harmonic component to the desired voltage output.

The tracking is way better than the first 0.01s when the raw state feedback was working. The errors are mainly at the parts where the d/dt of the desired voltage is very high. It is important to note at this point that a controller cannot overcome the design constraints of a system (in this case an inverter). Therefore the maximum d/dt that an inverter can achieve is limited by the choice of dc bus voltage, filter inductor and filter capacitor. So, are those errors the result of our control design or just the way the inverter is and whatever may be the answer, what do we do about it? This will be a future post.

For now, the state feedback controller has achieved both targets - very close tracking with negligible error and also hassle free control implementation - no synchronous transformations etc. Any waveform can be tracked - pure sinusoid or with harmonics.

Saturday, December 8, 2018

Linear Quadratic Regulator control of a single phase UPS with Scilab

So I am gradually getting used to Scilab. The documentation for Scilab is a bit scarce. The first link that I use as reference is the Scilab online help:

Another link that's good for getting started with controls in Scilab is:

Anyway, to get started, I chose a problem that I spent a good amount of time during my PhD - control of a UPS. Typically, a UPS can be controlled with PI controllers in the synchronous reference frame, a topic that I have covered in a tutorial:

Using multi-variable control has some significant advantages - you do not need to design multiple nested loops ensuring that the inner loop is super fast and the outer loop is reasonably fast. Here, there is a single loop. To begin with, a system, in the case, a single-phase UPS, which is an H-bridge inverter with an LC filter at the output, has to be represented in the state space form:

dx/dt = Ax + Bu
y = Cx + Du

Here, x is the state vector. y is the output. And u is the input. There is a very detailed theory behind state space analysis, and I would encourage you to read, if you need to know more. But, for this post, the state variables are those variables that have dynamics - inductor current and capacitor voltage. So,

x = [if Cf]

This is the code snippet for the entire LQR design:

To achieve a 120V RMS voltage output, I have chosen a 300V dc voltage bus (which is a bit of an overkill, but more on that later), a 1milli Henry inductor and a 200 micro Farad capacitor. I have written the dynamical equations at the top as comments and these can be expressed as state space equations with A, B, C, D matrices below that. I will be implementing the state feedback in discrete time and the sampling rate is 20 microseconds.

The first part - controllability of the system. For a system expressed in the state space form, the controllability matrix is:
[B AB]

Again, control theory. Without getting into details, the matrix B, AB, A^2B, .... define the space of vectors that can be achieved with a finite input. So, for a system with 2 states, B and AB, will map a state space. What this means is that the 2 vectors that are B and AB, will be like the x-axis and y-axis of this new space. In the 2 dimensional space, the x-axis and y-axis can be used to express any vector in 2 dimensions. And when any 2 vectors can be used to express any other vector in a 2 dimensional space, the 2 vectors are said to span the space. And if x-axis (1, 0) and y-axis (0,1) are put together as a matrix:
[0 1,
1 0]

This matrix has dimension 2 - which means 2 linearly independent vectors. Linearly independent vectors are vectors who cannot be projected on each other. Mathematically, of course, the definition is a bit more rigorous. A set of vectors v1, v2, ..., vn are linearly dependent if there exists a set of scalars a1, a2, ..., an such that:

a1*v1 + a2*v2 + .... + an*vn = 0

With all "a"s not being zero. If such a relation cannot be expressed, the vectors are linearly independent. Anyway, coming back to the matrix [B AB]. If this matrix has dimension 2 for our system of 2 state variables, the system is fully controllable. And if you try to execute the variable "controllability", you will find the rank is 2. Which means our UPS is fully controllable. And that is good news.

So, this means that there exists a feedback matrix K, which can be designed in various ways one of which is Linear Quadratic Regulator (LQR), such that the closed loop system:

u = K (xref - x)

Will be asymptotically stable. This means, when influenced by a disturbance, the system will return to a finite region around the reference (xref). And that is what we want.

The next part is to define the linear system for which I use the command syslin. This is a command for defining everything - state space representations, transfer functions etc. After that I define a matrix Q and a scalar (actually another matrix but since we have only one input, a scalar) R. These are for the function LQR. To obtain the state feedback matrix K from LQR, you need to solve the algebraic Riccatti equation. This equations takes inputs Q which are the weights giving to the state variables and R is the penalty on the control signal. Notice how I have given a much higher weight to the capacitor voltage vf (1) than the inductor current (if) as it is the capacitor voltage that we want to regulate. The penalty on the control R is 0.01 which is a number I chose by trial and error and I would recommend that you fiddle with Q and R too.

The result is the K matrix. The rest of the file I will talk about in a later blog post as that is a low pass filter I was trying to add after the controller but didn't work.

I take this K matrix and insert it into my simulator control code as:

mod_signal = (9.9966672*(volt_ref - volt_cf1) + 3.2144996*(curr_ref - curr_if1))/300.0

One important thing to note here is the factor 300 by which I divide the control signal. The control in this case is a modulation signal fed to a pulse width control. So it must be restricted between -1 and 1. However, the control as designed doesn't know about this and will try to spit out an actual value. So, to convert this actual value into a modulation signal, I divide it by the dc bus voltage.

This the result. The light blue line is the reference and the purple line is the actual voltage. To begin with, it doesn't track - there is a steady state error. I will address this in detail in a future blog. However, for now it is to do with the reference xref = [ifref vfref]. The reference for the voltage is easy, because we want to regulate it. But the reference for the inductor current is a bit tricky and for now I have assumed it to be the same as the output load current. Hence, the approximation. But, there is something very special with state feedback. Notice how quickly, the tracking begins - almost immediate. With PI control it would take several cycles to get even close. Also, there is no conversion from stationary reference frame to synchronous reference frame. So this is a great way to produce voltages that have harmonics. Check out the next plot.

To generate harmonic voltages with the regular method, you would need several reference frame transformations. Here, everything is instantaneous.

There are a couple of problems - the steady state error is the biggest glaring problem. Besides, a few others like a bit more ripple than expected. This topic is by no means over and there will be more blog posts to come.

Stay tuned for updates. I'll be back!