function machineplot(p,ns,nl,commutator_table,winding,l_w);
%machineplot(p,ns,nl,commutator_table,winding,l_w) is a function for draw the stator , rotor & winding
% the input arguments for this function is p,ns,nl,commutator_table,winding_table,l_w
% p is number of pole
% ns is number of slots
% nl is number of layer
% commutator_table is table of commutator slabs
% winding is table of winding
% l_w is type of winding. //if winding is lap (l_w=1 ) or wave (l_w=1)
% an example for this function run this in command
% p = 4
% ns = 11
% nl = 2
%commutator_table=[1,2,3,4,5,6,7,8,9,10,11,1]
%winding_table =[1,6,3,8,5,10,7,12,9,1,11,16,13,18,15,20,17,22,19,2,21,4,1]
%l_w=1 because this example is lapwinding
%machineplot(p,ns,nl,commutator_table,winding,l_w);
rr=10;%%%%%%%%%%% Determine the radius of the stator & rotor
rri=0.8*rr;
rs=.9*rri;
rsi=.75*rs;
figure
hold on
grid on
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Draw stator
teta=pi/2:2*pi/(p):(5*pi/2); % Finding the angle of each pole
teta_down=[teta-pi/(1.1*p);teta+pi/(1.1*p)];
xd=rri*cos(teta_down);
yd=rri*sin(teta_down);
xu=( xd + (rr-rri)*cos([teta;teta]) ) ;
yu=(yd +(rr-rri)*sin([teta;teta]) );
s=1;
for i=1:p+1
for j=1:2
x=xu(j,i);
y=yu(j,i);
if (y>0 & x<0 )|(y<0 & x<0)
alph=pi;
elseif x>0&y>0&s
alph=0;s=0;
else
alph=2*pi;
end
teta_up(j,i)=atan(y/x)+alph;
end
end
xx=xd(1,1);
yy=yd(1,1);
for i=1:p
x=rri*cos( teta_down(1,i) :0.1: teta_down(2,i) );
xx(size(xx,2)+1: size(xx,2)+size(x,2))=x;
y=rri*sin( teta_down(1,i) :0.1: teta_down(2,i) );
yy(size(yy,2)+1:size(yy,2)+size(y,2))=y;
x=rr*cos( teta_up(2,i) :0.1: teta_up(1,i+1) );
y=rr*sin( teta_up(2,i) :0.1: teta_up(1,i+1) );
xx(size(xx,2)+1: size(xx,2)+size(x,2))=x;
yy(size(yy,2)+1:size(yy,2)+size(y,2))=y;
end
xx(end+1)=xd(1,1);
yy(end+1)=yd(1,1);
xxx=[xx;xx];
yyy=[yy;yy];
z=[0*ones(size(xx));20*ones(size(xx))];
surf(xxx,yyy,z,'facecolor',[ .6 .6 .6],'linewidth',1.1);%Draw the inner surface of the stator
t=teta_down(1,1):2*pi/(size(xx,2)-1):teta_down(1,1)+2*pi;
z=[0*ones(size(t));20*ones(size(t))];
surf([1.3*rr*cos(t);1.3*rr*cos(t)],[1.3*rr*sin(t);1.3*rr*sin(t)],z,'facecolor',[ .6 .6 .6],'linewidth',.05);%Draw the outer surface of the stator
for i=1:p %Specify the type of each pole (S or N)
s=['s','n'];
text(1.1*rr*cos(teta(i)),1.1*rr*sin(teta(i)),22,s(2-rem(i,2)),'fontsize',24,'fontname','Engravers MT');
end
%Calculating The upper surface of custom stator
t=(teta_up(1,1)):2*pi/(size(xx,2)-1):teta_up(1,1)+2*pi;
s=[1.3*rr*cos(t);1*xx];
d=[1.3*rr*sin(t);1*yy];
z=20*ones(size(d));
surf(s,d,z,'facecolor',[ .6 .6 .6],'edgecolor','none')%draw The upper surface of custom stator
alpha(.1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Draw rotor
teta=pi/2:2*pi/(ns):(5*pi/2);% Finding the angle of each slot
teta_up=[teta-pi/(7*ns);teta+pi/(7*ns)];
xu=rs*cos(teta_up);
yu=rs*sin(teta_up);
yd=(yu +(rsi-rs)*sin([teta;teta]) );
xd=( xu + (rsi-rs)*cos([teta;teta]) );
xx=xd(1,1);
yy=yd(1,1);
for i=1:ns
xx(size(xx,2)+1: size(xx,2)+2)=[xd(1,i) xd(2,i)];
yy(size(yy,2)+1:size(yy,2)+2)=[yd(1,i) yd(2,i)];
x=rs*cos( teta_up(2,i) :0.1: teta_up(1,i+1) );
xx(size(xx,2)+1: size(xx,2)+size(x,2))=x;
y=rs*sin( teta_up(2,i) :0.1: teta_up(1,i+1) );
yy(size(yy,2)+1:size(yy,2)+size(y,2))=y;
end
xx(end+1)=xd(1,1);
yy(end+1)=yd(1,1);
xxx=[xx;xx];
yyy=[yy;yy];
z=[-2*ones(size(xx));20.1*ones(size(xx))];
surf(xxx,yyy,z,'Facecolor',[.3 .5 .5],'edgecolor',[.5 .5 1],'linewidth',1.2);%Draw the surface of the rotor
t=teta_up(1,1):2*pi/(size(xx,2)-1):teta_up(1,1)+2*pi;
s=[xx;0.55*rsi*cos(t)];
d=[yy;0.55*rsi*sin(t)];
z=20.1*ones(size(d));
mesh(s,d,z,'facecolor',[.85,.85,.85],'edgecolor','none')%Draw the upper surface of rotor
for i=1:ns %Marking the rotor slots
al=teta_up(1,i)-pi/(2*ns);
text(.95*rs*cos(al),.95*rs*sin( al),23,num2str(2*i-1),'fontsize',12,'fontname','Engravers MT');
text(.8*rs*cos(al),.8*rs*sin(al),23,num2str(2*i),'fontsize',12,'fontname','Engravers MT');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Draw commutator
t=0:2*pi/(50):2*pi;
x=.6*rsi*cos(t);
y=.6*rsi*sin(t);
z=20*ones(size(x));
surf([x;x],[y;y],[z;1.2*z],'facecolor','green','edgecolor','none');%Draw the outer surface of the commutator
nc=ns*nl/2;
t=pi/2:2*pi/(nc):(5/2)*pi;
x=[.6*rsi*cos(t);.3*rsi*cos(t)];
y=[.6*rsi*sin(t);.3*rsi*sin(t)];
z=24*ones(size(x));
surf(x,y,z,'facecolor','green');%Draw the upper surface of the commutator
surf([x(1,:);x(1,:)],[y(1,:);y(1,:)],[0.8333*z(1,:);z(1,:)],'facecolor','none')%Draw the slabs of commutator
tt=0:2*pi/(50):2*pi;
x=[.6*rsi*cos(tt);0.5*rsi*cos(tt)];
y=[.6*rsi*sin(tt);0.5*rsi*sin(tt)];
z=24*ones(size(x));
surf(x,y,z,'facecolor','green','edgecolor','none');
%%%%%%%%%%%%%
t=t+pi/(nc);
for i=1:nc
text(.4*rsi*cos(t(i)),.4*rsi*sin(t(i)),24,num2str(i),'fontsize',10,'fontname','Engravers MT');%Marking commutator slabs
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Draw winding
tt=0:.1:2*pi;
t=pi/2:2*pi/(nc):(5/2)*pi;
t(end+1)=5*pi/2;
t=t+pi/nc;
teta=pi/2:2*pi/(ns):(5*pi/2);
teta(end+1)=5*pi/2;
r=.1;%Determine the radius of the wire
f=.015;
cmap=jet(2*nc);%Create color box required for winding
for i=1:nc
j=size(tt,2);
l(1)=.58*rsi*cos(t(commutator_table(i))-2*f);
o(1)=.58*rsi*sin(t(commutator_table(i))-2*f);
l(2)=.95*rs*cos(teta(ceil(winding(2*i-1)/2))-3*f);
o(2)=.95*rs*sin(teta(ceil(winding(2*i-1)/2))-3*f);
k=20;
xxx=linspace(l(1),l(2),k);
yyy=interp1(l,o,xxx);
text(xxx(k)+0.02,yyy(k)+.02,24,char(i-1+double('a')),'fontsize',10,'fontname','Engravers MT');%Assign a name to each coil-sides
for c=1: k
x(c,1:j)=r*cos(tt)+xxx(c);
y(c,1:j)=r*sin(tt)+yyy(c);
z0(c)=23;
end
x(k+1,1:j)=r*cos(tt)+.95*rs*cos(teta(ceil(winding(2*i-1)/2))-3*f);
y(k+1,1:j)=r*sin(tt)+.95*rs*sin(teta(ceil(winding(2*i-1)/2))-3*f);
z0(k+1)=-2.5;
x(k+2,1:j)=r*cos(tt)+.8*rs*cos(teta(ceil(winding(2*i)/2))+2*f);
y(k+2,1:j)=r*sin(tt)+.8*rs*sin(teta(ceil(winding(2*i)/2))+2*f);
z0(k+2)=-2.5;
ttd=t(commutator_table(i+1))+2*f;
ttu=teta(ceil(winding(2*i)/2))+2*f;
if ttu >ttd & l_w
ttd=ttd+2*pi;
elseif ttu<ttd &l_w==0
ttd=ttd-2*pi;
end
te=linspace(ttu,ttd,k);
rrr=linspace(.8*rs,.58*rsi,k);
xxx=rrr.*cos(te);
yyy=rrr.*sin(te);
text(xxx(3)+0.01,yyy(4)+0.01,24,char(i-1+double('a')),'fontsize',10,'fontname','Engravers MT');%Assign a name to each coil-sides
for c=1: k
x(c+k+2,1:j)=r*cos(tt)+xxx(c);
y(c+k+2,1:j)=r*sin(tt)+yyy(c);
z0(c+k+2)=22.5;
end
for u=1:2*k+2
zz(u,1:j)=z0(u)+2*r*(sin(tt+pi/2)).^2;
end
surf(x,y,zz,'facecolor',cmap(2*i-1,:),'edgecolor','none');%draw the winding
end
axis([-13.2 13.2 -13.2 13.2 -3 26])
view([-1 0 3]);