qnode.cpp
Go to the documentation of this file.
00001 
00009 /*****************************************************************************
00010 ** Includes
00011 *****************************************************************************/
00012 
00013 #include <ros/ros.h>
00014 #include <ros/network.h>
00015 #include <string>
00016 #include <sstream>
00017 #include "../include/tibi_dabo_sequence_editor/qnode.hpp"
00018 #include "XmlRpcValue.h"
00019 
00020 /*****************************************************************************
00021 ** Namespaces
00022 *****************************************************************************/
00023 
00024 namespace tibi_dabo_sequence_editor {
00025 
00026 /*****************************************************************************
00027 ** Implementation
00028 *****************************************************************************/
00029 
00030 QNode::QNode(int argc, char** argv ) 
00031 {
00032   std::string config_files_list,config_file;
00033   unsigned int pos=0,new_pos=0;
00034 
00035   ros::init(argc,argv,"tibi_dabo_sequence_editor");// initialize ROS
00036   ros::start(); // explicitly needed since our nodehandle is going out of scope.
00037   ros::NodeHandle n(ros::this_node::getName());
00038   // initialize the parameter info
00039   if(n.hasParam("config_files"))
00040   {
00041     n.getParam("config_files", config_files_list);
00042     while((new_pos=config_files_list.find(",",pos))!=std::string::npos)
00043     {
00044       config_file=config_files_list.substr(pos,new_pos-pos);
00045       this->config_files.push_back(config_file);
00046       pos=new_pos+1;
00047     }
00048     config_file=config_files_list.substr(pos);
00049     this->config_files.push_back(config_file);
00050   }
00051   else
00052     emit rosShutdown();
00053 
00054   // Add your ros communications here.
00055   // [init subscribers]
00056   this->motion_feedback_subscriber_ = n.subscribe("joint_feedback", 100, &QNode::motion_feedback_callback, this);
00057   // init action client
00058   this->motion_.client=new SeqClient("motion",true);
00059   this->motion_.active=false;
00060   this->motion_.succeeded=false;
00061   this->motion_.loaded=false;
00062   this->motion_.percentage=0.0;
00063 
00064   // initialize the events
00065   this->event_server=CEventServer::instance();
00066   this->new_feedback_event_id="new_feedback_event_id";
00067   this->event_server->create_event(this->new_feedback_event_id);
00068   this->action_feedback_event_id="action_feedback_event_id";
00069   this->event_server->create_event(this->action_feedback_event_id);
00070  
00071   start();// start the thread
00072 }
00073 
00074 QNode::~QNode() 
00075 {
00076   if(this->new_feedback_event_id!="")
00077   {
00078     this->event_server->delete_event(this->new_feedback_event_id);
00079     this->new_feedback_event_id="";
00080   }
00081   if(this->action_feedback_event_id!="")
00082   {
00083     this->event_server->delete_event(this->action_feedback_event_id);
00084     this->action_feedback_event_id="";
00085   }
00086 
00087   if(ros::isStarted()) 
00088   {
00089     ros::shutdown(); // explicitly needed since we use ros::start();
00090     ros::waitForShutdown();
00091   }
00092   wait();// wait for the thread to end
00093 }
00094 
00095 void QNode::run() // main thread function
00096 {
00097   ros::Rate loop_rate(100);// set the loop rate
00098 
00099   while ( ros::ok() ) 
00100   {
00101     if(!this->motion_.client->isServerConnected())
00102     {
00103     }
00104     // Add your ros communications here.
00105     ros::spinOnce();
00106     loop_rate.sleep();
00107   }
00108   emit rosShutdown(); // used to signal the gui for a shutdown (useful to roslaunch)
00109 }
00110 
00111 void QNode::get_motion_feedback(std::vector<float> &position, std::vector<float> &velocity)
00112 {
00113   unsigned int i=0;
00114 
00115   this->motion_feedback_mutex_.enter();
00116   for(i=0;i<this->position.size();i++)
00117   {
00118     position.push_back(this->position[i]);
00119     velocity.push_back(this->velocity[i]);
00120   }
00121   this->motion_feedback_mutex_.exit();
00122 }
00123 
00124 std::vector<std::string> QNode::get_config_files(void)
00125 {
00126   return this->config_files;
00127 }
00128 
00129 bool QNode::is_connected(void)
00130 {
00131   return this->motion_.client->isServerConnected();
00132 }
00133 
00134 std::string QNode::get_new_feedback_event_id(void)
00135 {
00136   return this->new_feedback_event_id;
00137 }
00138 
00139 std::string QNode::get_action_feedback_event_id(void)
00140 {
00141   return this->action_feedback_event_id;
00142 }
00143 
00144 void QNode::execute_sequence(std::vector<TMotionStep> &seq)
00145 {
00146   control_msgs::FollowJointTrajectoryGoal goal;
00147   unsigned int i=0,j=0;
00148   
00149   // create the internal goal
00150   goal.trajectory.points.resize(seq.size());
00151   for(i=0;i<seq.size();i++)
00152   {
00153     for(j=0;j<seq[i].position.size();j++)
00154     {
00155       goal.trajectory.points[i].positions.push_back(seq[i].position[j]);
00156       goal.trajectory.points[i].velocities.push_back(seq[i].velocity[j]);
00157     }
00158     goal.trajectory.points[i].time_from_start=ros::Duration(seq[i].delay/1000.0);
00159   }
00160   // send the goal
00161   this->motionMakeActionRequest(goal);
00162 }
00163 
00164 void QNode::motion_feedback_callback(const sensor_msgs::JointState::ConstPtr& msg)
00165 {
00166   unsigned int i=0;
00167 
00168   this->motion_feedback_mutex_.enter();
00169 
00170   this->position.clear();
00171   this->velocity.clear();
00172   for(i=0;i<msg->position.size();i++)
00173   {
00174     this->position.push_back(msg->position[i]);
00175     this->velocity.push_back(msg->velocity[i]);
00176   }
00177   if(!this->event_server->event_is_set(this->new_feedback_event_id))
00178     this->event_server->set_event(this->new_feedback_event_id);
00179 
00180   this->motion_feedback_mutex_.exit();
00181 }
00182 
00183 void QNode::motionMakeActionRequest(const control_msgs::FollowJointTrajectoryGoal & motion_goal)
00184 {
00185   ROS_INFO("Starting New Request!");
00186 
00187   //wait for the action server to start 
00188   //will wait for infinite time 
00189   if(this->motion_.client->isServerConnected())
00190   {
00191     ROS_DEBUG("TibiDaboHriAlgNode::ttsMakeActionRequest: TTS Server is Available!");
00192 
00193     //send a goal to the action 
00194     this->motion_.client->sendGoal(motion_goal,
00195                boost::bind(&QNode::motionDone,     this, _1, _2),
00196                boost::bind(&QNode::motionActive,   this),
00197                boost::bind(&QNode::motionFeedback, this, _1));
00198 
00199     this->motion_.active=true;
00200     this->motion_.loaded=true;
00201   }
00202   else
00203   {
00204     throw std::string("No connection to the action server.");
00205   }
00206 }
00207 
00208 void QNode::motionDone(const actionlib::SimpleClientGoalState& state, const control_msgs::FollowJointTrajectoryResultConstPtr& result)
00209 {
00210   ROS_INFO("Motion sequence done");
00211 }
00212 
00213 void QNode::motionActive(void)
00214 {
00215   ROS_INFO("Motion sequence active");
00216 }
00217 
00218 void QNode::motionFeedback(const control_msgs::FollowJointTrajectoryFeedbackConstPtr& feedback)
00219 {
00220   ROS_INFO("Motion sequence feedback");
00221   if(!this->event_server->event_is_set(this->action_feedback_event_id))
00222     this->event_server->set_event(this->action_feedback_event_id);
00223 }
00224 
00225 }  // namespace tibi_dabo_sequence_editor


tibi_dabo_sequence_editor
Author(s): Sergi Hernandez Juan
autogenerated on Fri Dec 6 2013 23:02:16