-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinverterpwm.c
More file actions
72 lines (69 loc) · 1.54 KB
/
inverterpwm.c
File metadata and controls
72 lines (69 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <math.h>
#include "libsimul.h"
const double dt = 1e-7; // 100 ns
void set_switches(struct libsimul_ctx *ctx, int onoff)
{
if (onoff)
{
set_switch_state(ctx, "S1", 1);
set_switch_state(ctx, "S2", 1);
set_switch_state(ctx, "S3", 0);
set_switch_state(ctx, "S4", 0);
}
else
{
set_switch_state(ctx, "S1", 0);
set_switch_state(ctx, "S2", 0);
set_switch_state(ctx, "S3", 1);
set_switch_state(ctx, "S4", 1);
}
recalc(ctx);
}
int main(int argc, char **argv)
{
size_t i;
int cnt_remain = 500;
const double f = 50;
const double switch_f = 100e3;
double curduty;
const double V_in = 400;
const double V_sine = 325.27;
const double pi = 3.14159265358979;
double t = 0;
double ontime = 1e-5, offtime = 1e-5, V_out_ideal;
int onoff = 0;
struct libsimul_ctx ctx;
libsimul_init(&ctx, dt);
read_file(&ctx, "inverterpwm.txt");
init_simulation(&ctx);
set_switches(&ctx, 1);
cnt_remain = 1;
for (i = 0; i < 1000*1000; i++)
{
simulation_step(&ctx);
t += dt;
printf("%zu %g\n", i, get_V(&ctx, 5)-get_V(&ctx, 3));
cnt_remain--;
if (cnt_remain == 0 && onoff == 1)
{
set_switches(&ctx, 0);
cnt_remain = offtime/dt;
onoff = 0;
}
if (cnt_remain == 0 && onoff == 0)
{
V_out_ideal = V_sine*sin(2*pi*f*t);
V_out_ideal += V_sine*sin(2*pi*f*(t+1/70e3));
V_out_ideal /= 2;
curduty = 1.0 - (V_in - V_out_ideal)/(2*V_in);
ontime = curduty/switch_f;
offtime = (1.0-curduty)/switch_f;
cnt_remain = ontime/dt;
set_switches(&ctx, 1);
onoff = 1;
}
}
libsimul_free(&ctx);
return 0;
}