cartesian_controller_utils.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 <vector>
00020 #include <algorithm>
00021 #include <cob_cartesian_controller/cartesian_controller_utils.h>
00022 
00023 geometry_msgs::Pose CartesianControllerUtils::getPose(const std::string& target_frame, const std::string& source_frame)
00024 {
00025     geometry_msgs::Pose pose;
00026     tf::StampedTransform stamped_transform;
00027 
00028     stamped_transform = getStampedTransform(target_frame, source_frame);
00029 
00030     tf::pointTFToMsg(stamped_transform.getOrigin(), pose.position);
00031     tf::quaternionTFToMsg(stamped_transform.getRotation(), pose.orientation);
00032 
00033     return pose;
00034 }
00035 
00036 tf::StampedTransform CartesianControllerUtils::getStampedTransform(const std::string& target_frame, const std::string& source_frame)
00037 {
00038     tf::StampedTransform stamped_transform;
00039     bool transform = false;
00040 
00041     do
00042     {
00043         try
00044         {
00045             ros::Time now = ros::Time::now();
00046             tf_listener_.waitForTransform(target_frame, source_frame, now, ros::Duration(0.1));
00047             tf_listener_.lookupTransform(target_frame, source_frame, now, stamped_transform);
00048             transform = true;
00049         }
00050         catch (tf::TransformException& ex)
00051         {
00052             ROS_ERROR("CartesianControllerUtils::getStampedTransform: \n%s", ex.what());
00053             ros::Duration(0.1).sleep();
00054         }
00055     } while (!transform && ros::ok());
00056 
00057     return stamped_transform;
00058 }
00059 
00060 void CartesianControllerUtils::transformPose(const std::string source_frame, const std::string target_frame, const geometry_msgs::Pose pose_in, geometry_msgs::Pose& pose_out)
00061 {
00062     bool transform = false;
00063     geometry_msgs::PoseStamped stamped_in, stamped_out;
00064     stamped_in.header.frame_id = source_frame;
00065     stamped_in.pose = pose_in;
00066 
00067     do
00068     {
00069         try
00070         {
00071             ros::Time now = ros::Time::now();
00072             tf_listener_.waitForTransform(target_frame, source_frame, now, ros::Duration(0.1));
00073             tf_listener_.transformPose(target_frame, stamped_in, stamped_out);
00074             pose_out = stamped_out.pose;
00075             transform = true;
00076         }
00077         catch (tf::TransformException& ex)
00078         {
00079             ROS_ERROR("CartesianControllerUtils::transformPose: \n%s", ex.what());
00080             ros::Duration(0.1).sleep();
00081         }
00082     } while (!transform && ros::ok());
00083 }
00084 
00085 
00089 bool CartesianControllerUtils::inEpsilonArea(const tf::StampedTransform& stamped_transform, const double epsilon)
00090 {
00091     double roll, pitch, yaw;
00092     stamped_transform.getBasis().getRPY(roll, pitch, yaw);
00093 
00094     bool x_okay, y_okay, z_okay = false;
00095     bool roll_okay, pitch_okay, yaw_okay = false;
00096 
00097     x_okay      = std::fabs(stamped_transform.getOrigin().x()) < epsilon;
00098     y_okay      = std::fabs(stamped_transform.getOrigin().y()) < epsilon;
00099     z_okay      = std::fabs(stamped_transform.getOrigin().z()) < epsilon;
00100 
00101     roll_okay   = std::fabs(roll)  < epsilon;
00102     pitch_okay  = std::fabs(pitch) < epsilon;
00103     yaw_okay    = std::fabs(yaw)   < epsilon;
00104 
00105     if (x_okay && y_okay && z_okay && roll_okay && pitch_okay && yaw_okay)
00106     {
00107         return true;
00108     }
00109     else
00110     {
00111         return false;
00112     }
00113 }
00114 
00115 void CartesianControllerUtils::poseToRPY(const geometry_msgs::Pose& pose, double& roll, double& pitch, double& yaw)
00116 {
00117     tf::Quaternion q;
00118     tf::quaternionMsgToTF(pose.orientation, q);
00119     tf::Matrix3x3(q).getRPY(roll, pitch, yaw);
00120 }
00121 
00122 void CartesianControllerUtils::previewPath(const geometry_msgs::PoseArray pose_array)
00123 {
00124     visualization_msgs::Marker marker;
00125     marker.type = visualization_msgs::Marker::SPHERE;
00126     marker.lifetime = ros::Duration();
00127     marker.action = visualization_msgs::Marker::ADD;
00128     marker.header = pose_array.header;
00129     marker.ns = "preview";
00130     marker.scale.x = 0.01;
00131     marker.scale.y = 0.01;
00132     marker.scale.z = 0.01;
00133     marker.color.r = 1.0;
00134     marker.color.g = 0.0;
00135     marker.color.b = 1.0;
00136     marker.color.a = 1.0;
00137     
00138     // clear marker_array_ to only show preview of current path
00139     marker_array_.markers.clear();
00140 
00141     double id = marker_array_.markers.size();
00142 
00143     for (unsigned int i = 0; i < pose_array.poses.size(); i++)
00144     {
00145         marker.id = id + i;
00146         marker.pose = pose_array.poses.at(i);
00147         marker_array_.markers.push_back(marker);
00148     }
00149 
00150     marker_pub_.publish(marker_array_);
00151 }
00152 
00153 void CartesianControllerUtils::adjustArrayLength(std::vector<cob_cartesian_controller::PathArray>& m)
00154 {
00155     unsigned int max_steps = 0;
00156     for (unsigned int i = 0; i < m.size(); i++)
00157     {
00158         max_steps = std::max((unsigned int)m[i].array_.size(), max_steps);
00159     }
00160 
00161     for (unsigned int i = 0; i < m.size(); i++)
00162     {
00163         if (m[i].array_.size() < max_steps)
00164         {
00165             m[i].array_.resize(max_steps, m[i].array_.back());
00166         }
00167     }
00168 }
00169 
00170 void CartesianControllerUtils::copyMatrix(std::vector<double>* path_array, std::vector<cob_cartesian_controller::PathArray>& m)
00171 {
00172     for (unsigned int i = 0; i < m.size(); i++)
00173     {
00174         path_array[i] = m[i].array_;
00175     }
00176 }
00177 
00178 double CartesianControllerUtils::roundUpToMultiplier(const double numberToRound, const double multiplier)
00179 {
00180     return ( multiplier - std::fmod(numberToRound, multiplier) + numberToRound );
00181 }


cob_cartesian_controller
Author(s): Christoph Mark
autogenerated on Thu Jun 6 2019 21:19:40