2007年5月6日

5/7- 第八次作業

作業八 廖婉婷 B94611040



本週四 (4/26)曾來上課


Q1.設桿2角度theta2=45度時,求各點之位置、速度與加速度為何?

本題採用講義上之f4bar函式
先分析出四連趕各桿/接點之位置,速度,加速度值 再取其所需
而題目一開始給定之條件為
r=[4 3 3 5], theta1=0, td2=10rad/s, tdd2=0rad/s^2
以下為函式內容:

function [data,form] = f4bar(r,theta1,theta2,td2,tdd2,mode,linkdrive)
%輸入參數為
%r= [r1 r2 r3 r4]: 各桿之長度
%theta1= 桿一之水平角度
%theta2= 驅動桿(可為桿2或3)之水平角度
%td2= 驅動桿之角速度(rad/sec)
%tdd2= 驅動桿之角加速度(rad/sec^2)
%mode= +1 or -1 組合模數,負值表閉合型;正值表分支型
%linkdrive = 0表示驅動桿為第二桿; 1表示驅動桿為第三桿
if nargin<7,linkdrive=0;end
if nargin<6,mode=1;end
data=zeros(4,6);
% if coupler is the driver, interchange the vetor 3 & 2
if linkdrive==1,r=[r(1) r(3) r(2) r(4)];end
rr=r.*r;d2g=pi/180;
[theta,td,tdd]=deal(zeros(4,1));
theta(1:2)=[theta1 theta2]*d2g;
t1=theta(1);tx=theta(2);
s1=sin(t1);c1=cos(t1);
sx=sin(tx);cx=cos(tx);
% position calculations
A=2*r(1)*r(4)*c1-2*r(2)*r(4)*cx;
C=rr(1)+rr(2)+rr(4)-rr(3)-2*r(1)*r(2)*(c1*cx+s1*sx);
B=2*r(1)*r(4)*s1-2*r(2)*r(4)*sx;
pos=B*B-C*C+A*A;
if pos>=0,
form=1;
% Check for the denominator equal to zero
if abs(C-A)>=1e-5
t4=2*atan((-B+mode*sqrt(pos))/(C-A));
s4=sin(t4);c4=cos(t4);
t3=atan2((r(1)*s1+r(4)*s4-r(2)*sx),(r(1)*c1+r(4)*c4-r(2)*cx));
s3=sin(t3);c3=cos(t3);
else
% If the denominator is zero, compute theta(3) first
A=-2*r(1)*r(3)*c1+2*r(2)*r(3)*cx;
B=-2*r(1)*r(3)*s1+2*r(2)*r(3)*sx;
C=rr(1)+rr(2)+rr(3)-rr(4)-2*r(1)*r(2)*(c1*cx+s1*sx);
pos=B*B-C*C+A*A;
if pos>=0,
t3=2*atan((-B-mode*sqrt(pos))/(C-A));
s3=sin(t3); c3=cos(t3);
t4=atan2((-r(1)*s1+r(3)*s3+r(2)*sx),...
(-r(1)*c1+r(3)*c3+r(2)*cx));
s4=sin(t4);c4=cos(t4);
end
end
theta(3)=t3;theta(4)=t4;
%velocity calculation
td(2)=td2;
AM=[-r(3)*s3, r(4)*s4; -r(3)*c3, r(4)*c4];
BM=[r(2)*td(2)*sx;r(2)*td(2)*cx];
CM=AM\BM;
td(3)=CM(1);td(4)=CM(2);

%acceleration calculation
tdd(2)=tdd2;
BM=[r(2)*tdd(2)*sx+r(2)*td(2)*td(2)*cx+r(3)*td(3)*td(3)*c3-...
r(4)*td(4)*td(4)*c4;r(2)*tdd(2)*cx-r(2)*td(2)*td(2)*sx-...
r(3)*td(3)*td(3)*s3+r(4)*td(4)*td(4)*s4];
CM=AM\BM;
tdd(3)=CM(1);tdd(4)=CM(2);
%store results in array data
% coordinates of P and Q
if linkdrive==1,
c2=c3;c3=cx;s2=s3;s3=sx;
r(2:3)=[r(3) r(2)];theta(2:3)=[theta(3) theta(2)];
td(2:3)=[td(3) td(2)];tdd(2:3)=[tdd(3) tdd(2)];
else
c2=cx;s2=sx;
end
for j=1:4,
data(j,1:4)=[r(j)*exp(i*theta(j)) theta(j)/d2g td(j) tdd(j)] ;
end % position vectors
data(1,5)=r(2)*td(2)*exp(i*theta(2));%velocity for point Q
data(2,5)=r(4)*td(4)*exp(i*theta(4));%velocity for point P
data(3,5)=r(2)*(i*tdd(2)-td(2)*td(2))*exp(i*theta(2));%acc of Q
data(4,5)=r(4)*(i*tdd(4)-td(4)*td(4))*exp(i*theta(4));%acc of P
data(1,6)=data(2,1);%position of Q, again
data(2,6)=data(1,1)+data(4,1);% position of P

