image thumbnail
from Windscreen Wipers by Peter van Alem
Simulation of a simple windscreen wiping model

Screenwipers.m
% ScreenWipers simulates the kinematics of a four-bar mechanism. 
% In particular the simulation of a simple model of a windscreen wiper.
% On top the code shows and calculates the total wiping-area.
% Parameters as bar-lengths and number of turns can be adjusted in the first section of the code.

clear all;
close all;
set(gcf,'color',[1 1 1]);
% Initiate the plothandle
xdata=[0 0];
ydata=[0 0];
hold on;
box on;
% Different bar-lengths
a=.54;			str_1=['Length of rotating bar : ',num2str(a)];
b=1.25;			str_2=['Length of diagonal bar : ',num2str(b)];
c=.6;			str_3=['Length of lower vertical bars : ',num2str(c)];text_x=-c;text_y=-c;
d=1.3;			str_4=['Length of horizontal bars : ',num2str(d)];
factor=.9/c;	str_5=['Length of wipers : ',num2str(factor*abs(c))];
%===================================================
% Set the axes
axis([-factor*c  d+factor*c -d d]);
axis equal;
axis manual;
axis off;
set(gcf,'color',[1 1 1])
plothandle=plot(xdata,ydata,'Color','blue','LineWidth',2);
%===================================================
c=-c;
%===================================================
K_1=d/a;
K_2=d/c;
K_3=(a^2-b^2+c^2+d^2)/(2*a*c);
%===================================================
% Set the stepsize
Increment=.5*1e3;
stepsize=2*pi/(Increment);
%===================================================
% Set the total number of rotations
number_of_turns=3;
%===================================================
THETA_2=[0:stepsize:2*pi]';
theta_2=[];
THETA_4=[];
%===================================================
for i=1:number_of_turns
         theta_2=[theta_2;THETA_2];
