【发布时间】:2017-10-21 21:40:14
【问题描述】:
我正在尝试编写一个小型库来将 Gazebo 与 Python 接口(我尝试使用 pygazebo 库但没有成功)。我正在尝试从相机获取输出并找到 April Tags 并将位置数据存储在使用 SWIG 包装的 C++ 代码的类变量中,以便在 Python 中使用。我有一个独立的 C++ 应用程序打印这些数据,但我无法让它在一个类中工作。从我的测试来看,sub = node->Subscribe(IMAGE_TOPIC, &april::callback, this); 可能是问题所在。代码如下。
gazeboApril.cpp
#include "gazeboApril.hpp"
april::april(void) {
this->tag_size = (0.3 * (8.0 / 10.0)) / 2.0;
apriltag_family_t *tf = tag36h11_create();
this->td = apriltag_detector_create();
apriltag_detector_add_family(this->td, tf);
gazebo::client::setup();
gazebo::transport::NodePtr node(new gazebo::transport::Node());
gazebo::transport::SubscriberPtr sub;
node->Init();
sub = node->Subscribe(IMAGE_TOPIC, &april::callback, this);
}
void april::callback(ConstImageStampedPtr &msg) {
int width;
int height;
char *data;
width = (int) msg->image().width();
height = (int) msg->image().height();
data = new char[msg->image().data().length() + 1];
memcpy(data, msg->image().data().c_str(), msg->image().data().length());
cv::Mat image(height, width, CV_8UC3, data);
cv::Mat gray;
cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);
image_u8_t im = { .width = gray.cols,
.height = gray.rows,
.stride = gray.cols,
.buf = gray.data
};
zarray_t *detections = apriltag_detector_detect(td, &im);
apriltag_detection *det;
this->id.resize(zarray_size(detections));
this->d.resize(zarray_size(detections));
this->theta.resize(zarray_size(detections));
for (int i = 0; i < zarray_size(detections); i++) {
zarray_get(detections, i, &det);
this->id.at(i) = det->id;
matd_t *pose = homography_to_pose(det->H, -1108.77, 1108.77, 1280 / 2, 720 / 2);
this->d.at(i) = this->tag_size * sqrt(pow(MATD_EL(pose, 0, 3), 2) + pow(MATD_EL(pose, 2, 3), 2));
this->theta.at(i) = atan2(MATD_EL(pose, 0, 3), MATD_EL(pose, 2, 3));
}
delete data;
}
void april::stop(void) {
gazebo::client::shutdown();
}
gazeboApril.hpp
#include <vector>
#include <gazebo/gazebo_client.hh>
#include <gazebo/msgs/msgs.hh>
#include <gazebo/transport/transport.hh>
#include <opencv2/opencv.hpp>
#include "apriltag.h"
#include "tag36h11.h"
#include "common/homography.h"
#pragma once
#define IMAGE_TOPIC "/gazebo/default/pioneer3at/camera/link/camera/image"
class april {
public:
april(void);
void stop(void);
public:
void callback(ConstImageStampedPtr &msg);
public:
std::vector<int> id;
std::vector<double> d;
std::vector<double> theta;
protected:
apriltag_detector_t *td;
double tag_size;
};
gazeboApril.i
%module gazeboApril
%include "typemaps.i"
%include "std_vector.i"
namespace std {
%template(IntVector) vector<int>;
%template(DoubleVector) vector<double>;
};
%{
#include <Python.h>
#include "gazeboApril.hpp"
%}
%naturalvar april::id;
%naturalvar april::d;
%naturalvar april::theta;
%include "gazeboApril.hpp"
【问题讨论】:
-
如果您不提供更多信息,您将不会得到任何有意义的答案。你得到什么错误?编译错误还是运行时错误?请提供尽可能多的信息。
-
我没有收到任何错误。它编译并运行,但从未调用回调函数。
-
你能显示你正在使用的接口文件吗?
-
问题已更新为包含 SWIG 接口文件。
-
我也有同样的问题。你有想过吗?
标签: c++ callback swig gazebo-simu