Saturday, 23 April 2016

IIR Implementation of Low Pass and High Pass Filters on a Microcontroller or DSP

This post describes the steps involved in designing a digital filter and writing its equivalent code to be executed on a Microcontroller or Digital Signal Processor. This post will contain the derivation of the discretization process of a First order Low Pass Filter and will also contain the experimental results of the First order Low Pass and High Pass Filters. 

1. The s-domain Transfer Function:
Any continuous-time filter can be represented by an equivalent s-domain transfer function, which signifies the relationship between the input and output of the filter (In terms of Magnitude and Phase) throughout the operating frequency spectrum. The s-domain transfer functions can be obtained by performing frequency domain analysis to an electrical equivalent of the filter and finding the relationship between the input and output.
The s-domain transfer function of a First order Low Pass filter is:



2. Various Discretization Methods:
To convert the s-domain transfer function to it's equivalent z-domain transfer function, some of the methods that can be used are Forward/Backward Euler or Tustin's Method. These methods are used to map the s-domain factors to their equivalent factors on the z-plane, so that the continuous-time domain transfer function is effectively converted to the discrete-time domain transfer function.

i. Forward Euler:
ii. Backward Euler:

iii. Trapezoidal (Tustin):

Ts = Sampling interval

3. The z-domain Transfer Function:
While designing the filters, I have considered using the Tustin's method because it provides a better dynamic response. But any of the other methods mentioned above can be used, provided you compromise on the dynamic response of the filter while reducing the execution time of the filter equation by using any of the other 2 methods.



By taking inverse z-transform,


4. Formation of difference equation from the z-domain Transfer Function:
The transfer function cannot be directly executed in code, and has to be formed in terms of an executable equation. By solving the transfer function in terms of Y(z) and by applying properties of z-transform, the final executable equation in terms of y(n) can be found out.
While solving below, two constants A and B are assumed to replace the coefficients in final z transfer function stated above.



Applying the properties of z-transform,
Final executable equation:


5. Code:
The key to implement a filter with a good dynamic response is to have a very small sampling interval, Ts which is dependent upon the bandwidth of your system and the number of samples that you need to record per Hz (samples/Hz). The sampling interval should be repetitive and should not vary with time. A varying sampling interval will cause the approximation to fail and thus result in misbehaviour of the filter or may reproduce poor dynamics. It is ideal to start a Timer interrupt at a sampling frequency (Fs), which gives us the sampling interval (Ts) to be used in the filter equation. Initializing an interrupt ensures that the sampling interval will be fixed and periodic. If the signal is going to be read on the ADC of the MCU/DSP, then the vector address of the Timer ISR should also contain the routines to read the required ADC channel along with the code to execute the filter response, and then the code to write the processed variable on a DAC (If required). The code written is not specific to any MCU/DSP and can be ported to a suitable target device.


/*
IIR Implementation of First Order Low Pass Filter
*/

float x_n, x_n1, y_n, y_n1;
float Fc, wc, Tc, P, Q;
float Ts = 0.0000250; // Example sampling interval = 25 usec

void main()
{
//Peripheral Initialization

//Filter Variables Initialization
x_n = 0.0;
x_n1 = 0.0;
y_n = 0.0;
y_n1 = 0.0;

// Filter Properties : Fc = 300Hz (Example Fc)
Fc = 300.0;
wc = 2.0*3.141592*Fc;
Tc = 1.0/wc;

// One time co-efficient calculation
P = Ts/(Ts + 2.0*Tc);
Q = (Ts - 2.0*Tc)/(Ts + 2.0*Tc);

//Start Timer Interrupt
}

interrupt TIMER_ISR()
{
//Read ADC, Assign value to x_n

y_n = (x_n + x_n1)*P - y_n1*Q;
//Reassign values for next iteration
x_n1 = x_n;
y_n1 = y_n;

// Write value of y_n to DAC
}

