2016-09-06 235 views
0

我想写一个软件来计算机器人手臂的正向和反向运动特征矩阵库。我在初始化逗号分隔的动态矩阵时遇到了问题。我遇到了EXC_BAD_ACCESS LLDB调试器错误。我是LLDB调试器的新手,不太清楚使用它进行热调试。Eigen:逗号初始化动态矩阵

这是我的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调试器。谢谢。

回答

3

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

如果你必须使用一个MatrixX4d,然后确保在使用前调整矩阵,如:

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

谢谢@Avi金斯伯格:)我怕,我需要一个5x4的矩阵作为参数传递给RobotArm ::正向运动学()。所以我需要使用MatrixXd而不是Matrix4d。有什么想法吗? – Vino

+0

谢谢你的回答:)但我仍然有问题 - “断言失败”;我认为,这将是我学习使用LLDB调试器来编译我的代码的一个很好的起点:)欢呼声 – Vino