end
theta_2=[theta_2;[[0:stepsize:pi/2]']];
% Loop
X_5=[];
Y_5=[];
%=============================
% Initiate Wiper Data
Xw2=[];
Yw2=[];
%=============================
for i=1:length(theta_2);    
		 %======================
         A=-K_1+(1-K_2)*(cos(theta_2(i)))+K_3;
         B=-2*sin(theta_2(i));
		 C=K_1-(K_2+1)*cos(theta_2(i))+K_3;
         %======================
         theta_4=real(2*atan((-B-sqrt(B^2-4*A*C))/(2*A)));
         %======================
         if length(THETA_4)<Increment
         		THETA_4=[THETA_4;theta_4];
         end      
         %======================
         x_2=a*cos(theta_2(i));
         y_2=a*sin(theta_2(i));
         x_4=d+c*cos(theta_4);
         y_4=c*sin(theta_4);
         % Wipers
         x_5=d-factor*(c)*cos(theta_4);
         y_5=-factor*(c)*sin(theta_4);
         %======================
         X_5=[X_5 x_5];
         Y_5=[Y_5 y_5];
         X_6=X_5-d;
         Y_6=Y_5;
         %======================
         Xw2=[Xw2;x_5];
         Yw2=[Yw2;y_5];
         %======================
         xdata=[0 x_2 x_4 x_5 fliplr(X_5) X_5 d 0 x_4-d  x_4      x_4-d  x_5-d  fliplr(X_6) X_6 ];
         ydata=[0 y_2 y_4 y_5 fliplr(Y_5) Y_5 0 0 y_4    y_4      y_4    y_5    fliplr(Y_6) Y_6 ];
         %======================
         set(plothandle,'XData',xdata,'YData',ydata);
         drawnow;
         if length(X_5)==length(THETA_2)
         	   X_5=[];
         	   Y_5=[];
         end;
         %======================
end;
%============================================================================
min_THETA_4=min(THETA_4);
max_THETA_4=max(THETA_4);
%============================================================================
if min_THETA_4<=-pi;
         I=find((THETA_4<0)==1);
         THETA_4(I)=THETA_4(I)+2*pi;
         min_THETA_4=min(THETA_4);
       	 max_THETA_4=max(THETA_4);
end;
%============================================================================
theta_4=linspace(min_THETA_4,max_THETA_4,100);
%============================================================================
% Trajectory
Xw2=d-factor*(c)*cos(theta_4);
Yw2=-factor*(c)*sin(theta_4);
Xw1=Xw2-d;
Yw1=Yw2;
%============================================================================
Xend11=Xw1(1);
Xend21=Xw1(end);
Yend11=Yw1(1);
Yend21=Yw1(end);
Xend12=Xw2(1);
Xend22=Xw2(end);
Yend12=Yw2(1);
Yend22=Yw2(end);
%============================================================================
% Sort
[I,P]=sort(Xw1);
Xw1test=Xw1(P);
Yw1test=Yw1(P);
[I,P]=sort(Xw2);
Xw2test=Xw2(P);
Yw2test=Yw2(P);
%----------------------------------------------------------------------------
I=find((Xw1test<=d/2)==0);
if isempty(I)~=1&length(I)>1;
      	Xw1test=Xw1test(1:I);
      	Yw1test=Yw1test(1:I);
end;
I=find((Xw2test>=d/2)==1);
if isempty(I)~=1&length(I)>1;
         I=I(1);
         Xw2test=Xw2test(I:end);
         Yw2test=Yw2test(I:end);
end;
I=find((Yw1test<=Yw2test(end))==1);
if isempty(I)~=1&length(I)>1;
        	I=I(end);
        	Xw1test=Xw1test(I:end);
        	Yw1test=Yw1test(I:end);  
end
%============================================================================
% Determination of intersection
sx=Xw1test(end);
sy=Yw1test(end);
if sx>min(Xw2)&sx<max(Xw1)
        p=1;
else
        p=0;
end;
%============================================================================
plot(Xend11,Yend11,'o','MarkerFaceColor','r','MarkerEdgeColor','r','MarkerSize',4);
plot(Xend21,Yend21,'o','MarkerFaceColor','r','MarkerEdgeColor','r','MarkerSize',4);
plot(Xend12,Yend12,'o','MarkerFaceColor','b','MarkerEdgeColor','b','MarkerSize',4);
plot(Xend22,Yend22,'o','MarkerFaceColor','b','MarkerEdgeColor','b','MarkerSize',4)
%============================================================================
plot(Xw1test(end),Yw1test(end),'o','MarkerFaceColor','g','MarkerEdgeColor',[1 .6 .2],'MarkerSize',3);
%============================================================================
set(gcf,'color',[1 1 1])
if p==0
         Tw1x=[0 Xw1 0];
         Tw1y=[0 Yw1 0];
         Tw2x=[d Xw2 d];
         Tw2y=[0 Yw2 0];
         plot(Tw1x,Tw1y,'r--');
         plot(Tw2x,Tw2y,'b--');
         %=========================================================================
        	phi1=(acos(dot([Xend11,Yend11],[Xend21,Yend21])));
     		Oppwis1=(((abs(phi1))/(2*pi))*pi)*(factor*abs(c))^2;
        	phi2=(acos(dot([Xend12-d,Yend12],[Xend22-d,Yend22])));
     		Oppwis2=(((abs(phi2))/(2*pi))*pi)*(factor*abs(c))^2;
        	%------------------------------------------------------------------------
     		O_tot=Oppwis1+Oppwis2;
     		%------------------------------------------------------------------------
          	title(['O(W_1) = ',num2str(Oppwis1),' m^2, O(W_2) = ',num2str(Oppwis2),' m^2, O_{tot} = ',num2str(O_tot),' m^2'],'FontName','Times','FontSize',11)
end;
%============================================================================
if p==1	
	     r=factor*abs(c);
	     a=Yend11/Xend11;
	     x_snijpunt=((2*d)-sqrt((-2*d)^2-4*(a^2+1)*(d^2-r^2)))/(2*(a^2+1));
         y_snijpunt=a*x_snijpunt;
         %=======================
         Xw1=fliplr(Xw1);
         Xw2=fliplr(Xw2);
         Yw1=fliplr(Yw1);	
         Yw2=fliplr(Yw2);
         %=======================
         I=find(Xw1<sx==0);I=I(1);
         J=find(Xw2>sx==1);J=J(1);
         K=find(Yw2<x_snijpunt==1);
         %=======================
         if isempty(K)==0&length(K)==1
            K=K(end);
            %=================================================================
            a2=Yend22/(Xend22-d);
            x_snijpunt=((2*a2^2*d)+sqrt((-2*a2^2*d)^2-4*(a2^2+1)*(a2^2*d^2-r^2)))/(2*(a2^2+1));
            y_snijpunt=a2*(x_snijpunt-d);
            L=find(Xw1>x_snijpunt==1);
            if isempty(L)~=1
            	L=L(1);
            	%=================================================================
            	Overlapx=[x_snijpunt  Xw1(L:end)];
                Overlapy=[y_snijpunt  Yw1(L:end)];
                %=================================================================
            elseif isempty(L)==1
                %=================================================================
     			x_snijpunt2=(a2*d)/(a2-a);
            	y_snijpunt2=a*x_snijpunt2;
                %----------------------------------
	            Overlapx=[x_snijpunt2];
                Overlapy=[y_snijpunt2];
                %=================================================================
            end
            % plot(x_snijpunt,y_snijpunt,'o','MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',3);
            Twx=[0 Xw1(1:I) Xw2(J:end) d Overlapx  0];
         	Twy=[0 Yw1(1:I) Yw2(J:end) 0 Overlapy  0];
         end
         if isempty(K)==0&length(K)>1
            K=K(end);
            %=================================================================
            a2=Yend22/(Xend22-d);
			x_snijpunt=((2*a2^2*d)+sqrt((-2*a2^2*d)^2-4*(a2^2+1)*(a2^2*d^2-r^2)))/(2*(a2^2+1));
            y_snijpunt=a2*(x_snijpunt-d);
            L=find(Xw1>x_snijpunt==1);
            if isempty(L)~=1
            		L=L(1);
            		%=================================================================
            		Overlapx=[x_snijpunt Xw1(L:end)];
            		Overlapy=[y_snijpunt Yw1(L:end)];
                    %=================================================================
            elseif isempty(L)==1
            	    %=================================================================
            	    x_snijpunt=((2*d)-sqrt((-2*d)^2-4*(a^2+1)*(d^2-r^2)))/(2*(a^2+1));
            	    y_snijpunt=a*x_snijpunt;
            	    %----------------------------------
					M=find(Xw2<x_snijpunt==1);M=M(end);
            	    %----------------------------------
            	    Overlapx=[Xend22 Xw2(1:M)];
            	    Overlapy=[Yend22 Yw2(1:M)];
            	    %=================================================================
            end   
            % plot(x_snijpunt,y_snijpunt,'o','MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',3);
            Twx=[0 Xw1(1:I) Xw2(J:end) d Overlapx  0];
         	Twy=[0 Yw1(1:I) Yw2(J:end) 0 Overlapy  0];
         end
         if isempty(K)==1;
            a2=Yend22/(Xend22-d);
            x_snijpunt2=(a2*d)/(a2-a);
            y_snijpunt2=a*x_snijpunt2;
            Overlapx=x_snijpunt2;
            Overlapy=y_snijpunt2;
            % plot(x_snijpunt2,y_snijpunt2,'o','MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',3);
            Twx=[0 Xw1(1:I) Xw2(J:end) d Overlapx  0];
         	Twy=[0 Yw1(1:I) Yw2(J:end) 0 Overlapy  0];
         end;
         %=======================
         plot(Twx,Twy,'--','Color',[1 0.6 .2]);
         Opp_binnen=trapz(Twx,(Twy+min(Twy)));
         title(['Wiping Area = ',num2str(Opp_binnen),' m^2'],'FontName','Times','FontSize',12)
   		 %=======================

end
text(text_x,text_y,str_1,'FontName','Times','FontSize',9)
text(text_x,1.2*text_y,str_2,'FontName','Times','FontSize',9)
text(text_x,1.4*text_y,str_3,'FontName','Times','FontSize',9)
text(text_x,1.6*text_y,str_4,'FontName','Times','FontSize',9)
text(text_x,1.8*text_y,str_5,'FontName','Times','FontSize',9)


      

Contact us at files@mathworks.com