waypoint_publisher_file.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Fadri Furrer, ASL, ETH Zurich, Switzerland
3  * Copyright 2015 Michael Burri, ASL, ETH Zurich, Switzerland
4  * Copyright 2015 Mina Kamel, ASL, ETH Zurich, Switzerland
5  * Copyright 2015 Janosch Nikolic, ASL, ETH Zurich, Switzerland
6  * Copyright 2015 Markus Achtelik, ASL, ETH Zurich, Switzerland
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #include <fstream>
22 #include <iostream>
23 
24 #include <Eigen/Geometry>
25 #include <mav_msgs/conversions.h>
28 #include <ros/ros.h>
29 #include <sensor_msgs/Imu.h>
30 #include <trajectory_msgs/MultiDOFJointTrajectory.h>
31 
32 bool sim_running = false;
33 
34 static const int64_t kNanoSecondsInSecond = 1000000000;
35 
36 void callback(const sensor_msgs::ImuPtr& msg) {
37  sim_running = true;
38 }
39 
41  public:
43  : waiting_time(0), yaw(0.0) {
44  }
45 
46  WaypointWithTime(double t, float x, float y, float z, float _yaw)
47  : position(x, y, z), yaw(_yaw), waiting_time(t) {
48  }
49 
50  Eigen::Vector3d position;
51  double yaw;
52  double waiting_time;
53 };
54 
55 int main(int argc, char** argv) {
56  ros::init(argc, argv, "waypoint_publisher");
58 
59  ROS_INFO("Started waypoint_publisher.");
60 
62  ros::removeROSArgs(argc, argv, args);
63 
64  if (args.size() != 2 && args.size() != 3) {
65  ROS_ERROR("Usage: waypoint_publisher <waypoint_file>"
66  "\nThe waypoint file should be structured as: space separated: wait_time [s] x[m] y[m] z[m] yaw[deg])");
67  return -1;
68  }
69 
70  std::vector<WaypointWithTime> waypoints;
71  const float DEG_2_RAD = M_PI / 180.0;
72 
73  std::ifstream wp_file(args.at(1).c_str());
74 
75  if (wp_file.is_open()) {
76  double t, x, y, z, yaw;
77  // Only read complete waypoints.
78  while (wp_file >> t >> x >> y >> z >> yaw) {
79  waypoints.push_back(WaypointWithTime(t, x, y, z, yaw * DEG_2_RAD));
80  }
81  wp_file.close();
82  ROS_INFO("Read %d waypoints.", (int) waypoints.size());
83  } else {
84  ROS_ERROR_STREAM("Unable to open poses file: " << args.at(1));
85  return -1;
86  }
87 
88  // The IMU is used, to determine if the simulator is running or not.
89  ros::Subscriber sub = nh.subscribe("imu", 10, &callback);
90 
91  ros::Publisher wp_pub =
92  nh.advertise<trajectory_msgs::MultiDOFJointTrajectory>(
94 
95  ROS_INFO("Wait for simulation to become ready...");
96 
97  while (!sim_running && ros::ok()) {
98  ros::spinOnce();
99  ros::Duration(0.1).sleep();
100  }
101 
102  ROS_INFO("...ok");
103 
104  // Wait for 30s such that everything can settle and the mav flies to the initial position.
105  ros::Duration(30).sleep();
106 
107  ROS_INFO("Start publishing waypoints.");
108 
109  trajectory_msgs::MultiDOFJointTrajectoryPtr msg(new trajectory_msgs::MultiDOFJointTrajectory);
110  msg->header.stamp = ros::Time::now();
111  msg->points.resize(waypoints.size());
112  msg->joint_names.push_back("base_link");
113  int64_t time_from_start_ns = 0;
114  for (size_t i = 0; i < waypoints.size(); ++i) {
115  WaypointWithTime& wp = waypoints[i];
116 
117  mav_msgs::EigenTrajectoryPoint trajectory_point;
118  trajectory_point.position_W = wp.position;
119  trajectory_point.setFromYaw(wp.yaw);
120  trajectory_point.time_from_start_ns = time_from_start_ns;
121 
122  time_from_start_ns += static_cast<int64_t>(wp.waiting_time * kNanoSecondsInSecond);
123 
124  mav_msgs::msgMultiDofJointTrajectoryPointFromEigen(trajectory_point, &msg->points[i]);
125  }
126  wp_pub.publish(msg);
127 
128  ros::spinOnce();
129  ros::shutdown();
130 
131  return 0;
132 }
msg
ros::NodeHandle nh
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool sim_running
static const int64_t kNanoSecondsInSecond
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
#define M_PI
void msgMultiDofJointTrajectoryPointFromEigen(const EigenTrajectoryPoint &trajectory_point, trajectory_msgs::MultiDOFJointTrajectoryPoint *msg)
void publish(const boost::shared_ptr< M > &message) const
std::vector< std::string > V_string
void callback(const sensor_msgs::ImuPtr &msg)
WaypointWithTime(double t, float x, float y, float z, float _yaw)
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
int main(int argc, char **argv)
static Time now()
ROSCPP_DECL void shutdown()
ROSCPP_DECL void removeROSArgs(int argc, const char *const *argv, V_string &args_out)
bool sleep() const
#define ROS_ERROR_STREAM(args)
static constexpr char COMMAND_TRAJECTORY[]
std::vector< mavros_msgs::Waypoint > waypoints
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)


rotors_gazebo
Author(s): Fadri Furrer, Michael Burri, Mina Kamel, Janosch Nikolic, Markus Achtelik
autogenerated on Mon Feb 28 2022 23:39:12