写代码关于MDH六自由度机械臂系统的参数辨识标定
时间: 2023-06-18 14:07:16 浏览: 214
MDH(Modified Denavit-Hartenberg)方法是一种常用的机械臂运动学模型,常用于描述机械臂的末端位姿和关节角度等信息。在进行机械臂控制和规划时,需要对机械臂的参数进行辨识标定,以便更准确地控制机械臂的运动。以下是一个辨识标定MDH六自由度机械臂系统参数的代码示例:
```python
import numpy as np
from scipy.optimize import least_squares
# 机械臂DH参数
d1 = 0.3
d2 = 0.2
d3 = 0.15
d4 = 0.1
d5 = 0.1
d6 = 0.05
a1 = 0.1
a2 = 0.2
a3 = 0.15
a4 = 0.1
a5 = 0.1
a6 = 0.05
alpha1 = np.pi/2
alpha2 = 0
alpha3 = np.pi/2
alpha4 = 0
alpha5 = -np.pi/2
alpha6 = 0
# 机械臂运动学正解
def forward_kinematics(q):
T01 = np.array([[np.cos(q[0]), -np.sin(q[0]), 0, a1*np.cos(q[0])],
[np.sin(q[0]), np.cos(q[0]), 0, a1*np.sin(q[0])],
[0, 0, 1, d1],
[0, 0, 0, 1]])
T12 = np.array([[np.cos(q[1]), -np.sin(q[1]), 0, a2*np.cos(q[1])],
[0, 0, -1, -d2],
[np.sin(q[1]), np.cos(q[1]), 0, a2*np.sin(q[1])],
[0, 0, 0, 1]])
T23 = np.array([[np.cos(q[2]), -np.sin(q[2]), 0, a3*np.cos(q[2])],
[np.sin(q[2]), np.cos(q[2]), 0, a3*np.sin(q[2])],
[0, 0, 1, d3],
[0, 0, 0, 1]])
T34 = np.array([[np.cos(q[3]), -np.sin(q[3]), 0, a4*np.cos(q[3])],
[0, 0, 1, d4],
[-np.sin(q[3]), -np.cos(q[3]), 0, -a4*np.sin(q[3])],
[0, 0, 0, 1]])
T45 = np.array([[np.cos(q[4]), -np.sin(q[4]), 0, a5*np.cos(q[4])],
[np.sin(q[4]), np.cos(q[4]), 0, a5*np.sin(q[4])],
[0, 0, 1, d5],
[0, 0, 0, 1]])
T56 = np.array([[np.cos(q[5]), -np.sin(q[5]), 0, a6*np.cos(q[5])],
[0, 0, -1, -d6],
[np.sin(q[5]), np.cos(q[5]), 0, a6*np.sin(q[5])],
[0, 0, 0, 1]])
T06 = np.dot(np.dot(np.dot(np.dot(np.dot(T01, T12), T23), T34), T45), T56)
return T06[:3, 3]
# 机械臂运动学逆解
def inverse_kinematics(x, y, z):
q = np.zeros(6)
q[0] = np.arctan2(y, x)
q[1] = np.arctan2(z-d1, np.sqrt(x**2+y**2-a1**2)) - np.arctan2(a2*np.sin(q[1]), a2*np.cos(q[1]))
q[2] = np.arctan2(np.sqrt(x**2+y**2-a1**2)-(a2*np.cos(q[1]))**2+(z-d1-a2*np.sin(q[1]))**2-a3**2, a3*np.sqrt((a2*np.cos(q[1]))**2+(a2*np.sin(q[1])+z-d1)**2))
q[3] = np.arctan2(np.sin(q[1]-q[2])*np.sqrt((a2*np.cos(q[1]))**2+(a2*np.sin(q[1])+z-d1)**2), np.cos(q[1]-q[2])*(a2*np.cos(q[1])+a3*np.cos(q[1]-q[2]))+a2*np.sin(q[1])+a3*np.sin(q[1]-q[2]))
q[4] = np.arctan2(-np.sin(q[3])*(a2*np.cos(q[1])+a3*np.cos(q[1]-q[2]))-a2*np.sin(q[1])-a3*np.sin(q[1]-q[2]), np.cos(q[3])*(a2*np.cos(q[1])+a3*np.cos(q[1]-q[2]))-np.sin(q[3])*(a2*np.sin(q[1])+a3*np.sin(q[1]-q[2])))*np.sign(q[3])
q[5] = np.arctan2(np.sin(q[1]-q[2])*np.sqrt((a2*np.cos(q[1]))**2+(a2*np.sin(q[1])+z-d1)**2), -np.cos(q[1]-q[2])*(a4*np.cos(q[4]))-a5*np.sin(q[4])) - q[3]
return q
# 机械臂DH参数优化函数
def dh_params_optimization(params, sample_points, target_points):
error = []
for i in range(sample_points.shape[0]):
q = sample_points[i]
a1, a2, a3, a4, a5, a6, alpha1, alpha2, alpha3, alpha4, alpha5, alpha6, d1, d2, d3, d4, d5, d6 = params
T01 = np.array([[np.cos(q[0]), -np.sin(q[0])*np.cos(alpha1), np.sin(q[0])*np.sin(alpha1), a1*np.cos(q[0])],
[np.sin(q[0]), np.cos(q[0])*np.cos(alpha1), -np.cos(q[0])*np.sin(alpha1), a1*np.sin(q[0])],
[0, np.sin(alpha1), np.cos(alpha1), d1],
[0, 0, 0, 1]])
T12 = np.array([[np.cos(q[1]), -np.sin(q[1])*np.cos(alpha2), np.sin(q[1])*np.sin(alpha2), a2*np.cos(q[1])],
[np.sin(q[1]), np.cos(q[1])*np.cos(alpha2), -np.cos(q[1])*np.sin(alpha2), a2*np.sin(q[1])],
[0, np.sin(alpha2), np.cos(alpha2), d2],
[0, 0, 0, 1]])
T23 = np.array([[np.cos(q[2]), -np.sin(q[2])*np.cos(alpha3), np.sin(q[2])*np.sin(alpha3), a3*np.cos(q[2])],
[np.sin(q[2]), np.cos(q[2])*np.cos(alpha3), -np.cos(q[2])*np.sin(alpha3), a3*np.sin(q[2])],
[0, np.sin(alpha3), np.cos(alpha3), d3],
[0, 0, 0, 1]])
T34 = np.array([[np.cos(q[3]), -np.sin(q[3])*np.cos(alpha4), np.sin(q[3])*np.sin(alpha4), a4*np.cos(q[3])],
[np.sin(q[3]), np.cos(q[3])*np.cos(alpha4), -np.cos(q[3])*np.sin(alpha4), a4*np.sin(q[3])],
[0, np.sin(alpha4), np.cos(alpha4), d4],
[0, 0, 0, 1]])
T45 = np.array([[np.cos(q[4]), -np.sin(q[4])*np.cos(alpha5), np.sin(q[4])*np.sin(alpha5), a5*np.cos(q[4])],
[np.sin(q[4]), np.cos(q[4])*np.cos(alpha5), -np.cos(q[4])*np.sin(alpha5), a5*np.sin(q[4])],
[0, np.sin(alpha5), np.cos(alpha5), d5],
[0, 0, 0, 1]])
T56 = np.array([[np.cos(q[5]), -np.sin(q[5])*np.cos(alpha6), np.sin(q[5])*np.sin(alpha6), a6*np.cos(q[5])],
[np.sin(q[5]), np.cos(q[5])*np.cos(alpha6), -np.cos(q[5])*np.sin(alpha6), a6*np.sin(q[5])],
[0, np.sin(alpha6), np.cos(alpha6), d6],
[0, 0, 0, 1]])
T06 = np.dot(np.dot(np.dot(np.dot(np.dot(T01, T12), T23), T34), T45), T56)
error.append(np.linalg.norm(T06[:3, 3] - target_points[i]))
return error
# 机械臂DH参数标定
def dh_params_calibration(sample_points, target_points):
params0 = np.array([a1, a2, a3, a4, a5, a6, alpha1, alpha2, alpha3, alpha4, alpha5, alpha6, d1, d2, d3, d4, d5, d6])
bounds = ([0]*18, [np.inf]*18)
res = least_squares(dh_params_optimization, params0, bounds=bounds, args=(sample_points, target_points))
return res.x
```
其中,`forward_kinematics(q)`是机械臂的正解函数,输入机械臂的关节角度,输出机械臂的末端位姿;`inverse_kinematics(x, y, z)`是机械臂的逆解函数,输入机械臂末端的位置,输出机械臂的关节角度;`dh_params_optimization(params, sample_points, target_points)`是机械臂DH参数的优化函数,输入机械臂DH参数和一组样本点及其对应的目标点,输出优化误差;`dh_params_calibration(sample_points, target_points)`是机械臂DH参数标定函数,输入一组样本点及其对应的目标点,输出优化后的机械臂DH参数。
使用时,需要先准备一组样本点及其对应的目标点,例如:
```python
sample_points = np.array([[0, 0, 0, 0, 0, 0],
[np.pi/4, np.pi/4, np.pi/4, np.pi/4, np.pi/4, np.pi/4],
[np.pi/2, np.pi/2, np.pi/2, np.pi/2, np.pi/2, np.pi/2]])
target_points = np.array([[0.2, 0.2, 0.2],
[0.3, 0.3, 0.3],
[0.4, 0.4, 0.4]])
```
然后调用`dh_params_calibration`函数进行DH参数标定:
```python
params = dh_params_calibration(sample_points, target_points)
```
最终得到的`params`即为优化后的机械臂DH参数。
阅读全文