-
Notifications
You must be signed in to change notification settings - Fork 76
/
LaserPublisher.cpp
180 lines (158 loc) · 5.58 KB
/
LaserPublisher.cpp
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
#ifdef ADEPT_PKG
#include <Aria.h>
#else
#include <Aria/Aria.h>
#endif
#include "LaserPublisher.h"
#include "ArTimeToROSTime.h"
#include <math.h>
#include <boost/algorithm/string.hpp>
// TODO publish pointcloud of cumulative readings in separate topic?
// TODO generic pointcloud sensor publisher (seprate point cloud stuff there)
// TODO make similar sonar publisher?
LaserPublisher::LaserPublisher(ArLaser *_l, ros::NodeHandle& _n, bool _broadcast_tf, const std::string& _tf_frame, const std::string& _parent_tf_frame, const std::string& _global_tf_frame) :
laserReadingsCB(this, &LaserPublisher::readingsCB),
node(_n),
laser(_l),
tfname(_tf_frame),
parenttfname(_parent_tf_frame),
globaltfname(_global_tf_frame),
broadcast_tf(_broadcast_tf)
{
assert(_l);
laser->lockDevice();
laser->addReadingCB(&laserReadingsCB);
laser->unlockDevice();
std::string laserscan_name(laser->getName());
boost::erase_all(laserscan_name,".");
laserscan_name += "_laserscan";
std::string pointcloud_name(laser->getName());
boost::erase_all(pointcloud_name,".");
pointcloud_name += "_pointcloud";
laserscan_pub = node.advertise<sensor_msgs::LaserScan>(laserscan_name, 20);
pointcloud_pub = node.advertise<sensor_msgs::PointCloud>(pointcloud_name, 50);
tf::Quaternion q;
if(laser->hasSensorPosition())
{
lasertf.setOrigin(tf::Vector3(laser->getSensorPositionX()/1000.0, laser->getSensorPositionY()/1000.0, laser->getSensorPositionZ()/1000.0));
q.setRPY(0, 0, ArMath::degToRad(laser->getSensorPositionTh()));
}
else
{
lasertf.setOrigin(tf::Vector3(0, 0, 0));
q.setRPY(0, 0, 0);
}
lasertf.setRotation(q);
laserscan.header.frame_id = "laser_frame";
laserscan.angle_min = ArMath::degToRad(laser->getStartDegrees());
laserscan.angle_max = ArMath::degToRad(laser->getEndDegrees());
//laserscan.time_increment = ?
laserscan.range_min = 0; //laser->getMinRange() / 1000.0;
laserscan.range_max = laser->getMaxRange() / 1000.0;
pointcloud.header.frame_id = globaltfname;
// Get angle_increment of the laser
laserscan.angle_increment = 0;
if(laser->canSetIncrement()) {
laserscan.angle_increment = laser->getIncrement();
}
else if(laser->getIncrementChoice() != NULL) {
laserscan.angle_increment = laser->getIncrementChoiceDouble();
}
assert(laserscan.angle_increment > 0);
laserscan.angle_increment *= M_PI/180.0;
//readingsCallbackTime = new ArTime;
}
LaserPublisher::~LaserPublisher()
{
laser->lockDevice();
laser->remReadingCB(&laserReadingsCB);
laser->unlockDevice();
//delete readingsCallbackTime;
}
void LaserPublisher::readingsCB()
{
//printf("readingsCB(): %lu ms since last readingsCB() call.\n", readingsCallbackTime->mSecSince());
assert(laser);
laser->lockDevice();
publishLaserScan();
publishPointCloud();
laser->unlockDevice();
if(broadcast_tf)
transform_broadcaster.sendTransform(tf::StampedTransform(lasertf, convertArTimeToROS(laser->getLastReadingTime()), parenttfname, tfname));
//readingsCallbackTime->setToNow();
}
void LaserPublisher::publishLaserScan()
{
laserscan.header.stamp = convertArTimeToROS(laser->getLastReadingTime());
const std::list<ArSensorReading*> *readings = laser->getRawReadings();
assert(readings);
//printf("laserscan: %lu readings\n", readings->size());
laserscan.ranges.resize(readings->size());
size_t n = 0;
if (laser->getFlipped()) {
// Reverse the data
for(std::list<ArSensorReading*>::const_reverse_iterator r = readings->rbegin(); r != readings->rend(); ++r)
{
assert(*r);
if ((*r)->getIgnoreThisReading()) {
laserscan.ranges[n] = -1;
}
else {
laserscan.ranges[n] = (*r)->getRange() / 1000.0;
}
++n;
}
}
else {
for(std::list<ArSensorReading*>::const_iterator r = readings->begin(); r != readings->end(); ++r)
{
assert(*r);
if ((*r)->getIgnoreThisReading()) {
laserscan.ranges[n] = -1;
}
else {
laserscan.ranges[n] = (*r)->getRange() / 1000.0;
}
++n;
}
}
laserscan_pub.publish(laserscan);
}
void LaserPublisher::publishPointCloud()
{
assert(laser);
pointcloud.header.stamp = convertArTimeToROS(laser->getLastReadingTime());
#ifdef ARIACODA
const std::list<ArPoseWithTime> p = laser->getCurrentRangeBuffer().getBuffer();
pointcloud.points.resize(p.size());
size_t n = 0;
for(auto i = p.cbegin(); i != p.cend(); ++i)
{
pointcloud.points[n].x = i->getX() / 1000.0;
pointcloud.points[n].y = i->getY() / 1000.0;
pointcloud.points[n].z = (laser->hasSensorPosition() ? laser->getSensorPositionZ() / 1000.0 : 0.0);
// XXX TODO ^--- is this correct, or should laser position on robot be
// reflected in tf for laser (from URDF or from Aria params if not
// available?) and the height of sensor readings determined by ROS
// client using that?
++n;
}
#else
assert(laser->getCurrentRangeBuffer());
const std::list<ArPoseWithTime*>* p = laser->getCurrentRangeBuffer()->getBuffer();
pointcloud.points.resize(p->size());
size_t n = 0;
for(auto i = p->cbegin(); i != p->cend(); ++i)
{
pointcloud.points[n].x = (*i)->getX() / 1000.0;
pointcloud.points[n].y = (*i)->getY() / 1000.0;
pointcloud.points[n].z = (laser->hasSensorPosition() ? laser->getSensorPositionZ() / 1000.0 : 0.0);
// XXX TODO ^--- is this correct, or should laser position on robot be
// reflected in tf for laser (from URDF or from Aria params if not
// available?) and the height of sensor readings determined by ROS client
// using that?
++n;
}
#endif
pointcloud_pub.publish(pointcloud);
}