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

时间:2016-09-06 08:51:19

标签: c++ eigen

我正在尝试编写一个软件来计算具有特征矩阵库的机器人手臂的正向和反向运动学。我在初始化逗号分隔的动态矩阵时遇到了麻烦。我遇到了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调试器。谢谢。

1 个答案:

答案 0 :(得分:3)

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

如果您必须使用MatrixX4d,请确保在使用之前调整矩阵的大小,例如:

Eigen::MatrixX4d H;
H.resize(whateverSizeYouWantTheOtherDimensionToBe, Eigen::NoChange);
相关问题