force_robot_companion_learning_alg_node.cpp
Go to the documentation of this file.
00001 #include "force_robot_companion_learning_alg_node.h"
00002 #include <deque>
00003 #include <stdio.h>
00004 //#include <string.h>
00005 
00006 ForceRobotCompanionLearningAlgNode::ForceRobotCompanionLearningAlgNode(void) :
00007   algorithm_base::IriBaseAlgorithm<ForceRobotCompanionLearningAlgorithm>() ,
00008         tf_listener_(ros::Duration(30.f)),
00009     nearest_target_(0),
00010         simulation_state_(ForceRobotCompanionLearningAlgNode::Init_Sim),
00011         number_of_virtual_people_(0),
00012         num_experiment_(0),
00013         results_file_is_open_(false),
00014     learning_mode_( ForceRobotCompanionLearningAlgNode::Density )
00015 {
00016 
00017   //init class attributes if necessary
00018   this->loop_rate_ = 5;//in [Hz]
00019 
00020   // [init publishers]
00021   this->angle_robot_person_publisher_ = this->public_node_handle_.advertise<std_msgs::Float64>("angle_robot_person", 100);
00022   this->person_robot_dist_publisher_ = this->public_node_handle_.advertise<std_msgs::Float64>("person_robot_dist", 100);
00023   //this->target_pred_publisher_ = this->public_node_handle_.advertise<geometry_msgs::PoseArray>("target_pred", 100);
00024   this->target_person_position_publisher_ = this->public_node_handle_.advertise<geometry_msgs::PointStamped>("target_position", 100);
00025   this->robot_position_publisher_ = this->public_node_handle_.advertise<geometry_msgs::PoseStamped>("robot_position", 100);
00026   this->force_param_publisher_ = this->public_node_handle_.advertise<geometry_msgs::PointStamped>("force_param", 100);
00027   this->trajectories_publisher_ = this->public_node_handle_.advertise<visualization_msgs::MarkerArray>("vis/trajectories", 100);
00028   this->forces_publisher_ = this->public_node_handle_.advertise<visualization_msgs::MarkerArray>("vis/forces", 100);
00029   this->destinations_publisher_ = this->public_node_handle_.advertise<visualization_msgs::MarkerArray>("vis/destinations", 100);
00030   this->predictions_publisher_ = this->public_node_handle_.advertise<visualization_msgs::MarkerArray>("vis/predictions", 100);
00031   init_node();
00032   // [init subscribers]
00033 //  this->laser_subscriber_ = this->public_node_handle_.subscribe("laser", 100, &ForceRobotCompanionLearningAlgNode::laser_callback, this);
00034   this->dest_subscriber_ = this->public_node_handle_.subscribe("dest", 100, &ForceRobotCompanionLearningAlgNode::dest_callback, this);
00035   this->tracks_subscriber_ = this->public_node_handle_.subscribe("tracks", 100, &ForceRobotCompanionLearningAlgNode::tracks_callback, this);
00036   
00037   // [init services]
00038   //parameter import example
00039   //private_node_handle.param<std:tring>("scanTopicString", scanTopicString, "scan");
00040   
00041   // [init clients]
00042   reset_client_ = this->public_node_handle_.serviceClient<std_srvs::Empty>("reset");
00043   
00044   // [init action servers]
00045   
00046   // [init action clients]
00047 }
00048 
00049 ForceRobotCompanionLearningAlgNode::~ForceRobotCompanionLearningAlgNode(void)
00050 {
00051   // [free dynamic memory]
00052         if ( results_file_is_open_ )
00053                 fclose( file_results_out_ );
00054 }
00055 
00056 void ForceRobotCompanionLearningAlgNode::mainNodeThread(void)
00057 {
00058 
00059   // [fill msg structures]
00060   //this->Float64MultiArray_msg_.data = my_var;
00061         //prediction
00062     this->alg_.lock();
00063         pred_.scene_intentionality_prediction_bhmip();
00064         //Visualization
00065         now_ = ros::Time::now();
00066         vis_trajectories();
00067         vis_destinations();
00068 //      vis_intentionality_prediction();
00069 //      vis_predictions();
00070         
00071         
00072   // [fill srv structure and make request to the server]
00073 
00074   
00075   // [fill action structure and make request to the action server]
00076 
00077 
00078 
00079     angle_robot_person_msg_.data = target_followed_pose_.angle_heading_point( pred_.get_robot()->get_current_pointV() );
00080         switch( learning_mode_ )
00081         {
00082     // growing density, code not tested under the new structure TODO
00083     case ForceRobotCompanionLearningAlgNode::Density:
00084       growing_density_test();
00085       break;
00086 
00087 
00088     // learning mode: fixed density and learning of parameters
00089     case ForceRobotCompanionLearningAlgNode::Initial_paramters :
00090     default:
00091         initial_params_learning();
00092         break;
00093         }
00094   //ROS_INFO("Robot state = %d || (Init 0, Find 1, Test 2, End 3)  " , simulation_state_);
00095 
00096   this->alg_.unlock();
00097 
00098 
00099   // [publish messages]
00100   this->angle_robot_person_publisher_.publish(this->angle_robot_person_msg_);
00101   this->person_robot_dist_publisher_.publish(this->person_robot_dist_msg_);
00102   force_param_msg_.header.stamp = now_;
00103     this->target_person_position_publisher_.publish(this->target_person_position_msg_);
00104     this->robot_position_publisher_.publish(this->robot_position_msg_);
00105   this->force_param_publisher_.publish(this->force_param_msg_);
00106         this->trajectories_publisher_.publish(this->MarkerArray_trajectories_msg_);
00107         this->forces_publisher_.publish(this->MarkerArray_forces_msg_);
00108         this->destinations_publisher_.publish(this->MarkerArray_destinations_msg_);
00109         this->predictions_publisher_.publish(this->MarkerArray_predictions_msg_);
00110 }
00111 
00112 /*  [subscriber callbacks] */
00113 
00114 /*  [service callbacks] */
00115 
00116 /*  [action callbacks] */
00117 
00118 /*  [action requests] */
00119 
00120 
00121 
00122 
00123 
00124 
00125 
00126 
00127 //common fields, to be filled just once...
00128 void ForceRobotCompanionLearningAlgNode::init_node()
00129 {
00130   pred_.set_dt( 1.0 / 5.0 );
00131   this->public_node_handle_.getParam("reference_frame", target_frame_);
00132         this->public_node_handle_.getParam("force_map_path", force_map_path_);
00133     this->public_node_handle_.getParam("destination_map_path", destination_map_path_);
00134         if ( !pred_.read_force_map(  force_map_path_.c_str() ) )
00135         {
00136                 ROS_WARN("Could not read map force file !!!");
00137         }
00138         this->public_node_handle_.getParam("robot_x_ini", robot_x_ini_);
00139         this->public_node_handle_.getParam("robot_y_ini", robot_y_ini_);
00140 
00141 
00142         if ( !pred_.read_destination_map(  destination_map_path_.c_str() ) )
00143         {
00144                 ROS_ERROR("Could not read map destinations file !!!");
00145         }
00146     else{
00147                 ROS_WARN("read destinations map file : SUCCESS!!!");
00148         }
00149 
00150     //read destinations
00151         if ( !pred_.read_force_map(  force_map_path_.c_str() ) )
00152         {
00153                 ROS_ERROR("Could not read map force file !!!");
00154         }
00155     else{
00156                 ROS_WARN("read map force file : SUCCESS!!!");
00157         }
00158 
00159         
00160         //Robot platform initial config
00161         force_param_msg_.header.frame_id = target_frame_;
00162         now_ = ros::Time::now();
00163         Spose robot_current_position( robot_x_ini_,robot_y_ini_, 0.0, now_.toSec() );
00164         pred_.update_robot( SdetectionObservation( 0, robot_current_position.time_stamp, 
00165                                                                                            robot_current_position.x, robot_current_position.y ,
00166                                                                                            0.0, 0.0     ) );
00167   robot_position_msg_.header.stamp = now_;
00168   robot_position_msg_.header.frame_id = target_frame_;
00169   robot_position_msg_.pose.position.x = robot_x_ini_;
00170   robot_position_msg_.pose.position.y = robot_y_ini_;
00171         
00172                                                                                            
00173         //trajectory marker                                                                                
00174         traj_marker_.ns = "trajectories";
00175         traj_marker_.type = visualization_msgs::Marker::LINE_STRIP;
00176         traj_marker_.action = visualization_msgs::Marker::ADD;
00177         traj_marker_.lifetime = ros::Duration(1.0f);
00178         traj_marker_.scale.x = 0.1;
00179         traj_marker_.color.a = 0.5;
00180         traj_marker_.color.r = 1.0;
00181         traj_marker_.color.g = 0.4;
00182         traj_marker_.color.b = 0.0;
00183 
00184         //robot trajectory marker                                                                                  
00185         traj_robot_marker_.ns = "trajectories";
00186         traj_robot_marker_.type = visualization_msgs::Marker::LINE_STRIP;
00187         traj_robot_marker_.action = visualization_msgs::Marker::ADD;
00188         traj_robot_marker_.lifetime = ros::Duration(1.0f);
00189         traj_robot_marker_.scale.x = 0.1;
00190         traj_robot_marker_.color.a = 0.6;
00191         traj_robot_marker_.color.r = 0.26;
00192         traj_robot_marker_.color.g = 0.0;
00193         traj_robot_marker_.color.b = 0.5;
00194 
00195         //robot marker
00196         robot_marker_.ns = "trajectories";
00197         robot_marker_.type = visualization_msgs::Marker::MESH_RESOURCE;
00198         robot_marker_.mesh_resource = "package://tibi_dabo_base/model/meshes/tibi.stl";
00199         robot_marker_.action = visualization_msgs::Marker::ADD;
00200         robot_marker_.lifetime = ros::Duration(1.0f);
00201         robot_marker_.scale.x = 1.0;
00202         robot_marker_.scale.y = 0.6;
00203         robot_marker_.scale.z = 1.0;
00204         robot_marker_.color.a = 1.0;
00205         robot_marker_.color.r = 0.7;
00206         robot_marker_.color.g = 0.5;
00207         robot_marker_.color.b = 1.0;
00208 
00209         
00210         //target marker
00211         target_marker_.ns = "trajectories";
00212         target_marker_.type = visualization_msgs::Marker::CYLINDER;
00213         target_marker_.action = visualization_msgs::Marker::ADD;
00214         target_marker_.lifetime = ros::Duration(1.0f);
00215         target_marker_.scale.x = 0.5;
00216         target_marker_.scale.y = 0.5;
00217 
00218 
00219         //text marker
00220         text_marker_.ns = "trajectories";
00221         text_marker_.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
00222         text_marker_.action = visualization_msgs::Marker::ADD;
00223         text_marker_.lifetime = ros::Duration(1.0f);
00224         text_marker_.scale.z = 0.5;
00225         text_marker_.pose.position.z = 1.5;
00226         text_marker_.color.a = 0.5;
00227         text_marker_.color.r = 0.0;
00228         text_marker_.color.g = 0.0;
00229         text_marker_.color.b = 0.0;
00230         
00231         //force markers: resultant force (red)
00232         force_marker_.ns =  "trajectories";
00233         force_marker_.type = visualization_msgs::Marker::ARROW;
00234         force_marker_.action = visualization_msgs::Marker::ADD;
00235         force_marker_.lifetime = ros::Duration(1.0f);
00236         force_marker_.scale.x = 0.2;
00237         force_marker_.scale.y = 0.25;
00238         force_marker_.color.a = 0.8;
00239         force_marker_.color.r = 1.0;
00240         force_marker_.color.g = 0.0;
00241         force_marker_.color.b = 0.0;
00242         
00243         //force makers: force to goal: ( blue )
00244         force_goal_marker_ = force_marker_;
00245         force_goal_marker_.color.r = 0.0;
00246         force_goal_marker_.color.g = 0.4;
00247         force_goal_marker_.color.b = 1.0;
00248 
00249         //force markers: goal force to person - goal (orange)
00250         force_goal2_marker_ = force_marker_;
00251         force_goal2_marker_.color.r = 1.0;
00252         force_goal2_marker_.color.g = 0.4;
00253         force_goal2_marker_.color.b = 0.0;
00254 
00255         //force markers: scaled person interaction force (green)
00256         force_int_person_marker_ = force_marker_;
00257         force_int_person_marker_.color.r = 0.2;
00258         force_int_person_marker_.color.g = 0.85;
00259         force_int_person_marker_.color.b = 0.2;
00260 
00261         //force markers: robot interation force only valid to persons (pink)
00262         force_int_robot_marker_ = force_marker_;
00263     force_int_robot_marker_.color.r = 0.26;
00264     force_int_robot_marker_.color.g = 0.0;
00265     force_int_robot_marker_.color.b = 0.66;
00266 
00267         //force markers: scaled laser obstacles interaction force (cyan)
00268         force_obstacle_laser_marker_ = force_marker_;
00269         force_obstacle_laser_marker_.color.r = 0.0;
00270         force_obstacle_laser_marker_.color.g = 1.0;
00271         force_obstacle_laser_marker_.color.b = 1.0;
00272 
00273         //force markers: map obstacles interaction force (black)
00274         force_obstacle_map_marker_ = force_marker_;
00275     force_obstacle_map_marker_.color.r = 0.0;
00276     force_obstacle_map_marker_.color.g = 0.0;
00277     force_obstacle_map_marker_.color.b = 0.0;
00278 
00279         //destination marker
00280         dest_marker_.ns = "destinations";
00281         dest_marker_.type = visualization_msgs::Marker::CYLINDER;
00282         dest_marker_.action = visualization_msgs::Marker::ADD;
00283         dest_marker_.lifetime = ros::Duration(1.0f);
00284         dest_marker_.color.a = 0.8;
00285         
00286         //predictions marker
00287         pred_marker_.ns = "predictions";
00288         pred_marker_.type = visualization_msgs::Marker::LINE_STRIP;
00289         pred_marker_.action = visualization_msgs::Marker::ADD;
00290         pred_marker_.scale.x = 0.1;
00291         pred_marker_.lifetime = ros::Duration(1.0f);
00292         pred_marker_.color.a = 0.5;
00293         pred_marker_.color.r = 0.0;
00294         pred_marker_.color.g = 0.0;
00295         pred_marker_.color.b = 0.0;
00296 
00297 
00298 }
00299 
00300 
00301 void ForceRobotCompanionLearningAlgNode::growing_density_test()
00302 {
00303 /*              std::stringstream experimentString;
00304                 //update robot virtual position
00305                 //ROS_INFO("robot position (x,y) = (%f , %f)" , robot_desired_position_.x , robot_desired_position_.y);
00306                 if (robot_desired_position_.x !=  robot_desired_position_.x)//when invlaid commands are given, nan positions are sent...
00307                 {
00308                         robot_desired_position_.x = 0.0;
00309                         robot_desired_position_.y = 0.0;
00310                 }
00311                 pred_.update_robot( SdetectionObservation( 0, ros::Time::now().toSec(), 
00312                                                                            robot_desired_position_.x, robot_desired_position_.y ,
00313                                                                            0.0, 0.0     ) );
00314 
00315 
00316                 //automation of the simulation mode: 4 states {Init_Sim, Find_nearest, Test_Sim, End_Sim}
00317                 switch(simulation_state_)
00318                 {
00319                         case ForceRobotCompanionLearningAlgNode::Init_Sim:
00320                                 //update next experiments paramters
00321                                 timer_ = 0;
00322                                 ++num_experiment_;
00323                                 ROS_INFO( "experiment number %d", num_experiment_ );
00324                                 experimentString << num_experiment_;
00325                                 //open file
00326                                 file_results_out_ = fopen (  (results_folder_ + experimentString.str() ).c_str() , "w");
00327                                 if (file_results_out_ == NULL)
00328                                         results_file_is_open_ = false;
00329                                 else{
00330                                         results_file_is_open_ = true;
00331                                         fprintf (file_results_out_, "%%distance_robot-person, robot_angle_position_wrt_personheading, distance_to_nearest_person, v_robot, v_person, time\n");
00332                                 }
00333                                 simulation_state_ = ForceRobotCompanionLearningAlgNode::Find_Nearest;
00334                                 break;
00335                         case ForceRobotCompanionLearningAlgNode::Find_Nearest:
00336                                 //checks persons have been initialized (tracks_callback has entered) and
00337                                 //looks for the nearest person
00338                                 if( pred_.set_nearest_target_person() ) //if someone nearer 20m
00339                                         simulation_state_ = ForceRobotCompanionLearningAlgNode::Test_Sim;
00340                                 break;
00341                         case ForceRobotCompanionLearningAlgNode::Test_Sim:
00342                                 //write results
00343                                 if ( results_file_is_open_ )
00344                                 {
00345                                         fprintf (file_results_out_, "%f %f %f %f %f %f\n" ,
00346                                                                 person_robot_dist_msg_.data, 
00347                                                                 target_followed_pose_.angle_heading_pose( pred_.get_robot().get_current_pointV() ), 
00348                                                                 pred_.distance_to_nearest_person( pred_.get_robot() ),
00349                                                                 pred_.get_robot().get_current_pointV().v , 
00350                                                                 target_followed_pose_.v , 
00351                                                                 ros::Time::now().toSec());
00352                                 }
00353                                 //check if target has been removed == it has achieved its goal
00354                                 if( !pred_.find_person( pred_.get_target_person() ) )
00355                                         simulation_state_ = End_Sim;
00356                                 //if timer exceeds 5 minutes, experiment not valid (probably entering local minima when obstacles in the environment)
00357                                 if( timer_ > 5*60*5 ){
00358                                         simulation_state_ = ForceRobotCompanionLearningAlgNode::End_Sim;
00359                                         --num_experiment_;
00360                                 }
00361                                 else
00362                                         ++timer_;
00363                                 break;
00364                         case ForceRobotCompanionLearningAlgNode::End_Sim:
00365                         default:
00366                                 //close results file
00367                                 if ( results_file_is_open_ ){
00368                                         fclose( file_results_out_ );
00369                                         results_file_is_open_ = false;
00370                                 }
00371                                 //clear all vectors etc
00372                                 pred_.set_number_virtual_people( 0 );
00373                                 pred_.clear_people_scene();
00374                                 pred_.get_robot().reset();
00375                                 pred_.update_robot( SdetectionObservation( 0, ros::Time::now().toSec(), 
00376                                                                            robot_x_ini_, robot_y_ini_ ,
00377                                                                            0.0, 0.0     ) );
00378                                 target_followed_pose_ = Spose();
00379                                 traj_marker_.points.clear();
00380                                 traj_robot_marker_.points.clear();
00381                                 simulation_state_ = ForceRobotCompanionLearningAlgNode::Init_Sim;
00382                                 break;
00383                 }
00384 */
00385 }
00386 
00387 void ForceRobotCompanionLearningAlgNode::initial_params_learning()
00388 {
00389 
00390   std::stringstream experimentString;
00391   vector<double> params;
00392   switch(simulation_state_)
00393         {
00394                 case ForceRobotCompanionLearningAlgNode::Init_Sim:
00395 
00396       //update next experiments paramters
00397       timer_ = 0;
00398       ++num_experiment_;
00399       ROS_INFO( "experiment number %d", num_experiment_ );
00400       experimentString << num_experiment_;
00401       //open file
00402       file_results_out_ = fopen (  (results_folder_ + experimentString.str() ).c_str() , "w");
00403       if (file_results_out_ == NULL)
00404       {
00405               results_file_is_open_ = false;
00406         break;
00407       }
00408       else{
00409               results_file_is_open_ = true;
00410               fprintf (file_results_out_, "%%distance_robot-person, robot_angle_position_wrt_personheading, distance_to_nearest_person, v_robot, v_person, time\n");
00411       }
00412 
00413 
00414       //get sim paramters
00415       //params = pred_.learning_force_companion_params_MC( );
00416       params = pred_.learning_force_companion_params_alphabeta( );
00417       if( results_file_is_open_ )
00418       {
00419         simulation_state_ = ForceRobotCompanionLearningAlgNode::Find_Nearest;
00420         fprintf (file_results_out_, "%% alpha, beta, gamma,  delta(laser), delta(map)\n");
00421         fprintf (file_results_out_, "%f %f %f %f %f 0.0\n", params[0],params[1],params[2],params[3],params[4]);
00422       }
00423                         break;
00424 
00425     case ForceRobotCompanionLearningAlgNode::Find_Nearest:
00426                         //checks persons have been initialized (tracks_callback has entered) and
00427                         //looks for the nearest person
00428                         if( pred_.set_nearest_target_person() ) //if someone nearer 20m
00429                                 simulation_state_ = ForceRobotCompanionLearningAlgNode::Test_Sim;
00430       break;
00431 
00432     case ForceRobotCompanionLearningAlgNode::Test_Sim:
00433         //update robot position and rosms robot control
00434               robot_desired_position_ = pred_.robot_approach_pose( f1_, f2_, f3_, f4_,f5_, f_  );
00435               //fill  current robot pose message
00436               pred_.update_robot( SdetectionObservation( 0, now_.toSec(), 
00437                                                                                                  robot_desired_position_.x, robot_desired_position_.y ,
00438                                                                                                  0.0, 0.0       ) );
00439               robot_position_msg_.pose.position.x = robot_desired_position_.x;
00440               robot_position_msg_.pose.position.y = robot_desired_position_.y;
00441               robot_position_msg_.header.stamp = now_;
00442 
00443                                 //write results
00444                                 if ( results_file_is_open_ )
00445                                 {
00446                                         fprintf (file_results_out_, "%f %f %f %f %f %f\n" ,
00447                                                                 person_robot_dist_msg_.data, 
00448                                                                 target_followed_pose_.angle_heading_point( pred_.get_robot()->get_current_pointV() ), 
00449                                                                 pred_.distance_to_nearest_person( pred_.get_robot() ),
00450                                                                 pred_.get_robot()->get_current_pointV().v() , 
00451                                                                 target_followed_pose_.v() , 
00452                                                                 now_.toSec());
00453                                 }
00454                                 //check if target has been removed == it has achieved its goal
00455                                 if( !pred_.find_person( pred_.get_target_person() ) )
00456                                         simulation_state_ = End_Sim;
00457                                 //if timer exceeds 5 minutes, experiment not valid (probably entering local minima when obstacles in the environment)
00458                                 if( timer_ > 5*60*5 ){
00459                                         simulation_state_ = ForceRobotCompanionLearningAlgNode::End_Sim;
00460                                         --num_experiment_;
00461                                 }
00462                                 else
00463                                         ++timer_;
00464                                 break;
00465 
00466     case ForceRobotCompanionLearningAlgNode::End_Sim:
00467     default:
00468                                 //close results file
00469                                 if ( results_file_is_open_ ){
00470                                         fclose( file_results_out_ );
00471                                         results_file_is_open_ = false;
00472                                 }
00473               //reset configuration
00474       pred_.clear_scene();
00475       pred_.get_robot()->reset();
00476       pred_.update_robot( SdetectionObservation( 0, now_.toSec(), 
00477                                 robot_x_ini_, robot_y_ini_ ,
00478                                 0.0, 0.0        ) );
00479       target_followed_pose_ = SpointV_cov();
00480       traj_marker_.points.clear();
00481       traj_robot_marker_.points.clear();
00482       //reset people simulation node : send service
00483       ROS_INFO("ForceRobotCompanionLearningAlgNode:: Sending New Request!"); 
00484       if (reset_client_.call(reset_srv_)) 
00485       { 
00486         ROS_INFO("ForceRobotCompanionLearningAlgNode:: Response: "); 
00487         simulation_state_ = ForceRobotCompanionLearningAlgNode::Init_Sim;//only changes its state when service is sent
00488       } 
00489       else 
00490       { 
00491         ROS_INFO("ForceRobotCompanionLearningAlgNode:: Failed to Call Server on topic reset "); 
00492       }
00493       break;
00494   }
00495 
00496 }
00497 
00498 void ForceRobotCompanionLearningAlgNode::vis_trajectories()
00499 {
00500         std::vector<SpointV_cov> scene_targets_pointV;
00501         std::vector<unsigned int> scene_targets_ids;
00502   pred_.get_scene_observation(scene_targets_pointV,scene_targets_ids);
00503         //list<Cperson>& scene = pred_.get_scene();
00504         int cont = 0;
00505         geometry_msgs::Point ros_point, ros_point_ini;
00506 
00507         //fill headers
00508         traj_marker_.header.stamp = now_;
00509         traj_marker_.header.frame_id = target_frame_;
00510         traj_robot_marker_.header.stamp = traj_marker_.header.stamp;
00511         traj_robot_marker_.header.frame_id = target_frame_;
00512         robot_marker_.header.stamp = traj_marker_.header.stamp;
00513         robot_marker_.header.frame_id = target_frame_;
00514         target_marker_.header.stamp = traj_marker_.header.stamp;
00515         target_marker_.header.frame_id = target_frame_;
00516         text_marker_.header.stamp = traj_marker_.header.stamp;
00517         text_marker_.header.frame_id = target_frame_;
00518         force_marker_.header.stamp = traj_marker_.header.stamp;
00519         force_marker_.header.frame_id = target_frame_;
00520         force_goal_marker_.header.stamp = force_marker_.header.stamp;
00521         force_goal_marker_.header.frame_id = force_marker_.header.frame_id;
00522         force_goal2_marker_.header.stamp = force_marker_.header.stamp;
00523         force_goal2_marker_.header.frame_id = force_marker_.header.frame_id;
00524         force_int_person_marker_.header.stamp = force_marker_.header.stamp;
00525         force_int_person_marker_.header.frame_id = force_marker_.header.frame_id;
00526         force_int_robot_marker_.header.stamp = force_marker_.header.stamp;
00527         force_int_robot_marker_.header.frame_id = force_marker_.header.frame_id;
00528         force_obstacle_map_marker_.header.stamp = force_marker_.header.stamp;
00529         force_obstacle_map_marker_.header.frame_id = force_marker_.header.frame_id;
00530         force_obstacle_laser_marker_.header.stamp = force_marker_.header.stamp;
00531         force_obstacle_laser_marker_.header.frame_id = force_marker_.header.frame_id;
00532 
00533         MarkerArray_trajectories_msg_.markers.clear();
00534         MarkerArray_forces_msg_.markers.clear();
00535         
00536 
00537         //drawing robot forces ---------------------------------------------------------------
00538         clear_force_markers();
00539         //Sforce  force_to_goal, force_int_person , force_int_robot, force_obstacle;//TODO: change code to new function get_forces
00540         //pred_.get_robot().get_forces_person( force_to_goal, force_int_person , force_int_robot, force_obstacle );
00541 
00542         //initial point
00543         ros_point_ini.x = pred_.get_robot()->get_current_pointV().x;
00544         ros_point_ini.y = pred_.get_robot()->get_current_pointV().y;
00545 
00546         //drawing robot trajectory. As we are just plotting the target path, we will read the current pose
00547         if ( traj_robot_marker_.points.size() > 500)
00548         {
00549                 vector<geometry_msgs::Point> temp_poses;
00550                 temp_poses.reserve(250);
00551                 for( unsigned int i = 251; i <= 500; ++i)
00552                         temp_poses.push_back(traj_robot_marker_.points[i]);
00553                 traj_robot_marker_.points = temp_poses;
00554         }
00555         traj_robot_marker_.points.push_back(    ros_point_ini   );
00556         traj_robot_marker_.id = cont;
00557         ++cont;
00558         MarkerArray_trajectories_msg_.markers.push_back(  traj_robot_marker_  );
00559         
00560 
00561         //scaled force to goal: ( blue )
00562         force_goal_marker_.points.push_back(    ros_point_ini   );
00563         ros_point.x = ros_point_ini.x + f1_.fx;
00564         ros_point.y = ros_point_ini.y + f1_.fy;
00565         force_goal_marker_.points.push_back(    ros_point   );
00566         force_goal_marker_.id = cont;
00567         ++cont;
00568         MarkerArray_trajectories_msg_.markers.push_back(  force_goal_marker_  );
00569 
00570         //scaled force to person - goal (orange)
00571         force_goal2_marker_.points.push_back(    ros_point_ini   );
00572         ros_point.x = ros_point_ini.x + f2_.fx;
00573         ros_point.y = ros_point_ini.y + f2_.fy;
00574         force_goal2_marker_.points.push_back( ros_point );
00575         force_goal2_marker_.id = cont;
00576         ++cont;
00577         MarkerArray_trajectories_msg_.markers.push_back(  force_goal2_marker_  );
00578 
00579         //scaled person interaction force (green)
00580         force_int_person_marker_.points.push_back(    ros_point_ini   );
00581         ros_point.x = ros_point_ini.x + f3_.fx;
00582         ros_point.y = ros_point_ini.y + f3_.fy;
00583         force_int_person_marker_.points.push_back( ros_point);
00584         force_int_person_marker_.id = cont;
00585         ++cont;
00586         MarkerArray_trajectories_msg_.markers.push_back(  force_int_person_marker_  );
00587 
00588         //scaled laser obstacles interaction force (yellow)
00589         force_obstacle_laser_marker_.points.push_back(    ros_point_ini   );
00590         ros_point.x = ros_point_ini.x + f4_.fx;
00591         ros_point.y = ros_point_ini.y + f4_.fy;
00592         force_obstacle_laser_marker_.points.push_back( ros_point);
00593         force_obstacle_laser_marker_.id = cont;
00594         ++cont;
00595         MarkerArray_trajectories_msg_.markers.push_back(  force_obstacle_laser_marker_  );
00596 
00597         //map obstacles interaction force (black)
00598         force_obstacle_map_marker_.points.push_back(    ros_point_ini   );
00599     ros_point.x = ros_point_ini.x + f5_.fx;
00600     ros_point.y = ros_point_ini.y + f5_.fy;
00601     force_obstacle_map_marker_.points.push_back( ros_point);
00602     force_obstacle_map_marker_.id = cont;
00603     ++cont;
00604     MarkerArray_trajectories_msg_.markers.push_back(  force_obstacle_map_marker_  );
00605 
00606         //weighted resultant force (red)
00607         force_marker_.points.push_back(    ros_point_ini   );
00608         ros_point.x = ros_point_ini.x + f_.fx;
00609         ros_point.y = ros_point_ini.y + f_.fy;
00610         force_marker_.points.push_back(  ros_point);
00611         force_marker_.id = cont;
00612         ++cont;
00613         MarkerArray_trajectories_msg_.markers.push_back(  force_marker_  );
00614 
00615         //Draw robot as a (pink) .stl mesh
00616         double robot_orientation = pred_.get_robot()->get_current_pointV().orientation() - PI/2.0;
00617         robot_marker_.pose.position.x = ros_point_ini.x + 0.3*cos(robot_orientation);
00618         robot_marker_.pose.position.y = ros_point_ini.y + 0.3*sin(robot_orientation);
00619         robot_marker_.pose.position.z = 0.4;
00620         //geometry_msgs::Quaternion  tf::createQuaternionMsgFromRollPitchYaw(double roll,double pitch,double yaw)
00621         robot_marker_.pose.orientation = tf::createQuaternionMsgFromRollPitchYaw(PI/2.0,0.0, robot_orientation );
00622   robot_position_msg_.pose.orientation = robot_marker_.pose.orientation;
00623         robot_marker_.id = cont;
00624         ++cont;
00625         MarkerArray_trajectories_msg_.markers.push_back(  robot_marker_  );
00626         
00627         
00628         //draw pedestrians' trajectories ------------------------------------------------
00629         //ROS_INFO( "size of trajectories =%d" , scene_targets_pointV.size()  );
00630         Sforce  force_to_goal, force_int_person , force_int_robot, force_obstacle;
00631         for( unsigned int i = 0; i < (unsigned int)scene_targets_pointV.size() ; i++)
00632         {
00633                 SpointV_cov target_pose = scene_targets_pointV[i];
00634 
00635   //no plot of forces....
00636 
00637                 if ( scene_targets_ids[i] == (unsigned int)pred_.get_target_person())
00638                 {       
00639                         //ROS_INFO( "Current person velocity = %f" , target_pose.v);
00640                         target_followed_pose_ = target_pose;
00641                         //drawing person trajectory. As we are just plotting the target path, we will read the current pose
00642                         ros_point.x = target_pose.x;
00643                         ros_point.y = target_pose.y;
00644                         if ( traj_marker_.points.size() > 500)
00645                         {
00646                                 vector<geometry_msgs::Point> temp_poses;
00647                                 temp_poses.reserve(250);
00648                                 for( unsigned int i = 251; i <= 500; ++i)
00649                                         temp_poses.push_back(traj_marker_.points[i]);
00650                                 traj_marker_.points = temp_poses;
00651                         }
00652                         traj_marker_.points.push_back(    ros_point   );
00653                         traj_marker_.id = cont;
00654                         ++cont;
00655                         MarkerArray_trajectories_msg_.markers.push_back(  traj_marker_  );
00656                         
00657                         //target person drawn differently
00658                         target_marker_.scale.z = 0.8;
00659                         target_marker_.color.a = 0.8;
00660                         target_marker_.color.r = 1.0;
00661                         target_marker_.color.g = 0.4;
00662                         target_marker_.color.b = 0.0;
00663                         //draw targets as cylinders
00664                         target_marker_.pose.position.x = target_pose.x;
00665                         target_marker_.pose.position.y = target_pose.y;
00666                         target_marker_.pose.position.z = 0.4;
00667                         ++cont;
00668                         target_marker_.id = cont;
00669                         MarkerArray_trajectories_msg_.markers.push_back(  target_marker_  );
00670 
00671                         
00672                         //fill  current target pose message
00673                         target_person_position_msg_.point.x = target_pose.x;
00674                         target_person_position_msg_.point.y = target_pose.y;
00675                         target_person_position_msg_.point.z = target_pose.v();//overloading the pose message...not good-> TODO
00676                         target_person_position_msg_.header = traj_marker_.header;
00677 
00678 
00679                         //calcultate the absolute robot-person distance and create->fill topic
00680                         person_robot_dist_msg_.data = target_pose.distance( pred_.get_robot()->get_current_pointV() );
00681                 }
00682                 else
00683                 {
00684                         target_marker_.scale.z = 0.6;
00685                         target_marker_.color.a = 0.4;
00686                         target_marker_.color.r = 0.0;
00687                         target_marker_.color.g = 0.8;
00688                         target_marker_.color.b = 0.0;
00689                         //draw targets as cylinders
00690                         target_marker_.pose.position.x = target_pose.x;
00691                         target_marker_.pose.position.y = target_pose.y;
00692                         target_marker_.pose.position.z = 0.3;
00693                         ++cont;
00694                         target_marker_.id = cont;
00695                         MarkerArray_trajectories_msg_.markers.push_back(  target_marker_  );
00696                 }
00697                 
00698                 //draw target id
00699                 text_marker_.pose.position.x = target_pose.x;
00700                 text_marker_.pose.position.y = target_pose.y;
00701                 ++cont;
00702                 text_marker_.id = cont;
00703                 std::ostringstream target_id;
00704                 target_id << scene_targets_ids[i];
00705                 text_marker_.text = target_id.str();
00706                 //MarkerArray_trajectories_msg_.markers.push_back(  text_marker_  );
00707                 ++cont;
00708         }
00709         
00710 }
00711 
00712 void ForceRobotCompanionLearningAlgNode::clear_force_markers()
00713 {
00714         force_marker_.points.clear();
00715         force_goal_marker_.points.clear();
00716         force_goal2_marker_.points.clear();
00717         force_int_person_marker_.points.clear();
00718         force_int_robot_marker_.points.clear();
00719         force_obstacle_map_marker_.points.clear();
00720         force_obstacle_laser_marker_.points.clear();
00721 }
00722 
00723 void ForceRobotCompanionLearningAlgNode::vis_intentionality_prediction()
00724 {
00725         //return destinations [#person] [#destination] of all current observed persons
00726          vector<vector<Sdestination> >&  intentionality_pred = pred_.get_scene_intentionality_prediction_bhmip(  );
00727         //ROS_INFO("size of predictions = %d" , intentionality_pred.size() );
00728         dest_marker_.header.frame_id = target_frame_;
00729         MarkerArray_destinations_msg_.markers.clear();
00730         dest_marker_.color.r = 1.0;
00731         dest_marker_.color.g = 0.0;
00732         dest_marker_.color.b = 0.0;
00733         dest_marker_.scale.x = 0.5;
00734         dest_marker_.scale.y = 0.5;
00735         for (unsigned int i = 0;  i < intentionality_pred.size() ; ++i)
00736         {
00737                 for (unsigned int j = 0;  j < intentionality_pred[i].size() ; ++j)
00738                 {
00739                         dest_marker_.scale.z = intentionality_pred[i][j].prob;
00740                         dest_marker_.pose.position.z = 0.5*intentionality_pred[i][j].prob;
00741 //                      ROS_INFO( " prob value at i = %d , j = %d  --- prob = %f " , i , j , intentionality_pred[i][j].prob );
00742 //                      intentionality_pred[i][j].print();
00743                         dest_marker_.pose.position.x = intentionality_pred[i][j].x;
00744                         dest_marker_.pose.position.y = intentionality_pred[i][j].y;
00745                         dest_marker_.id = i*intentionality_pred[i].size() + j;
00746                         MarkerArray_destinations_msg_.markers.push_back( dest_marker_ );
00747                 }
00748         }
00749 }
00750 
00751 
00752 void ForceRobotCompanionLearningAlgNode::vis_destinations()
00753 {
00754         // vector<Sdestination>& dest = pred_.get_destinations();
00755         dest_marker_.header.frame_id = target_frame_;
00756         MarkerArray_destinations_msg_.markers.clear();
00757         int cont = 0;
00758         //ROS_INFO( "number of destinations %d" , dest.size()  );
00759         
00760         //Print scene destinations
00761         dest_marker_.color.r = 0.0;
00762         dest_marker_.color.g = 0.4;
00763         dest_marker_.color.b = 1.0;
00764         dest_marker_.scale.x = 1;
00765         dest_marker_.scale.y = 1;
00766         dest_marker_.scale.z = 0.01;
00767     dest_marker_.pose.position.z = 0.0;
00768         for ( unsigned int i = 0; i < pred_.get_destinations().size(); ++i)
00769         {
00770                 dest_marker_.pose.position.x = pred_.get_destinations()[i].x;
00771                 dest_marker_.pose.position.y = pred_.get_destinations()[i].y;
00772                 dest_marker_.id = cont;
00773                 ++cont;
00774                 MarkerArray_destinations_msg_.markers.push_back( dest_marker_ );
00775         }
00776         
00777         //print robot destinations
00778         dest_marker_.color.r = 1.0;
00779         dest_marker_.color.g = 1.0;
00780         dest_marker_.color.b = 1.0;
00781         dest_marker_.scale.x = 0.8;
00782         dest_marker_.scale.y = 0.8;
00783         dest_marker_.scale.z = 0.1;
00784         for ( unsigned int i = 0; i < pred_.get_robot()->get_destinations()->size(); ++i)
00785         {
00786                 dest_marker_.pose.position.x = pred_.get_robot()->get_destinations()->at(i).x;
00787                 dest_marker_.pose.position.y = pred_.get_robot()->get_destinations()->at(i).y;
00788                 dest_marker_.id = cont;
00789                 ++cont;
00790                 MarkerArray_destinations_msg_.markers.push_back( dest_marker_ );
00791         }
00792         
00793         //print target destinations + probabilities
00794         Cperson_abstract* iit;
00795         if( pred_.find_person( pred_.get_target_person() , &iit ) )
00796         {
00797                 dest_marker_.color.r = 0.0;
00798                 dest_marker_.color.g = 0.9;
00799                 dest_marker_.color.b = 0.9;
00800                 dest_marker_.scale.x = 0.5;
00801                 dest_marker_.scale.y = 0.5;
00802                 dest_marker_.scale.z = 0;
00803                 //ROS_INFO("Printing target destinations");
00804                 dest_marker_.scale.z = iit->get_best_dest().prob;
00805                 dest_marker_.pose.position.z = 0.5*dest_marker_.scale.z;
00806                 dest_marker_.pose.position.x = iit->get_best_dest().x;
00807                 dest_marker_.pose.position.y = iit->get_best_dest().y;
00808                 dest_marker_.id = cont;
00809                 ++cont;
00810                 MarkerArray_destinations_msg_.markers.push_back( dest_marker_ );
00811 
00812         }
00813 
00814         //print robot current desired position
00815         dest_marker_.color.r = 0.8;
00816         dest_marker_.color.g = 0.26;
00817         dest_marker_.color.b = 0.0;
00818         dest_marker_.scale.x = 0.2;
00819         dest_marker_.scale.y = 0.2;
00820         dest_marker_.scale.z = 0.1;
00821         dest_marker_.pose.position.x = robot_desired_position_.x;
00822         dest_marker_.pose.position.y = robot_desired_position_.y;
00823         dest_marker_.id = cont;
00824         ++cont;
00825         MarkerArray_destinations_msg_.markers.push_back( dest_marker_ );
00826 
00827 
00828 }
00829 
00830 void ForceRobotCompanionLearningAlgNode::vis_predictions()
00831 {
00832         //a vector of pointers to the predicted trajectories to all destinations...
00833          vector< vector<vector<Spose> >* > scene_pred = pred_.get_scene_motion_prediction();
00834         int cont = 0;
00835 
00836         //fill headers
00837         pred_marker_.header.frame_id = target_frame_;
00838         pred_marker_.header.stamp = now_;
00839         MarkerArray_predictions_msg_.markers.clear();
00840         //ROS_INFO( "size of prediction trajectories = %d" , scene_pred.size()  );
00841         //fill body
00842         for( unsigned int i = 0; i < scene_pred.size(); ++i )
00843         {
00844                 for(  unsigned int j = 0; j < scene_pred[i]->size() ; ++j  )
00845                 {
00846                         //ROS_INFO( "IN** pred traj size = %d" , scene_pred[i]->size()  );
00847                         pred_marker_.points.clear();
00848                         for(  unsigned int t = 0; t < scene_pred[i]->at(j).size() ; ++t  )
00849                         {
00850                                 geometry_msgs::Point ros_point;
00851                                 ros_point.x = scene_pred.at(i)->at(j).at(t).x;
00852                                 ros_point.y = scene_pred.at(i)->at(j).at(t).y;
00853                                 pred_marker_.points.push_back(  ros_point );
00854                         }
00855                         pred_marker_.id = cont;
00856                         ++cont;
00857                         MarkerArray_predictions_msg_.markers.push_back(  pred_marker_  );
00858                 }
00859         }
00860 }
00861 
00862 
00863 /*  [subscriber callbacks] */
00864 
00865 void ForceRobotCompanionLearningAlgNode::dest_callback(const geometry_msgs::PoseStamped::ConstPtr& msg) 
00866 { 
00867   //ROS_INFO("ForceRobotCompanionLearningAlgNode::dest_callback: New Message Received"); 
00868 
00869   //use appropiate mutex to shared variables if necessary 
00870   this->alg_.lock(); 
00871   //this->dest_mutex_.enter(); 
00872 
00873   
00874   //unlock previously blocked shared variables 
00875   this->alg_.unlock(); 
00876   //this->dest_mutex_.exit(); 
00877 }
00878 
00879 void ForceRobotCompanionLearningAlgNode::tracks_callback(const iri_perception_msgs::peopleTrackingArray::ConstPtr& msg) 
00880 { 
00881   //ROS_INFO("tracks_callback: New Message Received"); 
00882 
00883 
00884   //use appropiate mutex to shared variables if necessary 
00885   this->tracks_mutex_.enter();
00886   
00887         try
00888     {
00889                 //get transformation
00890                 //ROS_INFO("observation frame = %s" , msg->header.frame_id.c_str() );
00891                 //ROS_INFO("target frame  = %s" , target_frame_.c_str() );
00892                 /*
00893                 bool    waitForTransform (const std::string &target_frame,
00894                 const std::string &source_frame, const ros::Time &time, const ros::Duration &timeout, 
00895                 const ros::Duration &polling_sleep_duration=ros::Duration(0.01), 
00896                 std::string *error_msg=NULL) const 
00897                 */
00898                 bool tf_exists = tf_listener_.waitForTransform(target_frame_,
00899                                                                 msg->header.frame_id, 
00900                                                                 msg->header.stamp,
00901                                                                 ros::Duration(1), ros::Duration(0.01));
00902 
00903         if(tf_exists)
00904         {
00905             //Tracks detection observations
00906             vector<SdetectionObservation> obs;
00907             geometry_msgs::PointStamped observation_point, target_point;
00908             observation_point.header = msg->header;
00909             observation_point.point.z = 0.0;
00910             for(unsigned int i = 0; i < msg->peopleSet.size() ; ++i)
00911             {
00912                 observation_point.point.x = msg->peopleSet[i].x;
00913                 observation_point.point.y = msg->peopleSet[i].y;
00914 
00915                 /*void  transformPoint (const std::string &target_frame, 
00916                         const geometry_msgs::PointStamped &stamped_in, 
00917                         geometry_msgs::PointStamped &stamped_out) const 
00918                 */
00919                 //tf_listener_.transformPoint( target_frame_,  msg->header.stamp, target_point, msg->header.frame_id, observation_point);
00920                 tf_listener_.transformPoint( target_frame_, observation_point, target_point);
00921                 //SdetectionObservation( int id , double time_stamp ,double x, double y, double vx, double vy)
00922                 obs.push_back( SdetectionObservation(  msg->peopleSet[i].targetId , 
00923                                                 msg->header.stamp.toSec() ,   target_point.point.x ,
00924                                                 target_point.point.y , 0.0 , 0.0        ) );
00925             }
00926                         
00927             this->alg_.lock(); 
00928             pred_.update_scene( obs );
00929             this->alg_.unlock(); 
00930                         
00931                 }
00932                 else
00933                 {
00934                         ROS_ERROR("ForceRobotCompanionLearningAlgNode::No transform in tracks callback function");
00935                 }
00936         }
00937     catch (tf::TransformException &ex)
00938         {
00939                 ROS_ERROR("ForceRobotCompanionTracks:: %s",ex.what());
00940         }
00941 
00942 
00943         this->tracks_mutex_.exit(); 
00944 }
00945 
00946 /*  [service callbacks] */
00947 
00948 /*  [action callbacks] */
00949 
00950 
00951 void ForceRobotCompanionLearningAlgNode::node_config_update(Config &config, uint32_t level)
00952 {
00953   this->alg_.lock();
00954   
00955         ROS_INFO("         *******  algorithm config update  *******\n\n");
00956         using_prediction_ = config.using_prediction;
00957     learning_mode_ = (ForceRobotCompanionLearningAlgNode::learning_mode) config.learning_mode;
00958   results_folder_ = config.results_folder;
00959         target_frame_ = config.reference_frame;
00960         robot_frame_ = config.robot_frame;
00961         if( config.target_person_id != pred_.get_target_person() )
00962                 traj_marker_.points.clear();
00963         pred_.set_target_person( config.target_person_id );//updated via dynamic reconfigure. Def = 0
00964         pred_.set_v_max( config.v_max );
00965         pred_.set_v_cruise( config.v_cruise );
00966         pred_.set_time_horizon( config.time_horizon );
00967         if( config.force_parameters )
00968         {
00969                 pred_.set_force_params( config.force_goal, config.force_toperson, config.force_interaction ,
00970                                                                 config.force_laser, config.force_map);
00971                 force_param_msg_.point.x = config.force_goal;
00972                 force_param_msg_.point.y = config.force_toperson;
00973                 force_param_msg_.point.z = config.force_interaction;
00974         }
00975         laser_radi_ = config.laser_radi;
00976         pred_.set_companion_position(config.r , config.theta/180*3.14);
00977         if(config.follow_nearest)
00978         {
00979                 ROS_INFO("Following nearest target (id: %d)", nearest_target_);
00980                 pred_.set_target_person( nearest_target_ );
00981                 config.target_person_id = nearest_target_;
00982         }
00983         force_map_path_ = config.force_map_path;
00984         //robot-person parameters
00985         vector<double> param;
00986         param.push_back(config.K_rob_per);//K
00987         param.push_back(config.lambda_rob_per);//lambda
00988         param.push_back(config.A_rob_per);//A
00989         param.push_back(config.B_rob_per);//B
00990         param.push_back(config.d_rob_per);//d
00991         pred_.get_robot()->set_social_force_parameters_person_robot( param );
00992         //Obstacle spherical parameters obtained using our optimization method
00993         param.clear();
00994         param.push_back(config.K_obstacle);//K
00995         param.push_back(config.lambda_obs);//lambda
00996         param.push_back(config.A_obstacle);//A
00997         param.push_back(config.B_obstacle);//B
00998         param.push_back(config.d_obstacle);//d
00999         pred_.get_robot()->set_social_force_parameters_obstacle( param );
01000         if( config.clear_target_traj )
01001                 traj_marker_.points.clear();
01002                 traj_robot_marker_.points.clear();
01003         robot_x_ini_ = config.robot_x_ini;
01004         robot_y_ini_ = config.robot_y_ini;
01005   this->alg_.unlock();
01006 }
01007 
01008 
01009 void ForceRobotCompanionLearningAlgNode::addNodeDiagnostics(void)
01010 {
01011 }
01012 
01013 /* main function */
01014 int main(int argc,char *argv[])
01015 {
01016   return algorithm_base::main<ForceRobotCompanionLearningAlgNode>(argc, argv, "force_robot_companion_learning_alg_node");
01017 }


iri_force_robot_companion_learning
Author(s): gferrer
autogenerated on Fri Dec 6 2013 21:22:15