High Pass Filter:
By using the same approach as above, discretization of a First order High Pass Filter s-domain Transfer Function yields the following results.

s-domain Transfer Function:

z-domain Transfer Function:
Final executable equation:


/*
IIR Implementation of First Order High Pass Filter
#Initialization remains the same as Low Pass Filter
#Only coefficients and equation changed
*/

// Coefficients
P = (2.0*Tc)/(Ts + 2.0*Tc);
Q = (Ts - 2.0*Tc)/(Ts + 2.0*Tc);

// High Pass Fiter Code
y_n = (x_n - x_n1)*P - y_n1*Q;
x_n1 = x_n;
y_n1 = y_n;

6. Results:

The digital filters are implemented on the C2000 series TMS320F28027's LaunchPad. The filter variables are stored in a buffer and plotted on a graph in Code Composer Studio v6.0.1 instead of writing on a DAC.
A function x(t) = 0.80*sin(2*PI*f*t) + 0.20*sin(2*PI*3f*t), where f = 500 Hz is passed through the filters to observe following response:

Friday, 23 October 2015

A 2 HP Class-E Chopper for a Separately Excited DC Shunt Motor

The Electrical laboratory at my college required a Class-E chopper in order to demonstrate the Armature Voltage control method of a DC Shunt Motor using PWM to the students during practical hours. This post gives a brief description of the construction of the Chopper.


It consists of 3 units:
  • Control and Gate Drive PCB
  • Power Stage PCB
  • Auxiliary Supply
1. Control and Gate Drive PCB :


The control structure is built around the ATMEGA16A microcontroller and it is responsible of taking user inputs (Stop, Forward, Reverse, Duty Cycle). It is also responsible for latching and controlling the gate drive signals to select the direction of motor rotation.

The gate drive type is a isolated gate drive for each IGBT. Hence, there are 4 individual gate drivers (TLP250) for each IGBT on the Power Stage of the chopper. The isolated supplies for each gate driver arrive at the control board, and the gate drive signals are sent to the respective IGBTs on the Power Stage. The Chopper is switched at 3kHz.

Since this setup is for demonstration purposes of the Chopper, the armature current (Ia) is tapped via a WCS2702 hall effect CT. Even though the full load current of the motor was 8 Amps, it was supposed to be seldom loaded, hence I chose to use a CT of +/- 2 Amps which would have a higher sensitivity and wouldn't require any amplification in order to observe the waveform. Also, the no load current would be within the linear range of the CT and the current waveform could be observed without any deformation/saturation.

2. Power Stage PCB :



The Power Stage is a Full Bridge constructed out of 4 IKW75N60T (75 A/600 V) IGBTs. Although the body diode of the IGBT is sufficient to freewheel the inductive spike generated by the armature during every switching cycle of the converter, I have placed 2 MUR460 ultrafast diodes parallel to the drain-source junction of each of the IGBT. Each IGBT has a RC snubber made out of 0.082uF/400V polyester capacitor and a 4.7E/5W resistor. The IGBTs are placed under the PCB on a heatsink (Mounting screws visible via holes on the PCB).

3. Auxiliary Supply :


These are 4 linear supplies isolated from each other (separate ground point) in order to fulfill the power requirements of the control logic and the isolated gate drive circuitry. The auxiliary supply was initially supposed to be designed on a flyback converter having isolated secondaries, but going towards the simpler solution of 4 separate linear supplies seemed a feasible option due to lack of time.

05V/750mA x 1 - Microcontroller, Switching Logic, Buffers, LCD
15V/750mA x 1 - Power Stage Low-Side Gate Drive
15V/500mA x 2 - Power Stage High-Side Gate Drive

4. Operation:

The connections were made according to the above block diagram. The field winding of the motor was excited directly via a 200V DC Supply. The Chopper was given the same supply as it's input.
The armature of the motor was connected differentialy between the Full Bridge.

User inputs:

