-
Notifications
You must be signed in to change notification settings - Fork 0
/
trajectory_sampler.cpp
143 lines (129 loc) · 5.25 KB
/
trajectory_sampler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright (c) 2017, Markus Achtelik, ASL, ETH Zurich, Switzerland
* Copyright (c) 2017, Michael Burri, ASL, ETH Zurich, Switzerland
* Copyright (c) 2017, Helen Oleynikova, ASL, ETH Zurich, Switzerland
* Copyright (c) 2017, Rik Bähnemann, ASL, ETH Zurich, Switzerland
* Copyright (c) 2017, Marija Popovic, ASL, ETH Zurich, Switzerland
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <trajectory_planner/trajectory_sampler.h>
TrajectorySamplerNode::TrajectorySamplerNode(const ros::NodeHandle& nh,
const ros::NodeHandle& nh_private)
: nh_(nh),
nh_private_(nh_private),
publish_whole_trajectory_(false),
dt_(0.01),
current_sample_time_(0.0) {
nh_private_.param("publish_whole_trajectory", publish_whole_trajectory_,
publish_whole_trajectory_);
nh_private_.param("dt", dt_, dt_);
command_pub_ = nh_.advertise<trajectory_msgs::MultiDOFJointTrajectory>(
mav_msgs::default_topics::COMMAND_TRAJECTORY, 1);
trajectory_sub_ = nh_.subscribe(
"path_segments_4D", 1000, &TrajectorySamplerNode::pathSegmentsCallback, this);
// trajectory4D_sub_ = nh_.subscribe(
// "path_segments_4D", 1000, &TrajectorySamplerNode::pathSegments4DCallback, this);
stop_srv_ = nh_.advertiseService(
"stop_sampling", &TrajectorySamplerNode::stopSamplingCallback, this);
position_hold_client_ =
nh_.serviceClient<std_srvs::Empty>("back_to_position_hold");
const bool oneshot = false;
const bool autostart = false;
publish_timer_ = nh_.createTimer(ros::Duration(dt_),
&TrajectorySamplerNode::commandTimerCallback,
this, oneshot, autostart);
}
TrajectorySamplerNode::~TrajectorySamplerNode() { publish_timer_.stop(); }
void TrajectorySamplerNode::pathSegmentsCallback(
const mav_planning_msgs::PolynomialTrajectory4D& segments_message) {
if (segments_message.segments.empty()) {
ROS_WARN("Trajectory sampler: received empty waypoint message");
return;
} else {
ROS_INFO("Trajectory sampler: received %lu waypoints",
segments_message.segments.size());
}
bool success = mav_trajectory_generation::polynomialTrajectoryMsgToTrajectory(
segments_message, &trajectory_);
if (!success) {
return;
}
processTrajectory();
}
void TrajectorySamplerNode::pathSegments4DCallback(
const mav_planning_msgs::PolynomialTrajectory4D& segments_message) {
if (segments_message.segments.empty()) {
ROS_WARN("Trajectory sampler: received empty waypoint message");
return;
} else {
ROS_INFO("Trajectory sampler: received %lu waypoints",
segments_message.segments.size());
}
bool success = mav_trajectory_generation::polynomialTrajectoryMsgToTrajectory(
segments_message, &trajectory_);
if (!success) {
return;
}
processTrajectory();
}
void TrajectorySamplerNode::processTrajectory() {
// Call the service call to takeover publishing commands.
if (position_hold_client_.exists()) {
std_srvs::Empty empty_call;
position_hold_client_.call(empty_call);
}
if (publish_whole_trajectory_) {
// Publish the entire trajectory at once.
mav_msgs::EigenTrajectoryPoint::Vector trajectory_points;
mav_trajectory_generation::sampleWholeTrajectory(trajectory_, dt_,
&trajectory_points);
trajectory_msgs::MultiDOFJointTrajectory msg_pub;
msgMultiDofJointTrajectoryFromEigen(trajectory_points, &msg_pub);
command_pub_.publish(msg_pub);
} else {
publish_timer_.start();
current_sample_time_ = 0.0;
start_time_ = ros::Time::now();
}
}
bool TrajectorySamplerNode::stopSamplingCallback(
std_srvs::EmptyRequest& request, std_srvs::EmptyResponse& response) {
publish_timer_.stop();
return true;
}
void TrajectorySamplerNode::commandTimerCallback(const ros::TimerEvent&) {
if (current_sample_time_ <= trajectory_.getMaxTime()) {
trajectory_msgs::MultiDOFJointTrajectory msg;
mav_msgs::EigenTrajectoryPoint trajectory_point;
bool success = mav_trajectory_generation::sampleTrajectoryAtTime(
trajectory_, current_sample_time_, &trajectory_point);
if (!success) {
publish_timer_.stop();
}
mav_msgs::msgMultiDofJointTrajectoryFromEigen(trajectory_point, &msg);
msg.points[0].time_from_start = ros::Duration(current_sample_time_);
command_pub_.publish(msg);
current_sample_time_ += dt_;
} else {
publish_timer_.stop();
}
}
int main(int argc, char** argv) {
ros::init(argc, argv, "trajectory_sampler_node");
ros::NodeHandle nh("");
ros::NodeHandle nh_private("~");
TrajectorySamplerNode trajectory_sampler_node(nh, nh_private);
ROS_INFO("Initialized trajectory sampler.");
ros::spin();
}