44 #ifndef FASTRACK_PLANNING_TRAJECTORY_H 45 #define FASTRACK_PLANNING_TRAJECTORY_H 49 #include <fastrack_msgs/Trajectory.h> 50 #include <fastrack_msgs/State.h> 53 #include <visualization_msgs/Marker.h> 54 #include <geometry_msgs/Vector3.h> 55 #include <std_msgs/ColorRGBA.h> 58 namespace trajectory {
72 explicit Trajectory(
const std::vector<S> &states,
73 const std::vector<double> ×);
76 explicit Trajectory(
const fastrack_msgs::Trajectory::ConstPtr& msg);
101 fastrack_msgs::Trajectory
ToRos()
const;
104 void Visualize(
const ros::Publisher& pub,
const std::string& frame)
const;
108 std_msgs::ColorRGBA
Colormap(
double t)
const;
126 for (
const auto& traj : trajs) {
128 const double time_offset =
129 (
times_.empty()) ? 0.0 :
times_.back() - traj.times_.front();
132 states_.insert(
states_.end(), traj.states_.begin(), traj.states_.end());
133 for (
size_t ii = 0; ii < traj.times_.size(); ii++)
134 times_.push_back(traj.times_[ii] + time_offset);
141 for (
size_t ii = 1; ii <
times_.size(); ii++) {
143 throw std::runtime_error(
"Time was not monotone.");
150 const std::vector<double>& times)
157 ROS_ERROR(
"Trajectory: states/times are not the same length.");
168 for (
size_t ii = 1; ii <
times_.size(); ii++) {
170 ROS_ERROR(
"Trajectory: fixing an inversion in the list of times.");
180 size_t num_elements = msg->states.size();
183 if (msg->states.size() != msg->times.size()) {
184 ROS_ERROR(
"Trajectory: states/times are not the same length.");
185 num_elements = std::min(msg->states.size(), msg->times.size());
189 for (
size_t ii = 0; ii < num_elements; ii++) {
190 states_.push_back(S(msg->states[ii]));
191 times_.push_back(msg->times[ii]);
195 if (num_elements > 0)
196 configuration_ = msg->states.front().x.size() == S::ConfigurationDimension();
206 const auto iter = std::lower_bound(
times_.begin(),
times_.end(), t);
210 if (iter ==
times_.begin()) {
211 ROS_WARN_THROTTLE(1.0,
"Trajectory: interpolating before first time.");
217 if (iter ==
times_.end()) {
218 ROS_WARN_THROTTLE(1.0,
"Trajectory: interpolating after the last time.");
224 const size_t hi = (iter -
times_.begin());
225 const size_t lo = hi - 1;
235 interpolated.SetConfigurationDot(
238 interpolated.SetConfigurationDot(
239 VectorXd::Zero(S::ConfigurationDimension()));
249 ROS_ERROR(
"Trajectory: Tried to reset first time of empty trajectory.");
253 const double constant_shift = t -
times_.front();
254 for (
size_t ii = 0; ii <
times_.size(); ii++)
255 times_[ii] += constant_shift;
261 fastrack_msgs::Trajectory msg;
263 for (
size_t ii = 0; ii <
states_.size(); ii++) {
265 msg.times.push_back(
times_[ii]);
275 const std::string& frame)
const {
276 if (pub.getNumSubscribers() == 0) {
277 ROS_WARN_THROTTLE(1.0,
"Trajectory: I'm lonely. Please subscribe.");
282 visualization_msgs::Marker spheres;
283 spheres.ns =
"spheres";
284 spheres.header.frame_id = frame;
285 spheres.header.stamp = ros::Time::now();
287 spheres.type = visualization_msgs::Marker::SPHERE_LIST;
288 spheres.action = visualization_msgs::Marker::ADD;
289 spheres.scale.x = 0.1;
290 spheres.scale.y = 0.1;
291 spheres.scale.z = 0.1;
294 visualization_msgs::Marker lines;
296 lines.header.frame_id = frame;
297 lines.header.stamp = ros::Time::now();
299 lines.type = visualization_msgs::Marker::LINE_STRIP;
300 lines.action = visualization_msgs::Marker::ADD;
301 lines.scale.x = 0.05;
304 for (
size_t ii = 0; ii <
states_.size(); ii++) {
305 geometry_msgs::Point p;
312 spheres.points.push_back(p);
313 spheres.colors.push_back(c);
314 lines.points.push_back(p);
315 lines.colors.push_back(c);
319 pub.publish(spheres);
327 std_msgs::ColorRGBA c;
std_msgs::ColorRGBA Colormap(double t) const
void ResetFirstTime(double t)
const std::vector< S > & States() const
const std::vector< double > & Times() const
void Visualize(const ros::Publisher &pub, const std::string &frame) const
S Interpolate(double t) const
fastrack_msgs::Trajectory ToRos() const
std::vector< double > times_