2016-09-06 95 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