waypoints_saver.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2014, Daiki Maekawa and Robot Design and Control Lab.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  *  * Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  *  * Redistributions in binary form must reproduce the above
00014  *    copyright notice, this list of conditions and the following
00015  *    disclaimer in the documentation and/or other materials provided
00016  *    with the distribution.
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00018  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00021  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00024  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00025  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00026  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00027  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028  * POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 #include <ros/ros.h>
00032 #include <tf/transform_listener.h>
00033 
00034 #include <sensor_msgs/Joy.h>
00035 #include <geometry_msgs/PointStamped.h>
00036 #include <geometry_msgs/PoseStamped.h>
00037 #include <std_msgs/String.h>
00038 
00039 #include <vector>
00040 #include <fstream>
00041 #include <string>
00042 
00043 
00044 class WaypointsSaver{
00045 public:
00046     WaypointsSaver() : 
00047         filename_("waypoints.yaml")
00048     {
00049         waypoints_viz_sub_ = nh_.subscribe("waypoints_viz", 1, &WaypointsSaver::waypointsVizCallback, this);
00050         waypoints_joy_sub_ = nh_.subscribe("waypoints_joy", 1, &WaypointsSaver::waypointsJoyCallback, this);
00051         finish_pose_sub_ = nh_.subscribe("finish_pose", 1, &WaypointsSaver::finishPoseCallback, this);
00052 
00053         ros::NodeHandle private_nh("~");
00054         private_nh.param("filename", filename_, filename_);
00055         private_nh.param("save_joy_button", save_joy_button_, 0);
00056         private_nh.param("robot_frame", robot_frame_, std::string("/base_link"));
00057         private_nh.param("world_frame", world_frame_, std::string("/map"));
00058     }
00059 
00060     void waypointsJoyCallback(const sensor_msgs::Joy &msg){
00061         static ros::Time saved_time(0.0);
00062         //ROS_INFO_STREAM("joy = " << msg);
00063         if(msg.buttons[save_joy_button_] == 1 && (ros::Time::now() - saved_time).toSec() > 3.0){
00064             tf::StampedTransform robot_gl;
00065             try{
00066                 tf_listener_.lookupTransform(world_frame_, robot_frame_, ros::Time(0.0), robot_gl);
00067                 geometry_msgs::PointStamped point;
00068                 point.point.x = robot_gl.getOrigin().x();
00069                 point.point.y = robot_gl.getOrigin().y();
00070                 point.point.z = robot_gl.getOrigin().z();
00071                 waypoints_.push_back(point);
00072                 saved_time = ros::Time::now();
00073             }catch(tf::TransformException &e){
00074                 ROS_WARN_STREAM("tf::TransformException: " << e.what());
00075             }
00076         }
00077     }
00078     
00079     void waypointsVizCallback(const geometry_msgs::PointStamped &msg){
00080         ROS_INFO_STREAM("point = " << msg);
00081         waypoints_.push_back(msg);
00082     }
00083 
00084     void finishPoseCallback(const geometry_msgs::PoseStamped &msg){
00085         finish_pose_ = msg;
00086         save();
00087         waypoints_.clear();
00088     }
00089     
00090     void save(){
00091         std::ofstream ofs(filename_.c_str(), std::ios::out);
00092         
00093         ofs << "waypoints:" << std::endl;
00094         for(int i=0; i < waypoints_.size(); i++){
00095             ofs << "    " << "- point:" << std::endl;
00096             ofs << "        x: " << waypoints_[i].point.x << std::endl;
00097             ofs << "        y: " << waypoints_[i].point.y << std::endl;
00098             ofs << "        z: " << waypoints_[i].point.z << std::endl;
00099         }
00100         
00101         ofs << "finish_pose:"           << std::endl;
00102         ofs << "    header:"            << std::endl;
00103         ofs << "        seq: "          << finish_pose_.header.seq << std::endl;
00104         ofs << "        stamp: "        << finish_pose_.header.stamp << std::endl;
00105         ofs << "        frame_id: "     << finish_pose_.header.frame_id << std::endl;;
00106         ofs << "    pose:"              << std::endl;
00107         ofs << "        position:"      << std::endl;
00108         ofs << "            x: "        << finish_pose_.pose.position.x << std::endl;
00109         ofs << "            y: "        << finish_pose_.pose.position.y << std::endl;
00110         ofs << "            z: "        << finish_pose_.pose.position.z << std::endl;
00111         ofs << "        orientation:"   << std::endl;
00112         ofs << "            x: "        << finish_pose_.pose.orientation.x << std::endl;
00113         ofs << "            y: "        << finish_pose_.pose.orientation.y << std::endl;
00114         ofs << "            z: "        << finish_pose_.pose.orientation.z << std::endl;
00115         ofs << "            w: "        << finish_pose_.pose.orientation.w << std::endl;
00116 
00117         ofs.close();
00118 
00119         ROS_INFO_STREAM("write success");
00120     }
00121     
00122     void run(){
00123         ros::spin();
00124     }
00125     
00126 private:
00127     ros::Subscriber waypoints_viz_sub_;
00128     ros::Subscriber waypoints_joy_sub_;
00129     ros::Subscriber finish_pose_sub_;
00130     std::vector<geometry_msgs::PointStamped> waypoints_;
00131     geometry_msgs::PoseStamped finish_pose_;
00132     tf::TransformListener tf_listener_;
00133     int save_joy_button_;
00134     ros::NodeHandle nh_;
00135     std::string filename_;
00136     std::string world_frame_;
00137     std::string robot_frame_;
00138 };
00139 
00140 int main(int argc, char *argv[]){
00141     ros::init(argc, argv, "waypoints_saver");
00142     WaypointsSaver saver;
00143     saver.run();
00144 
00145     return 0;
00146 }


waypoints_navigation
Author(s): Daiki Maekawa
autogenerated on Wed Nov 4 2015 11:28:10