ARCS SDK API  0.24.0
math.h
浏览该文件的文档.
1 /** @file math.h
2  * @brief 数学方法接口,如欧拉角与四元数转换、位姿的加减运算
3  */
4 #ifndef AUBO_SDK_MATH_INTERFACE_H
5 #define AUBO_SDK_MATH_INTERFACE_H
6 
7 #include <vector>
8 #include <memory>
9 
10 #include <aubo/type_def.h>
11 #include <aubo/global_config.h>
12 
13 namespace arcs {
14 namespace common_interface {
15 
16 class ARCS_ABI_EXPORT Math
17 {
18 public:
19  Math();
20  virtual ~Math();
21 
22  /**
23  * Pose addition
24  *
25  * Both arguments contain three position parameters (x, y, z) jointly called
26  * P, and three rotation parameters (R_x, R_y, R_z) jointly called R. This
27  * function calculates the result x_3 as the addition of the given poses as
28  * follows:
29  *
30  * p_3.P = p_1.P + p_2.P
31  * p_3.R = p_1.R * p_2.R
32  *
33  * 位姿相加。
34  * 两个参数都包含三个位置参数(x、y、z),统称为P,
35  * 以及三个旋转参数(R_x、R_y、R_z),统称为R。
36  * 此函数根据以下方式计算结果 p_3,即给定位姿的相加:
37  * p_3.P = p_1.P + p_2.P,
38  * p_3.R = p_1.R * p_2.R
39  *
40  * @param p1 工具位姿1(pose)
41  * @param p2 工具位姿2(pose)
42  * @return Sum of position parts and product of rotation parts (pose)
43  * 位置部分之和和旋转部分之积(pose)
44  *
45  * @par Python函数原型
46  * poseAdd(self: pyaubo_sdk.Math, arg0: List[float], arg1: List[float]) ->
47  * List[float]
48  *
49  * @par Lua函数原型
50  * poseAdd(p1: table, p2: table) -> table
51  *
52  */
53  std::vector<double> poseAdd(const std::vector<double> &p1,
54  const std::vector<double> &p2);
55 
56  /**
57  * Pose subtraction
58  * 位姿相减
59  *
60  * 两个参数都包含三个位置参数(x、y、z),统称为P,
61  * 以及三个旋转参数(R_x、R_y、R_z),统称为R。
62  * 此函数根据以下方式计算结果 p_3,即给定位姿的相加:
63  * p_3.P = p_1.P - p_2.P,
64  * p_3.R = p_1.R * p_2.R.inverse
65  *
66  * @param p1 工具位姿1
67  * @param p2 工具位姿2
68  * @return 位姿相减计算结果
69  *
70  * @par Python函数原型
71  * poseSub(self: pyaubo_sdk.Math, arg0: List[float], arg1: List[float]) ->
72  * List[float]
73  *
74  * @par Lua函数原型
75  * poseSub(p1: table, p2: table) -> table
76  *
77  */
78  std::vector<double> poseSub(const std::vector<double> &p1,
79  const std::vector<double> &p2);
80 
81  /**
82  * 计算线性插值
83  *
84  * @param p1 起点的TCP位姿
85  * @param p2 终点的TCP位姿
86  * @param alpha 系数,
87  * 当0<alpha<1,返回p1和p2两点直线的之间靠近p1端且占总路径比例为alpha的点;
88  * 例如当alpha=0.3,返回的是靠近p1那端,总路径的百分之30的点;
89  * 当alpha>1,返回p2;
90  * 当alpha<0,返回p1;
91  * @return 插值计算结果
92  *
93  * @par Python函数原型
94  * interpolatePose(self: pyaubo_sdk.Math, arg0: List[float], arg1:
95  * List[float], arg2: float) -> List[float]
96  *
97  * @par Lua函数原型
98  * interpolatePose(p1: table, p2: table, alpha: number) -> table
99  *
100  */
101  std::vector<double> interpolatePose(const std::vector<double> &p1,
102  const std::vector<double> &p2,
103  double alpha);
104  /**
105  * Pose transformation
106  *
107  * The first argument, p_from, is used to transform the second argument,
108  * p_from_to, and the result is then returned. This means that the result is
109  * the resulting pose, when starting at the coordinate system of p_from, and
110  * then in that coordinate system moving p_from_to.
111  *
112  * This function can be seen in two different views. Either the function
113  * transforms, that is translates and rotates, p_from_to by the parameters
114  * of p_from. Or the function is used to get the resulting pose, when first
115  * making a move of p_from and then from there, a move of p_from_to. If the
116  * poses were regarded as transformation matrices, it would look like:
117  *
118  * T_world->to = T_world->from * T_from->to,
119  * T_x->to = T_x->from * T_from->to
120  *
121  * 位姿变换
122  *
123  * 第一个参数 p_from 用于转换第二个参数 p_from_to,并返回结果。
124  * 这意味着结果是从 p_from 的坐标系开始,
125  * 然后在该坐标系中移动 p_from_to后的位姿。
126  *
127  * 这个函数可以从两个不同的角度来看。
128  * 一种是函数将 p_from_to 根据 p_from 的参数进行转换,即平移和旋转。
129  * 另一种是函数被用于获取结果姿态,先对 p_from 进行移动,然后再对 p_from_to
130  * 进行移动。 如果将姿态视为转换矩阵,它看起来像是:
131  *
132  * T_world->to = T_world->from * T_from->to,
133  * T_x->to = T_x->from * T_from->to
134  *
135  * 这两个方程描述了姿态变换的基本原理,根据给定的起始姿态和相对于起始姿态的姿态变化,可以计算出目标姿态。
136  *
137  * 举个例子,已知B相对于A的位姿、C相对于B的位姿,求C相对于A的位姿。
138  * 第一个参数是B相对于A的位姿,第二个参数是C相对于B的位姿,
139  * 返回值是C相对于A的位姿。
140  *
141  * @param pose_from 起始位姿(空间向量)
142  * @param pose_from_to 相对于起始位姿的姿态变化(空间向量)
143  * @return 结果位姿 (空间向量)
144  *
145  * @par Python函数原型
146  * poseTrans(self: pyaubo_sdk.Math, arg0: List[float], arg1: List[float]) ->
147  * List[float]
148  *
149  * @par Lua函数原型
150  * poseTrans(pose_from: table, pose_from_to: table) -> table
151  *
152  */
153  std::vector<double> poseTrans(const std::vector<double> &pose_from,
154  const std::vector<double> &pose_from_to);
155 
156  /**
157  * 姿态逆变换
158  *
159  * 已知C相对于A的位姿、C相对于B的位姿,求B相对于A的位姿。
160  * 第一个参数是C相对于A的位姿,第二个参数是C相对于B的位姿,
161  * 返回值是B相对于A的位姿。
162  *
163  * @param pose_from 起始位姿
164  * @param pose_to_from 相对于结果位姿的姿态变化
165  * @return 结果位姿
166  *
167  * @par Python函数原型
168  * poseTransInv(self: pyaubo_sdk.Math, arg0: List[float], arg1: List[float])
169  * -> List[float]
170  *
171  * @par Lua函数原型
172  * poseTransInv(pose_from: table, pose_to_from: table) -> table
173  *
174  */
175  std::vector<double> poseTransInv(const std::vector<double> &pose_from,
176  const std::vector<double> &pose_to_from);
177 
178  /**
179  * Get the inverse of a pose
180  *
181  * 获取位姿的逆
182  *
183  * @param pose tool pose (spatial vector)
184  * 工具位姿(空间向量)
185  * @return inverse tool pose transformation (spatial vector)
186  * 工具位姿的逆转换(空间向量)
187  *
188  * @par Python函数原型
189  * poseInverse(self: pyaubo_sdk.Math, arg0: List[float]) -> List[float]
190  *
191  * @par Lua函数原型
192  * poseInverse(pose: table) -> table
193  *
194  */
195  std::vector<double> poseInverse(const std::vector<double> &pose);
196 
197  /**
198  * 计算两个位姿的位置距离
199  *
200  * @param p1 位姿1
201  * @param p2 位姿2
202  * @return 两个位姿的位置距离
203  */
204  double poseDistance(const std::vector<double> &p1,
205  const std::vector<double> &p2);
206 
207  /**
208  * 计算两个位姿的轴角距离
209  *
210  * @param p1 位姿1
211  * @param p2 位姿2
212  * @return 轴角距离
213  */
214  double poseAngleDistance(const std::vector<double> &p1,
215 
216  const std::vector<double> &p2);
217 
218  /**
219  * 判断两个位姿是否相等
220  *
221  * @param p1 位姿1
222  * @param p2 位姿2
223  * @param eps 误差
224  * @return 相等返回true,反之返回false
225  */
226  bool poseEqual(const std::vector<double> &p1, const std::vector<double> &p2,
227  double eps = 5e-5);
228 
229  /**
230  *
231  * @param F_b_a_old
232  * @param V_in_a
233  * @param type
234  * @return
235  *
236  * @par Python函数原型
237  * transferRefFrame(self: pyaubo_sdk.Math, arg0: List[float], arg1:
238  * List[float[3]], arg2: int) -> List[float]
239  *
240  * @par Lua函数原型
241  * transferRefFrame(F_b_a_old: table, V_in_a: table, type: number) -> table
242  *
243  */
244  std::vector<double> transferRefFrame(const std::vector<double> &F_b_a_old,
245  const Vector3d &V_in_a, int type);
246 
247  /**
248  * 姿态旋转
249  *
250  * @param pose
251  * @param rotv
252  * @return
253  *
254  * @par Python函数原型
255  * poseRotation(self: pyaubo_sdk.Math, arg0: List[float], arg1: List[float])
256  * -> List[float]
257  *
258  * @par Lua函数原型
259  * poseRotation(pose: table, rotv: table) -> table
260  *
261  */
262  std::vector<double> poseRotation(const std::vector<double> &pose,
263  const std::vector<double> &rotv);
264 
265  /**
266  * 欧拉角转四元数
267  *
268  * @param rpy 欧拉角
269  * @return 四元数
270  *
271  * @par Python函数原型
272  * rpyToQuaternion(self: pyaubo_sdk.Math, arg0: List[float]) -> List[float]
273  *
274  * @par Lua函数原型
275  * rpyToQuaternion(rpy: table) -> table
276  *
277  */
278  std::vector<double> rpyToQuaternion(const std::vector<double> &rpy);
279 
280  /**
281  * 四元数转欧拉角
282  *
283  * @param quat 四元数
284  * @return 欧拉角
285  *
286  * @par Python函数原型
287  * quaternionToRpy(self: pyaubo_sdk.Math, arg0: List[float]) -> List[float]
288  *
289  * @par Lua函数原型
290  * quaternionToRpy(quat: table) -> table
291  *
292  */
293  std::vector<double> quaternionToRpy(const std::vector<double> &quat);
294 
295  /**
296  * 四点法标定TCP偏移
297  *
298  * 找一个尖点,将机械臂工具末端点绕着尖点示教四个位置,姿态差别要大。
299  * 设置完毕后即可计算出来结果。
300  *
301  * @param poses 四个点的位姿集合
302  * @return TCP标定结果和标定结果是否有效
303  *
304  * @par Python函数原型
305  * tcpOffsetIdentify(self: pyaubo_sdk.Math, arg0: List[List[float]]) ->
306  * Tuple[List[float], int]
307  *
308  * @par Lua函数原型
309  * tcpOffsetIdentify(poses: table) -> table
310  *
311  */
312  ResultWithErrno tcpOffsetIdentify(
313  const std::vector<std::vector<double>> &poses);
314 
315  /**
316  * 三点法标定坐标系
317  *
318  * @param poses 三个点的位姿集合
319  * @param type 类型:\n
320  * 0 - oxy 原点 x轴正方向 xy平面(y轴正方向)\n
321  * 1 - oxz 原点 x轴正方向 xz平面(z轴正方向)\n
322  * 2 - oyz 原点 y轴正方向 yz平面(z轴正方向)\n
323  * 3 - oyx 原点 y轴正方向 yx平面(x轴正方向)\n
324  * 4 - ozx 原点 z轴正方向 zx平面(x轴正方向)\n
325  * 5 - ozy 原点 z轴正方向 zy平面(y轴正方向)\n
326  * @return 坐标系标定结果和标定结果是否有效
327  */
328  ResultWithErrno calibrateCoordinate(
329  const std::vector<std::vector<double>> &poses, int type);
330 
331  /**
332  * 根据圆弧的三个点,计算出拟合成的圆的另一半圆弧的中间点位置
333  *
334  * @param p1 圆弧的起始点
335  * @param p2 圆弧的中间点
336  * @param p3 圆弧的结束点
337  * @param mode 当mode等于1的时候,表示需要对姿态进行圆弧规划;
338  * 当mode等于0的时候,表示不需要对姿态进行圆弧规划
339  * @return 拟合成的圆的另一半圆弧的中间点位置和计算结果是否有效
340  */
341  ResultWithErrno calculateCircleFourthPoint(const std::vector<double> &p1,
342  const std::vector<double> &p2,
343  const std::vector<double> &p3,
344  int mode);
345  /**
346  * @brief forceTrans:
347  * 变换力和力矩的参考坐标系 force_in_b = pose_a_in_b * force_in_a
348  * @param pose_a_in_b: a 坐标系在 b 坐标系的位姿
349  * @param force_in_a: 力和力矩在 a 坐标系的描述
350  * @return force_in_b,力和力矩在 b 坐标系的描述
351  */
352  std::vector<double> forceTrans(const std::vector<double> &pose_a_in_b,
353  const std::vector<double> &force_in_a);
354 
355  /**
356  * @brief 通过距离计算工具坐标系下的位姿增量
357  * @param distances: N 个距离, N >=3
358  * @param position: 距离参考轨迹的保持高度
359  * @param radius: 传感器中心距离末端tcp的等效半径
360  * @param track_scale: 跟踪比例, 设置范围(0, 1], 1表示跟踪更快
361  * @return 基于工具坐标系的位姿增量
362  */
363  std::vector<double> getDeltaPoseBySensorDistance(
364  const std::vector<double> &distances, double position, double radius,
365  double track_scale);
366 
367  /**
368  * @brief changeFTFrame: 变换力和力矩的参考坐标系
369  * @param pose_a_in_b: a 坐标系在 b 坐标系的位姿
370  * @param ft_in_a: 作用在 a 点的力和力矩在 a 坐标系的描述
371  * @return ft_in_b,作用在 b 点的力和力矩在 b 坐标系的描述
372  */
373  std::vector<double> deltaPoseTrans(const std::vector<double> &pose_a_in_b,
374  const std::vector<double> &ft_in_a);
375 
376  /**
377  * @brief addDeltaPose: 计算以给定速度变换单位时间后的位姿
378  * @param pose_a_in_b: 当前时刻 a 相对于 b 的位姿
379  * @param v_in_b: 当前时刻 a 坐标系的速度在 b 的描述
380  * @return pose_in_b, 单位时间后的位姿在 b 的描述
381  */
382  std::vector<double> deltaPoseAdd(const std::vector<double> &pose_a_in_b,
383  const std::vector<double> &v_in_b);
384 
385 protected:
386  void *d_;
387 };
388 using MathPtr = std::shared_ptr<Math>;
389 
390 // clang-format off
391 #define Math_DECLARES \
392  _FUNC(Math, 2, poseAdd, p1, p2) \
393  _FUNC(Math, 2, poseSub, p1, p2) \
394  _FUNC(Math, 3, interpolatePose, p1, p2, alpha) \
395  _FUNC(Math, 2, poseTrans, pose_from, pose_from_to) \
396  _FUNC(Math, 2, poseTransInv, pose_from, pose_to_from) \
397  _FUNC(Math, 1, poseInverse, pose) \
398  _FUNC(Math, 2, poseDistance, p1, p2) \
399  _FUNC(Math, 2, poseAngleDistance, p1, p2) \
400  _FUNC(Math, 3, poseEqual, p1, p2, eps) \
401  _FUNC(Math, 3, transferRefFrame, F_b_a_old, V_in_a, type) \
402  _FUNC(Math, 2, poseRotation, pose, rotv) \
403  _FUNC(Math, 1, rpyToQuaternion, rpy) \
404  _FUNC(Math, 1, quaternionToRpy, quant) \
405  _FUNC(Math, 1, tcpOffsetIdentify, poses) \
406  _FUNC(Math, 2, calibrateCoordinate, poses, type) \
407  _FUNC(Math, 4, calculateCircleFourthPoint, p1, p2, p3, mode) \
408  _FUNC(Math, 2, forceTrans, pose_a_in_b, force_in_a) \
409  _FUNC(Math, 4, getDeltaPoseBySensorDistance, distances, position, radius, track_scale) \
410  _FUNC(Math, 2, deltaPoseTrans, pose_a_in_b, ft_in_a) \
411  _FUNC(Math, 2, deltaPoseAdd, pose_a_in_b, v_in_b)
412 // clang-format on
413 
414 } // namespace common_interface
415 } // namespace arcs
416 #endif
std::shared_ptr< Math > MathPtr
Definition: math.h:388
数据类型的定义
std::tuple< std::vector< double >, int > ResultWithErrno
Definition: type_def.h:659
std::array< double, 3 > Vector3d
Definition: type_def.h:29
Definition: aubo_api.h:17