ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

Take a look at this implementation of a "virtual bumper" that does essentially what you want, but by using comparison of the odometry and pose data to detect collisions (no bump sensors needed!).

The key part of the code that populates the pointcloud is:

  // place obstacle into point cloud 0.25 meters in front of robot
  float val = 0.25;
  memcpy(&pointcloud_.data[0 * pointcloud_.point_step + pointcloud_.fields[0].offset], &val, sizeof(float));
  val = 0.0;
  memcpy(&pointcloud_.data[0 * pointcloud_.point_step + pointcloud_.fields[1].offset], &val, sizeof(float));

  pointcloud_.header.stamp = ros::Time::now();
  pointcloud_pub_.publish(pointcloud_);

  collision_pub_.publish(true);

In the above, an obstacle is placed 0.25 meters in front of the robot (when a collision occurs), and with a Y translation of zero. Note: if you have left and right bumpers, you would add +/-Y numbers for that instead of zero.

To get it to work with gmapping, you could try converting the pointcloud into a laserscan and then publish that (on the same topic as the regular scan). There are tools for this conversion such as http://wiki.ros.org/pointcloud_to_laserscan

Here's the full implementation of the virtual bumper. It's a working standalone node that you could run without modification. It assumes you have pose and odometry data from somewhere. This was tested with the navigation stack and worked fine when used to populate the costmap obstacle layer.

/*********************************************************************
*
* Software License Agreement (BSD License)
*
*  Copyright (c) 2019, Rhoeby Dynamics LLC
*  All rights reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above
*     copyright notice, this list of conditions and the following
*     disclaimer in the documentation and/or other materials provided
*     with the distribution.
*   * Neither the name of the Rhoeby Dynamics nor the names of its
*     contributors may be used to endorse or promote products derived
*     from this software without specific prior written permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
*  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
*  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
*  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
*  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
*  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
*  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
*  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
*  POSSIBILITY OF SUCH DAMAGE.
*
*********************************************************************/

#include "ros/ros.h"
#include "nav_msgs/Odometry.h"
#include "geometry_msgs/Twist.h"
#include "geometry_msgs/PoseWithCovarianceStamped.h"
#include "tf/tf.h"
#include "std_msgs/Bool.h"
#include "sensor_msgs/PointCloud2.h"

class VirtualBumper
{
  public:
    VirtualBumper();
    virtual ~VirtualBumper() {};

  protected:
    void odomCallback(const nav_msgs::Odometry::ConstPtr& twist);
    void poseUpdateCallback(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr& pose);

  private:
    ros::NodeHandle private_nh_;
    ros::NodeHandle node_;
    ros::Subscriber odom_sub_;
    ros::Subscriber poseupdate_sub_;
    ros::Publisher  pointcloud_pub_;
    ros::Publisher collision_pub_;

    geometry_msgs::PoseWithCovarianceStamped prev_pose_;
    geometry_msgs::Twist curr_odom_;
    sensor_msgs::PointCloud2 pointcloud_;

    bool inCollision_;
    int collision_count_limit_;
    double diff_tolerance_linear_;
    double diff_tolerance_angular_;
};

/*---------------------------------------------------------------------
 * main
 *---------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
  ROS_INFO("Hello from VirtualBumper!");

  ros::init(argc, argv, "virtual_bumper");

  VirtualBumper vb;

  ros::spin();
}

/*---------------------------------------------------------------------
 * VirtualBumper()
 *---------------------------------------------------------------------*/
