loompa
Newbie level 3
Im simulating a 3 phase inverter circuit driven via SVPWM method. Im using Arduino for code. As it can been seen in diagrams, my input in as it should be.But output is weird, I dont know is causing it. I reviewed my design, but I cant find the issue after spending alot of time. Kindly review it, I/P signal and O/P signals are given below along with the code and schematic.
Mosfet state table with respect to each sector is shown below.
Mosfet Table
Im using symmetric switching pattern as shown in table.
Switch Table
INPUT
OUTPUT
SCHEMATIC
CODE
Mosfet state table with respect to each sector is shown below.
Mosfet Table
Im using symmetric switching pattern as shown in table.
Switch Table
INPUT
OUTPUT
SCHEMATIC
CODE
Code:
#include <math.h>
int sector_pos = 1; //current sector.
const float freq_sth = 5000.00; // Switching freqency 5000Hz
float tz = ((1 / freq_sth))/pow(10,-6); // Total switching time Tz in (us)
float freq = 50.0; // Variable fundamental freqency
float m = 0.6; // modulation index (Controls max Vref magnitude) 0>=m<=1.
int vdc = 100; //rectified voltage
float timer1 = 0, timer2 = 0, timer0 = 0; //Switching times
float sample_time = freq_sth / freq; // sample_time(per cycle) = switching_time/fundamental_output_freq.
float angle = 0; // (in degrees, must be changed to radians.) Gives us angle to be incremented per 'Tz' seconds.
float base_angle = 360.0 / sample_time; //Base angle will be used to increment angle per cycle.
float timer_div = 0; // Constant in time calculation equations. (refer to t1,t2 equations)
double theta1 = 0, theta2 = 0; // trignometric part of t1,t2 equations, computated seperately to reduce clutering of code in one line.(i.e t1 = timer_div*theta1)
float angle_rad = 0; //Angle in radians.Computed for each 'Tz' seconds in time_cal().
const int mf1 = 2, mf2 = 3, mf3 = 4, mf4 = 5, mf5 = 6, mf6 = 7; // Arduino digital pins
int vref = (vdc) * (m); //vref, controlled by some modulation index.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(mf1, OUTPUT);
pinMode(mf2, OUTPUT);
pinMode(mf3, OUTPUT);
pinMode(mf4, OUTPUT);
pinMode(mf5, OUTPUT);
pinMode(mf6, OUTPUT);
digitalWrite(mf1, LOW);
digitalWrite(mf2, LOW);
digitalWrite(mf3, LOW);
digitalWrite(mf4, LOW);
digitalWrite(mf5, LOW);
digitalWrite(mf6, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
sector_tracker();
time_cal();
mosfet_switch();
angle_incr();
// delay(1000);
}
void sector_tracker()
{
if(angle >= 60) //When angle is >= 60 that means we have covered one 1 sector.
{
if(sector_pos != 7) // this condition is here to make sure we are never in sector 7 (i.e wer
{
angle = 0;
sector_pos = sector_pos + 1;
}
if(sector_pos == 7) // if after expression 'sector_pos = sector_pos + 1' in above case,we get 7, we need to set everything to initial point since no sector 7 exists.
{ //reason for putting this condition after the above condition is because there was a case when sector_pos = 7 after we update the sector_pos.
angle = 0;
sector_pos = 1;
}
}
}
void angle_incr() {
angle = angle + base_angle;
}
void time_cal() {
timer_div = (2/sqrt(3) * tz * m);
angle_rad = angle * (M_PI / 180); //converting angle->degrees to angle->radians. Need radian term for sin.
theta1 = sin((M_PI/3.0)-angle_rad);
theta2 = sin(angle_rad);
timer1 = timer_div * theta1;
timer2 = timer_div * theta2;
timer0 = (tz - (timer1 + timer2))/2.0;
//all time in (us)
timer1 = round(timer1);
timer2 = round(timer2);
timer0 = round(timer0);
/*Serial.print("Sector: ");
Serial.println(sector_pos);
Serial.println("Timer 1:");
Serial.println(timer1, 5);
Serial.println("Timer 2:");
Serial.println(timer2, 5);
Serial.println("Timer 0:");
Serial.println(timer0, 5);*/
}
void mosfet_switch() {
//Symmetric switching pattern/sequences used for optimal results.(low THD etc)
//refer to 'document' for rules for each V-state switches.
//switches in each V-state are defined by table( using the one given at wikipedia)
if (sector_pos == 1)
{
//v0-v1-v2-v7-v2-v1-v0
//-----
//v0:
digitalWrite(mf1,LOW);
digitalWrite(mf3,LOW);
digitalWrite(mf5,LOW);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer0);
//v1:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,LOW);
digitalWrite(mf5,LOW);
digitalWrite(mf2,LOW);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer1);
//v2:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,LOW);
digitalWrite(mf2,LOW);
digitalWrite(mf4,LOW);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer2);
//v7:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,LOW);
digitalWrite(mf4,LOW);
digitalWrite(mf6,LOW);
delayMicroseconds(timer0);
//v2:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,LOW);
digitalWrite(mf2,LOW);
digitalWrite(mf4,LOW);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer2);
//v1:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,LOW);
digitalWrite(mf5,LOW);
digitalWrite(mf2,LOW);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer1);
//v0:
digitalWrite(mf1,LOW);
digitalWrite(mf3,LOW);
digitalWrite(mf5,LOW);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer0);
}
if (sector_pos == 2)
{
//v0-v3-v2-v7-v2-v3-v0
//v0:
digitalWrite(mf1,LOW);
digitalWrite(mf3,LOW);
digitalWrite(mf5,LOW);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer0);
//v3:
digitalWrite(mf1,LOW);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,LOW);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,LOW);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer2);
//v2:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,LOW);
digitalWrite(mf2,LOW);
digitalWrite(mf4,LOW);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer1);
//v7:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,LOW);
digitalWrite(mf4,LOW);
digitalWrite(mf6,LOW);
delayMicroseconds(timer0);
//v2:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,LOW);
digitalWrite(mf2,LOW);
digitalWrite(mf4,LOW);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer1);
//v3:
digitalWrite(mf1,LOW);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,LOW);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,LOW);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer2);
//v0:
digitalWrite(mf1,LOW);
digitalWrite(mf3,LOW);
digitalWrite(mf5,LOW);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer0);
}
if (sector_pos == 3)
{
//v0-v3-v4-v7-v4-v3-v0
//v0:
digitalWrite(mf1,LOW);
digitalWrite(mf3,LOW);
digitalWrite(mf5,LOW);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer0);
//v3:
digitalWrite(mf1,LOW);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,LOW);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,LOW);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer1);
//v4:
digitalWrite(mf1,LOW);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,LOW);
digitalWrite(mf6,LOW);
delayMicroseconds(timer2);
//v7:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,LOW);
digitalWrite(mf4,LOW);
digitalWrite(mf6,LOW);
delayMicroseconds(timer0);
//v4:
digitalWrite(mf1,LOW);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,LOW);
digitalWrite(mf6,LOW);
delayMicroseconds(timer2);
//v3:
digitalWrite(mf1,LOW);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,LOW);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,LOW);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer1);
//v0:
digitalWrite(mf1,LOW);
digitalWrite(mf3,LOW);
digitalWrite(mf5,LOW);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer0);
}
if (sector_pos == 4)
{
//v0-v5-v4-v7-v4-v5-v0
//v0:
digitalWrite(mf1,LOW);
digitalWrite(mf3,LOW);
digitalWrite(mf5,LOW);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer0);
//v5:
digitalWrite(mf1,LOW);
digitalWrite(mf3,LOW);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,LOW);
delayMicroseconds(timer2);
//v4:
digitalWrite(mf1,LOW);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,LOW);
digitalWrite(mf6,LOW);
delayMicroseconds(timer1);
//v7:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,LOW);
digitalWrite(mf4,LOW);
digitalWrite(mf6,LOW);
delayMicroseconds(timer0);
//v4:
digitalWrite(mf1,LOW);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,LOW);
digitalWrite(mf6,LOW);
delayMicroseconds(timer1);
//v5:
digitalWrite(mf1,LOW);
digitalWrite(mf3,LOW);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,LOW);
delayMicroseconds(timer2);
//v0:
digitalWrite(mf1,LOW);
digitalWrite(mf3,LOW);
digitalWrite(mf5,LOW);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer0);
}
if (sector_pos == 5)
{
//v0-v5-v6-v7-v6-v5-v0
//v0:
digitalWrite(mf1,LOW);
digitalWrite(mf3,LOW);
digitalWrite(mf5,LOW);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer0);
//v5:
digitalWrite(mf1,LOW);
digitalWrite(mf3,LOW);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,LOW);
delayMicroseconds(timer1);
//v6:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,LOW);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,LOW);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,LOW);
delayMicroseconds(timer2);
//v7:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,LOW);
digitalWrite(mf4,LOW);
digitalWrite(mf6,LOW);
delayMicroseconds(timer0);
//v6:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,LOW);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,LOW);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,LOW);
delayMicroseconds(timer2);
//v5:
digitalWrite(mf1,LOW);
digitalWrite(mf3,LOW);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,LOW);
delayMicroseconds(timer1);
//v0:
digitalWrite(mf1,LOW);
digitalWrite(mf3,LOW);
digitalWrite(mf5,LOW);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer0);
}
if (sector_pos == 6)
{
//v0-v1-v6-v7-v6-v1-v0
//v0:
digitalWrite(mf1,LOW);
digitalWrite(mf3,LOW);
digitalWrite(mf5,LOW);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer0);
//v1:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,LOW);
digitalWrite(mf5,LOW);
digitalWrite(mf2,LOW);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer2);
//v6:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,LOW);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,LOW);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,LOW);
delayMicroseconds(timer1);
//v7:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,HIGH);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,LOW);
digitalWrite(mf4,LOW);
digitalWrite(mf6,LOW);
delayMicroseconds(timer0);
//v6:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,LOW);
digitalWrite(mf5,HIGH);
digitalWrite(mf2,LOW);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,LOW);
delayMicroseconds(timer1);
//v1:
digitalWrite(mf1,HIGH);
digitalWrite(mf3,LOW);
digitalWrite(mf5,LOW);
digitalWrite(mf2,LOW);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer2);
//v0:
digitalWrite(mf1,LOW);
digitalWrite(mf3,LOW);
digitalWrite(mf5,LOW);
digitalWrite(mf2,HIGH);
digitalWrite(mf4,HIGH);
digitalWrite(mf6,HIGH);
delayMicroseconds(timer0);
}
}