interactive_frame_target.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 
00018 #include <string>
00019 #include <ros/ros.h>
00020 
00021 #include <cob_frame_tracker/interactive_frame_target.h>
00022 
00023 
00024 bool InteractiveFrameTarget::initialize()
00025 {
00026     ros::NodeHandle nh_tracker("frame_tracker");
00027 
00029     if (nh_tracker.hasParam("update_rate"))
00030     {    nh_tracker.getParam("update_rate", update_rate_);    }
00031     else
00032     {    update_rate_ = 50.0;    }    // hz
00033 
00034     if (nh_.hasParam("chain_tip_link"))
00035     {
00036         nh_.getParam("chain_tip_link", chain_tip_link_);
00037     }
00038     else
00039     {
00040         ROS_ERROR("No chain_tip_link specified. Aborting!");
00041         return false;
00042     }
00043 
00044     if (nh_.hasParam("root_frame"))
00045     {
00046         nh_.getParam("root_frame", root_frame_);
00047     }
00048     else
00049     {
00050         ROS_ERROR("No root_frame specified. Setting to 'base_link'!");
00051         root_frame_ = "base_link";
00052     }
00053 
00054     if (nh_tracker.hasParam("target_frame"))
00055     {
00056         nh_tracker.getParam("target_frame", target_frame_);
00057     }
00058     else
00059     {
00060         ROS_ERROR("No target_frame specified. Aborting!");
00061         return false;
00062     }
00063 
00064     if (nh_tracker.hasParam("movable_trans"))
00065     {    nh_tracker.getParam("movable_trans", movable_trans_);    }
00066     else
00067     {    movable_trans_ = true;    }
00068     if (nh_tracker.hasParam("movable_rot"))
00069     {    nh_tracker.getParam("movable_rot", movable_rot_);    }
00070     else
00071     {    movable_rot_ = true;    }
00072 
00073     tracking_ = false;
00074     lookat_ = false;
00075     tracking_frame_ = chain_tip_link_;
00076 
00077     start_tracking_client_ = nh_tracker.serviceClient<cob_srvs::SetString>("start_tracking");
00078     ROS_INFO("Waiting for StartTrackingServer...");
00079     start_tracking_client_.waitForExistence();
00080 
00081     start_lookat_client_ = nh_tracker.serviceClient<cob_srvs::SetString>("start_lookat");
00082     ROS_INFO("Waiting for StartLookatServer...");
00083     start_lookat_client_.waitForExistence();
00084 
00085     stop_client_ = nh_tracker.serviceClient<std_srvs::Trigger>("stop");
00086     ROS_INFO("Waiting for StopServer...");
00087     stop_client_.waitForExistence();
00088     ROS_INFO("InteractiveFrameTarget: All Services available!");
00089 
00090     bool transform_available = false;
00091     while (!transform_available)
00092     {
00093         try
00094         {
00095             tf_listener_.lookupTransform(root_frame_, tracking_frame_, ros::Time(), target_pose_);
00096             transform_available = true;
00097         }
00098         catch (tf::TransformException& ex)
00099         {
00100             // ROS_WARN("IFT::initialize: Waiting for transform...(%s)",ex.what());
00101             ros::Duration(0.1).sleep();
00102         }
00103     }
00104 
00105     ia_server_ = new interactive_markers::InteractiveMarkerServer("marker_server", "", false);
00106 
00107     int_marker_.header.frame_id = root_frame_;
00108     int_marker_.pose.position.x = target_pose_.getOrigin().x();
00109     int_marker_.pose.position.y = target_pose_.getOrigin().y();
00110     int_marker_.pose.position.z = target_pose_.getOrigin().z();
00111     int_marker_.pose.orientation.x = target_pose_.getRotation().getX();
00112     int_marker_.pose.orientation.y = target_pose_.getRotation().getY();
00113     int_marker_.pose.orientation.z = target_pose_.getRotation().getZ();
00114     int_marker_.pose.orientation.w = target_pose_.getRotation().getW();
00115     int_marker_.name = "interactive_target";
00116     // int_marker_.description = target_frame_;
00117     int_marker_.scale = nh_tracker.param("marker_scale", 0.5);
00118 
00119     visualization_msgs::InteractiveMarkerControl control;
00120     control.always_visible = true;
00121     control.orientation.w = 1;
00122     control.orientation.x = 1;
00123     control.orientation.y = 0;
00124     control.orientation.z = 0;
00125 
00126     visualization_msgs::Marker box_marker;
00127     box_marker.type = visualization_msgs::Marker::CUBE;
00128     box_marker.scale.x = 0.1;
00129     box_marker.scale.y = 0.1;
00130     box_marker.scale.z = 0.1;
00131     box_marker.color.r = 0.0;
00132     box_marker.color.g = 0.5;
00133     box_marker.color.b = 0.5;
00134     box_marker.color.a = 0.5;
00135     visualization_msgs::InteractiveMarkerControl control_3d;
00136     control_3d.always_visible = true;
00137     if (movable_trans_)
00138     {
00139         control.name = "move_x";
00140         control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
00141         int_marker_.controls.push_back(control);
00142         control.name = "move_y";
00143         control.orientation.x = 0;
00144         control.orientation.y = 1;
00145         control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
00146         int_marker_.controls.push_back(control);
00147         control.name = "move_z";
00148         control.orientation.y = 0;
00149         control.orientation.z = 1;
00150         control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
00151         int_marker_.controls.push_back(control);
00152         control_3d.name = "move_3D";
00153         control_3d.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_3D;
00154     }
00155     if (movable_rot_)
00156     {
00157         control.orientation.w = 1;
00158         control.orientation.x = 1;
00159         control.orientation.y = 0;
00160         control.orientation.z = 0;
00161         control.name = "rotate_x";
00162         control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
00163         int_marker_.controls.push_back(control);
00164         control.name = "rotate_y";
00165         control.orientation.x = 0;
00166         control.orientation.y = 1;
00167         control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
00168         int_marker_.controls.push_back(control);
00169         control.name = "rotate_z";
00170         control.orientation.y = 0;
00171         control.orientation.z = 1;
00172         control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
00173         int_marker_.controls.push_back(control);
00174         control_3d.name = "rotate_3D";
00175         control_3d.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_3D;
00176     }
00177     if (movable_trans_ && movable_rot_)
00178     {
00179         control_3d.name = "move_rotate_3D";
00180         control_3d.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_ROTATE_3D;
00181     }
00182     control_3d.markers.push_back(box_marker);
00183     int_marker_.controls.push_back(control_3d);
00184     ia_server_->insert(int_marker_, boost::bind(&InteractiveFrameTarget::markerFeedback,   this, _1));
00185 
00187     menu_handler_.insert("StartTracking", boost::bind(&InteractiveFrameTarget::startTracking,   this, _1));
00188     menu_handler_.insert("StartLookat", boost::bind(&InteractiveFrameTarget::startLookat,   this, _1));
00189     menu_handler_.insert("Stop", boost::bind(&InteractiveFrameTarget::stop,   this, _1));
00190 
00191     int_marker_menu_.header.frame_id = target_frame_;
00192     int_marker_menu_.name = "marker_menu";
00193     int_marker_menu_.pose.position.y = int_marker_.scale;
00194     visualization_msgs::Marker menu_marker;
00195     menu_marker.scale.x = 0.1;
00196     menu_marker.scale.y = 0.1;
00197     menu_marker.scale.z = 0.1;
00198     menu_marker.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
00199     menu_marker.text = nh_.getNamespace() + "_menu";
00200     menu_marker.color.r = 0.0;
00201     menu_marker.color.g = 0.0;
00202     menu_marker.color.b = 1.0;
00203     menu_marker.color.a = 0.85;
00204 
00205     visualization_msgs::InteractiveMarkerControl control_menu;
00206     control_menu.always_visible = true;
00207     control_menu.interaction_mode = visualization_msgs::InteractiveMarkerControl::MENU;
00208     control_menu.name = "menu_control";
00209     // control_menu.description = "InteractiveMenu";
00210     control_menu.markers.push_back(menu_marker);
00211     int_marker_menu_.controls.push_back(control_menu);
00212     ia_server_->insert(int_marker_menu_, boost::bind(&InteractiveFrameTarget::menuFeedback,   this, _1));
00213     menu_handler_.apply(*ia_server_, int_marker_menu_.name);
00214     ia_server_->applyChanges();
00215 
00216     timer_ = nh_.createTimer(ros::Duration(1/update_rate_), &InteractiveFrameTarget::sendTransform,   this);
00217     timer_.start();
00218 
00219     ROS_INFO("INTERACTIVE_MARKER...initialized!");
00220     return true;
00221 }
00222 
00223 void InteractiveFrameTarget::sendTransform(const ros::TimerEvent& event)
00224 {
00225     if (!tracking_ && !lookat_)
00226     {    updateMarker(tracking_frame_);    }
00227 
00228     target_pose_.stamp_ = ros::Time::now();
00229     target_pose_.child_frame_id_ = target_frame_;
00230     tf_broadcaster_.sendTransform(target_pose_);
00231 }
00232 
00233 void InteractiveFrameTarget::updateMarker(const std::string& frame)
00234 {
00235     bool transform_available = false;
00236     while (!transform_available)
00237     {
00238         try
00239         {
00240             tf_listener_.lookupTransform(root_frame_, frame, ros::Time(), target_pose_);
00241             transform_available = true;
00242         }
00243         catch (tf::TransformException& ex)
00244         {
00245             // ROS_WARN("IFT::updateMarker: Waiting for transform...(%s)",ex.what());
00246             ros::Duration(0.1).sleep();
00247         }
00248     }
00249 
00250     geometry_msgs::Pose new_pose;
00251     new_pose.position.x = target_pose_.getOrigin().x();
00252     new_pose.position.y = target_pose_.getOrigin().y();
00253     new_pose.position.z = target_pose_.getOrigin().z();
00254     new_pose.orientation.x = target_pose_.getRotation().getX();
00255     new_pose.orientation.y = target_pose_.getRotation().getY();
00256     new_pose.orientation.z = target_pose_.getRotation().getZ();
00257     new_pose.orientation.w = target_pose_.getRotation().getW();
00258     ia_server_->setPose(int_marker_.name, new_pose);
00259     ia_server_->applyChanges();
00260 }
00261 
00262 void InteractiveFrameTarget::startTracking(const visualization_msgs::InteractiveMarkerFeedbackConstPtr& feedback)
00263 {
00264     cob_srvs::SetString srv;
00265     srv.request.data = target_frame_;
00266     bool success = start_tracking_client_.call(srv);
00267 
00268     if (success && srv.response.success)
00269     {
00270         ROS_INFO_STREAM("StartTracking successful: " << srv.response.message);
00271         tracking_ = true;
00272         lookat_ = false;
00273     }
00274     else
00275     {
00276         ROS_ERROR_STREAM("StartTracking failed: " << srv.response.message);
00277     }
00278 }
00279 
00280 void InteractiveFrameTarget::startLookat(const visualization_msgs::InteractiveMarkerFeedbackConstPtr& feedback)
00281 {
00282     cob_srvs::SetString srv;
00283     srv.request.data = target_frame_;
00284     bool success = start_lookat_client_.call(srv);
00285 
00286     if (success && srv.response.success)
00287     {
00288         ROS_INFO_STREAM("StartLookat successful: " << srv.response.message);
00289 
00290         updateMarker("lookat_focus_frame");
00291 
00292         tracking_ = false;
00293         lookat_ = true;
00294     }
00295     else
00296     {
00297         ROS_ERROR_STREAM("StartLookat failed: " << srv.response.message);
00298     }
00299 }
00300 
00301 void InteractiveFrameTarget::stop(const visualization_msgs::InteractiveMarkerFeedbackConstPtr& feedback)
00302 {
00303     std_srvs::Trigger srv;
00304     bool success = stop_client_.call(srv);
00305 
00306     if (success && srv.response.success)
00307     {
00308         ROS_INFO_STREAM("Stop successful: " << srv.response.message);
00309         tracking_ = false;
00310         lookat_ = false;
00311     }
00312     else
00313     {
00314         ROS_ERROR_STREAM("Stop failed: " << srv.response.message);
00315     }
00316 }
00317 
00318 void InteractiveFrameTarget::menuFeedback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr& feedback)
00319 {
00320     return;
00321 }
00322 
00323 void InteractiveFrameTarget::markerFeedback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr& feedback)
00324 {
00325     target_pose_.stamp_ = feedback->header.stamp;
00326     target_pose_.frame_id_ = feedback->header.frame_id;
00327     target_pose_.child_frame_id_ = target_frame_;
00328     target_pose_.setOrigin(tf::Vector3(feedback->pose.position.x, feedback->pose.position.y, feedback->pose.position.z));
00329     target_pose_.setRotation(tf::Quaternion(feedback->pose.orientation.x, feedback->pose.orientation.y, feedback->pose.orientation.z, feedback->pose.orientation.w));
00330 
00331     ia_server_->applyChanges();
00332 }


cob_frame_tracker
Author(s): Felix Messmer
autogenerated on Thu Jun 6 2019 21:19:08