特征:逗号初始化动态矩阵

Eigen: Comma initialising dynamic matrix

本文关键字:动态 初始化 特征      更新时间:2023-10-16

我正在尝试用特征矩阵库编写一个软件来计算机械臂的正运动学和逆运动学。我在初始化逗号分隔的动态矩阵时遇到了麻烦。我遇到EXC_BAD_ACCESS LLDB调试器错误。我是LLDB调试器的新手,不太确定热调试使用它。

这是我的main.cpp代码,我的类定义和类RobotArm的实现似乎很好。

#include <iostream>
#include <Eigen/Dense>
#include "RobotArm.h"
using namespace Eigen;
using namespace std;
int main(int argc, const char * argv[])
{
    RobotArm testArm;
    MatrixXd E(5,4);
    E << 0, 0, 0, 10,
         90, 30, 5, 0,
         0, 30, 25, 0,
         0, 30, 25, 0,
         0, 30, 0, 0;
    Vector3d POS = testArm.forwardKinematics(E);
    cout << "Position vector [X Y Z]" << POS << endl;
    return 0;
}

这是我的RobotArm.h

#ifndef ____RobotArm__
#define ____RobotArm__
#include <stdio.h>
#include <Eigen/Dense>
#include <math.h>
using namespace std;
using namespace Eigen;
class RobotArm
{
public:
    //Constructor
    RobotArm();
    Vector3d forwardKinematics(MatrixXd DH);
    VectorXd inversekinematics();
    void homeArm();
private:
    double xPos, yPos, zPos;
    MatrixX4d T;
    MatrixX4d H;
    Vector3d pos;
    MatrixX4d substitute(double alpha, double theta, double a, double d);

};
#endif /* defined(____RobotArm__) */

这是我的RobotArm.cpp

#include "RobotArm.h"
#include <stdio.h>
#include <Eigen/Dense>
#include <cmath>
#include <iostream>
using namespace std;
using namespace Eigen;

RobotArm::RobotArm()
{
    cout << "Successfully created RobotArm object" << endl;
}
Vector3d RobotArm::forwardKinematics(MatrixXd DH)
{
   MatrixX4d H;
   //H = MatrixX4d::Constant(4,4,1);
   H << 1,1,1,1,
        1,1,1,1,
        1,1,1,1,
        1,1,1,1;

    //Update current link parameters
    for (int i = 0; i < 6; i++)
    {
        H *= substitute(DH(i,0), DH(i,1), DH(i,2), DH(i,3));
    }
    pos(0,0) = H(0,3);
    pos(1,0) = H(1,3);
    pos(1,0) = H(2,3);
    return pos;
}
MatrixX4d RobotArm::substitute(double alpha, double theta, double a, double d)
{
    T << cos(theta), -sin(theta), 0, a,
         (sin(theta)*cos(alpha)), (cos(theta)*cos(alpha)), -sin(alpha), (-sin(alpha)*d),
         (sin(theta)*sin(alpha)),(cos(theta)*sin(alpha)), cos(alpha), (cos(alpha)*d),
         0, 0, 0, 1;
    return T;
}

尝试在main.cpp上初始化矩阵E时出现错误

注:该软件仍在开发中。我所张贴的只是为了测试我的类。请建议如何学习使用LLDB调试器。谢谢你。

实际上,您的问题在RobotArm.h和RobotArm.cpp中。MatrixX4d是一个半动态矩阵。你想要的是Matrix4d,它是一个静态大小的4x4矩阵。对于MatrixX4d类型,在调用operator<<之前的大小是0x4,因此尝试分配任何值都会给您访问冲突。

如果你必须使用MatrixX4d,那么确保在使用它之前调整矩阵的大小,例如:

Eigen::MatrixX4d H;
H.resize(whateverSizeYouWantTheOtherDimensionToBe, Eigen::NoChange);