movement_publisher.cpp
Go to the documentation of this file.
00001 
00026 #include "sr_movements/movement_publisher.hpp"
00027 
00028 #include <math.h>
00029 #include <sr_utilities/sr_math_utils.hpp>
00030 
00031 namespace shadowrobot
00032 {
00033   MovementPublisher::MovementPublisher(double min_value, double max_value,
00034                                        double rate, unsigned int repetition, unsigned int nb_mvt_step, std::string controller_type)
00035     : nh_tilde("~"), publishing_rate( rate ), repetition(repetition),
00036       min(min_value), max(max_value), last_target_(0.0), nb_mvt_step(nb_mvt_step),
00037       SError_(0.0), MSError_(0.0), n_samples_(0), controller_type(controller_type)
00038   {
00039     pub = nh_tilde.advertise<std_msgs::Float64>("targets", 5);
00040     pub_2 = nh_tilde.advertise<std_msgs::Float64>("mse_out", 5);
00041     if(controller_type.compare("sr")==0)
00042     {
00043       //nb_mvt_step is used to set the size of the buffer
00044       sub_ = nh_tilde.subscribe("inputs", nb_mvt_step, &MovementPublisher::calculateErrorCallback, this);
00045     }
00046     else if(controller_type.compare("pr2")==0)
00047     {
00048       sub_ = nh_tilde.subscribe("inputs", nb_mvt_step, &MovementPublisher::pr2_calculateErrorCallback, this);
00049     }
00050     else
00051     {
00052       ROS_WARN_STREAM("Warning: You didn't choose a msg_type to listen. sr_robot_msgs was chosen by default");
00053       //nb_mvt_step is used to set the size of the buffer
00054       sub_ = nh_tilde.subscribe("inputs", nb_mvt_step, &MovementPublisher::calculateErrorCallback, this);
00055     }
00056   }
00057 
00058   MovementPublisher::~MovementPublisher()
00059   {}
00060 
00061   void MovementPublisher::start()
00062   {
00063     double last_target = 0.0;
00064 
00065     for(unsigned int i_rep = 0; i_rep < repetition; ++i_rep)
00066     {
00067       for( unsigned int i=0; i<partial_movements.size(); ++i)
00068       {
00069         for(unsigned int j=0; j<nb_mvt_step; ++j)
00070         {
00071           if( !ros::ok() )
00072             return;
00073 
00074           //get the target
00075           msg.data = partial_movements[i].get_target( static_cast<double>(j) / static_cast<double>(nb_mvt_step));
00076 
00077           //there was not target -> resend the last target
00078           if( msg.data == -1.0 )
00079           {
00080             msg.data = last_target;
00081           }
00082           else
00083           {
00084             //interpolate to the correct range
00085             msg.data = min + msg.data * (max - min);
00086           }
00087           //publish the message
00088           pub.publish( msg );
00089 
00090           //wait for a bit
00091           publishing_rate.sleep();
00092 
00093           ros::spinOnce();
00094 
00095           last_target = msg.data;
00096         }
00097       }
00098       //print the error information
00099       ROS_INFO_STREAM("MSE: " << MSError_ << " Error(deg): " << sr_math_utils::to_degrees( sqrt(MSError_) ) );
00100 
00101       //publish the error information
00102       msg.data = MSError_;
00103       pub_2.publish( msg );
00104 
00105       //Reset the error counter
00106       SError_ = 0.0;
00107       n_samples_ = 0;
00108     }
00109   }
00110 
00111   void MovementPublisher::calculateErrorCallback(const sr_robot_msgs::JointControllerState::ConstPtr& msg)
00112   {
00113     double error = msg->set_point - msg->process_value;
00114     ROS_DEBUG_STREAM("Error: " << error);
00115     SError_ = SError_ + ( error * error );
00116     ROS_DEBUG_STREAM("SError: " << SError_);
00117     n_samples_++;
00118     ROS_DEBUG_STREAM("Samples: " << n_samples_);
00119     MSError_ = SError_ / ( static_cast<double>(n_samples_) );
00120     ROS_DEBUG_STREAM("MSe: " << MSError_);
00121   }
00122 
00123   void MovementPublisher::pr2_calculateErrorCallback(const pr2_controllers_msgs::JointControllerState::ConstPtr& msg)
00124   {
00125     double error = msg->set_point - msg->process_value;
00126     ROS_DEBUG_STREAM("Error: " << error);
00127     SError_ = SError_ + ( error * error );
00128     ROS_DEBUG_STREAM("SError: " << SError_);
00129     n_samples_++;
00130     ROS_DEBUG_STREAM("Samples: " << n_samples_);
00131     MSError_ = SError_ / ( static_cast<double>(n_samples_) );
00132     ROS_DEBUG_STREAM("MSe: " << MSError_);
00133   }
00134 
00135   void MovementPublisher::execute_step(int index_mvt_step, int index_partial_movement)
00136   {
00137     if( !ros::ok() )
00138       return;
00139 
00140     //get the target
00141     msg.data = partial_movements[index_partial_movement].get_target( static_cast<double>(index_mvt_step) / static_cast<double>(nb_mvt_step));
00142     //interpolate to the correct range
00143     msg.data = min + msg.data * (max - min);
00144 
00145     //there was not target -> resend the last target
00146     if( msg.data == -1.0 )
00147       msg.data = last_target_;
00148 
00149     //publish the message
00150     pub.publish( msg );
00151 
00152     //wait for a bit
00153     publishing_rate.sleep();
00154 
00155     last_target_ = msg.data;
00156   }
00157 
00158   void MovementPublisher::stop()
00159   {}
00160 
00161   void MovementPublisher::add_movement(PartialMovement mvt)
00162   {
00163     partial_movements.push_back( mvt );
00164   }
00165 
00166   void MovementPublisher::set_publisher(ros::Publisher publisher)
00167   {
00168     pub = publisher;
00169   }
00170 }
00171 
00172 /* For the emacs weenies in the crowd.
00173 Local Variables:
00174    c-basic-offset: 2
00175 End:
00176 */


sr_movements
Author(s): Ugo Cupcic / ugo@shadowrobot.com
autogenerated on Fri Jan 3 2014 12:08:55