feat(minco_ws): MID360 + Point-LIO + SCAN/MINCO 导航工作区(19 个包)

- scan/: SCAN 无图局部规划(6 包)+ step_blocker(台阶封堵)+ body_odom(雷达→车体中心)
- navigation/: MINCO + Nav2 有图方案;perception/: Livox 驱动 + Point-LIO + rog_map
- chassis/: R2 底盘串口桥
- docs/: QUICKSTART(上机流程)/ SCAN_VEHICLE(方案与限制)/ DEPLOY_ROBOT(双机部署)
- scripts/: setup_deps.sh(依赖体检/安装)+ deploy_to_robot.sh(rsync 同步/远程编译)

排除 build/ install/ log/、PCD 地图(155 MB,单独 rsync)。
This commit is contained in:
2026-10-20 10:51:18 +08:00
parent eba4570f4e
commit a2947065b3
829 changed files with 272495 additions and 1 deletions
@@ -0,0 +1,28 @@
cmake_minimum_required(VERSION 3.8)
project(traj_utils)
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()
add_compile_options(-Wall -Wextra -Wpedantic)
find_package(ament_cmake REQUIRED)
find_package(bspline_opt REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(visualization_msgs REQUIRED)
add_library(traj_utils src/planning_visualization.cpp src/polynomial_traj.cpp)
target_include_directories(traj_utils PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
ament_target_dependencies(traj_utils bspline_opt rclcpp std_msgs visualization_msgs)
target_link_libraries(traj_utils Eigen3::Eigen)
install(TARGETS traj_utils EXPORT export_traj_utils
ARCHIVE DESTINATION lib LIBRARY DESTINATION lib RUNTIME DESTINATION bin)
install(DIRECTORY include/ DESTINATION include)
ament_export_targets(export_traj_utils HAS_LIBRARY_TARGET)
ament_export_dependencies(bspline_opt Eigen3 rclcpp std_msgs visualization_msgs)
ament_package()
@@ -0,0 +1,57 @@
#ifndef _PLANNING_VISUALIZATION_H_
#define _PLANNING_VISUALIZATION_H_
#include <Eigen/Eigen>
#include <algorithm>
#include <bspline_opt/uniform_bspline.h>
#include <geometry_msgs/msg/point.hpp>
#include <iostream>
#include <traj_utils/polynomial_traj.h>
#include <rclcpp/rclcpp.hpp>
#include <vector>
#include <visualization_msgs/msg/marker.hpp>
#include <visualization_msgs/msg/marker_array.hpp>
#include <stdlib.h>
using std::vector;
namespace scan_planner
{
class PlanningVisualization
{
private:
using MarkerPublisher = rclcpp::Publisher<visualization_msgs::msg::Marker>;
using MarkerArrayPublisher = rclcpp::Publisher<visualization_msgs::msg::MarkerArray>;
rclcpp::Node *node_{nullptr};
MarkerPublisher::SharedPtr goal_point_pub;
MarkerPublisher::SharedPtr global_list_pub;
MarkerPublisher::SharedPtr init_list_pub;
MarkerPublisher::SharedPtr optimal_list_pub;
MarkerPublisher::SharedPtr a_star_list_pub;
public:
PlanningVisualization(/* args */) {}
~PlanningVisualization() {}
explicit PlanningVisualization(rclcpp::Node *node);
typedef std::shared_ptr<PlanningVisualization> Ptr;
void displayMarkerList(const MarkerPublisher::SharedPtr &pub, const vector<Eigen::Vector3d> &list, double scale,
Eigen::Vector4d color, int id);
void generatePathDisplayArray(visualization_msgs::msg::MarkerArray &array,
const vector<Eigen::Vector3d> &list, double scale, Eigen::Vector4d color, int id);
void generateArrowDisplayArray(visualization_msgs::msg::MarkerArray &array,
const vector<Eigen::Vector3d> &list, double scale, Eigen::Vector4d color, int id);
void displayGoalPoint(Eigen::Vector3d goal_point, Eigen::Vector4d color, const double scale, int id);
void displayGlobalPathList(vector<Eigen::Vector3d> global_pts, const double scale, int id);
void displayInitPathList(vector<Eigen::Vector3d> init_pts, const double scale, int id);
void displayOptimalList(Eigen::MatrixXd optimal_pts, int id);
void displayOptimalTraj(UniformBspline position_traj, int id);
void displayAStarList(std::vector<std::vector<Eigen::Vector3d>> a_star_paths, int id);
void displayArrowList(const MarkerArrayPublisher::SharedPtr &pub, const vector<Eigen::Vector3d> &list,
double scale, Eigen::Vector4d color, int id);
// void displayIntermediateState(ros::Publisher& intermediate_pub, scan_planner::BsplineOptimizer::Ptr optimizer, double sleep_time, const int start_iteration);
// void displayNewArrow(ros::Publisher& guide_vector_pub, scan_planner::BsplineOptimizer::Ptr optimizer);
};
} // namespace scan_planner
#endif
@@ -0,0 +1,336 @@
#ifndef _POLYNOMIAL_TRAJ_H
#define _POLYNOMIAL_TRAJ_H
#include <Eigen/Eigen>
#include <vector>
using std::vector;
class PolynomialTraj
{
private:
vector<double> times; // time of each segment
vector<vector<double>> cxs; // coefficient of x of each segment, from n-1 -> 0
vector<vector<double>> cys; // coefficient of y of each segment
vector<vector<double>> czs; // coefficient of z of each segment
double time_sum;
int num_seg;
/* evaluation */
vector<Eigen::Vector3d> traj_vec3d;
double length;
public:
PolynomialTraj(/* args */)
{
}
~PolynomialTraj()
{
}
void reset()
{
times.clear(), cxs.clear(), cys.clear(), czs.clear();
time_sum = 0.0, num_seg = 0;
}
void addSegment(vector<double> cx, vector<double> cy, vector<double> cz, double t)
{
cxs.push_back(cx), cys.push_back(cy), czs.push_back(cz), times.push_back(t);
}
void init()
{
num_seg = times.size();
time_sum = 0.0;
for (int i = 0; i < times.size(); ++i)
{
time_sum += times[i];
}
}
vector<double> getTimes()
{
return times;
}
vector<vector<double>> getCoef(int axis)
{
switch (axis)
{
case 0:
return cxs;
case 1:
return cys;
case 2:
return czs;
default:
std::cout << "\033[31mIllegal axis!\033[0m" << std::endl;
}
vector<vector<double>> empty;
return empty;
}
Eigen::Vector3d evaluate(double t)
{
/* detetrmine segment num */
int idx = 0;
while (times[idx] + 1e-4 < t)
{
t -= times[idx];
++idx;
}
/* evaluation */
int order = cxs[idx].size();
Eigen::VectorXd cx(order), cy(order), cz(order), tv(order);
for (int i = 0; i < order; ++i)
{
cx(i) = cxs[idx][i], cy(i) = cys[idx][i], cz(i) = czs[idx][i];
tv(order - 1 - i) = std::pow(t, double(i));
}
Eigen::Vector3d pt;
pt(0) = tv.dot(cx), pt(1) = tv.dot(cy), pt(2) = tv.dot(cz);
return pt;
}
Eigen::Vector3d evaluateVel(double t)
{
/* detetrmine segment num */
int idx = 0;
while (times[idx] + 1e-4 < t)
{
t -= times[idx];
++idx;
}
/* evaluation */
int order = cxs[idx].size();
Eigen::VectorXd vx(order - 1), vy(order - 1), vz(order - 1);
/* coef of vel */
for (int i = 0; i < order - 1; ++i)
{
vx(i) = double(i + 1) * cxs[idx][order - 2 - i];
vy(i) = double(i + 1) * cys[idx][order - 2 - i];
vz(i) = double(i + 1) * czs[idx][order - 2 - i];
}
double ts = t;
Eigen::VectorXd tv(order - 1);
for (int i = 0; i < order - 1; ++i)
tv(i) = pow(ts, i);
Eigen::Vector3d vel;
vel(0) = tv.dot(vx), vel(1) = tv.dot(vy), vel(2) = tv.dot(vz);
return vel;
}
Eigen::Vector3d evaluateAcc(double t)
{
/* detetrmine segment num */
int idx = 0;
while (times[idx] + 1e-4 < t)
{
t -= times[idx];
++idx;
}
/* evaluation */
int order = cxs[idx].size();
Eigen::VectorXd ax(order - 2), ay(order - 2), az(order - 2);
/* coef of vel */
for (int i = 0; i < order - 2; ++i)
{
ax(i) = double((i + 2) * (i + 1)) * cxs[idx][order - 3 - i];
ay(i) = double((i + 2) * (i + 1)) * cys[idx][order - 3 - i];
az(i) = double((i + 2) * (i + 1)) * czs[idx][order - 3 - i];
}
double ts = t;
Eigen::VectorXd tv(order - 2);
for (int i = 0; i < order - 2; ++i)
tv(i) = pow(ts, i);
Eigen::Vector3d acc;
acc(0) = tv.dot(ax), acc(1) = tv.dot(ay), acc(2) = tv.dot(az);
return acc;
}
/* for evaluating traj, should be called in sequence!!! */
double getTimeSum()
{
return this->time_sum;
}
vector<Eigen::Vector3d> getTraj()
{
double eval_t = 0.0;
traj_vec3d.clear();
while (eval_t < time_sum)
{
Eigen::Vector3d pt = evaluate(eval_t);
traj_vec3d.push_back(pt);
eval_t += 0.01;
}
return traj_vec3d;
}
double getLength()
{
length = 0.0;
Eigen::Vector3d p_l = traj_vec3d[0], p_n;
for (int i = 1; i < traj_vec3d.size(); ++i)
{
p_n = traj_vec3d[i];
length += (p_n - p_l).norm();
p_l = p_n;
}
return length;
}
double getMeanVel()
{
double mean_vel = length / time_sum;
}
double getAccCost()
{
double cost = 0.0;
int order = cxs[0].size();
for (int s = 0; s < times.size(); ++s)
{
Eigen::Vector3d um;
um(0) = 2 * cxs[s][order - 3], um(1) = 2 * cys[s][order - 3], um(2) = 2 * czs[s][order - 3];
cost += um.squaredNorm() * times[s];
}
return cost;
}
double getJerk()
{
double jerk = 0.0;
/* evaluate jerk */
for (int s = 0; s < times.size(); ++s)
{
Eigen::VectorXd cxv(cxs[s].size()), cyv(cys[s].size()), czv(czs[s].size());
/* convert coefficient */
int order = cxs[s].size();
for (int j = 0; j < order; ++j)
{
cxv(j) = cxs[s][order - 1 - j], cyv(j) = cys[s][order - 1 - j], czv(j) = czs[s][order - 1 - j];
}
double ts = times[s];
/* jerk matrix */
Eigen::MatrixXd mat_jerk(order, order);
mat_jerk.setZero();
for (int i = 3; i < order; i += 1)
for (int j = 3; j < order; j += 1)
{
mat_jerk(i, j) =
i * (i - 1) * (i - 2) * j * (j - 1) * (j - 2) * pow(ts, i + j - 5) / (i + j - 5);
}
jerk += (cxv.transpose() * mat_jerk * cxv)(0, 0);
jerk += (cyv.transpose() * mat_jerk * cyv)(0, 0);
jerk += (czv.transpose() * mat_jerk * czv)(0, 0);
}
return jerk;
}
void getMeanAndMaxVel(double &mean_v, double &max_v)
{
int num = 0;
mean_v = 0.0, max_v = -1.0;
for (int s = 0; s < times.size(); ++s)
{
int order = cxs[s].size();
Eigen::VectorXd vx(order - 1), vy(order - 1), vz(order - 1);
/* coef of vel */
for (int i = 0; i < order - 1; ++i)
{
vx(i) = double(i + 1) * cxs[s][order - 2 - i];
vy(i) = double(i + 1) * cys[s][order - 2 - i];
vz(i) = double(i + 1) * czs[s][order - 2 - i];
}
double ts = times[s];
double eval_t = 0.0;
while (eval_t < ts)
{
Eigen::VectorXd tv(order - 1);
for (int i = 0; i < order - 1; ++i)
tv(i) = pow(ts, i);
Eigen::Vector3d vel;
vel(0) = tv.dot(vx), vel(1) = tv.dot(vy), vel(2) = tv.dot(vz);
double vn = vel.norm();
mean_v += vn;
if (vn > max_v)
max_v = vn;
++num;
eval_t += 0.01;
}
}
mean_v = mean_v / double(num);
}
void getMeanAndMaxAcc(double &mean_a, double &max_a)
{
int num = 0;
mean_a = 0.0, max_a = -1.0;
for (int s = 0; s < times.size(); ++s)
{
int order = cxs[s].size();
Eigen::VectorXd ax(order - 2), ay(order - 2), az(order - 2);
/* coef of acc */
for (int i = 0; i < order - 2; ++i)
{
ax(i) = double((i + 2) * (i + 1)) * cxs[s][order - 3 - i];
ay(i) = double((i + 2) * (i + 1)) * cys[s][order - 3 - i];
az(i) = double((i + 2) * (i + 1)) * czs[s][order - 3 - i];
}
double ts = times[s];
double eval_t = 0.0;
while (eval_t < ts)
{
Eigen::VectorXd tv(order - 2);
for (int i = 0; i < order - 2; ++i)
tv(i) = pow(ts, i);
Eigen::Vector3d acc;
acc(0) = tv.dot(ax), acc(1) = tv.dot(ay), acc(2) = tv.dot(az);
double an = acc.norm();
mean_a += an;
if (an > max_a)
max_a = an;
++num;
eval_t += 0.01;
}
}
mean_a = mean_a / double(num);
}
static PolynomialTraj minSnapTraj(const Eigen::MatrixXd &Pos, const Eigen::Vector3d &start_vel,
const Eigen::Vector3d &end_vel, const Eigen::Vector3d &start_acc,
const Eigen::Vector3d &end_acc, const Eigen::VectorXd &Time);
static PolynomialTraj one_segment_traj_gen(const Eigen::Vector3d &start_pt, const Eigen::Vector3d &start_vel, const Eigen::Vector3d &start_acc,
const Eigen::Vector3d &end_pt, const Eigen::Vector3d &end_vel, const Eigen::Vector3d &end_acc,
double t);
};
#endif
+15
View File
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<package format="3">
<name>traj_utils</name>
<version>0.1.0</version>
<description>Trajectory primitives and visualization for SCAN-Planner.</description>
<maintainer email="[email protected]">SCAN-Planner maintainers</maintainer>
<license>Apache-2.0</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>bspline_opt</depend>
<depend>eigen</depend>
<depend>rclcpp</depend>
<depend>std_msgs</depend>
<depend>visualization_msgs</depend>
<export><build_type>ament_cmake</build_type></export>
</package>
@@ -0,0 +1,318 @@
#include <traj_utils/planning_visualization.h>
#include <cmath>
#include <limits>
#include <std_msgs/msg/color_rgba.hpp>
using std::cout;
using std::endl;
namespace scan_planner
{
PlanningVisualization::PlanningVisualization(rclcpp::Node *node)
{
node_ = node;
const auto marker_qos = rclcpp::QoS(20).reliable().transient_local();
goal_point_pub = node_->create_publisher<visualization_msgs::msg::Marker>("goal_point", marker_qos);
global_list_pub = node_->create_publisher<visualization_msgs::msg::Marker>("global_list", marker_qos);
init_list_pub = node_->create_publisher<visualization_msgs::msg::Marker>("init_list", marker_qos);
optimal_list_pub = node_->create_publisher<visualization_msgs::msg::Marker>("optimal_list", marker_qos);
a_star_list_pub = node_->create_publisher<visualization_msgs::msg::Marker>("a_star_list", marker_qos);
}
// // real ids used: {id, id+1000}
void PlanningVisualization::displayMarkerList(const MarkerPublisher::SharedPtr &pub, const vector<Eigen::Vector3d> &list, double scale,
Eigen::Vector4d color, int id)
{
visualization_msgs::msg::Marker sphere, line_strip;
sphere.header.frame_id = line_strip.header.frame_id = "world";
sphere.header.stamp = line_strip.header.stamp = node_->now();
sphere.type = visualization_msgs::msg::Marker::SPHERE_LIST;
line_strip.type = visualization_msgs::msg::Marker::LINE_STRIP;
sphere.action = line_strip.action = visualization_msgs::msg::Marker::ADD;
sphere.id = id;
line_strip.id = id + 1000;
sphere.pose.orientation.w = line_strip.pose.orientation.w = 1.0;
sphere.color.r = line_strip.color.r = color(0);
sphere.color.g = line_strip.color.g = color(1);
sphere.color.b = line_strip.color.b = color(2);
sphere.color.a = line_strip.color.a = color(3) > 1e-5 ? color(3) : 1.0;
sphere.scale.x = scale;
sphere.scale.y = scale;
sphere.scale.z = scale;
line_strip.scale.x = scale / 2;
geometry_msgs::msg::Point pt;
for (int i = 0; i < int(list.size()); i++)
{
pt.x = list[i](0);
pt.y = list[i](1);
pt.z = list[i](2);
sphere.points.push_back(pt);
line_strip.points.push_back(pt);
}
pub->publish(sphere);
pub->publish(line_strip);
}
// real ids used: {id, id+1}
void PlanningVisualization::generatePathDisplayArray(visualization_msgs::msg::MarkerArray &array,
const vector<Eigen::Vector3d> &list, double scale, Eigen::Vector4d color, int id)
{
visualization_msgs::msg::Marker sphere, line_strip;
sphere.header.frame_id = line_strip.header.frame_id = "map";
sphere.header.stamp = line_strip.header.stamp = node_->now();
sphere.type = visualization_msgs::msg::Marker::SPHERE_LIST;
line_strip.type = visualization_msgs::msg::Marker::LINE_STRIP;
sphere.action = line_strip.action = visualization_msgs::msg::Marker::ADD;
sphere.id = id;
line_strip.id = id + 1;
sphere.pose.orientation.w = line_strip.pose.orientation.w = 1.0;
sphere.color.r = line_strip.color.r = color(0);
sphere.color.g = line_strip.color.g = color(1);
sphere.color.b = line_strip.color.b = color(2);
sphere.color.a = line_strip.color.a = color(3) > 1e-5 ? color(3) : 1.0;
sphere.scale.x = scale;
sphere.scale.y = scale;
sphere.scale.z = scale;
line_strip.scale.x = scale / 3;
geometry_msgs::msg::Point pt;
for (int i = 0; i < int(list.size()); i++)
{
pt.x = list[i](0);
pt.y = list[i](1);
pt.z = list[i](2);
sphere.points.push_back(pt);
line_strip.points.push_back(pt);
}
array.markers.push_back(sphere);
array.markers.push_back(line_strip);
}
// real ids used: {1000*id ~ (arrow nums)+1000*id}
void PlanningVisualization::generateArrowDisplayArray(visualization_msgs::msg::MarkerArray &array,
const vector<Eigen::Vector3d> &list, double scale, Eigen::Vector4d color, int id)
{
visualization_msgs::msg::Marker arrow;
arrow.header.frame_id = "map";
arrow.header.stamp = node_->now();
arrow.type = visualization_msgs::msg::Marker::ARROW;
arrow.action = visualization_msgs::msg::Marker::ADD;
// geometry_msgs::Point start, end;
// arrow.points
arrow.color.r = color(0);
arrow.color.g = color(1);
arrow.color.b = color(2);
arrow.color.a = color(3) > 1e-5 ? color(3) : 1.0;
arrow.scale.x = scale;
arrow.scale.y = 2 * scale;
arrow.scale.z = 2 * scale;
geometry_msgs::msg::Point start, end;
for (int i = 0; i < int(list.size() / 2); i++)
{
// arrow.color.r = color(0) / (1+i);
// arrow.color.g = color(1) / (1+i);
// arrow.color.b = color(2) / (1+i);
start.x = list[2 * i](0);
start.y = list[2 * i](1);
start.z = list[2 * i](2);
end.x = list[2 * i + 1](0);
end.y = list[2 * i + 1](1);
end.z = list[2 * i + 1](2);
arrow.points.clear();
arrow.points.push_back(start);
arrow.points.push_back(end);
arrow.id = i + id * 1000;
array.markers.push_back(arrow);
}
}
void PlanningVisualization::displayGoalPoint(Eigen::Vector3d goal_point, Eigen::Vector4d color, const double scale, int id)
{
visualization_msgs::msg::Marker sphere;
sphere.header.frame_id = "world";
sphere.header.stamp = node_->now();
sphere.type = visualization_msgs::msg::Marker::SPHERE;
sphere.action = visualization_msgs::msg::Marker::ADD;
sphere.id = id;
sphere.pose.orientation.w = 1.0;
sphere.color.r = 1.0;
sphere.color.g = 1.0;
sphere.color.b = 1.0;
sphere.color.a = color(3);
sphere.scale.x = scale;
sphere.scale.y = scale;
sphere.scale.z = scale;
sphere.pose.position.x = goal_point(0);
sphere.pose.position.y = goal_point(1);
sphere.pose.position.z = goal_point(2);
goal_point_pub->publish(sphere);
}
void PlanningVisualization::displayGlobalPathList(vector<Eigen::Vector3d> init_pts, const double scale, int id)
{
if (global_list_pub->get_subscription_count() == 0)
{
return;
}
Eigen::Vector4d color(0, 0.5, 0.5, 1);
displayMarkerList(global_list_pub, init_pts, scale, color, id);
}
void PlanningVisualization::displayInitPathList(vector<Eigen::Vector3d> init_pts, const double scale, int id)
{
if (init_list_pub->get_subscription_count() == 0)
{
return;
}
Eigen::Vector4d color(0, 0, 1, 1);
displayMarkerList(init_list_pub, init_pts, scale, color, id);
}
void PlanningVisualization::displayOptimalList(Eigen::MatrixXd optimal_pts, int id)
{
if (optimal_list_pub->get_subscription_count() == 0)
{
return;
}
vector<Eigen::Vector3d> list;
for (int i = 0; i < optimal_pts.cols(); i++)
{
Eigen::Vector3d pt = optimal_pts.col(i).transpose();
list.push_back(pt);
}
Eigen::Vector4d color(1, 0, 0, 1);
displayMarkerList(optimal_list_pub, list, 0.15, color, id);
}
void PlanningVisualization::displayOptimalTraj(UniformBspline position_traj, int id)
{
if (optimal_list_pub->get_subscription_count() == 0)
{
return;
}
const double duration = position_traj.getTimeSum();
if (duration < 1e-6)
{
return;
}
UniformBspline velocity_traj = position_traj.getDerivative();
const int sample_num = std::max(2, static_cast<int>(std::ceil(duration / 0.1)) + 1);
std::vector<Eigen::Vector3d> points;
std::vector<double> speeds;
points.reserve(sample_num);
speeds.reserve(sample_num);
double min_speed = std::numeric_limits<double>::max();
double max_speed = 0.0;
for (int i = 0; i < sample_num; ++i)
{
const double t = duration * static_cast<double>(i) / static_cast<double>(sample_num - 1);
Eigen::Vector3d pos = position_traj.evaluateDeBoorT(t);
const double speed = velocity_traj.evaluateDeBoorT(t).norm();
points.push_back(pos);
speeds.push_back(speed);
min_speed = std::min(min_speed, speed);
max_speed = std::max(max_speed, speed);
}
visualization_msgs::msg::Marker sphere, line_strip;
sphere.header.frame_id = line_strip.header.frame_id = "world";
sphere.header.stamp = line_strip.header.stamp = node_->now();
sphere.type = visualization_msgs::msg::Marker::SPHERE_LIST;
line_strip.type = visualization_msgs::msg::Marker::LINE_STRIP;
sphere.action = line_strip.action = visualization_msgs::msg::Marker::ADD;
sphere.id = id;
line_strip.id = id + 1000;
sphere.pose.orientation.w = line_strip.pose.orientation.w = 1.0;
sphere.scale.x = sphere.scale.y = sphere.scale.z = 0.08;
line_strip.scale.x = 0.08;
const double speed_range = std::max(1e-6, max_speed - min_speed);
for (size_t i = 0; i < points.size(); ++i)
{
geometry_msgs::msg::Point pt;
pt.x = points[i](0);
pt.y = points[i](1);
pt.z = points[i](2);
sphere.points.push_back(pt);
line_strip.points.push_back(pt);
const double ratio = std::min(1.0, std::max(0.0, (speeds[i] - min_speed) / speed_range));
std_msgs::msg::ColorRGBA color;
color.a = 1.0;
color.r = 1.0;
color.g = 1.0 - ratio;
color.b = 0.0;
sphere.colors.push_back(color);
line_strip.colors.push_back(color);
}
optimal_list_pub->publish(sphere);
optimal_list_pub->publish(line_strip);
}
void PlanningVisualization::displayAStarList(std::vector<std::vector<Eigen::Vector3d>> a_star_paths, int id /* = Eigen::Vector4d(0.5,0.5,0,1)*/)
{
if (a_star_list_pub->get_subscription_count() == 0)
{
return;
}
int i = 0;
vector<Eigen::Vector3d> list;
Eigen::Vector4d color = Eigen::Vector4d(0.5 + ((double)rand() / RAND_MAX / 2), 0.5 + ((double)rand() / RAND_MAX / 2), 0, 1); // make the A star paths different every time.
double scale = 0.05 + (double)rand() / RAND_MAX / 10;
// for ( int i=0; i<10; i++ )
// {
// //Eigen::Vector4d color(1,1,0,0);
// displayMarkerList(a_star_list_pub, list, scale, color, id+i);
// }
for (auto block : a_star_paths)
{
list.clear();
for (auto pt : block)
{
list.push_back(pt);
}
//Eigen::Vector4d color(0.5,0.5,0,1);
displayMarkerList(a_star_list_pub, list, scale, color, id + i); // real ids used: [ id ~ id+a_star_paths.size() ]
i++;
}
}
void PlanningVisualization::displayArrowList(const MarkerArrayPublisher::SharedPtr &pub,
const vector<Eigen::Vector3d> &list,
double scale, Eigen::Vector4d color, int id)
{
visualization_msgs::msg::MarkerArray array;
// clear
pub->publish(array);
generateArrowDisplayArray(array, list, scale, color, id);
pub->publish(array);
}
// PlanningVisualization::
} // namespace scan_planner
@@ -0,0 +1,224 @@
#include <iostream>
#include <traj_utils/polynomial_traj.h>
PolynomialTraj PolynomialTraj::minSnapTraj(const Eigen::MatrixXd &Pos, const Eigen::Vector3d &start_vel,
const Eigen::Vector3d &end_vel, const Eigen::Vector3d &start_acc,
const Eigen::Vector3d &end_acc, const Eigen::VectorXd &Time)
{
int seg_num = Time.size();
Eigen::MatrixXd poly_coeff(seg_num, 3 * 6);
Eigen::VectorXd Px(6 * seg_num), Py(6 * seg_num), Pz(6 * seg_num);
int num_f, num_p; // number of fixed and free variables
int num_d; // number of all segments' derivatives
const static auto Factorial = [](int x) {
int fac = 1;
for (int i = x; i > 0; i--)
fac = fac * i;
return fac;
};
/* ---------- end point derivative ---------- */
Eigen::VectorXd Dx = Eigen::VectorXd::Zero(seg_num * 6);
Eigen::VectorXd Dy = Eigen::VectorXd::Zero(seg_num * 6);
Eigen::VectorXd Dz = Eigen::VectorXd::Zero(seg_num * 6);
for (int k = 0; k < seg_num; k++)
{
/* position to derivative */
Dx(k * 6) = Pos(0, k);
Dx(k * 6 + 1) = Pos(0, k + 1);
Dy(k * 6) = Pos(1, k);
Dy(k * 6 + 1) = Pos(1, k + 1);
Dz(k * 6) = Pos(2, k);
Dz(k * 6 + 1) = Pos(2, k + 1);
if (k == 0)
{
Dx(k * 6 + 2) = start_vel(0);
Dy(k * 6 + 2) = start_vel(1);
Dz(k * 6 + 2) = start_vel(2);
Dx(k * 6 + 4) = start_acc(0);
Dy(k * 6 + 4) = start_acc(1);
Dz(k * 6 + 4) = start_acc(2);
}
else if (k == seg_num - 1)
{
Dx(k * 6 + 3) = end_vel(0);
Dy(k * 6 + 3) = end_vel(1);
Dz(k * 6 + 3) = end_vel(2);
Dx(k * 6 + 5) = end_acc(0);
Dy(k * 6 + 5) = end_acc(1);
Dz(k * 6 + 5) = end_acc(2);
}
}
/* ---------- Mapping Matrix A ---------- */
Eigen::MatrixXd Ab;
Eigen::MatrixXd A = Eigen::MatrixXd::Zero(seg_num * 6, seg_num * 6);
for (int k = 0; k < seg_num; k++)
{
Ab = Eigen::MatrixXd::Zero(6, 6);
for (int i = 0; i < 3; i++)
{
Ab(2 * i, i) = Factorial(i);
for (int j = i; j < 6; j++)
Ab(2 * i + 1, j) = Factorial(j) / Factorial(j - i) * pow(Time(k), j - i);
}
A.block(k * 6, k * 6, 6, 6) = Ab;
}
/* ---------- Produce Selection Matrix C' ---------- */
Eigen::MatrixXd Ct, C;
num_f = 2 * seg_num + 4; // 3 + 3 + (seg_num - 1) * 2 = 2m + 4
num_p = 2 * seg_num - 2; //(seg_num - 1) * 2 = 2m - 2
num_d = 6 * seg_num;
Ct = Eigen::MatrixXd::Zero(num_d, num_f + num_p);
Ct(0, 0) = 1;
Ct(2, 1) = 1;
Ct(4, 2) = 1; // stack the start point
Ct(1, 3) = 1;
Ct(3, 2 * seg_num + 4) = 1;
Ct(5, 2 * seg_num + 5) = 1;
Ct(6 * (seg_num - 1) + 0, 2 * seg_num + 0) = 1;
Ct(6 * (seg_num - 1) + 1, 2 * seg_num + 1) = 1; // Stack the end point
Ct(6 * (seg_num - 1) + 2, 4 * seg_num + 0) = 1;
Ct(6 * (seg_num - 1) + 3, 2 * seg_num + 2) = 1; // Stack the end point
Ct(6 * (seg_num - 1) + 4, 4 * seg_num + 1) = 1;
Ct(6 * (seg_num - 1) + 5, 2 * seg_num + 3) = 1; // Stack the end point
for (int j = 2; j < seg_num; j++)
{
Ct(6 * (j - 1) + 0, 2 + 2 * (j - 1) + 0) = 1;
Ct(6 * (j - 1) + 1, 2 + 2 * (j - 1) + 1) = 1;
Ct(6 * (j - 1) + 2, 2 * seg_num + 4 + 2 * (j - 2) + 0) = 1;
Ct(6 * (j - 1) + 3, 2 * seg_num + 4 + 2 * (j - 1) + 0) = 1;
Ct(6 * (j - 1) + 4, 2 * seg_num + 4 + 2 * (j - 2) + 1) = 1;
Ct(6 * (j - 1) + 5, 2 * seg_num + 4 + 2 * (j - 1) + 1) = 1;
}
C = Ct.transpose();
Eigen::VectorXd Dx1 = C * Dx;
Eigen::VectorXd Dy1 = C * Dy;
Eigen::VectorXd Dz1 = C * Dz;
/* ---------- minimum snap matrix ---------- */
Eigen::MatrixXd Q = Eigen::MatrixXd::Zero(seg_num * 6, seg_num * 6);
for (int k = 0; k < seg_num; k++)
{
for (int i = 3; i < 6; i++)
{
for (int j = 3; j < 6; j++)
{
Q(k * 6 + i, k * 6 + j) =
i * (i - 1) * (i - 2) * j * (j - 1) * (j - 2) / (i + j - 5) * pow(Time(k), (i + j - 5));
}
}
}
/* ---------- R matrix ---------- */
Eigen::MatrixXd R = C * A.transpose().inverse() * Q * A.inverse() * Ct;
Eigen::VectorXd Dxf(2 * seg_num + 4), Dyf(2 * seg_num + 4), Dzf(2 * seg_num + 4);
Dxf = Dx1.segment(0, 2 * seg_num + 4);
Dyf = Dy1.segment(0, 2 * seg_num + 4);
Dzf = Dz1.segment(0, 2 * seg_num + 4);
Eigen::MatrixXd Rff(2 * seg_num + 4, 2 * seg_num + 4);
Eigen::MatrixXd Rfp(2 * seg_num + 4, 2 * seg_num - 2);
Eigen::MatrixXd Rpf(2 * seg_num - 2, 2 * seg_num + 4);
Eigen::MatrixXd Rpp(2 * seg_num - 2, 2 * seg_num - 2);
Rff = R.block(0, 0, 2 * seg_num + 4, 2 * seg_num + 4);
Rfp = R.block(0, 2 * seg_num + 4, 2 * seg_num + 4, 2 * seg_num - 2);
Rpf = R.block(2 * seg_num + 4, 0, 2 * seg_num - 2, 2 * seg_num + 4);
Rpp = R.block(2 * seg_num + 4, 2 * seg_num + 4, 2 * seg_num - 2, 2 * seg_num - 2);
/* ---------- close form solution ---------- */
Eigen::VectorXd Dxp(2 * seg_num - 2), Dyp(2 * seg_num - 2), Dzp(2 * seg_num - 2);
Dxp = -(Rpp.inverse() * Rfp.transpose()) * Dxf;
Dyp = -(Rpp.inverse() * Rfp.transpose()) * Dyf;
Dzp = -(Rpp.inverse() * Rfp.transpose()) * Dzf;
Dx1.segment(2 * seg_num + 4, 2 * seg_num - 2) = Dxp;
Dy1.segment(2 * seg_num + 4, 2 * seg_num - 2) = Dyp;
Dz1.segment(2 * seg_num + 4, 2 * seg_num - 2) = Dzp;
Px = (A.inverse() * Ct) * Dx1;
Py = (A.inverse() * Ct) * Dy1;
Pz = (A.inverse() * Ct) * Dz1;
for (int i = 0; i < seg_num; i++)
{
poly_coeff.block(i, 0, 1, 6) = Px.segment(i * 6, 6).transpose();
poly_coeff.block(i, 6, 1, 6) = Py.segment(i * 6, 6).transpose();
poly_coeff.block(i, 12, 1, 6) = Pz.segment(i * 6, 6).transpose();
}
/* ---------- use polynomials ---------- */
PolynomialTraj poly_traj;
for (int i = 0; i < poly_coeff.rows(); ++i)
{
vector<double> cx(6), cy(6), cz(6);
for (int j = 0; j < 6; ++j)
{
cx[j] = poly_coeff(i, j), cy[j] = poly_coeff(i, j + 6), cz[j] = poly_coeff(i, j + 12);
}
reverse(cx.begin(), cx.end());
reverse(cy.begin(), cy.end());
reverse(cz.begin(), cz.end());
double ts = Time(i);
poly_traj.addSegment(cx, cy, cz, ts);
}
return poly_traj;
}
PolynomialTraj PolynomialTraj::one_segment_traj_gen(const Eigen::Vector3d &start_pt, const Eigen::Vector3d &start_vel, const Eigen::Vector3d &start_acc,
const Eigen::Vector3d &end_pt, const Eigen::Vector3d &end_vel, const Eigen::Vector3d &end_acc,
double t)
{
Eigen::MatrixXd C = Eigen::MatrixXd::Zero(6, 6), Crow(1, 6);
Eigen::VectorXd Bx(6), By(6), Bz(6);
C(0, 5) = 1;
C(1, 4) = 1;
C(2, 3) = 2;
Crow << pow(t, 5), pow(t, 4), pow(t, 3), pow(t, 2), t, 1;
C.row(3) = Crow;
Crow << 5 * pow(t, 4), 4 * pow(t, 3), 3 * pow(t, 2), 2 * t, 1, 0;
C.row(4) = Crow;
Crow << 20 * pow(t, 3), 12 * pow(t, 2), 6 * t, 2, 0, 0;
C.row(5) = Crow;
Bx << start_pt(0), start_vel(0), start_acc(0), end_pt(0), end_vel(0), end_acc(0);
By << start_pt(1), start_vel(1), start_acc(1), end_pt(1), end_vel(1), end_acc(1);
Bz << start_pt(2), start_vel(2), start_acc(2), end_pt(2), end_vel(2), end_acc(2);
Eigen::VectorXd Cofx = C.colPivHouseholderQr().solve(Bx);
Eigen::VectorXd Cofy = C.colPivHouseholderQr().solve(By);
Eigen::VectorXd Cofz = C.colPivHouseholderQr().solve(Bz);
vector<double> cx(6), cy(6), cz(6);
for (int i = 0; i < 6; i++)
{
cx[i] = Cofx(i);
cy[i] = Cofy(i);
cz[i] = Cofz(i);
}
PolynomialTraj poly_traj;
poly_traj.addSegment(cx, cy, cz, t);
return poly_traj;
}