waypoint_publisher_file.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2015 Fadri Furrer, ASL, ETH Zurich, Switzerland
00003  * Copyright 2015 Michael Burri, ASL, ETH Zurich, Switzerland
00004  * Copyright 2015 Mina Kamel, ASL, ETH Zurich, Switzerland
00005  * Copyright 2015 Janosch Nikolic, ASL, ETH Zurich, Switzerland
00006  * Copyright 2015 Markus Achtelik, ASL, ETH Zurich, Switzerland
00007  *
00008  * Licensed under the Apache License, Version 2.0 (the "License");
00009  * you may not use this file except in compliance with the License.
00010  * You may obtain a copy of the License at
00011  *
00012  *     http://www.apache.org/licenses/LICENSE-2.0
00013 
00014  * Unless required by applicable law or agreed to in writing, software
00015  * distributed under the License is distributed on an "AS IS" BASIS,
00016  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017  * See the License for the specific language governing permissions and
00018  * limitations under the License.
00019  */
00020 
00021 #include <fstream>
00022 #include <iostream>
00023 
00024 #include <Eigen/Geometry>
00025 #include <mav_msgs/conversions.h>
00026 #include <mav_msgs/default_topics.h>
00027 #include <mav_msgs/eigen_mav_msgs.h>
00028 #include <ros/ros.h>
00029 #include <sensor_msgs/Imu.h>
00030 #include <trajectory_msgs/MultiDOFJointTrajectory.h>
00031 
00032 bool sim_running = false;
00033 
00034 static const int64_t kNanoSecondsInSecond = 1000000000;
00035 
00036 void callback(const sensor_msgs::ImuPtr& msg) {
00037   sim_running = true;
00038 }
00039 
00040 class WaypointWithTime {
00041  public:
00042   WaypointWithTime()
00043       : waiting_time(0), yaw(0.0) {
00044   }
00045 
00046   WaypointWithTime(double t, float x, float y, float z, float _yaw)
00047       : position(x, y, z), yaw(_yaw), waiting_time(t) {
00048   }
00049 
00050   Eigen::Vector3d position;
00051   double yaw;
00052   double waiting_time;
00053 };
00054 
00055 int main(int argc, char** argv) {
00056   ros::init(argc, argv, "waypoint_publisher");
00057   ros::NodeHandle nh;
00058 
00059   ROS_INFO("Started waypoint_publisher.");
00060 
00061   ros::V_string args;
00062   ros::removeROSArgs(argc, argv, args);
00063 
00064   if (args.size() != 2 && args.size() != 3) {
00065     ROS_ERROR("Usage: waypoint_publisher <waypoint_file>"
00066         "\nThe waypoint file should be structured as: space separated: wait_time [s] x[m] y[m] z[m] yaw[deg])");
00067     return -1;
00068   }
00069 
00070   std::vector<WaypointWithTime> waypoints;
00071   const float DEG_2_RAD = M_PI / 180.0;
00072 
00073   std::ifstream wp_file(args.at(1).c_str());
00074 
00075   if (wp_file.is_open()) {
00076     double t, x, y, z, yaw;
00077     // Only read complete waypoints.
00078     while (wp_file >> t >> x >> y >> z >> yaw) {
00079       waypoints.push_back(WaypointWithTime(t, x, y, z, yaw * DEG_2_RAD));
00080     }
00081     wp_file.close();
00082     ROS_INFO("Read %d waypoints.", (int) waypoints.size());
00083   } else {
00084     ROS_ERROR_STREAM("Unable to open poses file: " << args.at(1));
00085     return -1;
00086   }
00087 
00088   // The IMU is used, to determine if the simulator is running or not.
00089   ros::Subscriber sub = nh.subscribe("imu", 10, &callback);
00090 
00091   ros::Publisher wp_pub =
00092       nh.advertise<trajectory_msgs::MultiDOFJointTrajectory>(
00093       mav_msgs::default_topics::COMMAND_TRAJECTORY, 10);
00094 
00095   ROS_INFO("Wait for simulation to become ready...");
00096 
00097   while (!sim_running && ros::ok()) {
00098     ros::spinOnce();
00099     ros::Duration(0.1).sleep();
00100   }
00101 
00102   ROS_INFO("...ok");
00103 
00104   // Wait for 30s such that everything can settle and the mav flies to the initial position.
00105   ros::Duration(30).sleep();
00106 
00107   ROS_INFO("Start publishing waypoints.");
00108 
00109   trajectory_msgs::MultiDOFJointTrajectoryPtr msg(new trajectory_msgs::MultiDOFJointTrajectory);
00110   msg->header.stamp = ros::Time::now();
00111   msg->points.resize(waypoints.size());
00112   msg->joint_names.push_back("base_link");
00113   int64_t time_from_start_ns = 0;
00114   for (size_t i = 0; i < waypoints.size(); ++i) {
00115     WaypointWithTime& wp = waypoints[i];
00116 
00117     mav_msgs::EigenTrajectoryPoint trajectory_point;
00118     trajectory_point.position_W = wp.position;
00119     trajectory_point.setFromYaw(wp.yaw);
00120     trajectory_point.time_from_start_ns = time_from_start_ns;
00121 
00122     time_from_start_ns += static_cast<int64_t>(wp.waiting_time * kNanoSecondsInSecond);
00123 
00124     mav_msgs::msgMultiDofJointTrajectoryPointFromEigen(trajectory_point, &msg->points[i]);
00125   }
00126   wp_pub.publish(msg);
00127 
00128   ros::spinOnce();
00129   ros::shutdown();
00130 
00131   return 0;
00132 }


rotors_gazebo
Author(s): Fadri Furrer, Michael Burri, Mina Kamel, Janosch Nikolic, Markus Achtelik
autogenerated on Thu Apr 18 2019 02:43:49