%find the accelerations
else
form=0;
if linkdrive==1,
r=[r(1) r(3) r(2) r(4)];
for j=1:4, data(j,1)=r(j).*exp(i*theta(j));end % positions
end
end




%由題目給定條件執行程式
>> [data,form]=f4bar([4 3 3 5],0,45,10,0,-1,0)

data =

1.0e+003 *

Columns 1 through 5

0.0040 0 0 0 0.0212 + 0.0212i
0.0021 + 0.0021i 0.0450 0.0100 0 0.0041 - 0.0245i
0.0011 + 0.0028i 0.0695 -0.0163 0.4914 -0.2121 - 0.2121i
-0.0008 + 0.0049i 0.0995 -0.0050 0.3836 -1.8712 - 0.4391i

Column 6

0.0021 + 0.0021i
0.0032 + 0.0049i
0
0

%column1: 各桿之位置向量(複數型式)
%column2: 各桿之水平夾角
%column3: 各桿之角速度
%column4: 各桿之角加速度
%column5-1: Q點之速度
%column5-2: P點之速度
%column5-3: Q點之加速度
%column5-4: P點之加速度
%column6-1: Q點之位置向量
%column6-2: P點之位置向量

form =

1

%form=1 表示此四連桿可構成,若為0 則表不行



由程式結果得到
O點位置: 0 + 0i
Q點位置: 0.0021 + 0.0021i
P點位置: 0.0032 + 0.0049i
R點位置: 0.0040 + 0i(同桿一)


>> abs(data(:,5))
%對column5取絕對值
ans =

1.0e+003 *

0.0300
0.0248
0.3000
1.9220


又對column5取絕對值後
Q點速度為0.03
P點速度為0.0248
R點和O點速度為0(因桿1為固定桿)
(速度單位為: 長度/時間)

同理
Q點加速度為0.3
p點加速度為1.9220
R點和O點加速度為0
(加速度為: 長度/時間^2)




Q2.繪出此四連桿之相關位置及標明各點之速度方向及大小(以程式為之)。

本題使用講義上drawlinks函式
繪出由第一題給定條件之四連桿接合情形
drawlinks函式由呼叫第一題f4bar函式,只取其column1的數值
即各桿之位置向量,就能構成基本O,P,Q,R四點,進而完成連桿接合圖
以下為函式內容:
function [values]=drawlinks(r,th1,th2,mode,linkdrive)
%r: 各桿之長度
%th1: 第一桿之水平角
%th2: 驅動桿之水平角
%mode: +1 or -1,組合模式,負值表示閉合型;正值為分支型
%linkdrive: 0是驅動桿為第二桿(crank);1是驅動桿為第三桿(coupler)
if nargin<5,linkdrive=0;end
if nargin<4,mode=1;end
[values b]=f4bar(r,th1,th2,0,0,mode,linkdrive);
rr=values(:,1);
rr(3,1)=rr(1,1)+rr(4,1);
rx=real(rr(:,1));rx(4)=0;
ry=imag(rr(:,1));ry(4)=0;
if b==1
plot([0 rx(1)],[0 ry(1)],'k-','LineWidth',4);
hold on;
if linkdrive==0
plot([0 rx(2)],[0 ry(2)],'b-','LineWidth',1.5);
plot([rx(2) rx(3)],[ry(2) ry(3)],'r-','LineWidth',2);
else
plot([0 rx(2)],[0 ry(2)],'r-','LineWidth',2);
plot([rx(2) rx(3)],[ry(2) ry(3)],'b-','LineWidth',1.5);
end
plot([rx(1) rx(3)],[ry(1) ry(3)],'g-','LineWidth',1.5);
plot(rx,ry,'bo');
text(0,0,' O');text(rx(1),ry(1),' R');
text(rx(2),ry(2),' Q');text(rx(3),ry(3),' P');
else
fprintf('Combination of links fail at degrees %6.1f\n',th2);
end
axis equal
grid on




