Skip to content

Commit dd04bf6

Browse files
committed
Working again, and working wayyyy faster
1 parent cfd981b commit dd04bf6

File tree

3 files changed

+124
-30
lines changed

3 files changed

+124
-30
lines changed

octocostmap/include/octocostmap/costmap_3d.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
#include <ros/ros.h>
3939
#include <tf/transform_listener.h>
40+
#include <tf/transform_datatypes.h>
4041
#include <octomap/octomap.h>
4142
#include <octomap_server/OctomapBinary.h>
4243
#include <geometry_msgs/PointStamped.h>
@@ -50,7 +51,7 @@ namespace octocostmap {
5051

5152
void octomapCallback(const octomap_server::OctomapBinary::ConstPtr& map);
5253

53-
bool checkCollisionVolume(const std::vector<geometry_msgs::PointStamped> &collision_volume);
54+
bool checkCollisionVolume(const std::vector<tf::Point > &collision_volume);
5455

5556
bool checkRectangularPrismBase(const geometry_msgs::PointStamped &origin, double width, double height, double length, double resolution);
5657
private:
@@ -60,6 +61,7 @@ namespace octocostmap {
6061
ros::NodeHandle nh_;
6162
ros::NodeHandle priv_nh_;
6263
ros::Subscriber octomap_sub_;
64+
ros::Publisher collision_volume_pub_;
6365
octomap::OcTree octree_;
6466
boost::shared_mutex octree_lock_;
6567
};

octocostmap/src/costmap_3d.cpp

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@
3535
#include <octocostmap/costmap_3d.h>
3636
#include <octomap_server/octomap_server.h>
3737
#include <boost/foreach.hpp>
38+
#include <pcl_ros/point_cloud.h>
39+
#include <pcl/point_types.h>
3840

3941
namespace octocostmap {
4042
Costmap3D::Costmap3D(const std::string &name, tf::TransformListener &tfl):
41-
name_(name), tfl_(tfl), nh_(), priv_nh_("~/" + name_), octree_(0.5), map_frame_("map") {
43+
name_(name), map_frame_("map"), tfl_(tfl), nh_(), priv_nh_("~/" + name_), octree_(0.5) {
4244
octomap_sub_ = nh_.subscribe<octomap_server::OctomapBinary>("octomap", 1, boost::bind(&Costmap3D::octomapCallback, this, _1));
45+
collision_volume_pub_ = nh_.advertise<pcl::PointCloud<pcl::PointXYZ> >("collison_volume_cloud", 1);
4346
}
4447

4548
void Costmap3D::octomapCallback(const octomap_server::OctomapBinary::ConstPtr& map) {
@@ -52,45 +55,65 @@ namespace octocostmap {
5255
}
5356

5457
bool Costmap3D::checkRectangularPrismBase(const geometry_msgs::PointStamped &origin, double width, double height, double length, double resolution) {
55-
std::vector<geometry_msgs::PointStamped> collision_pts;
56-
geometry_msgs::PointStamped temp = origin;
58+
std::vector<tf::Point > collision_pts;
59+
tf::Stamped<tf::Point > temp_origin;
60+
tf::pointStampedMsgToTF(origin, temp_origin);
61+
tf::StampedTransform transform;
5762
double num_pts = width * height * length / (resolution * resolution * resolution);
58-
collision_pts.reserve( (size_t) (num_pts + 1));
59-
for (double dx = 0.0; dx < length; dx += resolution) {
60-
for (double dy = 0.0; dy < width; dy += resolution) {
61-
for (double dz = 0.0; dz < height; dz += resolution) {
62-
temp.point.x = origin.point.x + dx;
63-
temp.point.y = origin.point.y + dy;
64-
temp.point.z = origin.point.z + dz;
65-
collision_pts.push_back(temp);
63+
try {
64+
tfl_.lookupTransform(map_frame_, temp_origin.frame_id_, temp_origin.stamp_, transform);
65+
tf::Point origin_pt = transform * temp_origin;
66+
tf::Transform rotation_only(transform.getRotation());
67+
tf::Vector3 x_vec = rotation_only * tf::Vector3(length,0,0);
68+
tf::Vector3 y_vec = rotation_only * tf::Vector3(0,width,0);
69+
tf::Vector3 z_vec = rotation_only * tf::Vector3(0,0,height);
70+
tf::Point temp;
71+
collision_pts.reserve( (size_t) (num_pts + 1));
72+
double x_resolution = resolution/length;
73+
double y_resolution = resolution/width;
74+
double z_resolution = resolution/height;
75+
for (double dx = 0.0; dx <= 1.0; dx += x_resolution) {
76+
for (double dy = 0.0; dy <= 1.0; dy += y_resolution) {
77+
for (double dz = 0.0; dz <= 1.0; dz += z_resolution) {
78+
/*temp.setX(temp_origin.x() + dx);
79+
temp.setY(temp_origin.y() + dy);
80+
temp.setZ(temp_origin.z() + dz);
81+
*/
82+
temp = origin_pt + dx*x_vec + dy*y_vec + dz*z_vec;
83+
collision_pts.push_back(temp);
84+
}
85+
}
86+
}
87+
if(collision_volume_pub_.getNumSubscribers() > 0) {
88+
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_ptr(new pcl::PointCloud<pcl::PointXYZ>);
89+
cloud_ptr->header.frame_id = map_frame_;
90+
cloud_ptr->header.stamp = temp_origin.stamp_;
91+
BOOST_FOREACH( tf::Point pt, collision_pts) {
92+
cloud_ptr->push_back(pcl::PointXYZ(pt.x(), pt.y(), pt.z()));
6693
}
94+
collision_volume_pub_.publish(cloud_ptr);
6795
}
96+
return checkCollisionVolume(collision_pts);
97+
} catch (tf::TransformException ex) {
98+
ROS_ERROR("%s", ex.what());
99+
//Err on the side of caution if we can't transform a point
100+
return true;
68101
}
69-
return checkCollisionVolume(collision_pts);
70102
}
71103

72-
bool Costmap3D::checkCollisionVolume(const std::vector<geometry_msgs::PointStamped> &collision_volume) {
104+
bool Costmap3D::checkCollisionVolume(const std::vector<tf::Point> &collision_volume) {
73105
uint32_t num_points_checked = 0;
74106
bool retval = false;
75-
geometry_msgs::PointStamped temp;
76107
octomap::OcTreeNode *current_cell;
77108
octree_lock_.lock_shared();
78-
BOOST_FOREACH( geometry_msgs::PointStamped pt, collision_volume) {
79-
try {
80-
tfl_.transformPoint(map_frame_, pt, temp);
81-
current_cell = octree_.search(temp.point.x, temp.point.y, temp.point.z);
82-
num_points_checked++;
83-
if(current_cell) {
84-
if (current_cell->isOccupied()) {
85-
retval = true;
86-
break;
87-
}
109+
BOOST_FOREACH( tf::Point pt, collision_volume) {
110+
current_cell = octree_.search(pt.x(), pt.y(), pt.z());
111+
num_points_checked++;
112+
if(current_cell) {
113+
if (current_cell->isOccupied()) {
114+
retval = true;
115+
break;
88116
}
89-
} catch (tf::TransformException ex) {
90-
ROS_ERROR("%s", ex.what());
91-
//Err on the side of caution if we can't transform a point
92-
retval = true;
93-
break;
94117
}
95118
}
96119
octree_lock_.unlock_shared();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*********************************************************************
2+
* Software License Agreement (BSD License)
3+
*
4+
* Copyright (c) 2011, Eric Perko
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* * Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* * Redistributions in binary form must reproduce the above
14+
* copyright notice, this list of conditions and the following
15+
* disclaimer in the documentation and/or other materials provided
16+
* with the distribution.
17+
* * Neither the name of Eric Perko nor the names of its
18+
* contributors may be used to endorse or promote products derived
19+
* from this software without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*********************************************************************/
34+
35+
#include <ros/ros.h>
36+
#include <tf/transform_listener.h>
37+
#include <geometry_msgs/PointStamped.h>
38+
#include <octocostmap/costmap_3d.h>
39+
40+
void timerCallback(octocostmap::Costmap3D *costmap, const ros::TimerEvent& event) {
41+
ROS_DEBUG("Last callback took %f seconds", event.profile.last_duration.toSec());
42+
geometry_msgs::PointStamped origin;
43+
origin.header.frame_id = "base_link";
44+
origin.header.stamp = ros::Time::now();
45+
origin.point.x = -0.711;
46+
origin.point.y = -0.3048;
47+
origin.point.z = 0.0;
48+
double width = 0.6096;
49+
double length = 1.422;
50+
double height = 2.00;
51+
double resolution = 0.05;
52+
53+
if (costmap->checkRectangularPrismBase(origin, width, height, length, resolution)) {
54+
ROS_DEBUG("collision detected");
55+
}
56+
}
57+
58+
int main(int argc, char *argv[]) {
59+
ros::init(argc,argv, "costmap_3d_tester");
60+
ros::NodeHandle nh;
61+
tf::TransformListener tfl;
62+
octocostmap::Costmap3D costmap("costmap", tfl);
63+
64+
ros::Timer timer = nh.createTimer(ros::Duration(0.1), boost::bind(timerCallback, &costmap, _1));
65+
66+
ros::MultiThreadedSpinner spinner(4); // Use 4 threads
67+
spinner.spin(); // spin() will not return until the node has been shutdown
68+
return 0;
69+
}

0 commit comments

Comments
 (0)