block_manipulation_demo.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <ros/ros.h>
00031 
00032 #include <actionlib/client/simple_action_client.h>
00033 #include <turtlebot_arm_block_manipulation/BlockDetectionAction.h>
00034 #include <turtlebot_arm_block_manipulation/PickAndPlaceAction.h>
00035 #include <turtlebot_arm_block_manipulation/InteractiveBlockManipulationAction.h>
00036 
00037 #include <string>
00038 #include <sstream>
00039 
00040 
00041 const std::string pick_and_place_topic = "/pick_and_place";
00042 
00043 namespace turtlebot_arm_block_manipulation
00044 {
00045 
00046 class BlockManipulationAction
00047 {
00048 private:
00049   ros::NodeHandle nh_;
00050 
00051   // Actions and services
00052   actionlib::SimpleActionClient<BlockDetectionAction> block_detection_action_;
00053   actionlib::SimpleActionClient<InteractiveBlockManipulationAction> interactive_manipulation_action_;
00054   actionlib::SimpleActionClient<PickAndPlaceAction> pick_and_place_action_;
00055 
00056   BlockDetectionGoal block_detection_goal_;
00057   InteractiveBlockManipulationGoal interactive_manipulation_goal_;
00058   PickAndPlaceGoal pick_and_place_goal_;
00059 
00060   // Parameters
00061   std::string arm_link;
00062   double gripper_open;
00063   double gripper_closed;
00064   double z_up;
00065   double z_down;
00066   double block_size;
00067   bool once;
00068 
00069 public:
00070 
00071   BlockManipulationAction() : nh_("~"),
00072     block_detection_action_("block_detection", true),
00073     interactive_manipulation_action_("interactive_manipulation", true),
00074     pick_and_place_action_("pick_and_place", true)
00075   {
00076     // Load parameters
00077     nh_.param<std::string>("arm_link", arm_link, "/arm_link");
00078     nh_.param<double>("gripper_open", gripper_open, 0.042);
00079     nh_.param<double>("gripper_closed", gripper_closed, 0.024);
00080     nh_.param<double>("z_up", z_up, 0.12);
00081     nh_.param<double>("table_height", z_down, 0.01);
00082     nh_.param<double>("block_size", block_size, 0.03);
00083 
00084     nh_.param<bool>("once", once, false);
00085 
00086     // Initialize goals
00087     block_detection_goal_.frame = arm_link;
00088     block_detection_goal_.table_height = z_down;
00089     block_detection_goal_.block_size = block_size;
00090 
00091     pick_and_place_goal_.frame = arm_link;
00092     pick_and_place_goal_.z_up = z_up;
00093     pick_and_place_goal_.gripper_open = gripper_open;
00094     pick_and_place_goal_.gripper_closed = gripper_closed;
00095     pick_and_place_goal_.topic = pick_and_place_topic;
00096 
00097     interactive_manipulation_goal_.block_size = block_size;
00098     interactive_manipulation_goal_.frame = arm_link;
00099 
00100     ROS_INFO("Finished initializing, waiting for servers...");
00101 
00102     block_detection_action_.waitForServer();
00103     ROS_INFO("Found block detection server.");
00104 
00105     interactive_manipulation_action_.waitForServer();
00106     ROS_INFO("Found interactive manipulation.");
00107 
00108     pick_and_place_action_.waitForServer();
00109     ROS_INFO("Found pick and place server.");
00110 
00111     ROS_INFO("Found servers.");
00112   }
00113 
00114   void detectBlocks()
00115   {
00116     block_detection_action_.sendGoal(block_detection_goal_, boost::bind( &BlockManipulationAction::addBlocks, this, _1, _2));
00117   }
00118 
00119   void addBlocks(const actionlib::SimpleClientGoalState& state, const BlockDetectionResultConstPtr& result)
00120   {
00121     ROS_INFO("Got block detection callback. Adding blocks.");
00122     geometry_msgs::Pose block;
00123 
00124     if (state == actionlib::SimpleClientGoalState::SUCCEEDED)
00125       ROS_INFO("Succeeded!");
00126     else
00127     {
00128       ROS_INFO("Did not succeed! %s",  state.toString().c_str());
00129       ros::shutdown();
00130     }
00131     interactive_manipulation_action_.sendGoal(interactive_manipulation_goal_, boost::bind( &BlockManipulationAction::pickAndPlace, this, _1, _2));
00132   }
00133 
00134   void pickAndPlace(const actionlib::SimpleClientGoalState& state, const InteractiveBlockManipulationResultConstPtr& result)
00135   {
00136     ROS_INFO("Got interactive marker callback. Picking and placing.");
00137 
00138     if (state == actionlib::SimpleClientGoalState::SUCCEEDED)
00139       ROS_INFO("Succeeded!");
00140     else
00141     {
00142       ROS_INFO("Did not succeed! %s",  state.toString().c_str());
00143       ros::shutdown();
00144     }
00145     pick_and_place_action_.sendGoal(pick_and_place_goal_, boost::bind( &BlockManipulationAction::finish, this, _1, _2));
00146   }
00147 
00148   void finish(const actionlib::SimpleClientGoalState& state, const PickAndPlaceResultConstPtr& result)
00149   {
00150     ROS_INFO("Got pick and place callback. Finished!");
00151     if (state == actionlib::SimpleClientGoalState::SUCCEEDED)
00152       ROS_INFO("Succeeded!");
00153     else
00154       ROS_INFO("Did not succeed! %s",  state.toString().c_str());
00155 
00156     if (once)
00157       ros::shutdown();
00158     else
00159       detectBlocks();
00160   }
00161 };
00162 
00163 };
00164 
00165 int main(int argc, char** argv)
00166 {
00167   // initialize node
00168   ros::init(argc, argv, "block_manipulation");
00169 
00170   turtlebot_arm_block_manipulation::BlockManipulationAction manip;
00171 
00172   // everything is done in cloud callback, just spin
00173   ros::AsyncSpinner spinner(2);
00174   spinner.start();
00175 
00176   while (ros::ok())
00177   {
00178     manip.detectBlocks();
00179 
00180     // Allow user restarting, for the case that the block detection fails
00181     std::cout << "Press Enter for restarting block detection" << std::endl;
00182     std::cin.ignore();
00183   }
00184 
00185   spinner.stop();
00186 }


turtlebot_arm_block_manipulation
Author(s): Michael Ferguson, Helen Oleynikova
autogenerated on Thu Jun 6 2019 20:54:14