// trifasico.sq // Juan Claudio Regidor, 10/3/2004 title "Voltajes Trif‡sicos" help {@ Display rotating tri-phase fasors. @} variable phi // phase for Va variable w1 // horizontal frequency variable w2 // vertical frequency variable t // time variable theta // time vector variable hor // Phase a amplitude vector variable horb // Phase b amplitude vector variable horc // Phase c amplitude vector variable verta // Phase a imaginary amplitude vector variable vertb // Phase b imaginary amplitude vector variable vertc // Phase c imaginary amplitude vector variable xa // Phase a amplitude value in time t variable ya // Phase a imaginary amplitude value in time t variable xb // Phase b amplitude value in time t variable yb // Phase b imaginary amplitude value in time t variable xc // Phase c amplitude value in time t variable yc // Phase c imaginary amplitude value in time t variable faseb // phase for Vb variable fasec // phase for Vc variable sc // scale variable an // animation flag variable fn // voltage menu variable sec // sequence menu variable w // window size menu init (phi, w1, w2, t, theta, sc, an, faseb, fasec, fn, sec, w) = init // initialization idle (t, _idleamount) = idleAnimate(w1, w2, t, an) // idle handler make (xa, ya) = xy(t, w1, w2, phi, 0) // calculate horizontal and vertical values make (xb, yb) = xy(t, w1, w2, phi, faseb) make (xc, yc) = xy(t, w1, w2, phi, fasec) make (hor, verta) = xy(theta, w1, w2, phi, 0) // calculate horizontal and vertical vectors Va make (horb, vertb) = xy(theta, w1, w2, phi, faseb) // calculate horizontal and vertical vectors Vb make (horc, vertc) = xy(theta, w1, w2, phi, fasec) // calculate horizontal and vertical vectors Vc menu "Voltajes de fase" _checkmark(fn == 0) fn = 0 menu "Voltajes de linea" _checkmark(fn == 1) fn = 1 menu "Voltajes de fase y linea" _checkmark(fn == 2) fn = 2 separator menu "Secuencia abc" _checkmark(sec == 0) (sec, faseb, fasec) = ff(0) menu "Secuencia acb" _checkmark(sec == 1) (sec, faseb, fasec) = ff(1) separator menu "800x600" _checkmark(w == 1) w = graf_layout(1) menu "1024x768" _checkmark(w == 2) w = graf_layout(2) menu "1152x768" _checkmark(w == 3) w = graf_layout(3) figure "Fasores" // Voltage phasors draw sc = drawFasor(hor, verta, xa, ya, xb, yb, xc, yc, t, phi, fn, sec) figure "Par‡metros" // slider to set the phase and draw drawParameters(phi, an) // checkmark button for animation flag mousedrag (phi, an) = dragParameters(phi, an, _id, _nb, _x1) // _nb is the slider number, or [] if // the click is not on a slider // _x1 is the current value figure "Voltajes" // Voltage in time figure draw drawVertsin(hor, horb, horc, theta, xa, xb, xc, phi, t, fn, sec) mousedrag (t, _msg) = drag(t, 1, _x, _x1) mouseover _msg = overT(_id, t) functions {@ function (op, faseb, fasec) = ff(op1) // Set phase for Vb and Vc if (op1 == 0) faseb = -2*pi/3; else faseb = 2*pi/3; end fasec = -faseb; op = op1; function (phi, w1, w2, t, theta, sc, an, faseb, fasec, fn, sec, w) = init // initialization phi = 0; // phase for vertical function w1 = 1; // unit frequencies on both channels w2 = 1; t = 0; // time theta = 2 * pi * (0:360) / 360; // time vector sc = [0, 0, -1, 1]; an = 0; // do not animate fasec = 2*pi/3; // 120 grados faseb = -fasec; fn = 0; sec = 0; w = 0; graf_layout(w); function w1 = graf_layout(w) w1 = w; switch w case 0 x = 510; y = 400; case 1 x = 800; y = 600; case 2 x = 1024; y = 768; case 3 x = 1152; y = 768; end x=x-26;y=y-80; ux=x/8; uy=y/8; uy3 = 8; subplots('Fasores\tVoltajes\nPar‡metros'); subplotpos([0,ux/2,0,uy-uy3; ux/2,ux,0,uy-uy3; 0,ux,uy-uy3,uy;0,0,0,0]); function sc = drawFasor(hor, vert, xa, ya, xb, yb, xc, yc, t, phi, fn, sec) // Fasor color = 'h(80f080)'; line([1, 0], 0., 'h(d0d0ff)'); // draw x and y axis, light gray line([0, 1], 0., 'h(d0d0ff)'); scale('equal', [-2,2;-2,2]); if (fn == 0 || fn == 2) //plot(hor, vert, 'h(e0e0e0)'); // plot figure circle(0, 0, 1, 'h(e0e0e0)'); plot(xa, ya, 'xg'); // plot blue 'x' on intersection of plot([0, xa], [0, ya], 'g'); plot(xa, 0, 'og'); text(1.1*xa, 1.1*ya, 'Va') plot(xb, yb, 'xg'); // plot blue 'x' on intersection of plot([0, xb], [0, yb], 'g'); plot(xb, 0, 'og'); text(1.1*xb, 1.1*yb, 'Vb') plot(xc, yc, 'xg'); // plot blue 'x' on intersection of plot([0, xc], [0, yc], 'g'); plot(xc, 0, 'og'); text(1.1*xc, 1.1*yc, 'Vc') end if (fn == 1 || fn == 2) //plot(sqrt(3)*hor, sqrt(3)*vert, 'h(e0e0e0)'); // plot figure circle(0, 0, sqrt(3), 'h(e0e0e0)'); plot([0, xa-xb], [0, ya-yb], 'r'); plot(xa-xb, ya-yb, 'rx'); plot(xa-xb, 0, 'or'); text(1.1*(xa-xb), 1.1*(ya-yb), 'Vab') plot([0, xb-xc], [0, yb-yc], 'r'); plot(xb-xc, yb-yc, 'rx'); plot(xb-xc, 0, 'or'); text(1.1*(xb-xc), 1.1*(yb-yc), 'Vbc') plot([0, xc-xa], [0, yc-ya], 'r'); plot(xc-xa, yc-ya, 'rx'); plot(xc-xa, 0, 'or'); text(1.1*(xc-xa), 1.1*(yc-ya), 'Vca') end if (fn == 2) plot([0, -xb], [0, -yb], color); plot([xa, xa-xb], [ya, ya-yb], color); plot([-xb, xa-xb], [-yb, ya-yb], color); plot([0, -xc], [0, -yc], color); plot([xb, xb-xc], [yb, yb-yc], color); plot([-xc, xb-xc], [-yc, yb-yc], color); plot([0, -xa], [0, -ya], color); plot([xc, xc-xa], [yc, yc-ya], color); plot([-xa, xc-xa], [-ya, yc-ya], color); end if (fn == 0) plot([xa, xa], [0, ya], color); plot([xb, xb], [0, yb], color); plot([xc, xc], [0, yc], color); end if (fn == 1) plot([xa-xb, xa-xb], [0, ya-yb], color); plot([xb-xc, xb-xc], [0, yb-yc], color); plot([xc-xa, xc-xa], [0, yc-ya], color); end t = (sec == 0)? 'Secuencia abc' : 'Secuencia acb'; text(1.1, 1.9, t); //line([1, 0], xa, 'r-'); // horizontal and //line([0, 1], ya, 'r-'); // vertical values sc = scale; function drawVertsin(verta, vertb, vertc, theta, ya, yb, yc, phi, t, fn, sec) // vertical channel figure line([1, 0], 0., 'h(d0d0d0)'); // draw x and y axis, light gray line([0, 1], 0., 'h(d0d0d0)'); scale([0, 2*pi, -2, 2]); fasea = -phi; faseb = -phi + ((sec == 0)? 2*pi/3 : 4*pi/3); fasec = -phi + ((sec == 1)? 2*pi/3 : 4*pi/3); sgn = (sec == 0)? 1 : -1; if (fn == 0 || fn == 2) plot(theta, verta, 'h(00c000)'); plot(theta, vertb, 'h(00c000)'); plot(theta, vertc, 'h(00c000)'); plot(t, ya, 'og', 1); // plot red 'o' on intersection of plot(t, yb, 'og', 1); // plot red 'o' on intersection of plot(t, yc, 'og', 1); // plot red 'o' on intersection of text(adj_fase(fasea), 1.1, 'Va'); text(adj_fase(faseb), 1.1, 'Vb'); text(adj_fase(fasec), 1.1, 'Vc'); end if (fn == 1 || fn == 2) plot(theta, verta-vertb, 'h(c00000)'); plot(theta, vertb-vertc, 'h(c00000)'); plot(theta, vertc-verta, 'h(c00000)'); plot(t, ya-yb, 'or', 1); // plot red 'o' on intersection of plot(t, yb-yc, 'or', 1); // plot red 'o' on intersection of plot(t, yc-ya, 'or', 1); // plot red 'o' on intersection of text(adj_fase(2*pi + fasea - sgn*pi/6), 1.8, 'Vab'); text(adj_fase(faseb - sgn*pi/6), 1.8, 'Vbc'); text(adj_fase(fasec - sgn*pi/6), 1.8, 'Vca'); end line([1, 0], t, 'c', 1); // time and //line([0, 1], ya, 'r-'); // vertical values function f1 = adj_fase(f) f1 = max(0.2, mod(f, 2*pi)); function (x, y) = xy(t, w1, w2, phi, fase) // calculate horizontal and vertical values x = cos(w1 * t + phi + fase); y = sin(w2 * t + phi + fase); function drawParameters(phi, an) settabs('Fase de Va = -120 ¼ '); slider(sprintf('Fase de Va = %d ¼', round(180*phi/pi)), [phi], [-pi, pi], '-', '', 1); button(' \tAnimar tiempo', [an], 'checkmark', '', 4); function (phi, an) = dragParameters(phi, an, id, nb, x1) if isempty(id) cancel; elseif (id == 1) switch nb case 1 phi = round(180*x1/pi)*pi/180; end elseif (id == 4) an = x1; end function (t, idleamount) = idleAnimate(w1, w2, t, an) idleamount = 0.9; if (an == 1) a = clock; if (w1 > 4) || (w2 > 4) // k reduces the sweep time as a function of k = 8; // hor. and vert. frequencies elseif (w1 > 8) || (w2 > 8) k = 4; else k = 2; end // t = mod((60*a(5)+a(6))/k,2*pi); // real time is 60*minutes + seconds t = mod(t+0.16/k, 2*pi); msg = sprintf('t = %.2f', t); end function (t, msg) = drag(t, s, y, y1) if isempty(y) cancel; end t = s*round(180*y1/pi)*pi/180; if (t < 0) t = 0; end if (t > 2*pi) t = 2*pi; end msg = sprintf('t = %.2f', t); function msg = overT(id, t) if ~same(id, 1) cancel; end msg = sprintf('t = %.2f', t); @}