tibi_dabo_servo_move_alg_node.cpp
Go to the documentation of this file.
00001 #include "tibi_dabo_servo_move_alg_node.h"
00002 
00003 TibiDaboServoMoveAlgNode::TibiDaboServoMoveAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<TibiDaboServoMoveAlgorithm>(),
00005   servo_angle_aserver_(public_node_handle_, "servo_angle")
00006 {
00007   //init class attributes if necessary
00008   //this->loop_rate_ = 2;//in [Hz]
00009 
00010   // [init publishers]
00011   
00012   // [init subscribers]
00013   
00014   // [init services]
00015   
00016   // [init clients]
00017   
00018   // [init action servers]
00019   servo_angle_aserver_.registerStartCallback(boost::bind(&TibiDaboServoMoveAlgNode::servo_angleStartCallback, this, _1)); 
00020   servo_angle_aserver_.registerStopCallback(boost::bind(&TibiDaboServoMoveAlgNode::servo_angleStopCallback, this)); 
00021   servo_angle_aserver_.registerIsFinishedCallback(boost::bind(&TibiDaboServoMoveAlgNode::servo_angleIsFinishedCallback, this)); 
00022   servo_angle_aserver_.registerHasSucceedCallback(boost::bind(&TibiDaboServoMoveAlgNode::servo_angleHasSucceedCallback, this)); 
00023   servo_angle_aserver_.registerGetResultCallback(boost::bind(&TibiDaboServoMoveAlgNode::servo_angleGetResultCallback, this, _1)); 
00024   servo_angle_aserver_.registerGetFeedbackCallback(boost::bind(&TibiDaboServoMoveAlgNode::servo_angleGetFeedbackCallback, this, _1)); 
00025   servo_angle_aserver_.start();
00026   
00027   // [init action clients]
00028   
00029    //Servo initialization
00030   event_server=CEventServer::instance();
00031   std::string servo_name="AX-12+_2";
00032   CDynamixelServer *dyn_server=CDynamixelServer::instance();
00033   std::vector<bool> enable(1,true);
00034   servo_found=false;
00035   
00036   ROS_INFO("Initializing servo...");
00037   try{
00038     if(dyn_server->get_num_buses()>0)
00039     {
00040       ROS_INFO("OK");
00041       //servo=new CDynamixelMotor(servo_name,0,1000000,1);
00042       servo=new CDynamixelMotor(servo_name,1,1000000,1);
00043       enable[0]=true;
00044       servo->enable(enable);
00045       #ifdef _HAVE_XSD
00046       servo->load_config(cont_config_file);
00047       events.push_back(servo->get_position_feedback_event());
00048       #else
00049       events.push_back(servo->config_position_feedback(fb_polling,100.0));
00050       #endif
00051       servo->set_torque(100.0);
00052       servo_found=true;
00053       ROS_INFO("Servo is ready");
00054     }
00055   }catch(CException &e){
00056     std::cout << e.what() << std::endl;
00057   }
00058   
00059 }
00060 
00061 TibiDaboServoMoveAlgNode::~TibiDaboServoMoveAlgNode(void)
00062 {
00063   // [free dynamic memory]
00064 }
00065 
00066 void TibiDaboServoMoveAlgNode::mainNodeThread(void)
00067 {  
00068   // [fill msg structures]
00069   
00070   // [fill srv structure and make request to the server]
00071   
00072   // [fill action structure and make request to the action server]
00073 
00074   // [publish messages]
00075 }
00076 
00077 /*  [subscriber callbacks] */
00078 
00079 /*  [service callbacks] */
00080 
00081 /*  [action callbacks] */
00082 void TibiDaboServoMoveAlgNode::servo_angleStartCallback(const control_msgs::FollowJointTrajectoryGoalConstPtr& goal)
00083 { 
00084   alg_.lock(); 
00085     //check goal 
00086     //execute goal
00087     
00088     //ROS_WARN("GOAL RECEIVED");
00089     
00090     if(servo_found){
00091       
00092       std::vector<float> pos(1),vel(1),acc(1);
00093       pos[0]=-goal->trajectory.points[0].positions[0]+150.0;
00094       vel[0]=goal->trajectory.points[0].velocities[0];
00095       acc[0]=vel[0]*vel[0]/(0.05*pos[0]);
00096     
00097       ROS_WARN_STREAM("servo moving to " << pos[0]);
00098       servo->load(pos,vel,acc);
00099       servo->move();
00100     }
00101 
00102     
00103   alg_.unlock(); 
00104 } 
00105 
00106 void TibiDaboServoMoveAlgNode::servo_angleStopCallback(void) 
00107 { 
00108   alg_.lock(); 
00109     //stop action 
00110   alg_.unlock(); 
00111 } 
00112 
00113 bool TibiDaboServoMoveAlgNode::servo_angleIsFinishedCallback(void) 
00114 { 
00115   bool ret = false; 
00116 
00117   alg_.lock(); 
00118     //if action has finish for any reason 
00119     //ret = true; 
00120   alg_.unlock(); 
00121 
00122   return ret; 
00123 } 
00124 
00125 bool TibiDaboServoMoveAlgNode::servo_angleHasSucceedCallback(void) 
00126 { 
00127   bool ret = false; 
00128 
00129   alg_.lock(); 
00130     //if goal was accomplished 
00131     //ret = true; 
00132   alg_.unlock(); 
00133 
00134   return ret; 
00135 } 
00136 
00137 void TibiDaboServoMoveAlgNode::servo_angleGetResultCallback(control_msgs::FollowJointTrajectoryResultPtr& result) 
00138 { 
00139   alg_.lock(); 
00140     //update result data to be sent to client 
00141     //result->data = data; 
00142   alg_.unlock(); 
00143 } 
00144 
00145 void TibiDaboServoMoveAlgNode::servo_angleGetFeedbackCallback(control_msgs::FollowJointTrajectoryFeedbackPtr& feedback) 
00146 { 
00147   alg_.lock(); 
00148     //keep track of feedback 
00149     //ROS_INFO("feedback: %s", feedback->data.c_str()); 
00150     
00151     std::vector<float> pos(1);
00152     feedback->actual.positions.resize(1);
00153     
00154     if(servo_found) pos=servo->get_position();
00155     else pos[0]=150.0;
00156     
00157     //ROS_WARN_STREAM("Sending feedback with pos "<< pos[0]);
00158   
00159     feedback->actual.positions[0]=-(pos[0]-150.0);
00160     
00161   alg_.unlock(); 
00162 }
00163 
00164 /*  [action requests] */
00165 
00166 void TibiDaboServoMoveAlgNode::node_config_update(Config &config, uint32_t level)
00167 {
00168   this->alg_.lock();
00169 
00170   this->alg_.unlock();
00171 }
00172 
00173 void TibiDaboServoMoveAlgNode::addNodeDiagnostics(void)
00174 {
00175 }
00176 
00177 /* main function */
00178 int main(int argc,char *argv[])
00179 {
00180   return algorithm_base::main<TibiDaboServoMoveAlgNode>(argc, argv, "tibi_dabo_servo_move_alg_node");
00181 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


tibi_dabo_servo_move
Author(s): dblasco
autogenerated on Thu Sep 12 2013 13:26:32