Go to the documentation of this file.00001 #include <iostream>
00002 #include <sstream>
00003 #include <fstream>
00004 #include <ros/ros.h>
00005 #include <jsk_recognition_msgs/BoundingBoxArray.h>
00006 #include <boost/tokenizer.hpp>
00007 #include <tf/transform_broadcaster.h>
00008 #include <tf/tf.h>
00009
00010 #include <ros/package.h>
00011
00012 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
00013
00014 class FakeTargetDetector
00015 {
00016 public:
00017 FakeTargetDetector():
00018 rate_(10)
00019 {
00020 std::string filename;
00021
00022 ros::NodeHandle n("~");
00023 n.param<std::string>("waypointsfile", filename,
00024 ros::package::getPath("fake_target_detector")
00025 + "/targetfiles/targetlist.csv");
00026 readFakeTargetsList(filename);
00027 recognized_pub_ = nh_.advertise<jsk_recognition_msgs::BoundingBoxArray>("/recognized_result", 1);
00028 tf_frame_timer_ = nh_.createTimer(ros::Duration(0.1), boost::bind(&FakeTargetDetector::tfSendTransformCallback, this, _1));
00029 }
00030 int readFakeTargetsList(std::string filename)
00031 {
00032 const int rows_num = 2;
00033 boost::char_separator<char> sep("," ,"", boost::keep_empty_tokens);
00034 std::ifstream ifs(filename.c_str());
00035 std::string line;
00036 while(ifs.good()){
00037 getline(ifs, line);
00038 if(line.empty()){ break; }
00039 tokenizer tokens(line, sep);
00040 std::vector<double> data;
00041 tokenizer::iterator it = tokens.begin();
00042 for(; it != tokens.end() ; ++it){
00043 std::stringstream ss;
00044 double d;
00045 ss << *it;
00046 ss >> d;
00047 data.push_back(d);
00048 }
00049 if(data.size() != rows_num){
00050 ROS_ERROR("Row size is mismatch!!");
00051 return -1;
00052 }else{
00053 jsk_recognition_msgs::BoundingBox box;
00054 box.header.frame_id ="map";
00055 box.header.stamp = ros::Time::now();
00056 box.pose.position.x = data[0];
00057 box.pose.position.y = data[1];
00058 box.pose.position.z = 0;
00059 box.pose.orientation.x = 0;
00060 box.pose.orientation.y = 0;
00061 box.pose.orientation.z = 0;
00062 box.pose.orientation.w = 1;
00063 box.dimensions.x = 1.0;
00064 box.dimensions.y = 1.0;
00065 box.dimensions.z = 1.0;
00066 box_array_.boxes.push_back(box);
00067 }
00068 }
00069 return 0;
00070 }
00071 void tfSendTransformCallback(const ros::TimerEvent&)
00072 {
00073 tf::Transform t;
00074 ros::Time time = ros::Time::now();
00075
00076 for (size_t i = 0; i < box_array_.boxes.size(); ++i) {
00077 std::stringstream s;
00078 s << "fake_target_" << i;
00079 t.setOrigin(tf::Vector3(box_array_.boxes[i].pose.position.x,
00080 box_array_.boxes[i].pose.position.y,
00081 box_array_.boxes[i].pose.position.z));
00082 t.setRotation(tf::Quaternion(box_array_.boxes[i].pose.orientation.x,
00083 box_array_.boxes[i].pose.orientation.y,
00084 box_array_.boxes[i].pose.orientation.z,
00085 box_array_.boxes[i].pose.orientation.w));
00086 br_.sendTransform(tf::StampedTransform(t, time, "map", s.str()));
00087 }
00088 }
00089
00090 void fakePublisher(){
00091 box_array_.header.stamp = ros::Time::now();
00092 box_array_.header.frame_id = "map";
00093 recognized_pub_.publish(box_array_);
00094 }
00095 void run(){
00096 while(nh_.ok()){
00097 this->fakePublisher();
00098 ros::spinOnce();
00099 rate_.sleep();
00100 }
00101 }
00102 private:
00103 ros::NodeHandle nh_;
00104 ros::Rate rate_;
00105 ros::Publisher recognized_pub_;
00106 jsk_recognition_msgs::BoundingBoxArray box_array_;
00107 tf::TransformBroadcaster br_;
00108 ros::Timer tf_frame_timer_;
00109 };
00110
00111 int main(int argc, char *argv[])
00112 {
00113 ros::init(argc, argv, "fake_target_detector");
00114
00115 FakeTargetDetector fake_target_detector;
00116 fake_target_detector.run();
00117 return 0;
00118 }