1. Stop
2. Forward
3. Reverse
4. Duty Cycle (Potentiometer based)



Final Setup

Upper Waveform: PWM Output at 70% Duty Cycle (5V Logic)
Lower Waveform: Armature Current (Ia) tapped via WCS2702

Forward Motoring (Ist Quadrant)

Reverse Motoring (IIIrd Quadrant)

*Sorry for the poor quality images. All the photos were taken in haste.

Monday, 25 May 2015

Design of a 30 Watt Boost Converter using the UC3843 (Part 1 - Converter Design)

The Boost Converter is a non-isolated DC-DC converter topology which is used to derive output voltages that are greater in magnitude than the converter's input voltage. There are numerous articles available to understand the functioning of the boost converter and this post specifically contains detailed steps required to design and construct it.

Converter Specifications :

Vin (min)  - 20 V DC
Vin (max) - 30 V DC

Vout - 75 V DC / ( 0.4 A )
Switching Frequency ( Fs ) - 80 kHz

Selection of the switch-mode controller :

In order to select the switch-mode controller IC, it is essential to find the maximum duty cycle at which the converter is expected to operate. The converter will operate at the maximum duty cycle at minimum input voltage and at rated load on the output. The converter efficiency is assumed to be 0.8 i.e. 80%.
 [1]

Hence, a controller IC needs to be selected whose duty cycle can reach the maximum required duty cycle whenever required. For that reason, the UC3843 converter IC is selected. It is a very versatile IC which can be used to control numerous topologies and has a provision to implement current-mode control/ voltage-mode control/ both.

Calculation of the desired Inductance :

While the converter operates, the inductor is subjected to a continuously varying current that flows through it. The ripple current is the maximum allowable swing between the current magnitude with respect to the DC current. Since it is a boost converter, the ripple current through the inductor will be the product of the output ripple current and the converter gain. The ripple current is considered 0.2 i.e. 20% of the rated output current. 

 [1]

After calculation of the maximum allowable ripple current, the minimum value of the inductor required to stay within the limits of the ripple current can be calculated from the equation below. If the inductor is increased beyond this value, it will lead to further decline in the inductor ripple current.
 [1]

Inductor Design :

Once the value of the required inductance is calculated, it is time to move on to the the calculations for the physical design of the inductor. The method used for designing the inductor is called the Area-Product method. 
The Area-Product (Ap) of any core is the product of the Effective Cross Sectional Area (Ac) and the Window Area (Aw). i.e. Ap = AwAc

While designing any inductor, it is important to know the following parameters :
  • Maximum Current
  • Frequency of Operation
  • Nature of applied voltage ( Sine, Square, Triangular, PWM, etc. )
The maximum current that the inductor will be subjected to can be found out by the following formula. The inductor has to be designed for this current rating or a higher current in order to avoid core saturation.



The inductor is responsible for storing a certain amount of energy that needs to be free-wheeled during the off-time of the converter to ensure proper voltage regulation at the output. This is the amount of energy that needs to be repetitively stored in it during every switching cycle and signifies the power handling requirement of the inductor.

 [2]

This approach goes by calculating the minimum required area product of a ferrite core to successfully achieve the power handling requirement of the application.

All the variables in the equation are handled in the MKS system.

E = Energy handling required in Joules

Window Utilization Factor (Kw)
Some of the window area in which the windings of the inductor are placed is occupied by the bobbin/former, insulation/ Mylar tape and also the enamel insulation on the magnet wire. It is safe to assume that only 60% of the window area will be available to accommodate the windings. (Kw = 0.6)

Crest Factor (Kc)
The crest factor is the ratio of the peak value to the RMS value. The crest factor is dependent upon the type of waveform that the inductor is subjected to.

Square   : 1.0 
Sine       : 1.414
PWM     : sqrt(1/Dmax)

Since, the boost inductor is subjected to a PWM waveform with Dmax = 0.7866 (78.66%) for the required application, the resultant crest factor is Kc  = sqrt(1/0.7866) = 1.127

