After removing collision objects from the world they still remain in RVIZ
Hi all,
I need to remove the collision objects from the world instantaneously. I have the detected objects from the darknet_ros and want to include them in the scene as collision objects. After they are no longer anymore in the scene would like to remove them from the world in RVIZ. I check this ROS Answer ROS answers, but it still not working properly. Sometimes the collision objects still remain in RVIZ although they are not anymore in the scene. Here is the code
void chatterCallback(const darknet_ros_3d_msgs::BoundingBoxes3d& boxes)
{
int counter_id = 0;
for(auto bb : boxes.bounding_boxes)
{
//moveit_visual_tools::MoveItVisualTools visual_tools("BOX_");
const std::string PLANNING_GROUP = "crane_control";
moveit::planning_interface::PlanningSceneInterface planning_scene_interface;
//sleep(1);
shape_msgs::SolidPrimitive primitive;
primitive.type = primitive.BOX;
primitive.dimensions.resize(3);
string str= to_string(counter_id++);
moveit_msgs::CollisionObject collision_object;
collision_object.header.frame_id = "world";
collision_object.id = "BOX_" + str;
//collision_object.header.stamp = ros::Time::now();
//collision_object.header.stamp = boxes.header.stamp;
//Pose
geometry_msgs::Pose box_pose;
box_pose.position.x = (bb.xmax + bb.xmin)/2;
box_pose.position.y = (bb.ymax + bb.ymin)/2;
box_pose.position.z = (bb.zmax + bb.zmin)/2;
box_pose.orientation.w = 1.0;
//Dimension
primitive.dimensions[0] = (bb.xmax - bb.xmin);
primitive.dimensions[1] = (bb.ymax - bb.ymin);
primitive.dimensions[2] = (bb.zmax - bb.zmin);
//Collision object
/*collision_object.primitives.push_back(primitive);
collision_object.primitive_poses.push_back(box_pose);
collision_object.operation = collision_object.ADD;
std::vector<moveit_msgs::CollisionObject> collision_objects;
collision_objects.push_back(collision_object);
planning_scene_interface.applyCollisionObjects(collision_objects);
sleep(0.4);*/
//Collision object
collision_object.primitives.push_back(primitive);
collision_object.primitive_poses.push_back(box_pose);
//Remove
//collision_object.operation = collision_object.Remove;
bool applyCollisionObject(const moveit_msgs::CollisionObject& collision_objects);
collision_object.operation = collision_object.REMOVE;
//std::vector<std::string> object_ids;
//object_ids.push_back("BOX_" + str);
//planning_scene_interface.removeCollisionObjects(object_ids);
//sleep(0.5);
//Collision object
//collision_object.primitives.push_back(primitive);
//collision_object.primitive_poses.push_back(box_pose);
collision_object.operation = collision_object.ADD;
std::vector<moveit_msgs::CollisionObject> collision_objects;
collision_objects.push_back(collision_object);
planning_scene_interface.applyCollisionObjects(collision_objects);
sleep(0.4);
}
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "cpp_subscriber");
ros::NodeHandle n;
//moveit_visual_tools::MoveItVisualTools visual_tools("BOX_");
ros::Subscriber sub = n.subscribe("/darknet_ros_3d/bounding_boxes", 10, chatterCallback);// bese 5
//markers_pub_ = n.advertise<visualization_msgs::MarkerArray> ("msg_marker", 1);
ros::spin();
}
Any help? Thanks