>> axis([-1 5 -1 5]);
axis equal;
drawlinks([4 3 3 5],0,45,-1,0)
%在顯示畫面上標明三點速度方向及大小
gtext ('速度方向0.0212 + 0.0212i 大小0.03')
gtext ('速度方向0.0041 - 0.0245i 大小0.0248')
gtext ('速度方向0 + 0 大小0')
gtext ('速度方向0 + 0 大小0')


ans =

Columns 1 through 5

4.0000 0 0 0 0
2.1213 + 2.1213i 45.0000 0 0 0
1.0513 + 2.8098i 69.4856 0 0 0
-0.8274 + 4.9311i 99.5246 0 0 0

Column 6

2.1213 + 2.1213i
3.1726 + 4.9311i
0
0


%Q速度方向0.0212 + 0.0212i 大小0.03
%P速度方向0.0041 - 0.0245i 大小0.0248
%R及0速度方向0 + 0 大小0


<圖> 四連桿之相關位置及各點之速度方向及大小(點圖可放大)



Q3.當桿2迴轉時,求出此組四連桿之限制角度,並繪出其位置(以程式為之)。

此題以講義中drawlimits函式
繪出四連桿之極限位置及限制角度
以下為函式內容:

function drawlimits(r,th1,sigma,driver)
%輸入參數為
%r: 四桿之位置向量
%th1: 第一桿之水平角度
%sigma: +1 or -1 組合模數,負值表閉合型;正值表分支型
%driver: 0表示驅動桿為第二桿; 1表示驅動桿為第三桿
[Qstart, Qstop]=fb_angle_limits(r,th1,driver)
clf;
[values b]=f4bar(r,th1,Qstart,0,0,sigma,driver);
rr=values(:,1);
rr(3)=rr(1)+rr(4);
rx=real(rr);rx(4)=0;
ry=imag(rr);ry(4)=0;
if b==1
plot([0 rx(1)],[0 ry(1)],'k-','LineWidth',4);
hold on;
if driver==0
plot([0 rx(2)],[0 ry(2)],'b-','LineWidth',1.5);
plot([rx(2) rx(3)],[ry(2) ry(3)],'r-','LineWidth',2);
else
plot([0 rx(2)],[0 ry(2)],'r-','LineWidth',2);
plot([rx(2) rx(3)],[ry(2) ry(3)],'b-','LineWidth',1.5);
end
plot([rx(1) rx(3)],[ry(1) ry(3)],'-g');
plot(rx,ry,'bo');
text(0,0,' O');text(rx(1),ry(1),' R');
text(rx(2),ry(2),' P');text(rx(3),ry(3),' Q');
text(rx(2)/2,ry(2)/2,['s1=' num2str(Qstart,'%6.1f')]);
else
fprintf('Combination of links fails at degrees %6.1f\n',Qstart);
end
[values b]=f4bar(r,th1,Qstop,0,0,sigma,driver);
rr=values(:,1);
rr(3)=rr(1)+rr(4);
rx=real(rr);rx(4)=0;
ry=imag(rr);ry(4)=0;
if b==1
if driver==0
plot([0 rx(2)],[0 ry(2)],'b-','LineWidth',1);
plot([rx(2) rx(3)],[ry(2) ry(3)],'r-','LineWidth',1.5);
else
plot([0 rx(2)],[0 ry(2)],'r-','LineWidth',1.5);
plot([rx(2) rx(3)],[ry(2) ry(3)],'b-','LineWidth',1);
end
plot([rx(1) rx(3)],[ry(1) ry(3)],'g-');
plot(rx,ry,'bo');
text(rx(2),ry(2),' p');text(rx(3),ry(3),' q');
text(rx(2)/2,ry(2)/2,[' s2=' num2str(Qstop,'%6.1f')]);
else
fprintf('Combination of links fail at degrees %6.1f\n',Qstop);
end
axis equal
grid on




%同1,2題之條件執行程式
>> drawlimits([4 3 3 5],0,-1,0)

Qstart =

28.9550


Qstop =

331.0450



<圖> 四連桿之極限位置(點圖可放大)