VirtualBumper::VirtualBumper()
  : private_nh_("~")
  , inCollision_(false)
{
  private_nh_.param("collision_count_limit_", collision_count_limit_, 5);
  private_nh_.param("diff_tolerance_linear_", diff_tolerance_linear_, 0.015);
  private_nh_.param("diff_tolerance_angular_", diff_tolerance_angular_, 0.2);

  ROS_INFO("collision_count_limit_: %d", collision_count_limit_);
  ROS_INFO("diff_tolerance_linear_: %f", diff_tolerance_linear_);
  ROS_INFO("diff_tolerance_angular_: %f", diff_tolerance_angular_);

  odom_sub_ = node_.subscribe("odom", 100, &VirtualBumper::odomCallback, this);
  poseupdate_sub_ = node_.subscribe("poseupdate", 100, &VirtualBumper::poseUpdateCallback, this);

  prev_pose_.pose.pose.orientation.w = 1.0;

  pointcloud_.header.frame_id = "base_link";

  pointcloud_.width  = 3;
  pointcloud_.height = 1;
  pointcloud_.fields.resize(3);

  // Set x/y/z as the only fields
  pointcloud_.fields[0].name = "x";
  pointcloud_.fields[1].name = "y";
  pointcloud_.fields[2].name = "z";

  int offset = 0;
  // All offsets are *4, as all field data types are float32
  for (size_t d = 0; d < pointcloud_.fields.size(); ++d, offset += 4)
  {
    pointcloud_.fields[d].count    = 1;
    pointcloud_.fields[d].offset   = offset;
    pointcloud_.fields[d].datatype = sensor_msgs::PointField::FLOAT32;
  }

  pointcloud_.point_step = offset;
  pointcloud_.row_step   = pointcloud_.point_step * pointcloud_.width;

  pointcloud_.data.resize(3 * pointcloud_.point_step);
  pointcloud_.is_bigendian = false;
  pointcloud_.is_dense     = true;

  // z: constant elevation from base frame
  float val = 0.02;
  memcpy(&pointcloud_.data[0 * pointcloud_.point_step + pointcloud_.fields[2].offset], &val, sizeof(float));
  memcpy(&pointcloud_.data[1 * pointcloud_.point_step + pointcloud_.fields[2].offset], &val, sizeof(float));
  memcpy(&pointcloud_.data[2 * pointcloud_.point_step + pointcloud_.fields[2].offset], &val, sizeof(float));

  pointcloud_pub_ = node_.advertise <sensor_msgs::PointCloud2> ("collision_pointcloud", 10);
  collision_pub_ = node_.advertise<std_msgs::Bool>("collision_status", 50);
}

/*----------------------------------------------------------
 * odomCallback()
 *--------------------------------------------------------*/
void VirtualBumper::odomCallback(const nav_msgs::Odometry::ConstPtr& msg)
{
  ROS_DEBUG("twist linear.x: %f, y: %f, angular.z: %f\r", 
           msg->twist.twist.linear.x, msg->twist.twist.linear.y, msg->twist.twist.angular.z);  

  curr_odom_.linear = msg->twist.twist.linear;
  curr_odom_.angular = msg->twist.twist.angular;
}

/*----------------------------------------------------------
 * poseUpdateCallback()
 *--------------------------------------------------------*/
void VirtualBumper::poseUpdateCallback(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr& msg)
{
  ros::Time current_time = ros::Time::now();
  static ros::Time last_time = current_time;

  double vx = curr_odom_.linear.x;
  double vy = curr_odom_.linear.y;
  double vth = curr_odom_.angular.z;
  double th = tf::getYaw(msg->pose.pose.orientation);
  double prev_th = tf::getYaw(prev_pose_.pose.pose.orientation);

  // compute deltas for ded reckoning, given odometry velocities
  double dt = (current_time - last_time).toSec();
  double delta_x = (vx * cos(th) - vy * sin(th)) * dt;
  double delta_y = (vx * sin(th) + vy * cos(th)) * dt;
  double delta_th = vth * dt;

  // compare ded reckoning to current pose
  double diff_x = (prev_pose_.pose.pose.position.x + delta_x) - msg->pose.pose.position.x;
  double diff_y = (prev_pose_.pose.pose.position.y + delta_y) - msg->pose.pose.position.y;
  double diff_th = (prev_th + delta_th) - th;

  if(fabs(diff_x) > diff_tolerance_linear_ || 
     fabs(diff_y) > diff_tolerance_linear_ || 
     fabs(diff_th) > diff_tolerance_angular_) {
    inCollision_ = true;
    ROS_INFO("diff_x: %f, diff_y: %f, diff_th: %f", diff_x, diff_y, diff_th);
  }
  else if(inCollision_) {
    inCollision_ = false;
    ROS_INFO("diff --> no diff");
  }

  static int count = 0;
  if(inCollision_) {
    count++;
    if(count > collision_count_limit_) {
      count = 0;
      ROS_WARN("Robot in collision!");

      // place obstacle into point cloud 0.25 meters in front of robot
      float val = 0.25;
      memcpy(&pointcloud_.data[0 * pointcloud_.point_step + pointcloud_.fields[0].offset], &val, sizeof(float));
      val = 0.0;
      memcpy(&pointcloud_.data[0 * pointcloud_.point_step + pointcloud_.fields[1].offset], &val, sizeof(float));

      pointcloud_.header.stamp = ros::Time::now();
      pointcloud_pub_.publish(pointcloud_);

      collision_pub_.publish(true);
    }
  }
  else {
    count = 0;
    collision_pub_.publish(false);
  }

  prev_pose_.pose = msg->pose;
  prev_pose_.header = msg->header;

  last_time = current_time;
}