特征:逗号初始化动态矩阵
Eigen: Comma initialising dynamic matrix
我正在尝试用特征矩阵库编写一个软件来计算机械臂的正运动学和逆运动学。我在初始化逗号分隔的动态矩阵时遇到了麻烦。我遇到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);
相关文章:
- 动态初始化 C 与 C++ 中的静态值
- 虚拟成员函数的定义是否强制在同一转换单元中动态初始化静态数据成员?
- 使用 constexpr 和 std::array 进行静态初始化来替换动态初始化的 std::vector 的闰年
- 如何在程序中动态初始化对象
- 3D阵列C 的动态初始化
- 无法解决动态初始化 2D 数组的问题
- 保证非 odr 使用的全局变量的延迟动态初始化
- 静态变量的 Constexpr 构造函数会导致动态初始化
- 使用构造函数动态初始化对象与 c++ 中的新函数
- 在某些情况下,如何理解允许实现将非局部变量的动态初始化视为静态初始化
- 指针阵列的动态初始化
- 给定内存地址的内存动态初始化
- 无法动态初始化类中的数组,除非大小 var 在类之外
- 动态初始化
- 抽象类实例的动态初始化
- 静态 const 数组在 MSVC 中动态初始化
- C++编译器在 main 之后实现动态初始化
- 模板结构内枚举类的动态初始化
- 动态初始化的 2D 数组输出错误
- 当静态或动态初始化时