因為r1+r2 < r3+r4
且|r1-r2| < |r3-r4|
故屬於第四類型,角度限制在右側(28.9度和331度)
又由葛拉索準則,最短及最長桿之和大於其他兩桿
故應屬雙曲柄機構





Q4.設theta2=[0:20:360],試繪出此組四連桿之重疊影像,解釋為何有些沒有值。

藉由loop讓程式繪出以20度為桿2移動間隔之四連桿組成方式
以下為程式執行結果:
>>  for i=0:20:360,drawlinks([4 3 3 5],0,i,-1,0); 
end

Combination of links fail at degrees 0.0
Combination of links fail at degrees 20.0
Combination of links fail at degrees 340.0
Combination of links fail at degrees 360.0


<圖> theta2=[0:20:360],四連桿之重疊影像(點圖可放大)

由程式結果可知theta2為0,20,340,360這四個角度之連桿組合無法顯示
結合問題三我們注意到長度4 3 3 5之連桿組合
其限制角度為28.9及331度,未達及超過該角就無法組合
故本題以20度為間隔 顯示角度只會在40~320度間





Q5.若將問題三考慮在內,只在可迴轉的範圍內迴轉,請問你能讓此組四連桿作成動畫方式迴轉嗎?


首先以講義body函式
作桿上任一點位置,速度,加速度之運算:
function [val]=body2(r6,alph,values,link)
%輸入參數為:
% r6 = length of point from link's origin,Q
%alph= angle in deg. between the base link and r
%values= vetor output from the call of f4bar.m
%link =the link that the point lays on, 1-4
%outputs:(in complex form)
%val(1):initial position,(say Q, for link 2)
%val(3):middle position,(say P, for link 2)
%val(2):final position,or position of A
%val(4):velocity of A
%val(5):acceleration of A
%
r2=values(link,1);
d2g=pi/180;
switch link
case 2 % on the crank
val(1)=0;v0=0; a0=0;
val(3)=values(2,1);
case 3 % on the coupler
val(1)=values(2,1);
val(3)=values(1,1)+values(4,1);
v0=values(1,5);
a0=values(3,5);
case 4 % on the rocker
val(1)=values(1,1);
val(3)=values(1,1)+values(4,1);
v0=0; a0=0;
case 1 % on the frame
val(1)=0; v0=0; a0=0;
val(3)=values(1,1);
end
th6=(values(link,2)+alph)*d2g;
efact=exp(j*th6);
w6=values(link,3);
a6=values(link,4);
val(2)=val(1)+r6*efact;
val(4)=v0+j*r6*w6*efact;
val(5)=a0+(j*r6*a6-r6*w6*w6)*efact;



接著函式draw4link
與之前drawlinks類似
繪出四連桿連接情形,主動桿使用藍色表示,搖桿為綠色,地桿為黑色
function h=draw4link(values,linkdrive)
%function draw4link(values,b,linkdrive)
%draw the positions of four-bar links
% values are data from f4bar.m
%linkdrive: 0 for crank, 1 for coupler
%clf;
if nargin<2, linkdrive=0;end
rr=values(:,1);
rr(3,1)=rr(1,1)+rr(4,1);
rx=real(rr(:,1));rx(4)=0;
ry=imag(rr(:,1));ry(4)=0;
h(1)=line([0 rx(1)],[0 ry(1)],'color','k','LineWidth',4);
h(4)=line([rx(1) rx(3)],[ry(1) ry(3)],'color','g','LineWidth',1.5);
if linkdrive==0
h(2)=line([0 rx(2)],[0 ry(2)],'color','b','LineWidth',2);
h(3)=line([rx(2) rx(3)],[ry(2) ry(3)],'color','r',...
'LineWidth',1.5,'marker','o');
else
h(2)=line([0 rx(2)],[0 ry(2)],'color','r','LineWidth',1.5);
h(3)=line([rx(2) rx(3)],[ry(2) ry(3)],'color','b',...
'LineWidth',2,'marker','o');
end
text(0,0,' O');text(rx(1),ry(1),' R');
text(rx(2),ry(2),' P');text(rx(3),ry(3),' Q');
axis equal
grid on




