系统位置分析

2024-10-03 09:12:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用Numpy库在Python中进行这种运动学位置分析。但是,下面的代码面临一些问题。xCxP的位置分析不正确

import numpy as np
import math as mt
import matplotlib.pyplot as plt


#Linkage Dimensions in meter
a = 0.130  #Crank Lenght
b = 0.200  #Coupler lenght
c = 0.170  #Rocker Lenght
d = 0.220  #Lenght between ground pins
p = 0.250  #Lenght from B to P
gamma = 20*mt.pi/180  #Angle between BP and coupler converted radian

print('Math module Pi',mt.pi)

#Coordinates of ground pins
x0 = np.array([[0],[0]])  #point A the origin of system
xD = np.array([[d],[0]])  #point D

N=360 #Number of times perform position analysis 

此创建位置矩阵:

#Allocate for position B,C and P
xB = np.zeros([2,N])
xC = np.zeros([2,N])
xP = np.zeros([2,N])

#Allocate space for angles
theta2 = np.zeros([1,N])
theta3 = np.zeros([1,N])
theta4 = np.zeros([1,N])



#Defining Unit Vector Function
#Calculates the unit vector and unit normal for a given angle
#theta = angle of unitvector
#e     = unit vector in the direction of theta
#n     = unit normal to the vector e

def UnitVector(theta):
    e = np.array([[np.cos(theta)], [np.sin(theta)]])
    n = np.array([[-np.sin(theta)], [np.cos(theta)]])
    return [e,n]


#Defining FindPos function
#Calculate the position of a point on a link using relative position
#formula
#x0 = position of first point on the link
#L  = Lenght of vector between first and second points
#e  = Unit Vector between first and second points
#x  = Position of second point on the link

def FindPos(x0,L,e):
    x = x0 + L + e
    return x

t3 = float((mt.pi)/4)   #Initial Guess for Newton-Raphson
t4 = float((mt.pi)/2)   #Initial Guess for Newton-Raphson

print('Initial Guess for t3: ',t3)
print('Initial Guess for t4: ',t4)

下面的方块是关于二维牛顿-拉夫逊算法:

for i in range(1,N):

    theta2[0,(i-1)] = (i-1)*(2*mt.pi)/(N-1)

    #Newton-Raphson Calculation
    for j in range(1,6):
        phi11 = a*mt.cos(theta2[0,(i-1)]) + b*mt.cos(t3) - c*mt.cos(t4) - d
        phi12 = a*mt.sin(theta2[0,(i-1)]) + b*mt.sin(t3) - c*mt.sin(t4)

        phi = np.array([[phi11], [phi12]])

        norm = ((phi[0])**2 + (phi[1])**2)**(0.5)

        #If constrain equations are satisfied then terminate 
        if (norm < 0.000000000001):

            theta3[0,i-1] = t3
            theta4[0,i-1] = t4

            break

雅可比矩阵的计算:

        #Calculate Jacobien Matrix
        J00 = -b*mt.sin(t3)   #(1,1) element of matrix
        J01 = c*mt.sin(t4)    #(1,2) eleemnt of matrix
        J10 = b*mt.cos(t3)    #(2,1) element of matrşx
        J11 = -c*mt.cos(t4)   #(2,2) element of mattix

        J = np.array([[J00, J01],[J10, J11]])

        #Update Variables using Newton-Raphson Equation
        dq = np.linalg.solve(J,phi)

        t3 = t3 + dq[0,0]
        t4 = t4 + dq[1,0]

    #Calculating UnitVectors
    [e2,n2]   = UnitVector(theta2[0,(i-1)])
    [e3,n3]   = UnitVector(theta3[0,(i-1)])
    [e4,n4]   = UnitVector(theta4[0,(i-1)])
    [eBP,nBP] = UnitVector(theta3[0,(i-1)]+gamma)

用矩阵中的零替换计算值:

    #Solve for position of posints B, C and P on the linkage
    xB[:,(i-1)][0]   = FindPos(x0,a,e2)[0][0]
    xB[:,(i-1)][1]   = FindPos(x0,a,e2)[1][0]

将这些值绘制为位置:

plt.plot(xB[0,:359],xB[1,:359])
plt.xlabel('x position [m]')
plt.ylabel('y position [m]')
plt.title('Four Bar Crank Position Analysis')
plt.grid(True)
plt.show()

这就是错误的结果xCxP图形不正确

xC[:,(i-1)][0]   = FindPos(xD,c,e4)[0][0]
xC[:,(i-1)][1]   = FindPos(xD,c,e4)[1][0]
xP[:,(i-1)][0]   = FindPos(xB[:,i-1],p,eBP)[0][0]
xP[:,(i-1)][1]   = FindPos(xB[:,i-1],p,eBP)[1][0]

plt.plot(xC[0,:359],xC[1,:359])
plt.xlabel('x position [m]')
plt.ylabel('y position [m]')
plt.title('Four Bar Crank Position Analysis')
plt.grid(True)
plt.show()

plt.plot(xP[0,:359],xP[1,:359])
plt.xlabel('x position [m]')
plt.ylabel('y position [m]')
plt.title('Four Bar Crank Position Analysis')
plt.grid(True)
plt.show()

欢迎任何建议。谢谢


Tags: ofthefornppositionpltcosxp