ros_conversions.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (Apache License)
00003  *
00004  * Copyright (c) 2016, Southwest Research Institute
00005  *
00006  * Licensed under the Apache License, Version 2.0 (the "License");
00007  * you may not use this file except in compliance with the License.
00008  * You may obtain a copy of the License at
00009  *
00010  * http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 /*
00019  *  ros_conversions.cpp
00020  *
00021  *  Created on: Feb 28, 2016
00022  *  Author: Jonathan Meyer
00023  */
00024 
00025 #include "descartes_utilities/ros_conversions.h"
00026 #include <algorithm>
00027 #include <console_bridge/console.h>
00028 
00035 static double minTime(const std::vector<double>& pose_a, const std::vector<double>& pose_b, double max_vel)
00036 {
00037   std::vector<double> diff;
00038   diff.reserve(pose_a.size());
00039 
00040   // compute joint-wise minimum time required based on relative distance between joint positions
00041   // and the maximum allowable joint velocity
00042   std::transform(pose_a.begin(), pose_a.end(), pose_b.begin(), std::back_inserter(diff), [max_vel](double a, double b)
00043                  {
00044                    return std::abs(a - b) / max_vel;
00045                  });
00046   // The biggest time across all of the joints is the min time
00047   return *std::max_element(diff.begin(), diff.end());
00048 }
00049 
00050 bool descartes_utilities::toRosJointPoints(const descartes_core::RobotModel& model,
00051                                            const std::vector<descartes_core::TrajectoryPtPtr>& joint_traj,
00052                                            double default_joint_vel,
00053                                            std::vector<trajectory_msgs::JointTrajectoryPoint>& out)
00054 {
00055   if (default_joint_vel <= 0.0)
00056   {
00057     logError("%s: Invalid value for default joint velocity. Must be > 0 (radians/second)", __FUNCTION__);
00058     return false;
00059   }
00060 
00061   const static double max_default_joint_velocity = 100.0;  // (radians / s); approx 1000 rpm
00062   if (default_joint_vel > max_default_joint_velocity)
00063   {
00064     logError("%s: Default joint velocity of %f exceeds assumed limit of %f.", __FUNCTION__, default_joint_vel,
00065              max_default_joint_velocity);
00066     return false;
00067   }
00068 
00069   ros::Duration from_start(0.0);
00070   std::vector<trajectory_msgs::JointTrajectoryPoint> ros_trajectory;
00071   ros_trajectory.reserve(joint_traj.size());
00072 
00073   std::vector<double> joint_point;
00074   std::vector<double> dummy;
00075 
00076   for (std::size_t i = 0; i < joint_traj.size(); ++i)
00077   {
00078     if (!joint_traj[i])
00079     {
00080       logError("%s: Input trajectory contained null pointer at index %lu", __FUNCTION__, static_cast<unsigned long>(i));
00081       return false;
00082     }
00083 
00084     descartes_core::TrajectoryPt& pt = *joint_traj[i];
00085 
00086     if (!pt.getNominalJointPose(dummy, model, joint_point))
00087     {
00088       logError("%s: Failed to extract joint positions from input trajectory at index %lu", __FUNCTION__,
00089                static_cast<unsigned long>(i));
00090       return false;
00091     }
00092 
00093     trajectory_msgs::JointTrajectoryPoint ros_pt;
00094     ros_pt.positions = joint_point;
00095     // Descartes has no internal representation of velocity, acceleration, or effort so we fill these field with zeros.
00096     ros_pt.velocities.resize(joint_point.size(), 0.0);
00097     ros_pt.accelerations.resize(joint_point.size(), 0.0);
00098     ros_pt.effort.resize(joint_point.size(), 0.0);
00099 
00100     if (pt.getTiming().isSpecified())
00101     {
00102       from_start += ros::Duration(pt.getTiming().upper);
00103     }
00104     else
00105     {
00106       // If we have a previous point, compute dt based on default, max joint velocity
00107       // otherwise trajectory starts at current location (time offset == 0).
00108       double dt;
00109       if (i == 0)
00110         dt = 0.0;
00111       else
00112         dt = minTime(joint_point, ros_trajectory.back().positions, default_joint_vel);
00113 
00114       from_start += ros::Duration(dt);
00115     }
00116 
00117     ros_pt.time_from_start = from_start;
00118     ros_trajectory.push_back(ros_pt);
00119   }
00120 
00121   out = ros_trajectory;
00122   return true;
00123 }


descartes_utilities
Author(s):
autogenerated on Thu Jun 6 2019 21:36:15