og_builder_stage.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <nav_msgs/OccupancyGrid.h>
00003 #include <geometry_msgs/Twist.h>
00004 #include <nav_msgs/Odometry.h>
00005 #include <dynamic_reconfigure/server.h>
00006 #include <riskrrt/OccupancyGridArray.h>
00007 #include <riskrrt/PoseTwistStamped.h>
00008 #include <riskrrt/Trajectory.h>
00009 #include <vector>
00010 #include <iostream>
00011 #include <tf/tf.h>
00012 #include <algorithm>
00013 #include <visualization_msgs/MarkerArray.h>
00014 
00015 using namespace std;
00016 
00017 struct custom_pose{//ros pose msgs use quaternion and the function provided by the tf library to get yaw from quaternion is terrible, hence this.
00018   double x;//in meters
00019   double y;//in meters
00020   double theta;//in radians (-PI, PI)
00021 };
00022 
00023 nav_msgs::OccupancyGrid grid;
00024 //nav_msgs::OccupancyGrid modified_grid;
00025 riskrrt::OccupancyGridArray og_array;
00026 int nbMap;
00027 double timeStep;
00028 double arg_vel;
00029 int ped_num;
00030 custom_pose temp_pose, ped_pose;
00031 
00032 visualization_msgs::MarkerArray marker_array;
00033   visualization_msgs::Marker marker;
00034 
00035 
00036 int gridIFromPose(custom_pose pose){
00037   return (int)round((pose.x - grid.info.origin.position.x) / grid.info.resolution);
00038 }
00039 
00040 int gridJFromPose(custom_pose pose){
00041   return (int)round((pose.y - grid.info.origin.position.y) / grid.info.resolution);
00042 }
00043 
00044 int gridIndexFromCoord(int i, int j){
00045   return i + grid.info.width * j;
00046 }
00047 
00048 int gridIFromIndex(int index){
00049   return  index % grid.info.width;
00050 }
00051 
00052 int gridJFromIndex(int index){
00053   return floor(index / grid.info.width);
00054 }
00055 
00056 custom_pose poseFromGridCoord(int i, int j){
00057   custom_pose pose;
00058   pose.x = grid.info.resolution * i + grid.info.origin.position.x;
00059         pose.y = grid.info.resolution * j + grid.info.origin.position.y;
00060   return pose;
00061 }
00062 
00063 int gridIndexFromPose(custom_pose pose){
00064   int index, i, j;
00065   i = gridIFromPose(pose);
00066   j = gridJFromPose(pose);
00067   index = gridIndexFromCoord(i, j);
00068   return index;
00069 }
00070 
00071 custom_pose poseFromGridIndex(int index){//not really a pose but rather a point
00072   int i, j;
00073   custom_pose pose;
00074   
00075   i = gridIFromIndex(index);
00076   j = gridJFromIndex(index);
00077   pose = poseFromGridCoord(i, j);
00078   
00079   return pose;
00080 }
00081 
00082 void OGCallback(const nav_msgs::OccupancyGrid::ConstPtr& msg)
00083 {
00084   int i;
00085   
00086   grid = *msg;
00087   //modified_grid = grid;
00088   og_array.array.clear();
00089   for(i=0;i<nbMap;i++){
00090     og_array.array.push_back(grid);
00091   }
00092   
00093 }
00094 
00095 void pedposeCallback(const nav_msgs::Odometry::ConstPtr& msg)
00096 {
00097   
00098   int i, j, k;
00099   int ped_grid_index;
00100   int ped_grid_i;
00101   int ped_grid_j;
00102   
00103   int max_i, min_i;
00104   int max_j, min_j;
00105   
00106   ped_pose.x = msg->pose.pose.position.x;
00107   ped_pose.y = msg->pose.pose.position.y;
00108   ped_pose.theta = tf::getYaw(msg->pose.pose.orientation);
00109   
00110   
00111   marker.header.frame_id = "/map";
00112   marker.header.stamp = ros::Time();
00113   marker.ns = "humans";
00114   marker.id = rand();
00115   marker.type = visualization_msgs::Marker::MESH_RESOURCE;
00116   marker.action = visualization_msgs::Marker::ADD;
00117   marker.pose.position.x = ped_pose.x;
00118   marker.pose.position.y = ped_pose.y;
00119   marker.pose.position.z = 0;
00120   //marker.pose.orientation = msg->pose.pose.orientation;
00121   marker.pose.orientation = tf::createQuaternionMsgFromYaw(ped_pose.theta + 1.57);
00122   marker.scale.x = 0.4*0.0254;
00123   marker.scale.y = 0.4*0.0254;
00124   marker.scale.z = 0.4*0.0254;
00125   //marker.color.a = 1.0; // Don't forget to set the alpha!
00126   //marker.color.r = 1.0;
00127   //marker.color.g = 1.0;
00128   //marker.color.b = 1.0;
00129   marker.lifetime = ros::Duration(0.125);
00130   marker.mesh_use_embedded_materials = true;
00131   marker.mesh_resource = "package://riskrrt/meshes/female2/models/female2.dae";
00132   marker_array.markers.push_back(marker);
00133   
00134   for(k=0;k<nbMap;k++){
00135     
00136     temp_pose.x = ped_pose.x + k*timeStep*arg_vel*cos(ped_pose.theta);
00137     temp_pose.y = ped_pose.y + k*timeStep*arg_vel*sin(ped_pose.theta);
00138     ped_grid_i = gridIFromPose(temp_pose);
00139     ped_grid_j = gridJFromPose(temp_pose);
00140     
00141     min_i = max(ped_grid_i - 5, 0);
00142     max_i = min(ped_grid_i + 5, (int)grid.info.width);
00143     min_j = max(ped_grid_j - 5, 0);
00144     max_j = min(ped_grid_j + 5, (int)grid.info.height);
00145     
00146     for(i=min_i ; i<=max_i ; i++){
00147       for(j=min_j ; j<=max_j ; j++){
00148         og_array.array[k].data[gridIndexFromCoord(i,j)] = 100;
00149       }
00150     }
00151   }
00152   
00153 }
00154 
00155 int main(int argc, char **argv)
00156 {
00157   ros::init(argc, argv, "og_builder_stage");
00158 
00159   ros::NodeHandle n;
00160   
00161   if (argc != 3){
00162     ROS_INFO("USAGE: og_builder_stage [number of pedestrians] [speed]");
00163     return 0;
00164   }
00165   
00166   n.param("maxDepth", nbMap, 10);
00167   n.param("timeStep", timeStep, 0.5);
00168   
00169   geometry_msgs::Twist ped_vel;
00170   arg_vel = atof(argv[2]);
00171   ped_num = atoi(argv[1]);
00172   ped_vel.linear.x = arg_vel;
00173   
00174   stringstream ss;
00175   string str;
00176   int i;
00177 
00178   ros::Subscriber og_sub = n.subscribe("map", 1, OGCallback);
00179   //ped pose subs
00180   vector<ros::Subscriber> sub_vect;
00181   ros::Subscriber sub;
00182   for(i=0;i<ped_num;i++){
00183     ss << "/robot_" << i+1 << "/base_pose_ground_truth";
00184     str = ss.str();
00185     sub = n.subscribe(str, 10, pedposeCallback);
00186     sub_vect.push_back(sub);
00187     ss.clear();//clear any bits set
00188     ss.str(string());
00189   }
00190   
00191   ros::Publisher test_pub = n.advertise<nav_msgs::OccupancyGrid>("test_map", 1);
00192   ros::Publisher og_pub = n.advertise<riskrrt::OccupancyGridArray>("ogarray", 1);
00193   //ped speed pubs
00194   vector<ros::Publisher> pub_vect;
00195   ros::Publisher pub;
00196   for(i=0;i<ped_num;i++){
00197     ss << "/robot_" << i+1 << "/cmd_vel";
00198     str = ss.str();
00199     pub = n.advertise<geometry_msgs::Twist>(str, 1);
00200     pub_vect.push_back(pub);
00201     ss.clear();//clear any bits set
00202     ss.str(string());
00203   }
00204 
00205   ros::Rate loop_rate(10);
00206   ros::Publisher vis_pub = n.advertise<visualization_msgs::MarkerArray>( "human_marker", 0 );
00207 
00208   while (ros::ok())
00209   {
00210     
00211     marker_array.markers.clear();
00212     
00213     ros::spinOnce();
00214     
00215     og_pub.publish(og_array);
00216     test_pub.publish(og_array.array[nbMap-1]);
00217     og_array.array.clear();
00218     for(i=0;i<nbMap;i++){
00219       og_array.array.push_back(grid);
00220     }
00221     //modified_grid = grid;
00222     
00223     for(i=0;i<ped_num;i++){
00224       pub_vect[i].publish(ped_vel);
00225     }
00226     
00227     vis_pub.publish(marker_array);
00228     marker_array.markers.clear();
00229 
00230     loop_rate.sleep();
00231   }
00232   return 0;
00233 }


riskrrt
Author(s): Gregoire Vignon
autogenerated on Thu Jun 6 2019 18:42:06