最後以move_4paths將四連桿運動情形繪製成動畫
做動態模擬
function move_4paths(r,r6,th6,nlink,th1,td2,tdd2,sigma,driver,ntimes,npts)
%Inputs:
% r: row vector for four links
% th1: frame angle
% th2: crank angle or couple angle
% td2,tdd2:angular velocity and acceleration of the driving link.
% sigma: assembly mode
% driver: 0 for crank, 1 for coupler
% ntimes: no. of cycles
% npts: number of points divided
% r6,rh6,nlink:additional length and angle for nlink link.
%example:
% move_4paths([4 2 3 4],2,-30,3,0,10,0,1,0,4,100)
%
%clf;
if nargin<10, ntimes=3;npts=100;end;
figure(1);
[Qstart, Qstop]=fb_angle_limits(r,th1,driver);
npoint=abs(npts);
th2=linspace(Qstart,Qstop,npoint);
val=zeros(6,npoint);
for i=1:npoint,
[vr b]=f4bar(r,th1,th2(i),td2,tdd2,sigma,driver);
[para]=body2(r6,th6,vr,nlink);
val(1:3,i)=[vr(1,1);vr(2,1);vr(1,1)+vr(4,1)];
val(4:6,i)=[para(1);para(3);para(2)];
end
x=real(val);y=imag(val);
h=drawlimits(r,th1,sigma,driver);
line(x(5,:)',y(5,:)','color','r','linestyle',':');
line(x(4,:)',y(4,:)','color','b','linestyle','-.');
line(x(6,:)',y(6,:)','color','k','linestyle',':');
range=1.2*([min(min(x)) max(max(x)) min(min(y)) max(max(y))]);
axis(range);axis equal;grid off;
for i=2:4,set(h(i),'erasemode','xor');end
h0=patch('xdata',[],'ydata',[],'erasemode','xor','facecolor','r',...
'marker','o');
i=0;s=-1;axis off;
for m=1:ntimes
s=-s;
if abs(Qstop-Qstart-360)<1,i=0;s=1;end;
while 1,
i=i+s;
if i>npoint|i==0,break;end;
set(h(2),'xdata',[0 x(2,i)], 'ydata',[0 y(2,i)]);%crank
set(h(3),'xdata',[x(2,i) x(3,i)], 'ydata',[y(2,i) y(3,i)]);%coupler
set(h(4),'xdata',[x(1,i) x(3,i)], 'ydata',[y(1,i) y(3,i)]);%Rocker
set(h0,'xdata',[x(4:6,i)], 'ydata',[y(4:6,i)]);
drawnow; %flush the draw buffer
pause(0.1);
end
end % for m loop


function h=drawlimits(r,th1,sigma,driver)
%function drawlmits(r,th1,sigma,driver)
%draw the positions of four-bar links
%call f4bar funcion
%r: row vector for four links
%th1: frame angle
%sigma: assembly mode
%driver: 0 for crank, 1 for coupler
% Example:h=f4limits([4 2 3 4],0,1,0)
[Qstart, Qstop]=fb_angle_limits(r,th1,driver)
[values b]=f4bar(r,th1,Qstart,0,0,sigma,driver);
if b==1,
h=drawlinks(values,driver);
else
fprintf('Combination of links fails at degrees %6.1f\n',Qstart);
end
[values b]=f4bar(r,th1,Qstop,0,0,sigma,driver);
if b==1,
h=drawlinks(values,driver);
else
fprintf('Combination of links fails at degrees %6.1f\n',Qstart);
end
axis equal
grid on




%執行程式
%可以看到驅動桿及搖桿之運動軌跡
%還有第三桿上任一點之路徑
move_4paths([4 3 3 5],2,-30,3,0,10,0,1,0,4,100)



另外,
若只需將連桿運動情形製成動畫模式
不需其軌跡路徑,亦可由以下指令執行:
clear;
frames = 200;
LENGTH = [4 3 3 5];
[start, stop] = fb_angle_limits(LENGTH, 0, 0);
for repeat = 1:3,
for i = 0:frames,
clf;
drawlinks(LENGTH, 0, start + (i*((stop-start)/frames)));
axis([-3.5 4.5 -5.5 4]);
pause(0.01);
end;
for i = 0:frames,
clf;
drawlinks(LENGTH, 0, stop - (i*((stop-start)/frames)));
axis([-3.5 4.5 -5.5 4]);
pause(0.008);
end;
end;

1 則留言:

不留白老人 提到...

未見到速度分析,body所述之A點參數應加以說明,以表示你瞭解個中情況