Current Density (J)
A lower current density results in lesser copper losses and reduced winding heating during operation. Inductors are designed to operate at current densities ranging between 3 A/sq.mm to 5 A/sq.mm. Operating at a any lower current density is not a problem, but will result in increased winding area requirement. J = (3 A/ 10^-6) = 3 x 10^6

Operating Flux Density (B)
The inductor/transformer is generally designed to operate at 25% of the saturation flux density of a core. Since the core material is ferrite, it is safe to operate the inductor at 0.2 Tesla with 20 kHz switching frequency. When the switching frequency is increased beyond 20 kHz, the operating flux density is de rated in order to avoid switching losses at higher frequencies. I have considered operating the inductor at 0.13 Tesla for the application switching at 80 kHz.

 [2]

The pot core P30/19 has an area product of 10160 mm^4, cross sectional area (Ac) of 136 sq.mm, winding area (Aw) of 74.7 sq.mm and is sufficient to handle the amount of power required since it's area product is greater than the required area product. Number of turns is given by :

 [2]

Wire gauge selection:
The thickness of the wire is dependent upon the selected current density of operation. The RMS value of the current flowing through the conductor will be the ratio of the peak current to the crest factor.

Irms = Imax/Kc

Hence, Irms = 1.65 A/1.127 = 1.465 A

The cross sectional area of the wire to operate at 3 A/sq.mm with a RMS current of 1.465 A is :

 [2]

A magnet wire of 21 SWG has a cross sectional area of 0.51890 sq.mm and is suitable for use at the specified current density. To check whether the winding will be able to fit into the available winding area, a check satisfying the below condition should be performed.
Condition : Available Area > Required Area

 [2]

The condition is satisfied, and hence the core can be used to wind the required inductor.

Air Gap Calculation :
There is a necessity to introduce an air gap while constructing the inductor and it is required to avoid core saturation at high inductor currents. The calculation for the air gap :

 [2]

Capacitor Selection :

The required output capacitor ( Cout ) can be calculated from a pretty straight forward equation. The output current is driven by the capacitor during the on time of the converter. Considering that the desired output ripple is 20mVpp, the resultant equation is :

 [1]

The output capacitor can be 2 x 150 uF Electrolytic capacitors shunted to each other in order to reduce the ESR exhibited by the capacitors. 150uF instead of 100uF ones in order to compensate for the extra ripple created due to the ESR of the capacitor.


The input capacitor can be 2 x 560 uF Electrolytic capacitors shunted to each other in order to reduce the ESR.

Provision of Feedbacks :

Current Sense Resistor : To include cycle by cycle current limiting in case the current tries to exceed the rated input current.

Voltage Divider n/w : To attenuate the voltage to a desired level in order to feed back the output voltage to the switch mode controller.



Schematic of the Boost Converter

The diodes need to be ultra fast rectifiers to exhibit fast switching characteristics and to minimize switching losses.Also, it should be made sure that the diodes and the MOSFETs have a sufficient PIV/Breakdown Voltage respectively to sustain under transient conditions.
The MUR420/ UF5048 or any other ultra fast diodes can be used. I have used an IRFP250 MOSFET since that was the one that I had at home. Any other suitable MOSFET can be used instead of it.

The detailed explanation of the feedbacks and control structure of the converter will be explained in the second part of this post.

References :

[1] "Basic Calculation of a Boost Converter's Power Stage" Texas Instuments Application Note - SLVA372C

[2] "Design of Magnetic Elements for Switched Mode Power Converters" - Umanand, L., Bhat, S.R.

Monday, 4 May 2015

An Expansion Board for TI's C2000 Series Piccolo LaunchPad



PCB of the C2000 LaunchPad Expansion Board

I have been using TI's C2000 Piccolo LaunchPad from the past 6 months and it's a great tool which is available at an affordable price. I enjoy working with this LaunchPad and have been using it my projects, which generally are focused on Power Electronics applications.

