arm_manip_node.h
Go to the documentation of this file.
00001 /******************************************************************************
00002  * \file but_arm_manipulation_node.h
00003  * \brief Definition of ManualArmManipActionServer and CArmManipulationEditor classes.
00004  * \author Zdenek Materna (imaterna@fit.vutbr.cz)
00005  *
00006  * $Id:$
00007  *
00008  * Copyright (C) Brno University of Technology
00009  *
00010  * This file is part of software developed by dcgm-robotics@FIT group.
00011  *
00012  * Author: Zdenek Materna (imaterna@fit.vutbr.cz)
00013  * Supervised by: Michal Spanel (spanel@fit.vutbr.cz)
00014  * Date: dd/mm/2012
00015  * 
00016  * This file is free software: you can redistribute it and/or modify
00017  * it under the terms of the GNU Lesser General Public License as published by
00018  * the Free Software Foundation, either version 3 of the License, or
00019  * (at your option) any later version.
00020  * 
00021  * This file is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  * GNU Lesser General Public License for more details.
00025  * 
00026  * You should have received a copy of the GNU Lesser General Public License
00027  * along with this file.  If not, see <http://www.gnu.org/licenses/>.
00028  */
00029 
00037 #pragma once
00038 #ifndef BUT_ARMNAVIGATION_NODE_H
00039 #define BUT_ARMNAVIGATION_NODE_H
00040 
00041 
00042 #include <ros/ros.h>
00043 #include "srs_assisted_arm_navigation/move_arm_utils/move_arm_utils.h"
00044 #include <assert.h>
00045 #include <unistd.h>
00046 #include <time.h>
00047 #include <boost/circular_buffer.hpp>
00048 #include <boost/thread.hpp>
00049 #include <geometry_msgs/Vector3.h>
00050 #include "math.h"
00051 
00052 #include "srs_assisted_arm_navigation_msgs/ArmNavNew.h"
00053 #include "srs_assisted_arm_navigation_msgs/ArmNavPlan.h"
00054 #include "srs_assisted_arm_navigation_msgs/ArmNavPlay.h"
00055 #include "srs_assisted_arm_navigation_msgs/ArmNavReset.h"
00056 #include "srs_assisted_arm_navigation_msgs/ArmNavRefresh.h"
00057 #include "srs_assisted_arm_navigation_msgs/ArmNavExecute.h"
00058 #include "srs_assisted_arm_navigation_msgs/ArmNavStart.h"
00059 #include "srs_assisted_arm_navigation_msgs/ArmNavCollObj.h"
00060 #include "srs_assisted_arm_navigation_msgs/ArmNavSetAttached.h"
00061 #include "srs_assisted_arm_navigation_msgs/ArmNavMovePalmLink.h"
00062 #include "srs_assisted_arm_navigation_msgs/ArmNavMovePalmLinkRel.h"
00063 #include "srs_assisted_arm_navigation_msgs/ArmNavSwitchAttCO.h"
00064 #include "srs_assisted_arm_navigation_msgs/ArmNavStep.h"
00065 #include "srs_assisted_arm_navigation_msgs/ArmNavStop.h"
00066 #include "srs_assisted_arm_navigation_msgs/ArmNavSuccess.h"
00067 #include "srs_assisted_arm_navigation_msgs/ArmNavFailed.h"
00068 #include "srs_assisted_arm_navigation_msgs/ArmNavRepeat.h"
00069 #include "srs_assisted_arm_navigation_msgs/ArmNavRemoveCollObjects.h"
00070 #include "srs_assisted_arm_navigation_msgs/AssistedArmNavigationState.h"
00071 
00072 #include "srs_interaction_primitives/ClickablePositions.h"
00073 #include "srs_interaction_primitives/PositionClicked.h"
00074 
00075 #include "srs_assisted_arm_navigation_msgs/ManualArmManipAction.h"
00076 
00077 #include <actionlib/server/simple_action_server.h>
00078 
00079 #include "srs_assisted_arm_navigation/services_list.h"
00080 #include "srs_assisted_arm_navigation/topics_list.h"
00081 
00082 #include "std_msgs/MultiArrayLayout.h"
00083 #include "std_msgs/MultiArrayDimension.h"
00084 #include "std_msgs/Int32MultiArray.h"
00085 #include "sensor_msgs/Joy.h"
00086 #include "std_srvs/Empty.h"
00087 
00088 #include <message_filters/subscriber.h>
00089 #include <message_filters/synchronizer.h>
00090 //#include <message_filters/time_synchronizer.h>
00091 #include <message_filters/sync_policies/approximate_time.h>
00092 #include "geometry_msgs/Vector3.h"
00093 #include <tf/transform_broadcaster.h>
00094 
00095 namespace srs_assisted_arm_navigation {
00096 
00116 static const std::string VIS_TOPIC_NAME = "planning_scene_visualizer_markers";
00117 static const std::string EXECUTE_RIGHT_TRAJECTORY = "none";
00118 static const std::string EXECUTE_LEFT_TRAJECTORY = "/arm_controller/follow_joint_trajectory";
00119 static const std::string LEFT_IK_NAME = "/cob3_arm_kinematics/get_constraint_aware_ik";
00120 static const std::string RIGHT_IK_NAME = "none";
00121 static const std::string NON_COLL_LEFT_IK_NAME = "/cob3_arm_kinematics/get_ik";
00122 static const std::string NON_COLL_RIGHT_IK_NAME = "none";
00123 static const std::string RIGHT_ARM_GROUP = "none";
00124 static const std::string LEFT_ARM_GROUP = "arm";
00125 static const std::string RIGHT_ARM_REDUNDANCY = "none";
00126 static const std::string LEFT_ARM_REDUNDANCY = "none";
00127 static const std::string LEFT_IK_LINK = "arm_7_link";
00128 static const std::string RIGHT_IK_LINK = "none";
00129 static const std::string PLANNER_1_SERVICE_NAME = "/ompl_planning/plan_kinematic_path";
00130 static const std::string PLANNER_2_SERVICE_NAME = "none";
00131 static const std::string LEFT_INTERPOLATE_SERVICE_NAME = "none";
00132 static const std::string RIGHT_INTERPOLATE_SERVICE_NAME = "none";
00133 static const std::string TRAJECTORY_FILTER_1_SERVICE_NAME = "/trajectory_filter_server/filter_trajectory_with_constraints";
00134 static const std::string TRAJECTORY_FILTER_2_SERVICE_NAME = "none";
00135 static const std::string PROXIMITY_SPACE_SERVICE_NAME = "none";
00136 static const std::string PROXIMITY_SPACE_VALIDITY_NAME = "none";
00137 static const std::string PROXIMITY_SPACE_PLANNER_NAME = "none";
00138 static const std::string LIST_CONTROLLERS_SERVICE = "/pr2_controller_manager/list_controllers";
00139 static const std::string LOAD_CONTROLLERS_SERVICE = "/pr2_controller_manager/load_controller";
00140 static const std::string UNLOAD_CONTROLLERS_SERVICE = "/pr2_controller_manager/unload_controller";
00141 static const std::string SWITCH_CONTROLLERS_SERVICE = "/pr2_controller_manager/switch_controller";
00142 static const std::string GAZEBO_ROBOT_MODEL = "robot";
00143 static const std::string ROBOT_DESCRIPTION_PARAM = "robot_description";
00144 static const ros::Duration PLANNING_DURATION = ros::Duration(5.0);
00145 static const std::string SET_PLANNING_SCENE_DIFF_NAME = "environment_server/set_planning_scene_diff";
00146 
00147 static const std::string WORLD_FRAME = "/map";
00148 
00149 
00150 
00151 static const double START_TIMEOUT = 60.0; 
00152 static const double SOLVE_TIMEOUT = (5*60.0); 
00155 static const double INFLATE_BB = 1.0;
00156 
00169 class ManualArmManipActionServer{
00170 
00171 public:
00172 
00179   ManualArmManipActionServer(std::string name):
00180     server_(nh_, name, boost::bind(&ManualArmManipActionServer::executeCB, this, _1), false), action_name_(name) {
00181 
00182     inited_ = false;
00183     state_ = S_NONE;
00184 
00185     start_timeout_ = ros::Duration(START_TIMEOUT);
00186     solve_timeout_ = ros::Duration(SOLVE_TIMEOUT);
00187 
00188     server_.start(); 
00190   }
00191 
00192   ~ManualArmManipActionServer(void) {};
00193 
00194 
00196   enum {S_NONE,S_NEW,S_PLAN,S_EXECUTE,S_RESET,S_SUCCESS,S_FAILED, S_REPEAT};
00197 
00203   unsigned int get_state() {
00204 
00205           return state_;
00206 
00207   }
00208 
00209   void srv_set_state(unsigned int n);
00210 
00211   ros::Duration start_timeout_; 
00212   ros::Duration solve_timeout_; 
00215   protected:
00216 
00217     unsigned int state_;             
00218     unsigned int new_state_;         
00220     ros::NodeHandle nh_;
00221 
00222 
00224     actionlib::SimpleActionServer<srs_assisted_arm_navigation_msgs::ManualArmManipAction> server_;
00225     std::string action_name_;
00226     srs_assisted_arm_navigation_msgs::ManualArmManipActionFeedback feedback_;
00227     srs_assisted_arm_navigation_msgs::ManualArmManipActionResult result_;
00228 
00229     ros::Time start_time_; 
00230     ros::Time timeout_; 
00232     bool inited_; 
00234     planning_scene_utils::PlanningSceneParameters params_;
00235 
00236 
00237 
00242     void setFeedbackFalse();
00243 
00250     void executeCB(const srs_assisted_arm_navigation_msgs::ManualArmManipGoalConstPtr &goal);
00251 
00252 
00253   private:
00254 
00255 
00256 };
00257 
00258 
00262 typedef struct {
00263 
00264   std::string name;
00265   std::string id;
00266   geometry_msgs::PoseStamped pose;
00267   geometry_msgs::Point bb_lwh;
00268   bool allow_collision;
00269   bool attached;
00270   bool allow_pregrasps;
00271 
00272   std::string topic_name;
00273 
00274 } t_det_obj;
00275 
00284 class CArmManipulationEditor : public planning_scene_utils::PlanningSceneEditor
00285 {
00286 
00287 public:
00288 
00295    CArmManipulationEditor(planning_scene_utils::PlanningSceneParameters& params,  std::vector<string> clist);
00296   ~CArmManipulationEditor();
00297 
00301   virtual void onPlanningSceneLoaded();
00302   virtual void updateState();
00303 
00304   virtual void planCallback(arm_navigation_msgs::ArmNavigationErrorCodes& errorCode);
00305   virtual void filterCallback(arm_navigation_msgs::ArmNavigationErrorCodes& errorCode);
00306 
00307   virtual void attachObjectCallback(const std::string& name);
00308   virtual void selectedTrajectoryCurrentPointChanged( unsigned int new_current_point );
00309 
00314   void createAttachedObj();
00315 
00320   bool ArmNavNew(srs_assisted_arm_navigation_msgs::ArmNavNew::Request &req, srs_assisted_arm_navigation_msgs::ArmNavNew::Response &res);
00321   bool ArmNavPlan(srs_assisted_arm_navigation_msgs::ArmNavPlan::Request &req, srs_assisted_arm_navigation_msgs::ArmNavPlan::Response &res);
00322   bool ArmNavPlay(srs_assisted_arm_navigation_msgs::ArmNavPlay::Request &req, srs_assisted_arm_navigation_msgs::ArmNavPlay::Response &res);
00323   bool ArmNavExecute(srs_assisted_arm_navigation_msgs::ArmNavExecute::Request &req, srs_assisted_arm_navigation_msgs::ArmNavExecute::Response &res);
00324   bool ArmNavReset(srs_assisted_arm_navigation_msgs::ArmNavReset::Request &req, srs_assisted_arm_navigation_msgs::ArmNavReset::Response &res);
00325   bool ArmNavRefresh(srs_assisted_arm_navigation_msgs::ArmNavRefresh::Request &req, srs_assisted_arm_navigation_msgs::ArmNavRefresh::Response &res);
00326 
00327   bool ArmNavSuccess(srs_assisted_arm_navigation_msgs::ArmNavSuccess::Request &req, srs_assisted_arm_navigation_msgs::ArmNavSuccess::Response &res);
00328   bool ArmNavFailed(srs_assisted_arm_navigation_msgs::ArmNavFailed::Request &req, srs_assisted_arm_navigation_msgs::ArmNavFailed::Response &res);
00329   bool ArmNavRepeat(srs_assisted_arm_navigation_msgs::ArmNavRepeat::Request &req, srs_assisted_arm_navigation_msgs::ArmNavRepeat::Response &res);
00330 
00331   bool ArmNavCollObj(srs_assisted_arm_navigation_msgs::ArmNavCollObj::Request &req, srs_assisted_arm_navigation_msgs::ArmNavCollObj::Response &res);
00332   bool ArmNavSetAttached(srs_assisted_arm_navigation_msgs::ArmNavSetAttached::Request &req, srs_assisted_arm_navigation_msgs::ArmNavSetAttached::Response &res);
00333   bool ArmNavMovePalmLink(srs_assisted_arm_navigation_msgs::ArmNavMovePalmLink::Request &req, srs_assisted_arm_navigation_msgs::ArmNavMovePalmLink::Response &res);
00334   bool ArmNavMovePalmLinkRel(srs_assisted_arm_navigation_msgs::ArmNavMovePalmLinkRel::Request &req, srs_assisted_arm_navigation_msgs::ArmNavMovePalmLinkRel::Response &res);
00335   bool ArmNavSwitchACO(srs_assisted_arm_navigation_msgs::ArmNavSwitchAttCO::Request &req, srs_assisted_arm_navigation_msgs::ArmNavSwitchAttCO::Response &res);
00336   bool ArmNavStep(srs_assisted_arm_navigation_msgs::ArmNavStep::Request &req, srs_assisted_arm_navigation_msgs::ArmNavStep::Response &res);
00337   bool ArmNavRemoveCollObjects(srs_assisted_arm_navigation_msgs::ArmNavRemoveCollObjects::Request &req, srs_assisted_arm_navigation_msgs::ArmNavRemoveCollObjects::Response &res);
00338   
00339   bool ArmNavStop(srs_assisted_arm_navigation_msgs::ArmNavStop::Request &req, srs_assisted_arm_navigation_msgs::ArmNavStop::Response &res);
00340 
00341 
00342   void joyCallback(const sensor_msgs::Joy::ConstPtr& joy);
00343 
00344   typedef message_filters::sync_policies::ApproximateTime<geometry_msgs::Vector3, geometry_msgs::Vector3> MySyncPolicy;
00345 
00346   ros::Subscriber offset_sub_;
00347   ros::Subscriber rot_offset_sub_;
00348   ros::Subscriber joy_sub_;
00349 
00350   void spacenavOffsetCallback(const geometry_msgs::Vector3ConstPtr& offset);
00351   void spacenavRotOffsetCallback(const geometry_msgs::Vector3ConstPtr& rot_offset);
00352 
00353 
00354 
00355   //ros::Subscriber joy_sub_;
00356 
00360   void spin_callback(const ros::TimerEvent&);
00361 
00366   void reset();
00367 
00371   void remove_coll_objects();
00372 
00373   bool refresh();
00374 
00376   ManualArmManipActionServer * action_server_ptr_;
00377   tf::TransformListener *tfl_;
00378 
00382   double inflate_bb_;
00383 
00384   bool aco_;
00385 
00386   ros::Publisher gripper_rpy_publisher_;
00387 
00388   std::string world_frame_;
00389 
00390   bool transf(std::string target_frame, geometry_msgs::PoseStamped& pose);
00391 
00392 protected:
00393 
00394   ros::Subscriber sub_click_;
00395 
00396   void subClick(const srs_interaction_primitives::PositionClickedConstPtr &msg);
00397 
00398   void tfTimerCallback(const ros::TimerEvent& ev);
00399 
00400   tf::TransformBroadcaster br_;
00401 
00402   void timerCallback(const ros::TimerEvent& ev);
00403 
00404   void spacenavCallback(const ros::TimerEvent& ev);
00405 
00406   ros::Timer tf_timer_;
00407 
00408   ros::Timer state_timer_;
00409 
00410   ros::Timer spacenav_timer_;
00411 
00412   bool use_spacenav_;
00413 
00414   struct {
00415 
00416           boost::mutex mutex_;
00417 
00418           bool offset_received_;
00419           bool rot_offset_received_;
00420 
00421           bool lock_position_;
00422           bool lock_orientation_;
00423 
00424           geometry_msgs::Vector3 offset;
00425           geometry_msgs::Vector3 rot_offset;
00426 
00427           double max_val_;
00428           double min_val_th_;
00429           double step_;
00430           double rot_step_;
00431           bool use_rviz_cam_;
00432           std::string rviz_cam_link_;
00433 
00434           vector<int32_t> buttons_;
00435 
00436 
00437   } spacenav;
00438 
00439   ros::Publisher arm_nav_state_pub_;
00440 
00441   void normAngle(double& a);
00442 
00443   void processSpaceNav();
00444 
00445 
00446   std::string planning_scene_id; 
00447   unsigned int mpr_id; 
00448   unsigned int traj_id; 
00449   unsigned int filt_traj_id; 
00450   std::vector<std::string> coll_obj_attached_id; 
00451   //std::vector<std::string> coll_obj_bb_id;
00452   bool inited; 
00454   bool planned_;
00455 
00456   std::vector<std::string> links_; 
00458   boost::circular_buffer<geometry_msgs::Pose> * gripper_poses_;
00459 
00460   boost::thread gripper_poses_thread_;
00461 
00462   bool disable_gripper_poses_;
00463 
00464   void GripperPoses();
00465   void GripperPosesClean();
00466 
00467   geometry_msgs::Vector3 GetAsEuler(geometry_msgs::Quaternion quat);
00468 
00469   bool step_used_;
00470 
00471   //boost::mutex im_server_mutex_;
00472 
00486   std::string add_coll_obj_attached(double x, double y, double z, double scx, double scz);
00487   //std::string add_coll_obj_bb(std::string name, geometry_msgs::PoseStamped pose, geometry_msgs::Point bb_lwh, bool coll, bool attached);
00488   std::string add_coll_obj_bb(t_det_obj &obj);
00489 
00490   std::string collision_objects_frame_id_;
00491   bool coll_objects_selectable_;
00492 
00493   //void armHasStoppedMoving();
00494 
00495 
00499   std::vector<t_det_obj> coll_obj_det;
00500 
00501   bool findIK(geometry_msgs::Pose new_pose);
00502 
00503   bool checkPose(geometry_msgs::PoseStamped &p, std::string frame);
00504 
00505   bool joint_controls_;
00506   std::string aco_link_;
00507 
00508   std::string end_eff_link_;
00509   //bool aco_state_;
00510 
00511   struct {
00512 
00513           arm_navigation_msgs::ArmNavigationErrorCodes plan_error_code;
00514           arm_navigation_msgs::ArmNavigationErrorCodes filter_error_code;
00515 
00516           std::string plan_desc;
00517           std::string filter_desc;
00518 
00519           bool out_of_limits;
00520 
00521   } arm_nav_state_;
00522 
00523 
00524 
00525 
00526 private:
00527 
00528 };
00529 
00530 }
00531 
00532 #endif


srs_assisted_arm_navigation
Author(s): Zdenek Materna (imaterna@fit.vutbr.cz), Michal Spanel (spanel@fit.vutbr.cz)
autogenerated on Mon Oct 6 2014 08:12:08