forked from aschulz90/gunther
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathar_single.cpp
More file actions
357 lines (295 loc) · 12.5 KB
/
ar_single.cpp
File metadata and controls
357 lines (295 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Single Marker Pose Estimation using ARToolkit
* Copyright (C) 2010, CCNY Robotics Lab
* Ivan Dryanovski <ivan.dryanovski@gmail.com>
* William Morris <morris@ee.ccny.cuny.edu>
* Gautier Dumonteil <gautier.dumonteil@gmail.com>
* http://robotics.ccny.cuny.edu
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ar_pose/ar_single.h"
#include <std_msgs/Float32MultiArray.h>
#include <math.h>
#define minimalOffset 0.0000000001
ros::Publisher marker_pub;
std_msgs::Float32MultiArray array;
bool seen_once;
int main (int argc, char **argv)
{
seen_once = false;
ros::init (argc, argv, "ar_single");
ros::NodeHandle n;
ar_pose::ARSinglePublisher arSingle(n);
ros::spin();
return 0;
}
namespace ar_pose
{
ARSinglePublisher::ARSinglePublisher (ros::NodeHandle & n):n_ (n), it_ (n_)
{
std::string local_path;
std::string package_path = ros::package::getPath (ROS_PACKAGE_NAME);
std::string default_path = "data/patt.hiro";
ros::NodeHandle n_param ("~");
XmlRpc::XmlRpcValue xml_marker_center;
ROS_INFO("Starting ArSinglePublisher");
// **** get parameters
if (!n_param.getParam("publish_tf", publishTf_))
publishTf_ = true;
ROS_INFO ("\tPublish transforms: %d", publishTf_);
if (!n_param.getParam("publish_visual_markers", publishVisualMarkers_))
publishVisualMarkers_ = true;
ROS_INFO ("\tPublish visual markers: %d", publishVisualMarkers_);
if (!n_param.getParam("threshold", threshold_))
threshold_ = 100;
ROS_INFO ("\tThreshold: %d", threshold_);
if (!n_param.getParam("marker_width", markerWidth_))
markerWidth_ = 80.0;
ROS_INFO ("\tMarker Width: %.1f", markerWidth_);
if (!n_param.getParam("reverse_transform", reverse_transform_))
reverse_transform_ = false;
ROS_INFO("\tReverse Transform: %d", reverse_transform_);
if (!n_param.getParam("marker_frame", markerFrame_))
markerFrame_ = "ar_marker";
ROS_INFO ("\tMarker frame: %s", markerFrame_.c_str());
// If mode=0, we use arGetTransMat instead of arGetTransMatCont
// The arGetTransMatCont function uses information from the previous image
// frame to reduce the jittering of the marker
if (!n_param.getParam("use_history", useHistory_))
useHistory_ = true;
ROS_INFO("\tUse history: %d", useHistory_);
// n_param.param ("marker_pattern", local_path, std::string ("data/patt.hiro"));
// sprintf (pattern_filename_, "%s/%s", package_path.c_str (), local_path.c_str ());
// ROS_INFO ("\tMarker Pattern Filename: %s", pattern_filename_);
//modifications to allow patterns to be loaded from outside the package
n_param.param ("marker_pattern", local_path, default_path);
if (local_path.compare(0,5,"data/") == 0){
//to keep working on previous implementations, check if first 5 chars equal "data/"
sprintf (pattern_filename_, "%s/%s", package_path.c_str (), local_path.c_str ());
}
else{
//for new implementations, can pass a path outside the package_path
sprintf (pattern_filename_, "%s", local_path.c_str ());
}
n_param.param ("marker_center_x", marker_center_[0], 0.0);
n_param.param ("marker_center_y", marker_center_[1], 0.0);
ROS_INFO ("\tMarker Center: (%.1f,%.1f)", marker_center_[0], marker_center_[1]);
// **** subscribe
ROS_INFO ("Subscribing to info topic");
sub_ = n_.subscribe (cameraInfoTopic_, 1, &ARSinglePublisher::camInfoCallback, this);
getCamInfo_ = false;
// **** advertsie
arMarkerPub_ = n_.advertise<ar_pose::ARMarker>("ar_pose_marker", 0);
marker_pub = n_.advertise<std_msgs::Float32MultiArray>("marker_data", 1);
if(publishVisualMarkers_){
rvizMarkerPub_ = n_.advertise<visualization_msgs::Marker>("visualization_marker", 0);
}
}
ARSinglePublisher::~ARSinglePublisher (void)
{
//cvReleaseImage(&capture); //Don't know why but crash when release the image
arVideoCapStop ();
arVideoClose ();
}
void ARSinglePublisher::camInfoCallback (const sensor_msgs::CameraInfoConstPtr & cam_info)
{
if (!getCamInfo_)
{
cam_info_ = (*cam_info);
cam_param_.xsize = cam_info_.width;
cam_param_.ysize = cam_info_.height;
cam_param_.mat[0][0] = cam_info_.P[0];
cam_param_.mat[1][0] = cam_info_.P[4];
cam_param_.mat[2][0] = cam_info_.P[8];
cam_param_.mat[0][1] = cam_info_.P[1];
cam_param_.mat[1][1] = cam_info_.P[5];
cam_param_.mat[2][1] = cam_info_.P[9];
cam_param_.mat[0][2] = cam_info_.P[2];
cam_param_.mat[1][2] = cam_info_.P[6];
cam_param_.mat[2][2] = cam_info_.P[10];
cam_param_.mat[0][3] = cam_info_.P[3];
cam_param_.mat[1][3] = cam_info_.P[7];
cam_param_.mat[2][3] = cam_info_.P[11];
cam_param_.dist_factor[0] = cam_info_.K[2]; // x0 = cX from openCV calibration
cam_param_.dist_factor[1] = cam_info_.K[5]; // y0 = cY from openCV calibration
cam_param_.dist_factor[2] = -100*cam_info_.D[0]; // f = -100*k1 from CV. Note, we had to do mm^2 to m^2, hence 10^8->10^2
cam_param_.dist_factor[3] = 1.0; // scale factor, should probably be >1, but who cares...
arInit();
ROS_INFO ("Subscribing to image topic");
cam_sub_ = it_.subscribe (cameraImageTopic_, 1, &ARSinglePublisher::getTransformationCallback, this);
getCamInfo_ = true;
}
}
void ARSinglePublisher::arInit ()
{
arInitCparam (&cam_param_);
ROS_INFO ("*** Camera Parameter ***");
arParamDisp (&cam_param_);
// load pattern file
ROS_INFO ("Loading pattern");
patt_id_ = arLoadPatt (pattern_filename_);
if (patt_id_ < 0)
{
ROS_ERROR ("Pattern file load error: %s", pattern_filename_);
ROS_BREAK ();
}
sz_ = cvSize (cam_param_.xsize, cam_param_.ysize);
capture_ = cvCreateImage (sz_, IPL_DEPTH_8U, 4);
}
void ARSinglePublisher::getTransformationCallback (const sensor_msgs::ImageConstPtr & image_msg)
{
ARUint8 *dataPtr;
ARMarkerInfo *marker_info;
int marker_num;
int i, k;
/* Get the image from ROSTOPIC
* NOTE: the dataPtr format is BGR because the ARToolKit library was
* build with V4L, dataPtr format change according to the
* ARToolKit configure option (see config.h).*/
try
{
capture_ = bridge_.imgMsgToCv (image_msg, "bgr8");
}
catch (sensor_msgs::CvBridgeException & e)
{
ROS_ERROR ("Could not convert from '%s' to 'bgr8'.", image_msg->encoding.c_str ());
}
//cvConvertImage(capture_,capture_,CV_CVTIMG_FLIP); //flip image
dataPtr = (ARUint8 *) capture_->imageData;
// detect the markers in the video frame
if (arDetectMarker (dataPtr, threshold_, &marker_info, &marker_num) < 0)
{
ROS_FATAL ("arDetectMarker failed");
ROS_BREAK (); // FIXME: I don't think this should be fatal... -Bill
}
// check for known patterns
k = -1;
for (i = 0; i < marker_num; i++)
{
if (marker_info[i].id == patt_id_)
{
ROS_DEBUG ("Found pattern: %d ", patt_id_);
// make sure you have the best pattern (highest confidence factor)
if (k == -1)
k = i;
else if (marker_info[k].cf < marker_info[i].cf)
k = i;
}
}
if (k != -1)
{
// **** get the transformation between the marker and the real camera
double arQuat[4], arPos[3];
if (!useHistory_ || contF == 0)
arGetTransMat (&marker_info[k], marker_center_, markerWidth_, marker_trans_);
else
arGetTransMatCont (&marker_info[k], marker_trans_, marker_center_, markerWidth_, marker_trans_);
contF = 1;
//arUtilMatInv (marker_trans_, cam_trans);
arUtilMat2QuatPos (marker_trans_, arQuat, arPos);
// **** convert to ROS frame
double quat[4], pos[3];
pos[0] = arPos[0] * AR_TO_ROS;
pos[1] = arPos[1] * AR_TO_ROS;
pos[2] = arPos[2] * AR_TO_ROS;
quat[0] = -arQuat[0];
quat[1] = -arQuat[1];
quat[2] = -arQuat[2];
quat[3] = arQuat[3];
ROS_DEBUG (" QUAT: Pos x: %3.5f y: %3.5f z: %3.5f", pos[0], pos[1], pos[2]);
ROS_DEBUG (" Quat qx: %3.5f qy: %3.5f qz: %3.5f qw: %3.5f", quat[0], quat[1], quat[2], quat[3]);
// **** publish the marker
ar_pose_marker_.header.frame_id = image_msg->header.frame_id;
ar_pose_marker_.header.stamp = image_msg->header.stamp;
ar_pose_marker_.id = marker_info->id;
ar_pose_marker_.pose.pose.position.x = pos[0];
ar_pose_marker_.pose.pose.position.y = pos[1];
ar_pose_marker_.pose.pose.position.z = pos[2];
ar_pose_marker_.pose.pose.orientation.x = quat[0];
ar_pose_marker_.pose.pose.orientation.y = quat[1];
ar_pose_marker_.pose.pose.orientation.z = quat[2];
ar_pose_marker_.pose.pose.orientation.w = quat[3];
ar_pose_marker_.confidence = marker_info->cf;
array.data.clear();
array.data.push_back(arPos[0]); // x - 0
array.data.push_back(arPos[1]); // y - 1
array.data.push_back(arPos[2]); // z - 2
array.data.push_back( atan(arPos[0] / (2*150)) ); //winkel - 3
//if (array.data.at(1) > 0) array.data.push_back( atan(arPos[0] / (-1*2*arPos[1])) );
//else if (array.data.at(1) < 0) array.data.push_back(atan(arPos[0] / (2*arPos[1])) );
//else array.data.push_back( atan(arPos[0] / (2*(arPos[1] + minimalOffset))) );//winkel - 3
ROS_INFO("x: %f y: %f z: %f wi: %f",array.data.at(0),array.data.at(1), array.data.at(2), array.data.at(3));
array.data.push_back(1); // visible - 4
array.data.push_back(1); // seen_once - 5
marker_pub.publish(array);
seen_once = true;
arMarkerPub_.publish(ar_pose_marker_);
ROS_DEBUG ("Published ar_single marker");
// **** publish transform between camera and marker
btQuaternion rotation (quat[0], quat[1], quat[2], quat[3]);
btVector3 origin(pos[0], pos[1], pos[2]);
btTransform t(rotation, origin);
if(publishTf_)
{
if(reverse_transform_)
{
tf::StampedTransform markerToCam (t.inverse(), image_msg->header.stamp, markerFrame_.c_str(), image_msg->header.frame_id);
broadcaster_.sendTransform(markerToCam);
} else {
tf::StampedTransform camToMarker (t, image_msg->header.stamp, image_msg->header.frame_id, markerFrame_.c_str());
broadcaster_.sendTransform(camToMarker);
}
}
// **** publish visual marker
if(publishVisualMarkers_)
{
btVector3 markerOrigin(0, 0, 0.25 * markerWidth_ * AR_TO_ROS);
btTransform m(btQuaternion::getIdentity(), markerOrigin);
btTransform markerPose = t * m; // marker pose in the camera frame
tf::poseTFToMsg(markerPose, rvizMarker_.pose);
rvizMarker_.header.frame_id = image_msg->header.frame_id;
rvizMarker_.header.stamp = image_msg->header.stamp;
rvizMarker_.id = 1;
rvizMarker_.scale.x = 1.0 * markerWidth_ * AR_TO_ROS;
rvizMarker_.scale.y = 1.0 * markerWidth_ * AR_TO_ROS;
rvizMarker_.scale.z = 0.5 * markerWidth_ * AR_TO_ROS;
rvizMarker_.ns = "basic_shapes";
rvizMarker_.type = visualization_msgs::Marker::CUBE;
rvizMarker_.action = visualization_msgs::Marker::ADD;
rvizMarker_.color.r = 0.0f;
rvizMarker_.color.g = 1.0f;
rvizMarker_.color.b = 0.0f;
rvizMarker_.color.a = 1.0;
rvizMarker_.lifetime = ros::Duration(1.0);
rvizMarkerPub_.publish(rvizMarker_);
ROS_DEBUG ("Published visual marker");
}
}
else
{
array.data.clear();
array.data.push_back(0); // x - 0
array.data.push_back(0); // y - 1
array.data.push_back(0); // z - 2
array.data.push_back(0); // Winkel - 3
ROS_INFO("kein Marker sichtbar");
array.data.push_back(0); // visible - 4
if (seen_once) array.data.push_back(1); else array.data.push_back(0); // seen_once - 5
marker_pub.publish(array);
contF = 0;
ROS_DEBUG ("Failed to locate marker");
}
}
} // end namespace ar_pose