The main charm about this LaunchPad is that it provides isolated JTAG Emulation. Since the C2000 Series Microcontrollers are targeted towards real-time Power Electronics applications, isolation being included in the LaunchPad itself makes real-time debugging of any application safe in the prototyping stage. So you can at least make sure that your PC / Laptop isn't fried while debugging the code if there is any fault on the converter side.

In order to make prototyping easier, and making some sections of the microcontroller compatible to 5V logic devices as well, I thought that I should design an expansion board dedicated to the Piccolo LaunchPad.

Features of the Expansion Board :

1. To make use of the Isolation section provided on the LaunchPad, it is important that the jumpers JP1, JP2, JP3 on it are removed first. Removing the jumpers isolates the grounds of the JTAG Section and the microcontroller (Beyond the isolation boundary). Hence, an auxiliary supply is required to power up the microcontroller side after the disconnection of the jumpers.
The microcontroller side requires a 5 V and a 3.3 V Supply. The 3.3 V Supply is required to power up the microcontroller, the ISO72xx ICs, and the LED drivers on the LaunchPad. It seems that the other 5 V Supply is simply made accessible on one of the header pins, probably to power up any booster-pack connected to the LaunchPad.

The Expansion Board accepts inputs in between 6.5 V - 9 V, which powers up a LM1117-N-5.0 LDO, and it provides the 5 V Supply. A LM1117-N-3.3 is then cascaded after the 5 V LDO and that provides the 3.3 V Supply to power up the microcontroller and it's peripherals.

2. I also intend to use the serial communication modules (UART, SPI, I2C) along with 5V logic devices, hence I routed a 3.3 V to 5 V level translator that I found on Arduino's website (Link). Since I wanted to keep the board single sided and wanted to avoid surface mount components, I decided to use 2N7000 MOSFETs along with 4.7k pull-ups on the transmission rails of all pins. 
In case I need to use the communication pins as standard GPIO, the LaunchPad is ready to communicate with 5 V devices for bi-directional usage of the pins.

Note : The SCK pin of the SPI Module is routed for 5 V logic via a unidirectional buffer, hence the module cannot receive a clock from the other device. Hence, the microcontroller will always have to be used as a master and will not be able to function as a slave. The chip-select function can be implemented in software and any unused GPIO can be used for the purpose.

3. One of my LaunchPad was damaged due to over voltage on an ADC pin. I had initially thought that there are inbuilt protection diodes on each ADC input. I was mistaken and found that after I read the reference guide of the ADC Module (Link).

Internal ADC structure of a 28x Series DSP

6 ADC Channels are made available on header pins as well as screw-terminals. 3 of the channels  
( B3, B1, A0 ) have been added diodes to clamp positive and negative over-voltages to +/- 0.7 V with respect to Vcc and GND using UF4007 ultrafast diodes. These channels should be connected to analog voltages which have high chances of transient voltages in their signals ( Eg. Current Transformers).
The remaining unprotected channels ( A1, A3, A7 ) can be connected to analog signals with a predictable output which lies within limits and that are less susceptible to transients.

All the 6 channels routed via the expansion board receive the analog signals through a 10 ohm Resistor.

4. The pins GPIO0 - GPIO5 can be also used as EPWM pins. These pins are connected to the unidirectional buffer IC 74HC7541 and always need to be configured as output pins. The buffer IC makes the signal matched to 5 V Logic and also increases it's driving capability. Also while using the GPIOs as EPWM outputs or while operating the SPI module, there are chances that the signal may induce noise in it from the power supply. Hence I added a 4x10mm ferrite bead (That was the smallest that I had) and a local decoupling capacitor near the buffer IC to filter any transients created in the supply due to high frequency switching in the IC.



Silkscreen and LaunchPad template


Silkscreen and Copper

I will be making the PCB within a few days and will keep you updated.