JointTrajectoryFollower.cpp
Go to the documentation of this file.
00001 #include <robodyn_controllers/JointTrajectoryFollower.h>
00002 
00003 #include "nasa_common_logging/Logger.h"
00004 /***************************************************************************/
00009 JointTrajectoryFollower::JointTrajectoryFollower()
00010     : velocityFactor(1.)
00011 {
00012     goalManager.setReadyState(false);
00013 }
00014 /***************************************************************************/
00019 void JointTrajectoryFollower::abort(const std::string& msg)
00020 {
00021     goalManager.setReadyState(false);
00022     goalManager.setStatus(actionlib_msgs::GoalStatus::ABORTED);
00023     goalManager.setText(msg);
00024 }
00025 /***************************************************************************/
00030 void JointTrajectoryFollower::preempt(const std::string& msg)
00031 {
00032     goalManager.setReadyState(false);
00033     goalManager.setStatus(actionlib_msgs::GoalStatus::PREEMPTED);
00034     goalManager.setText(msg);
00035 }
00036 /***************************************************************************/
00041 PreplannedJointTrajectoryFollower::PreplannedJointTrajectoryFollower()
00042 {
00043     trajIt = trajectory.points.end();
00044 }
00045 /***************************************************************************/
00053 void PreplannedJointTrajectoryFollower::setTrajectory(const trajectory_msgs::JointTrajectory& trajectoryIn, double timestep_in, double velocityFactorIn)
00054 {
00055     if (isActive())
00056     {
00057         //  active
00058         std::stringstream err;
00059         err << "PreplannedJointTrajectoryFollower::setTrajectory() - currently following a trajectory";
00060         NasaCommonLogging::Logger::log("gov.nasa.controllers.PreplannedJointTrajectoryFollower", log4cpp::Priority::ERROR, err.str());
00061         throw std::runtime_error(err.str());
00062         return;
00063     }
00064 
00065     velocityFactor = velocityFactorIn;
00066     trajectory = trajectoryIn;
00067     trajIt = trajectory.points.begin();
00068     goalManager.reset(true);
00069     goalManager.setGoalId(trajectory.header.stamp, trajectory.header.frame_id);
00070 
00071     if (trajIt == trajectory.points.end())
00072     {
00073         goalManager.setReadyState(false);
00074         goalManager.setStatus(actionlib_msgs::GoalStatus::REJECTED);
00075         goalManager.setText("No points in trajectory.");
00076     }
00077     else if (trajIt->time_from_start != ros::Duration(0.0))
00078     {
00079         goalManager.setReadyState(false);
00080         goalManager.setStatus(actionlib_msgs::GoalStatus::REJECTED);
00081         goalManager.setText("Can't delay start.");
00082     }
00083     else
00084     {
00085         goalManager.setStatus(actionlib_msgs::GoalStatus::PENDING);
00086         goalManager.setText("Joint trajectory received at follower.");
00087     }
00088 
00089 
00090     currentTrajCount = 0;
00091     timestep = timestep_in;
00092 }
00093 /***************************************************************************/
00101 void PreplannedJointTrajectoryFollower::getNextPoint(sensor_msgs::JointState& nextPoint, ros::Duration& timeFromStart)
00102 {
00103     if (!isActive())
00104     {
00105         // not active
00106         std::stringstream err;
00107         err << "PreplannedJointTrajectoryFollower::getNextPoint() - trajectory is inactive";
00108         NasaCommonLogging::Logger::log("gov.nasa.controllers.PreplannedJointTrajectoryFollower", log4cpp::Priority::ERROR, err.str());
00109         throw std::runtime_error(err.str());
00110         return;
00111     }
00112 
00113     if (trajIt == trajectory.points.end())
00114     {
00115         std::stringstream err;
00116         err << "PreplannedJointTrajectoryFollower::getNextPoint() - reached final trajectory point before allotted time";
00117         NasaCommonLogging::Logger::log("gov.nasa.controllers.PreplannedJointTrajectoryFollower", log4cpp::Priority::INFO, err.str());
00118         throw std::runtime_error(err.str());
00119         return;
00120     }
00121 
00122     nextPoint.header.frame_id = trajectory.header.frame_id;
00123 
00124     if (trajectory.joint_names.size() != trajIt->positions.size() || trajectory.joint_names.size() != trajIt->velocities.size()
00125             || trajectory.joint_names.size() != trajIt->accelerations.size())
00126     {
00127         std::stringstream err;
00128         err << "PreplannedJointTrajectoryFollower::getNextPoint() - trajectory names, positions, velocities, accelerations not properly sized";
00129         NasaCommonLogging::Logger::log("gov.nasa.controllers.PreplannedJointTrajectoryFollower", log4cpp::Priority::ERROR, err.str());
00130         throw std::runtime_error(err.str());
00131         return;
00132     }
00133 
00134     if (currentTrajCount == 0)
00135     {
00136         nextPoint.name     = trajectory.joint_names;
00137         nextPoint.position = trajIt->positions;
00138         nextPoint.velocity.resize(trajIt->velocities.size());
00139 
00140         for (unsigned int i = 0; i < trajIt->velocities.size(); ++i)
00141         {
00142             nextPoint.velocity[i] = trajIt->velocities[i] * velocityFactor;
00143         }
00144 
00145         nextPoint.effort = trajIt->accelerations;
00146     }
00147 
00148 
00149     //If timestep is shorter than 30 hz:
00150     while ( trajIt->time_from_start <= ros::Duration(currentTrajCount * timestep))
00151     {
00152         nextPoint.name     = trajectory.joint_names;
00153         nextPoint.position = trajIt->positions;
00154         nextPoint.velocity.resize(trajIt->velocities.size());
00155         timeFromStart = trajIt->time_from_start;
00156 
00157         for (unsigned int i = 0; i < trajIt->velocities.size(); ++i)
00158         {
00159             nextPoint.velocity[i] = trajIt->velocities[i] * velocityFactor;
00160         }
00161 
00162         nextPoint.effort = trajIt->accelerations;
00163         trajIt++;
00164 
00165         if (trajIt == trajectory.points.end())
00166         {
00167             goalManager.setReadyState(false);
00168             goalManager.setStatus(actionlib_msgs::GoalStatus::SUCCEEDED);
00169             goalManager.setText("Joint trajectory finished.");
00170             return;
00171         }
00172 
00173         if (trajIt->time_from_start == ros::Duration(0.0))
00174         {
00175             goalManager.setStatus(actionlib_msgs::GoalStatus::ACTIVE);
00176             goalManager.setText("Joint trajectory sending next point.");
00177             return;
00178         }
00179 
00180         std::stringstream msg;
00181         msg << "PreplannedJointTrajectoryFollower::getNextPoint() - currentTrajCount: " << currentTrajCount << ", timestep: " << timestep << std::endl;
00182         msg << "PreplannedJointTrajectoryFollower::getNextPoint() - found a valid point. trajIt->time_from_start is " << trajIt->time_from_start << " and currentTrajCount*timestep is " << ros::Duration(currentTrajCount * timestep) ;
00183         NasaCommonLogging::Logger::log("gov.nasa.controllers.PreplannedJointTrajectoryFollower", log4cpp::Priority::DEBUG, msg.str());
00184 
00185     }
00186 
00187     std::stringstream msg;
00188     msg << "PreplannedJointTrajectoryFollower::getNextPoint() - sending next point";
00189     NasaCommonLogging::Logger::log("gov.nasa.controllers.PreplannedJointTrajectoryFollower", log4cpp::Priority::INFO, msg.str());
00190 
00191     goalManager.setStatus(actionlib_msgs::GoalStatus::ACTIVE);
00192     goalManager.setText("Joint trajectory sending next point.");
00193 
00194     currentTrajCount++;
00195 
00196 }
00197 /***************************************************************************/
00202 OnlineJointTrajectoryFollower::OnlineJointTrajectoryFollower()
00203     : time(0.), timestep(0.)
00204 {
00205 }
00206 /***************************************************************************/
00215 void OnlineJointTrajectoryFollower::setTrajectory(boost::shared_ptr<RosMsgJointTrajectory> trajectoryIn, double timestep_in, double velocityFactorIn)
00216 {
00217     if (isActive())
00218     {
00219         //  active
00220         std::stringstream err;
00221         err << "OnlineJointTrajectoryFollower::setTrajectory() - currently following a trajectory";
00222         NasaCommonLogging::Logger::log("gov.nasa.controllers.OnlineJointTrajectoryFollower", log4cpp::Priority::ERROR, err.str());
00223         throw std::runtime_error(err.str());
00224         return;
00225     }
00226 
00227     velocityFactor = velocityFactorIn;
00228     trajectory     = trajectoryIn;
00229     goalManager.reset(true);
00230     goalManager.setGoalId(trajectory->getGoalId());
00231     time     = 0.;
00232     timestep = timestep_in;
00233     goalManager.setStatus(actionlib_msgs::GoalStatus::PENDING);
00234     goalManager.setText("Joint trajectory received at follower.");
00235 }
00236 /***************************************************************************/
00244 void OnlineJointTrajectoryFollower::getNextPoint(sensor_msgs::JointState& nextPoint, ros::Duration& timeFromStart)
00245 {
00246     if (!isActive())
00247     {
00248         // not active
00249         std::stringstream err;
00250         err << "OnlineJointTrajectoryFollower::getNextPoint() - trajectory is inactive";
00251         NasaCommonLogging::Logger::log("gov.nasa.controllers.OnlineJointTrajectoryFollower", log4cpp::Priority::ERROR, err.str());
00252         throw std::runtime_error(err.str());
00253         return;
00254     }
00255 
00256     bool deact = false;
00257     time += timestep;
00258 
00259     if (time >= trajectory->getDuration())
00260     {
00261         time = trajectory->getDuration();
00262         deact = true;
00263     }
00264 
00265     nextPoint.header.frame_id = trajectory->getGoalId().id;
00266     nextPoint.name            = trajectory->getJointNames();
00267     trajectory_msgs::JointTrajectoryPoint pose;
00268     trajectory->getPose(time, pose);
00269     nextPoint.position = pose.positions;
00270     nextPoint.velocity.resize(pose.velocities.size());
00271 
00272     for (unsigned int i = 0; i < pose.velocities.size(); ++i)
00273     {
00274         nextPoint.velocity[i] = pose.velocities[i] * velocityFactor;
00275     }
00276 
00277     nextPoint.effort = pose.accelerations;
00278     timeFromStart    = pose.time_from_start;
00279 
00280     if (deact)
00281     {
00282         goalManager.setReadyState(false);
00283         goalManager.setStatus(actionlib_msgs::GoalStatus::SUCCEEDED);
00284         goalManager.setText("Trajectory executed successfully!");
00285     }
00286     else
00287     {
00288         goalManager.setStatus(actionlib_msgs::GoalStatus::ACTIVE);
00289         goalManager.setText("Joint trajectory currently being executed.");
00290     }
00291 }


robodyn_controllers
Author(s):
autogenerated on Sat Jun 8 2019 20:20:53