赞
踩
参考文献:B. Zhou, F. Gao, L. Wang, C. Liu and S. Shen, “Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight,” in IEEE Robotics and Automation Letters, vol. 4, no. 4, pp. 3529-3536, Oct. 2019, doi: 10.1109/LRA.2019.2927938.
代码地址:https://github.com/HKUST-Aerial-Robotics/Fast-Planner
之所以选择这篇文章的代码进行详细阅读,是因为这篇文章在无人机运动规划中起到了承前起后的作用。且涉及到的技术模块特别完整。
包括了前端 kinodynamic a_star 路径搜索(在控制空间进行motion primitive)以及基于帕特利亚金最小值原理的两点边界值问题求解(解决控制空间稀疏采样无法抵达目标点的问题-在状态空间进行motion primitive)。
后端首先利用 均匀B样条曲线 对前端得到的路径进行拟合,之后基于距离场地图( SDF )对轨迹平滑性(纯几何性质,不是控制的Norm的时间积分,因为之后还要对时间进行调整),离障碍物距离,速度加速度超限等进行软约束优化。
得到优化的轨迹过后,使用 非均匀B样条曲线 对轨迹进行表达,然后对每一段超过速度,加速度上界的曲线进行时间节点的迭代调整,直到该段曲线的速度加速度符合约束。
主要对应的是Fast planner->path ->path_searching->kinodynamic a_star.cpp。路径搜索的主要函数为kinodynamicAstar类的search函数,我们从这一函数开始阅读并逐一分析其中的重要函数:
search函数的参数包含起始点以及重点的位置,速度,加速度。以及两个标志位init 和dynamic,还有一个搜索起始时间。上图代码中主要是将起始点及目标点的三维位置转化至栅格地图的index. 并计算第一个扩展点的Heuristic cost.
estimateHeuristic()是第一个重要的函数,对应文章中的III.B小节,主要原理是利用庞特里亚金原理解决两点边值问题,得到最优解后用最优解的控制代价作为启发函数。
具体对应代码如下:
首先通过设置启发函数对时间求导等于0,得到启发函数关于时间T的四次方程,再通过求解该四次方程,得到一系列实根,通过比较这些实根所对应的cost大小,得到最优时间。这里需要注意,关于时间的一元四次方程是通过费拉里方法求解的,需要嵌套一个元三次方程进行求解,也就是代码中应的cubic()函数。
一元四次方程的求解过程参见wikipedia中的费拉里方法:
https://zh.wikipedia.org/wiki/%E5%9B%9B%E6%AC%A1%E6%96%B9%E7%A8%8B
一元三次方程的求见过程参见wikipedia中的求根系数法。需要加以说明的是,代码中的判别式大于0及等于0的情况利用了求根公式,判别式小于0的情况则是使用了三角函数解法:
https://zh.wikipedia.org/wiki/%E4%B8%89%E6%AC%A1%E6%96%B9%E7%A8%8B
计算出起始节点的启发项后,就基本进入搜索循环,第一步是判断当前节点是否超出horizon或是离终点较近了,并计算一条直达曲线,检查这条曲线上是否存在。若存在,则搜索完成,返回路径点即可。
这里我们遇到了第二个重要的函数ComputeShotTraj. 即利用庞特里亚金原理解一个两点边值问题。因为最优控制时间已经在estimateHeuristic中计算过了,所以这里只要引入该时间进行多项式计算即可。这部分的目的是为了验证该轨迹是安全的,即不发生碰撞,速度、加速度不超限。
bool KinodynamicAstar::computeShotTraj(Eigen::VectorXd state1, Eigen::VectorXd state2, double time_to_goal) { /* ---------- get coefficient ---------- */ const Vector3d p0 = state1.head(3); const Vector3d dp = state2.head(3) - p0; const Vector3d v0 = state1.segment(3, 3); const Vector3d v1 = state2.segment(3, 3); const Vector3d dv = v1 - v0; double t_d = time_to_goal; MatrixXd coef(3, 4); end_vel_ = v1; Vector3d a = 1.0 / 6.0 * (-12.0 / (t_d * t_d * t_d) * (dp - v0 * t_d) + 6 / (t_d * t_d) * dv); Vector3d b = 0.5 * (6.0 / (t_d * t_d) * (dp - v0 * t_d) - 2 / t_d * dv); Vector3d c = v0; Vector3d d = p0; // 1/6 * alpha * t^3 + 1/2 * beta * t^2 + v0 // a*t^3 + b*t^2 + v0*t + p0 coef.col(3) = a, coef.col(2) = b, coef.col(1) = c, coef.col(0) = d; Vector3d coord, vel, acc; VectorXd poly1d, t, polyv, polya; Vector3i index; Eigen::MatrixXd Tm(4, 4); Tm << 0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0; /* ---------- forward checking of trajectory ---------- */ double t_delta = t_d / 10; for (double time = t_delta; time <= t_d; time += t_delta) { t = VectorXd::Zero(4); for (int j = 0; j < 4; j++) t(j) = pow(time, j); for (int dim = 0; dim < 3; dim++) { poly1d = coef.row(dim); coord(dim) = poly1d.dot(t); vel(dim) = (Tm * poly1d).dot(t); acc(dim) = (Tm * Tm * poly1d).dot(t); if (fabs(vel(dim)) > max_vel_ || fabs(acc(dim)) > max_acc_) { // cout << "vel:" << vel(dim) << ", acc:" << acc(dim) << endl; // return false; } } if (coord(0) < origin_(0) || coord(0) >= map_size_3d_(0) || coord(1) < origin_(1) || coord(1) >= map_size_3d_(1) || coord(2) < origin_(2) || coord(2) >= map_size_3d_(2)) { return false; } // if (edt_environment_->evaluateCoarseEDT(coord, -1.0) <= margin_) { // return false; // } if (edt_environment_->sdf_map_->getInflateOccupancy(coord) == 1) { return false; } } coef_shot_ = coef; t_shot_ = t_d; is_shot_succ_ = true; return true; }
若当前节点没有抵达终点的可能,就要进行节点扩张
open_set_.pop(); cur_node->node_state = IN_CLOSE_SET; iter_num_ += 1; double res = 1 / 2.0, time_res = 1 / 1.0, time_res_init = 1 / 20.0; Eigen::Matrix<double, 6, 1> cur_state = cur_node->state; Eigen::Matrix<double, 6, 1> pro_state; vector<PathNodePtr> tmp_expand_nodes; Eigen::Vector3d um; double pro_t; vector<Eigen::Vector3d> inputs; vector<double> durations; if (init_search) { inputs.push_back(start_acc_); for (double tau = time_res_init * init_max_tau_; tau <= init_max_tau_ + 1e-3; tau += time_res_init * init_max_tau_) durations.push_back(tau); init_search = false; } else { for (double ax = -max_acc_; ax <= max_acc_ + 1e-3; ax += max_acc_ * res) for (double ay = -max_acc_; ay <= max_acc_ + 1e-3; ay += max_acc_ * res) for (double az = -max_acc_; az <= max_acc_ + 1e-3; az += max_acc_ * res) { um << ax, ay, az; inputs.push_back(um); } for (double tau = time_res * max_tau_; tau <= max_tau_; tau += time_res * max_tau_) durations.push_back(tau); } // cout << "cur state:" << cur_state.head(3).transpose() << endl; for (int i = 0; i < inputs.size(); ++i) for (int j = 0; j < durations.size(); ++j) { um = inputs[i]; double tau = durations[j]; stateTransit(cur_state, pro_state, um, tau); pro_t = cur_node->time + tau; Eigen::Vector3d pro_pos = pro_state.head(3); // Check if in close set Eigen::Vector3i pro_id = posToIndex(pro_pos); int pro_t_id = timeToIndex(pro_t); PathNodePtr pro_node = dynamic ? expanded_nodes_.find(pro_id, pro_t_id) : expanded_nodes_.find(pro_id); if (pro_node != NULL && pro_node->node_state == IN_CLOSE_SET) { if (init_search) std::cout << "close" << std::endl; continue; } // Check maximal velocity Eigen::Vector3d pro_v = pro_state.tail(3); if (fabs(pro_v(0)) > max_vel_ || fabs(pro_v(1)) > max_vel_ || fabs(pro_v(2)) > max_vel_) { if (init_search) std::cout << "vel" << std::endl; continue; } // Check not in the same voxel Eigen::Vector3i diff = pro_id - cur_node->index; int diff_time = pro_t_id - cur_node->time_idx; if (diff.norm() == 0 && ((!dynamic) || diff_time == 0)) { if (init_search) std::cout << "same" << std::endl; continue; } // Check safety Eigen::Vector3d pos; Eigen::Matrix<double, 6, 1> xt; bool is_occ = false; for (int k = 1; k <= check_num_; ++k) { double dt = tau * double(k) / double(check_num_); stateTransit(cur_state, xt, um, dt); pos = xt.head(3); if (edt_environment_->sdf_map_->getInflateOccupancy(pos) == 1 ) { is_occ = true; break; } } if (is_occ) { if (init_search) std::cout << "safe" << std::endl; continue; } double time_to_goal, tmp_g_score, tmp_f_score; tmp_g_score = (um.squaredNorm() + w_time_) * tau + cur_node->g_score; tmp_f_score = tmp_g_score + lambda_heu_ * estimateHeuristic(pro_state, end_state, time_to_goal);
以上代码在当前节点的基础上,根据对输入、时间的离散进行扩展得到tmp临时节点,首先判断节点是否已经被扩展过,是否与当前节点在同一个节点,检查速度约束,检查碰撞,都通过的话,就计算当前节点的g_score以及f_score. 其中的state transit函数即通过前向积分得到扩展节点的位置和速度。接下来,就要进行节点剪枝。
// Compare nodes expanded from the same parent bool prune = false; for (int j = 0; j < tmp_expand_nodes.size(); ++j) { PathNodePtr expand_node = tmp_expand_nodes[j]; if ((pro_id - expand_node->index).norm() == 0 && ((!dynamic) || pro_t_id == expand_node->time_idx)) { prune = true; if (tmp_f_score < expand_node->f_score) { expand_node->f_score = tmp_f_score; expand_node->g_score = tmp_g_score; expand_node->state = pro_state; expand_node->input = um; expand_node->duration = tau; if (dynamic) expand_node->time = cur_node->time + tau; } break; } } // This node end up in a voxel different from others if (!prune) { if (pro_node == NULL) { pro_node = path_node_pool_[use_node_num_]; pro_node->index = pro_id; pro_node->state = pro_state; pro_node->f_score = tmp_f_score; pro_node->g_score = tmp_g_score; pro_node->input = um; pro_node->duration = tau; pro_node->parent = cur_node; pro_node->node_state = IN_OPEN_SET; if (dynamic) { pro_node->time = cur_node->time + tau; pro_node->time_idx = timeToIndex(pro_node->time); } open_set_.push(pro_node); if (dynamic) expanded_nodes_.insert(pro_id, pro_node->time, pro_node); else expanded_nodes_.insert(pro_id, pro_node); tmp_expand_nodes.push_back(pro_node); use_node_num_ += 1; if (use_node_num_ == allocate_num_) { cout << "run out of memory." << endl; return NO_PATH; } } else if (pro_node->node_state == IN_OPEN_SET) { if (tmp_g_score < pro_node->g_score) { // pro_node->index = pro_id; pro_node->state = pro_state; pro_node->f_score = tmp_f_score; pro_node->g_score = tmp_g_score; pro_node->input = um; pro_node->duration = tau; pro_node->parent = cur_node; if (dynamic) pro_node->time = cur_node->time + tau; } } else { cout << "error type in searching: " << pro_node->node_state << endl; } }
首先判断当前临时扩展节点与current node的其他临时扩展节点是否在同一个voxel中,如果是的话,就要进行剪枝。要判断当前临时扩展节点的fscore是否比同一个voxel的对比fscore小,如果是的话,则更新这一Voxel节点为当前临时扩展节点。
如果不剪枝的话,则首先判断当前临时扩展节点pro_node是否出现在open集中,如果是不是的话,则可以直接将pro_node加入open集中。如果存在于open集但还未扩展的话,则比较当前临时扩展节点与对应VOXEL节点的fscore,若更小,则更新voxel中的节点。
需要进行说明的是,在Fast planner的实现中,open集是通过两个数据结构实现的,一个队列用来存储,弹出open集中的节点。另一个哈希表NodeHashtable 用来查询节点是否已经存在于open集中。而判断一个节点是否存在于close set中,则是通过Nodehashtable 与nodestate来决定的,如果nodeState 是 InCloseSet, 且存在于NodeHashtable, 则说明该节点已经被扩展过了,存在于close set中。
getKinoTraj这一 函数多作用是在完成路径搜索后按照预设的时间分辨率delta_t通过节点回溯和状态前向积分得到分辨率更高的路径点。如果最后的shot trajectory存在的话,则还要加上最后一段shot trajectory(即通过computeshottraj)算出来得到的。
std::vector<Eigen::Vector3d> KinodynamicAstar::getKinoTraj(double delta_t) { vector<Vector3d> state_list; /* ---------- get traj of searching ---------- */ PathNodePtr node = path_nodes_.back(); Matrix<double, 6, 1> x0, xt; while (node->parent != NULL) { Vector3d ut = node->input; double duration = node->duration; x0 = node->parent->state; for (double t = duration; t >= -1e-5; t -= delta_t) { stateTransit(x0, xt, ut, t); state_list.push_back(xt.head(3)); } node = node->parent; } reverse(state_list.begin(), state_list.end()); /* ---------- get traj of one shot ---------- */ if (is_shot_succ_) { Vector3d coord; VectorXd poly1d, time(4); for (double t = delta_t; t <= t_shot_; t += delta_t) { for (int j = 0; j < 4; j++) time(j) = pow(t, j); for (int dim = 0; dim < 3; dim++) { poly1d = coef_shot_.row(dim); coord(dim) = poly1d.dot(time); } state_list.push_back(coord); } } return state_list; }
getSamples这一函数则是离散的获得一些轨迹点以及起始点的速度与加速度。
void KinodynamicAstar::getSamples(double& ts, vector<Eigen::Vector3d>& point_set, vector<Eigen::Vector3d>& start_end_derivatives) { /* ---------- path duration ---------- */ double T_sum = 0.0; if (is_shot_succ_) T_sum += t_shot_; PathNodePtr node = path_nodes_.back(); while (node->parent != NULL) { T_sum += node->duration; node = node->parent; } // cout << "duration:" << T_sum << endl; // Calculate boundary vel and acc Eigen::Vector3d end_vel, end_acc; double t; if (is_shot_succ_) { t = t_shot_; end_vel = end_vel_; for (int dim = 0; dim < 3; ++dim) { Vector4d coe = coef_shot_.row(dim); end_acc(dim) = 2 * coe(2) + 6 * coe(3) * t_shot_; } } else { t = path_nodes_.back()->duration; end_vel = node->state.tail(3); end_acc = path_nodes_.back()->input; } // Get point samples int seg_num = floor(T_sum / ts); seg_num = max(8, seg_num); ts = T_sum / double(seg_num); bool sample_shot_traj = is_shot_succ_; node = path_nodes_.back(); for (double ti = T_sum; ti > -1e-5; ti -= ts) { if (sample_shot_traj) { // samples on shot traj Vector3d coord; Vector4d poly1d, time; for (int j = 0; j < 4; j++) time(j) = pow(t, j); for (int dim = 0; dim < 3; dim++) { poly1d = coef_shot_.row(dim); coord(dim) = poly1d.dot(time); } point_set.push_back(coord); t -= ts; /* end of segment */ if (t < -1e-5) { sample_shot_traj = false; if (node->parent != NULL) t += node->duration; } } else { // samples on searched traj Eigen::Matrix<double, 6, 1> x0 = node->parent->state; Eigen::Matrix<double, 6, 1> xt; Vector3d ut = node->input; stateTransit(x0, xt, ut, t); point_set.push_back(xt.head(3)); t -= ts; // cout << "t: " << t << ", t acc: " << T_accumulate << endl; if (t < -1e-5 && node->parent->parent != NULL) { node = node->parent; t += node->duration; } } } reverse(point_set.begin(), point_set.end()); // calculate start acc Eigen::Vector3d start_acc; if (path_nodes_.back()->parent == NULL) { // no searched traj, calculate by shot traj start_acc = 2 * coef_shot_.col(2); } else { // input of searched traj start_acc = node->input; } start_end_derivatives.push_back(start_vel_); start_end_derivatives.push_back(end_vel); start_end_derivatives.push_back(start_acc); start_end_derivatives.push_back(end_acc); }
至此,Kinodynamic_astar的所有重要函数都已经分析完毕,根据search函数流程即可获得一条包含一定动力学约束的collision free的路径。
首先重温一下B样条曲线的几个基本性质
得到初始路径后,需要在前端初始路径的基础上进行B样条优化。B样条的第一部分是利用均匀B样条进行轨迹平顺性、安全性、速度和加速度优化。在non_uniform_bspline.cpp中,均匀B样条的设置函数为setUniformBspline()。 这一函数在获得控制点,轨迹次数,以及时间间隔的情况下,设置时间区间(Knot vector), 需要注意的是,在Fast-planner的实现中, t p = 0 t_p=0 tp=0, 以文章中的3次样条函数为例, t 0 = − 3 Δ t t_0=-3\Delta t t0=−3Δt, t 1 = − 2 Δ t t_1=-2\Delta t t1=−2Δt, t 2 = − Δ t t_2=-\Delta t t2=−Δt。
void NonUniformBspline::setUniformBspline(const Eigen::MatrixXd& points, const int& order, const double& interval) { control_points_ = points; p_ = order; interval_ = interval; n_ = points.rows() - 1; m_ = n_ + p_ + 1; u_ = Eigen::VectorXd::Zero(m_ + 1); for (int i = 0; i <= m_; ++i) { if (i <= p_) { u_(i) = double(-p_ + i) * interval_; } else if (i > p_ && i <= m_ - p_) { u_(i) = u_(i - 1) + interval_; } else if (i > m_ - p_) { u_(i) = u_(i - 1) + interval_; } } }
给定一个时间
μ
\mu
μ, 如何计算该点的坐标值?通常的做法是根据Cox-DeBoor公式把整个B样条函数计算出来。但在evaluateDeBoor()这一函数中,作者采用的是递归的DeBoor算法,如下图所示,具体参见wikipedia,https://en.wikipedia.org/wiki/De_Boor%27s_algorithm
其中
k
k
k是
x
x
x落在的时间区间,即
x
∈
[
t
k
,
t
k
+
1
]
x \in[t_k,t_{k+1}]
x∈[tk,tk+1]。
假设原有与此段轨迹相关的P+1个控制点为0阶‘控制点’,通过两个循环计算出第K个P阶的‘控制点’,该点即为要求的B样条函数值。内循环通过递归公式求得高一阶的P+1个控制点,外循环提高阶数。
Eigen::VectorXd NonUniformBspline::evaluateDeBoor(const double& u) { double ub = min(max(u_(p_), u), u_(m_ - p_)); // determine which [ui,ui+1] lay in int k = p_; while (true) { if (u_(k + 1) >= ub) break; ++k; } /* deBoor's alg */ vector<Eigen::VectorXd> d; for (int i = 0; i <= p_; ++i) { d.push_back(control_points_.row(k - p_ + i)); // cout << d[i].transpose() << endl; } for (int r = 1; r <= p_; ++r) { for (int i = p_; i >= r; --i) { double alpha = (ub - u_[i + k - p_]) / (u_[i + 1 + k - r] - u_[i + k - p_]); // cout << "alpha: " << alpha << endl; d[i] = (1 - alpha) * d[i - 1] + alpha * d[i]; } } return d[p_]; }
同样的,evaluateDeBoorT()函数只是直接得到一个 [ t p , t m − p ] [t_p,t_{m-p}] [tp,tm−p]作用域中的B样条函数值。
通过上面两小节,我们已经能够在已知控制点的情况下计算任一时间对应的轨迹值,那么,控制点要如何获得呢。在Fast-Planner的实现中,初始控制点时通过对前端hybrid A*寻找到的初始路径进行拟合得到的。
void NonUniformBspline::parameterizeToBspline(const double& ts, const vector<Eigen::Vector3d>& point_set, const vector<Eigen::Vector3d>& start_end_derivative, Eigen::MatrixXd& ctrl_pts) { if (ts <= 0) { cout << "[B-spline]:time step error." << endl; return; } if (point_set.size() < 2) { cout << "[B-spline]:point set have only " << point_set.size() << " points." << endl; return; } if (start_end_derivative.size() != 4) { cout << "[B-spline]:derivatives error." << endl; } int K = point_set.size(); // write A Eigen::Vector3d prow(3), vrow(3), arow(3); prow << 1, 4, 1; vrow << -1, 0, 1; arow << 1, -2, 1; Eigen::MatrixXd A = Eigen::MatrixXd::Zero(K + 4, K + 2); for (int i = 0; i < K; ++i) A.block(i, i, 1, 3) = (1 / 6.0) * prow.transpose(); A.block(K, 0, 1, 3) = (1 / 2.0 / ts) * vrow.transpose(); A.block(K + 1, K - 1, 1, 3) = (1 / 2.0 / ts) * vrow.transpose(); A.block(K + 2, 0, 1, 3) = (1 / ts / ts) * arow.transpose(); A.block(K + 3, K - 1, 1, 3) = (1 / ts / ts) * arow.transpose(); // cout << "A:\n" << A << endl; // A.block(0, 0, K, K + 2) = (1 / 6.0) * A.block(0, 0, K, K + 2); // A.block(K, 0, 2, K + 2) = (1 / 2.0 / ts) * A.block(K, 0, 2, K + 2); // A.row(K + 4) = (1 / ts / ts) * A.row(K + 4); // A.row(K + 5) = (1 / ts / ts) * A.row(K + 5); // write b Eigen::VectorXd bx(K + 4), by(K + 4), bz(K + 4); for (int i = 0; i < K; ++i) { bx(i) = point_set[i](0); by(i) = point_set[i](1); bz(i) = point_set[i](2); } for (int i = 0; i < 4; ++i) { bx(K + i) = start_end_derivative[i](0); by(K + i) = start_end_derivative[i](1); bz(K + i) = start_end_derivative[i](2); } // solve Ax = b Eigen::VectorXd px = A.colPivHouseholderQr().solve(bx); Eigen::VectorXd py = A.colPivHouseholderQr().solve(by); Eigen::VectorXd pz = A.colPivHouseholderQr().solve(bz); // convert to control pts ctrl_pts.resize(K + 2, 3); ctrl_pts.col(0) = px; ctrl_pts.col(1) = py; ctrl_pts.col(2) = pz; // cout << "[B-spline]: parameterization ok." << endl; }
虽然在计算B样条曲线上某一点的值时论文用的是DeBoor公式,但是在使用均匀B样条对前端路径进行拟合时用的是B样条的矩阵表达方法,具体参见论文:K. Qin, “General matrix representations for b-splines,” The Visual Computer, vol. 16, no. 3, pp. 177–186, 2000.
首先假设获得的离散轨迹点一共有K个,则有K-1段轨迹,根据B样条性质,这K-1段3次B样条曲线的定义域是 [ u 3 , u 3 + k − 1 ] [u_3,u_{3+k-1}] [u3,u3+k−1]。则一共有K+5个knot vector,即 M = K − 1 + 3 + 3 M=K-1+3+3 M=K−1+3+3 ,所以应该有M-3即K+2个控制点。
如以下公式所示,对于B样条曲线上定义在
t
m
,
t
m
+
1
t_m,t_{m+1}
tm,tm+1上的一小段曲线,其被
[
p
m
−
p
b
,
p
m
]
[p_{m-p_b},p_m]
[pm−pb,pm]这四个控制点所决定。其中
s
(
t
)
=
t
−
t
m
Δ
t
s(t)=\frac{t-t_m}{\Delta t}
s(t)=Δtt−tm。
M
p
b
M_{p_b}
Mpb是四维常数矩阵,具体参见以上论文链接。
p
(
s
(
t
)
)
=
s
(
t
)
⊤
M
p
b
+
1
q
m
s
(
t
)
=
[
1
s
(
t
)
s
2
(
t
)
⋯
s
p
b
(
t
)
]
⊤
q
m
=
[
Q
m
−
p
b
Q
m
−
p
b
∣
+
1
Q
m
−
p
b
+
2
⋯
Q
m
]
⊤
通过矩阵运算,我们可以得到第一个路径点对应的位置约束:
x
1
=
1
6
(
Q
2
−
Q
0
)
x_1 =\frac{1}{6}(Q_2-Q_0)
x1=61(Q2−Q0)
同理可得其余K-1个路径点的位置约束。
对于速度约束与加速度约束,只需要对时间 t t t求一次及二次微分即可,所得约束关系如代码中所示。需要注意的是,由于s(t)是关于t的函数,具有常数项 1 Δ t \frac{1}{\Delta t} Δt1, 所以一次及二次微分需要乘以对应的常数项 1 Δ t \frac{1}{\Delta t} Δt1 1 Δ t 2 \frac{1}{\Delta t^2} Δt21。
通过对K+2个控制点构建K+4个等式约束(K个位置约束,两个速度约束,两个加速度约束),利用 A x = b Ax=b Ax=b进行线性拟合,即可得到拟合的控制点。
当我们得到一条时间节点等同的均匀B样条曲线后,我们希望能够对这条曲线上的速度及加速度进行动力学检查,以备之后进行时间节点调整。于是我们需要计算出非均匀形式下的速度控制点及加速度控制点。(直接用均匀B样条的控制点算是因为均匀B样条可以看做特殊形式的非均匀B样条)
Eigen::MatrixXd NonUniformBspline::getDerivativeControlPoints() { // The derivative of a b-spline is also a b-spline, its order become p_-1 // control point Qi = p_*(Pi+1-Pi)/(ui+p_+1-ui+1) Eigen::MatrixXd ctp = Eigen::MatrixXd::Zero(control_points_.rows() - 1, control_points_.cols()); for (int i = 0; i < ctp.rows(); ++i) { ctp.row(i) = p_ * (control_points_.row(i + 1) - control_points_.row(i)) / (u_(i + p_ + 1) - u_(i + 1)); } return ctp; } NonUniformBspline NonUniformBspline::getDerivative() { Eigen::MatrixXd ctp = getDerivativeControlPoints(); NonUniformBspline derivative(ctp, p_ - 1, interval_); /* cut the first and last knot */ Eigen::VectorXd knot(u_.rows() - 2); knot = u_.segment(1, u_.rows() - 2); derivative.setKnot(knot); return derivative; }
代码中利用递归的形式求得速度与加速度,B样条的一阶微分是次数-1,控制点数-1的B样条曲线,因此相应的Knot vector-2。 在利用上图公式获得一阶微分控制点后,新定义一个NonUniformBspline对象,并将新的控制点,次数,Knot vector赋值给它。
bool NonUniformBspline::checkFeasibility(bool show) { bool fea = true; // SETY << "[Bspline]: total points size: " << control_points_.rows() << endl; Eigen::MatrixXd P = control_points_; int dimension = control_points_.cols(); /* check vel feasibility and insert points */ double max_vel = -1.0; for (int i = 0; i < P.rows() - 1; ++i) { Eigen::VectorXd vel = p_ * (P.row(i + 1) - P.row(i)) / (u_(i + p_ + 1) - u_(i + 1)); if (fabs(vel(0)) > limit_vel_ + 1e-4 || fabs(vel(1)) > limit_vel_ + 1e-4 || fabs(vel(2)) > limit_vel_ + 1e-4) { if (show) cout << "[Check]: Infeasible vel " << i << " :" << vel.transpose() << endl; fea = false; for (int j = 0; j < dimension; ++j) { max_vel = max(max_vel, fabs(vel(j))); } } } /* acc feasibility */ double max_acc = -1.0; for (int i = 0; i < P.rows() - 2; ++i) { Eigen::VectorXd acc = p_ * (p_ - 1) * ((P.row(i + 2) - P.row(i + 1)) / (u_(i + p_ + 2) - u_(i + 2)) - (P.row(i + 1) - P.row(i)) / (u_(i + p_ + 1) - u_(i + 1))) / (u_(i + p_ + 1) - u_(i + 2)); if (fabs(acc(0)) > limit_acc_ + 1e-4 || fabs(acc(1)) > limit_acc_ + 1e-4 || fabs(acc(2)) > limit_acc_ + 1e-4) { if (show) cout << "[Check]: Infeasible acc " << i << " :" << acc.transpose() << endl; fea = false; for (int j = 0; j < dimension; ++j) { max_acc = max(max_acc, fabs(acc(j))); } } } double ratio = max(max_vel / limit_vel_, sqrt(fabs(max_acc) / limit_acc_)); return fea; } double NonUniformBspline::checkRatio() { Eigen::MatrixXd P = control_points_; int dimension = control_points_.cols(); // find max vel double max_vel = -1.0; for (int i = 0; i < P.rows() - 1; ++i) { Eigen::VectorXd vel = p_ * (P.row(i + 1) - P.row(i)) / (u_(i + p_ + 1) - u_(i + 1)); for (int j = 0; j < dimension; ++j) { max_vel = max(max_vel, fabs(vel(j))); } } // find max acc double max_acc = -1.0; for (int i = 0; i < P.rows() - 2; ++i) { Eigen::VectorXd acc = p_ * (p_ - 1) * ((P.row(i + 2) - P.row(i + 1)) / (u_(i + p_ + 2) - u_(i + 2)) - (P.row(i + 1) - P.row(i)) / (u_(i + p_ + 1) - u_(i + 1))) / (u_(i + p_ + 1) - u_(i + 2)); for (int j = 0; j < dimension; ++j) { max_acc = max(max_acc, fabs(acc(j))); } } double ratio = max(max_vel / limit_vel_, sqrt(fabs(max_acc) / limit_acc_)); ROS_ERROR_COND(ratio > 2.0, "max vel: %lf, max acc: %lf.", max_vel, max_acc); return ratio; } bool NonUniformBspline::reallocateTime(bool show) { // SETY << "[Bspline]: total points size: " << control_points_.rows() << endl; // cout << "origin knots:\n" << u_.transpose() << endl; bool fea = true; Eigen::MatrixXd P = control_points_; int dimension = control_points_.cols(); double max_vel, max_acc; /* check vel feasibility and insert points */ for (int i = 0; i < P.rows() - 1; ++i) { Eigen::VectorXd vel = p_ * (P.row(i + 1) - P.row(i)) / (u_(i + p_ + 1) - u_(i + 1)); if (fabs(vel(0)) > limit_vel_ + 1e-4 || fabs(vel(1)) > limit_vel_ + 1e-4 || fabs(vel(2)) > limit_vel_ + 1e-4) { fea = false; if (show) cout << "[Realloc]: Infeasible vel " << i << " :" << vel.transpose() << endl; max_vel = -1.0; for (int j = 0; j < dimension; ++j) { max_vel = max(max_vel, fabs(vel(j))); } double ratio = max_vel / limit_vel_ + 1e-4; if (ratio > limit_ratio_) ratio = limit_ratio_; double time_ori = u_(i + p_ + 1) - u_(i + 1); double time_new = ratio * time_ori; double delta_t = time_new - time_ori; double t_inc = delta_t / double(p_); for (int j = i + 2; j <= i + p_ + 1; ++j) { u_(j) += double(j - i - 1) * t_inc; if (j <= 5 && j >= 1) { // cout << "vel j: " << j << endl; } } for (int j = i + p_ + 2; j < u_.rows(); ++j) { u_(j) += delta_t; } } } /* acc feasibility */ for (int i = 0; i < P.rows() - 2; ++i) { Eigen::VectorXd acc = p_ * (p_ - 1) * ((P.row(i + 2) - P.row(i + 1)) / (u_(i + p_ + 2) - u_(i + 2)) - (P.row(i + 1) - P.row(i)) / (u_(i + p_ + 1) - u_(i + 1))) / (u_(i + p_ + 1) - u_(i + 2)); if (fabs(acc(0)) > limit_acc_ + 1e-4 || fabs(acc(1)) > limit_acc_ + 1e-4 || fabs(acc(2)) > limit_acc_ + 1e-4) { fea = false; if (show) cout << "[Realloc]: Infeasible acc " << i << " :" << acc.transpose() << endl; max_acc = -1.0; for (int j = 0; j < dimension; ++j) { max_acc = max(max_acc, fabs(acc(j))); } double ratio = sqrt(max_acc / limit_acc_) + 1e-4; if (ratio > limit_ratio_) ratio = limit_ratio_; // cout << "ratio: " << ratio << endl; double time_ori = u_(i + p_ + 1) - u_(i + 2); double time_new = ratio * time_ori; double delta_t = time_new - time_ori; double t_inc = delta_t / double(p_ - 1); if (i == 1 || i == 2) { // cout << "acc i: " << i << endl; for (int j = 2; j <= 5; ++j) { u_(j) += double(j - 1) * t_inc; } for (int j = 6; j < u_.rows(); ++j) { u_(j) += 4.0 * t_inc; } } else { for (int j = i + 3; j <= i + p_ + 1; ++j) { u_(j) += double(j - i - 2) * t_inc; if (j <= 5 && j >= 1) { // cout << "acc j: " << j << endl; } } for (int j = i + p_ + 2; j < u_.rows(); ++j) { u_(j) += delta_t; } } } } return fea; } void NonUniformBspline::lengthenTime(const double& ratio) { int num1 = 5; int num2 = getKnot().rows() - 1 - 5; double delta_t = (ratio - 1.0) * (u_(num2) - u_(num1)); double t_inc = delta_t / double(num2 - num1); for (int i = num1 + 1; i <= num2; ++i) u_(i) += double(i - num1) * t_inc; for (int i = num2 + 1; i < u_.rows(); ++i) u_(i) += delta_t; }
这一部分的三个函数 checkFeasibility(), checkRatio(), reallocateTime()的大部分内容都是一致的。都是利用如下两个公式计算每个控制点的速度和加速度是否超限,最大速度是多少,并获得调整比例。
真正进行重新时间调整的函数时reallocateTime,通过计算当前控制点是否超限,以及调整表比例。对于当前控制点
i
i
i有关的时间区间进行时间调整,
[
t
i
,
t
i
+
p
b
+
1
]
[t_i,t_{i+p_b +1}]
[ti,ti+pb+1]。注意,这里的
p
b
p_b
pb是当前B样条的次数,如果是速度则是3-1=2, 加速度则是3-2=1(针对论文中的3次B样条曲线而言)。在
t
i
+
p
b
+
1
t_{i+p_b +1}
ti+pb+1以后的是时间节点则是直接加上总的扩张时间就可以。
需要注意的是,代码中有一处地方暂时无法理解为何需要单独考虑,即加速度超限时间调整的i=1 及 i=2的部分
if (i == 1 || i == 2) {
// cout << "acc i: " << i << endl;
for (int j = 2; j <= 5; ++j) {
u_(j) += double(j - 1) * t_inc;
}
for (int j = 6; j < u_.rows(); ++j) {
u_(j) += 4.0 * t_inc;
}
到此为止,我们已经通过前端动力学搜索找到一条初步的路径,并利用均匀B样条对这条路径进行拟合得到初步控制点。我们还知道了如何用非均匀B样条的特性对均匀B样条曲线进行速度,加速度检查。并进行迭代时间调整。目前我们还缺少的,是如何通过均匀B样条进行轨迹平滑性,安全性、速度与加速度优化。
利用均匀B样条进行优化,首先要看三部分的优化项及相应梯度是如何表达的。三项优化项分别是jerk平顺项,障碍物距离项,以及速度、加速度可达项。
其中,平顺项,速度、加速度项都是利用均匀B样条的微分公式,求出速度控制点,加速度控制点,jerk控制点,从而构造与相应控制点相关的优化项。梯度则是利用链式法则,用cost对影响这一cost的控制点求导,然后在循环中对cost及各控制点梯度进行累加(一个控制点会影响多个控制点的优化项)。
这里需要注意的是,速度与加速度的优化项及梯度都考虑了时间项 Δ t \Delta t Δt,但jerk项省略了时间项-或许对应文中说的后续纯几何构造法,便于后续进行时间调整。但是,平顺项的实现方式与原论文中的elastic band的方法也是不一样的。
至于障碍物项。控制点距离最近障碍物的距离及这一距离关于控制点的梯度是通过SDF地图实现的,关于SDF地图留待以后讨论。
void BsplineOptimizer::calcSmoothnessCost(const vector<Eigen::Vector3d>& q, double& cost, vector<Eigen::Vector3d>& gradient) { cost = 0.0; Eigen::Vector3d zero(0, 0, 0); std::fill(gradient.begin(), gradient.end(), zero); Eigen::Vector3d jerk, temp_j; for (int i = 0; i < q.size() - order_; i++) { /* evaluate jerk */ jerk = q[i + 3] - 3 * q[i + 2] + 3 * q[i + 1] - q[i]; cost += jerk.squaredNorm(); temp_j = 2.0 * jerk; /* jerk gradient */ gradient[i + 0] += -temp_j; gradient[i + 1] += 3.0 * temp_j; gradient[i + 2] += -3.0 * temp_j; gradient[i + 3] += temp_j; } } void BsplineOptimizer::calcDistanceCost(const vector<Eigen::Vector3d>& q, double& cost, vector<Eigen::Vector3d>& gradient) { cost = 0.0; Eigen::Vector3d zero(0, 0, 0); std::fill(gradient.begin(), gradient.end(), zero); double dist; Eigen::Vector3d dist_grad, g_zero(0, 0, 0); int end_idx = (cost_function_ & ENDPOINT) ? q.size() : q.size() - order_; for (int i = order_; i < end_idx; i++) { edt_environment_->evaluateEDTWithGrad(q[i], -1.0, dist, dist_grad); if (dist_grad.norm() > 1e-4) dist_grad.normalize(); if (dist < dist0_) { cost += pow(dist - dist0_, 2); gradient[i] += 2.0 * (dist - dist0_) * dist_grad; } } } void BsplineOptimizer::calcFeasibilityCost(const vector<Eigen::Vector3d>& q, double& cost, vector<Eigen::Vector3d>& gradient) { cost = 0.0; Eigen::Vector3d zero(0, 0, 0); std::fill(gradient.begin(), gradient.end(), zero); /* abbreviation */ double ts, vm2, am2, ts_inv2, ts_inv4; vm2 = max_vel_ * max_vel_; am2 = max_acc_ * max_acc_; ts = bspline_interval_; ts_inv2 = 1 / ts / ts; ts_inv4 = ts_inv2 * ts_inv2; /* velocity feasibility */ for (int i = 0; i < q.size() - 1; i++) { Eigen::Vector3d vi = q[i + 1] - q[i]; for (int j = 0; j < 3; j++) { double vd = vi(j) * vi(j) * ts_inv2 - vm2; if (vd > 0.0) { cost += pow(vd, 2); double temp_v = 4.0 * vd * ts_inv2; gradient[i + 0](j) += -temp_v * vi(j); gradient[i + 1](j) += temp_v * vi(j); } } } /* acceleration feasibility */ for (int i = 0; i < q.size() - 2; i++) { Eigen::Vector3d ai = q[i + 2] - 2 * q[i + 1] + q[i]; for (int j = 0; j < 3; j++) { double ad = ai(j) * ai(j) * ts_inv4 - am2; if (ad > 0.0) { cost += pow(ad, 2); double temp_a = 4.0 * ad * ts_inv4; gradient[i + 0](j) += temp_a * ai(j); gradient[i + 1](j) += -2 * temp_a * ai(j); gradient[i + 2](j) += temp_a * ai(j); } } } }
因为在KinoReplan中只用到Smoothness, distance,以及feasibility这三项优化项,因此,其余的优化项计算我们暂且不谈。现在考虑如何将这几项优化项结合起来,并将其自变量从控制点转化为Nlopt优化的变量x。
void BsplineOptimizer::combineCost(const std::vector<double>& x, std::vector<double>& grad, double& f_combine) { /* convert the NLopt format vector to control points. */ // This solver can support 1D-3D B-spline optimization, but we use Vector3d to store each control point // For 1D case, the second and third elements are zero, and similar for the 2D case. for (int i = 0; i < order_; i++) { for (int j = 0; j < dim_; ++j) { g_q_[i][j] = control_points_(i, j); } for (int j = dim_; j < 3; ++j) { g_q_[i][j] = 0.0; } } for (int i = 0; i < variable_num_ / dim_; i++) { for (int j = 0; j < dim_; ++j) { g_q_[i + order_][j] = x[dim_ * i + j]; } for (int j = dim_; j < 3; ++j) { g_q_[i + order_][j] = 0.0; } } if (!(cost_function_ & ENDPOINT)) { for (int i = 0; i < order_; i++) { for (int j = 0; j < dim_; ++j) { g_q_[order_ + variable_num_ / dim_ + i][j] = control_points_(control_points_.rows() - order_ + i, j); } for (int j = dim_; j < 3; ++j) { g_q_[order_ + variable_num_ / dim_ + i][j] = 0.0; } } } f_combine = 0.0; grad.resize(variable_num_); fill(grad.begin(), grad.end(), 0.0); /* evaluate costs and their gradient */ double f_smoothness, f_distance, f_feasibility, f_endpoint, f_guide, f_waypoints; f_smoothness = f_distance = f_feasibility = f_endpoint = f_guide = f_waypoints = 0.0; if (cost_function_ & SMOOTHNESS) { calcSmoothnessCost(g_q_, f_smoothness, g_smoothness_); f_combine += lambda1_ * f_smoothness; for (int i = 0; i < variable_num_ / dim_; i++) for (int j = 0; j < dim_; j++) grad[dim_ * i + j] += lambda1_ * g_smoothness_[i + order_](j); } if (cost_function_ & DISTANCE) { calcDistanceCost(g_q_, f_distance, g_distance_); f_combine += lambda2_ * f_distance; for (int i = 0; i < variable_num_ / dim_; i++) for (int j = 0; j < dim_; j++) grad[dim_ * i + j] += lambda2_ * g_distance_[i + order_](j); } if (cost_function_ & FEASIBILITY) { calcFeasibilityCost(g_q_, f_feasibility, g_feasibility_); f_combine += lambda3_ * f_feasibility; for (int i = 0; i < variable_num_ / dim_; i++) for (int j = 0; j < dim_; j++) grad[dim_ * i + j] += lambda3_ * g_feasibility_[i + order_](j); } if (cost_function_ & ENDPOINT) { calcEndpointCost(g_q_, f_endpoint, g_endpoint_); f_combine += lambda4_ * f_endpoint; for (int i = 0; i < variable_num_ / dim_; i++) for (int j = 0; j < dim_; j++) grad[dim_ * i + j] += lambda4_ * g_endpoint_[i + order_](j); } if (cost_function_ & GUIDE) { calcGuideCost(g_q_, f_guide, g_guide_); f_combine += lambda5_ * f_guide; for (int i = 0; i < variable_num_ / dim_; i++) for (int j = 0; j < dim_; j++) grad[dim_ * i + j] += lambda5_ * g_guide_[i + order_](j); } if (cost_function_ & WAYPOINTS) { calcWaypointsCost(g_q_, f_waypoints, g_waypoints_); f_combine += lambda7_ * f_waypoints; for (int i = 0; i < variable_num_ / dim_; i++) for (int j = 0; j < dim_; j++) grad[dim_ * i + j] += lambda7_ * g_waypoints_[i + order_](j); } /* print cost */ // if ((cost_function_ & WAYPOINTS) && iter_num_ % 10 == 0) { // cout << iter_num_ << ", total: " << f_combine << ", acc: " << lambda8_ * f_view // << ", waypt: " << lambda7_ * f_waypoints << endl; // } // if (optimization_phase_ == SECOND_PHASE) { // << ", smooth: " << lambda1_ * f_smoothness // << " , dist:" << lambda2_ * f_distance // << ", fea: " << lambda3_ * f_feasibility << endl; // << ", end: " << lambda4_ * f_endpoint // << ", guide: " << lambda5_ * f_guide // } } double BsplineOptimizer::costFunction(const std::vector<double>& x, std::vector<double>& grad, void* func_data) { BsplineOptimizer* opt = reinterpret_cast<BsplineOptimizer*>(func_data); double cost; opt->combineCost(x, grad, cost); opt->iter_num_++; /* save the min cost result */ if (cost < opt->min_cost_) { opt->min_cost_ = cost; opt->best_variable_ = x; } return cost; // /* evaluation */ // ros::Time te1 = ros::Time::now(); // double time_now = (te1 - opt->time_start_).toSec(); // opt->vec_time_.push_back(time_now); // if (opt->vec_cost_.size() == 0) // { // opt->vec_cost_.push_back(f_combine); // } // else if (opt->vec_cost_.back() > f_combine) // { // opt->vec_cost_.push_back(f_combine); // } // else // { // opt->vec_cost_.push_back(opt->vec_cost_.back()); // } }
结合三种优化项的函数是combineCost()函数,函数有三个参数,第一个std::vector& x即是Nlopt优化的变量,应该与三个维度的控制点对应。第二个参数是std::vector& grad,即总的优化项关于每个优化变量的梯度信息。第三个参数是double& f_combine 。即结合后的Cost。
首先是给g_q赋值,g_q是用来计算每次优化循环三个优化项的控制点。值得注意的是,前 p b p_b pb个控制点和最后 p b p_b pb个控制点是不进行优化的,始终保持线性拟合得到控制点原值。中间的控制点则是因为每一次迭代优化后都不同,因此用x来赋值。这里的x是通过Nlopt的opt 对象在set_min_objective中进行初始化的,具体的大小在构造 Nlopt optimizer对象时就通过variable_num的大小确定了。而初始值则是在Nlopt求解时 .optimize函数中进行赋值。
接下来就是利用CalcSmoothneesCost, CalcDistanceCost, CalcFeasibilityCost 三个函数计算每一部分的cost和grad,并乘上权重后累加至grad 和 f_combine中,唯一需要注意的是grad和各部分优化项梯度之间维度上差了两个B样条次数。
最后,构建符合Nlopt要求的目标函数,即costFunction函数,该函数通过combinecost返回总的cost,并在参数中提供梯度信息。
至此,我们已经构造完成了优化变量、目标函数、梯度信息,接下来,我们就可以通过Nlopt对控制点进行非线性优化。
Eigen::MatrixXd BsplineOptimizer::BsplineOptimizeTraj(const Eigen::MatrixXd& points, const double& ts, const int& cost_function, int max_num_id, int max_time_id) { setControlPoints(points); setBsplineInterval(ts); setCostFunction(cost_function); setTerminateCond(max_num_id, max_time_id); optimize(); return this->control_points_; } void BsplineOptimizer::optimize() { /* initialize solver */ iter_num_ = 0; min_cost_ = std::numeric_limits<double>::max(); const int pt_num = control_points_.rows(); g_q_.resize(pt_num); g_smoothness_.resize(pt_num); g_distance_.resize(pt_num); g_feasibility_.resize(pt_num); g_endpoint_.resize(pt_num); g_waypoints_.resize(pt_num); g_guide_.resize(pt_num); if (cost_function_ & ENDPOINT) { variable_num_ = dim_ * (pt_num - order_); // end position used for hard constraint end_pt_ = (1 / 6.0) * (control_points_.row(pt_num - 3) + 4 * control_points_.row(pt_num - 2) + control_points_.row(pt_num - 1)); } else { variable_num_ = max(0, dim_ * (pt_num - 2 * order_)) ; } /* do optimization using NLopt slover */ nlopt::opt opt(nlopt::algorithm(isQuadratic() ? algorithm1_ : algorithm2_), variable_num_); opt.set_min_objective(BsplineOptimizer::costFunction, this); opt.set_maxeval(max_iteration_num_[max_num_id_]); opt.set_maxtime(max_iteration_time_[max_time_id_]); opt.set_xtol_rel(1e-5); vector<double> q(variable_num_); for (int i = order_; i < pt_num; ++i) { if (!(cost_function_ & ENDPOINT) && i >= pt_num - order_) continue; for (int j = 0; j < dim_; j++) { q[dim_ * (i - order_) + j] = control_points_(i, j); } } if (dim_ != 1) { vector<double> lb(variable_num_), ub(variable_num_); const double bound = 10.0; for (int i = 0; i < variable_num_; ++i) { lb[i] = q[i] - bound; ub[i] = q[i] + bound; } opt.set_lower_bounds(lb); opt.set_upper_bounds(ub); } try { // cout << fixed << setprecision(7); // vec_time_.clear(); // vec_cost_.clear(); // time_start_ = ros::Time::now(); double final_cost; nlopt::result result = opt.optimize(q, final_cost); /* retrieve the optimization result */ // cout << "Min cost:" << min_cost_ << endl; } catch (std::exception& e) { ROS_WARN("[Optimization]: nlopt exception"); cout << e.what() << endl; } for (int i = order_; i < control_points_.rows(); ++i) { if (!(cost_function_ & ENDPOINT) && i >= pt_num - order_) continue; for (int j = 0; j < dim_; j++) { control_points_(i, j) = best_variable_[dim_ * (i - order_) + j]; } } if (!(cost_function_ & GUIDE)) ROS_INFO_STREAM("iter num: " << iter_num_); }
首先是BsplineOptimizeTraj()函数,将优化的控制点,均匀B样条的时间间隔,cost Function包含的优化项,以及终止条件(最大优化次数及最长优化时间)都设置好以后,就利用BsplineOptimizer的optimize()函数进行优化。
BsplineOptimizer的optimize()函数首先根据pt_num确定各部分优化项梯度vector的大小,然后再根据是否有终值点约束确定优化变量的个数。
接下来就是实例化Nlopt::opt类对象 opt。并设定目标函数。设定最大优化次数与最长优化时间,设定目标函数的最小值(这三者都是设定终止条件)。接着,根据线性你和得到的控制点设置优化变量的初值,并设置每个优化变量的上下界(初始值±10)。
最后利用Nlopt::opt类对行opt的optimize函数进行迭代优化求解,在求解结束后,通过costFunction中保留的best_Variable对control_point进行赋值。
至此,我们已经知道如何通过前端寻找到一条路径,如何通过均匀B样条对轨迹进行拟合得到控制点,如何通过Nlopt对控制点进行优化,并利用非均匀B样条对优化后得到的轨迹进行时间调整。整个文章的主要算法皆已分析完毕。接下来,我们将从规划开始到规划结束的逻辑梳理一遍整个规划流程。
首先来看 plan_manage 包中的plan_manager.h 和planner_manager.cpp。 这两个文件主要是定义了一个FastPlannerManager类,用以对整个规划过程进行管理。
#ifndef _PLANNER_MANAGER_H_ #define _PLANNER_MANAGER_H_ #include <bspline_opt/bspline_optimizer.h> #include <bspline/non_uniform_bspline.h> #include <path_searching/astar.h> #include <path_searching/kinodynamic_astar.h> #include <path_searching/topo_prm.h> #include <plan_env/edt_environment.h> #include <plan_manage/plan_container.hpp> #include <ros/ros.h> namespace fast_planner { // Fast Planner Manager // Key algorithms of mapping and planning are called class FastPlannerManager { // SECTION stable public: FastPlannerManager(); ~FastPlannerManager(); /* main planning interface */ bool kinodynamicReplan(Eigen::Vector3d start_pt, Eigen::Vector3d start_vel, Eigen::Vector3d start_acc, Eigen::Vector3d end_pt, Eigen::Vector3d end_vel); bool planGlobalTraj(const Eigen::Vector3d& start_pos); bool topoReplan(bool collide); void planYaw(const Eigen::Vector3d& start_yaw); void initPlanModules(ros::NodeHandle& nh); void setGlobalWaypoints(vector<Eigen::Vector3d>& waypoints); bool checkTrajCollision(double& distance); PlanParameters pp_; LocalTrajData local_data_; GlobalTrajData global_data_; MidPlanData plan_data_; EDTEnvironment::Ptr edt_environment_; private: /* main planning algorithms & modules */ SDFMap::Ptr sdf_map_; unique_ptr<Astar> geo_path_finder_; unique_ptr<KinodynamicAstar> kino_path_finder_; unique_ptr<TopologyPRM> topo_prm_; vector<BsplineOptimizer::Ptr> bspline_optimizers_; void updateTrajInfo(); // topology guided optimization void findCollisionRange(vector<Eigen::Vector3d>& colli_start, vector<Eigen::Vector3d>& colli_end, vector<Eigen::Vector3d>& start_pts, vector<Eigen::Vector3d>& end_pts); void optimizeTopoBspline(double start_t, double duration, vector<Eigen::Vector3d> guide_path, int traj_id); Eigen::MatrixXd reparamLocalTraj(double start_t, double& dt, double& duration); Eigen::MatrixXd reparamLocalTraj(double start_t, double duration, int seg_num, double& dt); void selectBestTraj(NonUniformBspline& traj); void refineTraj(NonUniformBspline& best_traj, double& time_inc); void reparamBspline(NonUniformBspline& bspline, double ratio, Eigen::MatrixXd& ctrl_pts, double& dt, double& time_inc); // heading planning void calcNextYaw(const double& last_yaw, double& yaw); // !SECTION stable // SECTION developing public: typedef unique_ptr<FastPlannerManager> Ptr; // !SECTION }; } // namespace fast_planner #endif
FastPlannerManager首先是包含了一系列plan的接口函数,然后,包含了一些定义在plan_container.hpp中的数据结构和类。接着是SDF地图的智能指针以及前端路径寻找类以及后端B样条优化类的智能指针。关于 tology guided optimization的部分这篇博客我们先不谈。 最后包含了一个FastPlannerManager类的智能指针 Ptr。
// #include <fstream> #include <plan_manage/planner_manager.h> #include <thread> namespace fast_planner { // SECTION interfaces for setup and query FastPlannerManager::FastPlannerManager() {} FastPlannerManager::~FastPlannerManager() { std::cout << "des manager" << std::endl; } void FastPlannerManager::initPlanModules(ros::NodeHandle& nh) { /* read algorithm parameters */ nh.param("manager/max_vel", pp_.max_vel_, -1.0); nh.param("manager/max_acc", pp_.max_acc_, -1.0); nh.param("manager/max_jerk", pp_.max_jerk_, -1.0); nh.param("manager/dynamic_environment", pp_.dynamic_, -1); nh.param("manager/clearance_threshold", pp_.clearance_, -1.0); nh.param("manager/local_segment_length", pp_.local_traj_len_, -1.0); nh.param("manager/control_points_distance", pp_.ctrl_pt_dist, -1.0); bool use_geometric_path, use_kinodynamic_path, use_topo_path, use_optimization, use_active_perception; nh.param("manager/use_geometric_path", use_geometric_path, false); nh.param("manager/use_kinodynamic_path", use_kinodynamic_path, false); nh.param("manager/use_topo_path", use_topo_path, false); nh.param("manager/use_optimization", use_optimization, false); local_data_.traj_id_ = 0; sdf_map_.reset(new SDFMap); sdf_map_->initMap(nh); edt_environment_.reset(new EDTEnvironment); edt_environment_->setMap(sdf_map_); if (use_geometric_path) { geo_path_finder_.reset(new Astar); geo_path_finder_->setParam(nh); geo_path_finder_->setEnvironment(edt_environment_); geo_path_finder_->init(); } if (use_kinodynamic_path) { kino_path_finder_.reset(new KinodynamicAstar); kino_path_finder_->setParam(nh); kino_path_finder_->setEnvironment(edt_environment_); kino_path_finder_->init(); } if (use_optimization) { bspline_optimizers_.resize(10); for (int i = 0; i < 10; ++i) { bspline_optimizers_[i].reset(new BsplineOptimizer); bspline_optimizers_[i]->setParam(nh); bspline_optimizers_[i]->setEnvironment(edt_environment_); } } if (use_topo_path) { topo_prm_.reset(new TopologyPRM); topo_prm_->setEnvironment(edt_environment_); topo_prm_->init(nh); } } void FastPlannerManager::setGlobalWaypoints(vector<Eigen::Vector3d>& waypoints) { plan_data_.global_waypoints_ = waypoints; } bool FastPlannerManager::checkTrajCollision(double& distance) { double t_now = (ros::Time::now() - local_data_.start_time_).toSec(); double tm, tmp; local_data_.position_traj_.getTimeSpan(tm, tmp); Eigen::Vector3d cur_pt = local_data_.position_traj_.evaluateDeBoor(tm + t_now); double radius = 0.0; Eigen::Vector3d fut_pt; double fut_t = 0.02; while (radius < 6.0 && t_now + fut_t < local_data_.duration_) { fut_pt = local_data_.position_traj_.evaluateDeBoor(tm + t_now + fut_t); double dist = edt_environment_->evaluateCoarseEDT(fut_pt, -1.0); if (dist < 0.1) { distance = radius; return false; } radius = (fut_pt - cur_pt).norm(); fut_t += 0.02; } return true; } // !SECTION // SECTION kinodynamic replanning bool FastPlannerManager::kinodynamicReplan(Eigen::Vector3d start_pt, Eigen::Vector3d start_vel, Eigen::Vector3d start_acc, Eigen::Vector3d end_pt, Eigen::Vector3d end_vel) { std::cout << "[kino replan]: -----------------------" << std::endl; cout << "start: " << start_pt.transpose() << ", " << start_vel.transpose() << ", " << start_acc.transpose() << "\ngoal:" << end_pt.transpose() << ", " << end_vel.transpose() << endl; if ((start_pt - end_pt).norm() < 0.2) { cout << "Close goal" << endl; return false; } ros::Time t1, t2; local_data_.start_time_ = ros::Time::now(); double t_search = 0.0, t_opt = 0.0, t_adjust = 0.0; Eigen::Vector3d init_pos = start_pt; Eigen::Vector3d init_vel = start_vel; Eigen::Vector3d init_acc = start_acc; // kinodynamic path searching t1 = ros::Time::now(); kino_path_finder_->reset(); int status = kino_path_finder_->search(start_pt, start_vel, start_acc, end_pt, end_vel, true); if (status == KinodynamicAstar::NO_PATH) { cout << "[kino replan]: kinodynamic search fail!" << endl; // retry searching with discontinuous initial state kino_path_finder_->reset(); status = kino_path_finder_->search(start_pt, start_vel, start_acc, end_pt, end_vel, false); if (status == KinodynamicAstar::NO_PATH) { cout << "[kino replan]: Can't find path." << endl; return false; } else { cout << "[kino replan]: retry search success." << endl; } } else { cout << "[kino replan]: kinodynamic search success." << endl; } plan_data_.kino_path_ = kino_path_finder_->getKinoTraj(0.01); t_search = (ros::Time::now() - t1).toSec(); // parameterize the path to bspline double ts = pp_.ctrl_pt_dist / pp_.max_vel_; vector<Eigen::Vector3d> point_set, start_end_derivatives; kino_path_finder_->getSamples(ts, point_set, start_end_derivatives); Eigen::MatrixXd ctrl_pts; NonUniformBspline::parameterizeToBspline(ts, point_set, start_end_derivatives, ctrl_pts); NonUniformBspline init(ctrl_pts, 3, ts); // bspline trajectory optimization t1 = ros::Time::now(); int cost_function = BsplineOptimizer::NORMAL_PHASE; if (status != KinodynamicAstar::REACH_END) { cost_function |= BsplineOptimizer::ENDPOINT; } ctrl_pts = bspline_optimizers_[0]->BsplineOptimizeTraj(ctrl_pts, ts, cost_function, 1, 1); t_opt = (ros::Time::now() - t1).toSec(); // iterative time adjustment t1 = ros::Time::now(); NonUniformBspline pos = NonUniformBspline(ctrl_pts, 3, ts); double to = pos.getTimeSum(); pos.setPhysicalLimits(pp_.max_vel_, pp_.max_acc_); bool feasible = pos.checkFeasibility(false); int iter_num = 0; while (!feasible && ros::ok()) { feasible = pos.reallocateTime(); if (++iter_num >= 3) break; } // pos.checkFeasibility(true); // cout << "[Main]: iter num: " << iter_num << endl; double tn = pos.getTimeSum(); cout << "[kino replan]: Reallocate ratio: " << tn / to << endl; if (tn / to > 3.0) ROS_ERROR("reallocate error."); t_adjust = (ros::Time::now() - t1).toSec(); // save planned results local_data_.position_traj_ = pos; double t_total = t_search + t_opt + t_adjust; cout << "[kino replan]: time: " << t_total << ", search: " << t_search << ", optimize: " << t_opt << ", adjust time:" << t_adjust << endl; pp_.time_search_ = t_search; pp_.time_optimize_ = t_opt; pp_.time_adjust_ = t_adjust; updateTrajInfo(); return true; }
首先是FastPlannerManager::initPlanModules()函数,这部分除了设置ROS nh句柄空间下的参数外,最重要的是将kino_path_finder以及Bspline_optimizers进行初始化。
下面的FastPlannerManager::checkTrajCollision()函数则是根据Local_data中属于NonUniformBspline类的position_traj的eveluateDeBoor()函数计算局部轨迹上的每个离散点的值,并根据SDF类的evaluateCorseEDT()函数计算距离障碍物的距离。如果轨迹中的任一点距离障碍物的距离小于阈值,则返回当前局部轨迹从起始点到检查到碰撞点的距离(即最远可执行的安全距离)。
接下来则是最重要的FastPlannerManager::kinodynamicReplan()函数,当把起止点状态都设置好后,就利用kino_path_finder的search函数进行路径寻找。需要注意的是,search函数最开始会以start_acc作为起始点输入进行查找,如果找不到,则再进行一轮离散化输入的寻找,若都找不到,则路径寻找失败。
成功寻找到一条路径后,利用NonUniformBspline::parameterizeToBspline()函数对所找到的路径进行均匀B样条函数的拟合,然后得到相应控制点。
之后便进行均匀B样条优化,但需要加以注意的是,在当前轨迹只是达到感知距离以外并未达到目标点时,目标函数需要加上ENDPOINT优化项,此时的优化变量应该包含最后 p b p_b pb控制点。但当前端寻找的路径的状态已经是REACH_END时,由于拟合最后 p b p_b pb个控制点已经能保证位置点约束,因此优化项中不再包含EDNPOINT,优化变量也不再包含最后 p b p_b pb个控制点
最后利用非均匀B样条类进行迭代时间调整,将调整后的B样条轨迹赋值给local_data.position_traj. 并利用updateTrajInfo函数对local_data的其他数据进行更新。
最后一个比较重要的cpp文件,是kino_replan_fsm.cpp. 这之中定义了一个KinoReplanFSM类,这个类包含了一系列的参数, flag, 规划数据,ROS组件。最重要的是FastPlannerManager::Ptr 对象 plan_manager 以及一系列回调函数及一个init函数。
#ifndef _KINO_REPLAN_FSM_H_ #define _KINO_REPLAN_FSM_H_ #include <Eigen/Eigen> #include <algorithm> #include <iostream> #include <nav_msgs/Path.h> #include <ros/ros.h> #include <std_msgs/Empty.h> #include <vector> #include <visualization_msgs/Marker.h> #include <bspline_opt/bspline_optimizer.h> #include <path_searching/kinodynamic_astar.h> #include <plan_env/edt_environment.h> #include <plan_env/obj_predictor.h> #include <plan_env/sdf_map.h> #include <plan_manage/Bspline.h> #include <plan_manage/planner_manager.h> #include <traj_utils/planning_visualization.h> using std::vector; namespace fast_planner { class Test { private: /* data */ int test_; std::vector<int> test_vec_; ros::NodeHandle nh_; public: Test(const int& v) { test_ = v; } Test(ros::NodeHandle& node) { nh_ = node; } ~Test() { } void print() { std::cout << "test: " << test_ << std::endl; } }; class KinoReplanFSM { private: /* ---------- flag ---------- */ enum FSM_EXEC_STATE { INIT, WAIT_TARGET, GEN_NEW_TRAJ, REPLAN_TRAJ, EXEC_TRAJ, REPLAN_NEW }; enum TARGET_TYPE { MANUAL_TARGET = 1, PRESET_TARGET = 2, REFENCE_PATH = 3 }; /* planning utils */ FastPlannerManager::Ptr planner_manager_; PlanningVisualization::Ptr visualization_; /* parameters */ int target_type_; // 1 mannual select, 2 hard code double no_replan_thresh_, replan_thresh_; double waypoints_[50][3]; int waypoint_num_; /* planning data */ bool trigger_, have_target_, have_odom_; FSM_EXEC_STATE exec_state_; Eigen::Vector3d odom_pos_, odom_vel_; // odometry state Eigen::Quaterniond odom_orient_; Eigen::Vector3d start_pt_, start_vel_, start_acc_, start_yaw_; // start state Eigen::Vector3d end_pt_, end_vel_; // target state int current_wp_; /* ROS utils */ ros::NodeHandle node_; ros::Timer exec_timer_, safety_timer_, vis_timer_, test_something_timer_; ros::Subscriber waypoint_sub_, odom_sub_; ros::Publisher replan_pub_, new_pub_, bspline_pub_; /* helper functions */ bool callKinodynamicReplan(); // front-end and back-end method bool callTopologicalTraj(int step); // topo path guided gradient-based // optimization; 1: new, 2: replan void changeFSMExecState(FSM_EXEC_STATE new_state, string pos_call); void printFSMExecState(); /* ROS functions */ void execFSMCallback(const ros::TimerEvent& e); void checkCollisionCallback(const ros::TimerEvent& e); void waypointCallback(const nav_msgs::PathConstPtr& msg); void odometryCallback(const nav_msgs::OdometryConstPtr& msg); public: KinoReplanFSM(/* args */) { } ~KinoReplanFSM() { } void init(ros::NodeHandle& nh); EIGEN_MAKE_ALIGNED_OPERATOR_NEW }; } // namespace fast_planner #endif
首先来看 KinoReplanFSM::init()函数,这一函数首先是设置了一系列参数,然后对规划管理类的智能指针对象planner_manager_进行了初始化.接着,定义了一系列的ROS组件,包括定时器,订阅器,发布器。规划的具体步骤就在这些组件的调用及回调函数里实现。
void KinoReplanFSM::init(ros::NodeHandle& nh) { current_wp_ = 0; exec_state_ = FSM_EXEC_STATE::INIT; have_target_ = false; have_odom_ = false; /* fsm param */ nh.param("fsm/flight_type", target_type_, -1); nh.param("fsm/thresh_replan", replan_thresh_, -1.0); nh.param("fsm/thresh_no_replan", no_replan_thresh_, -1.0); nh.param("fsm/waypoint_num", waypoint_num_, -1); for (int i = 0; i < waypoint_num_; i++) { nh.param("fsm/waypoint" + to_string(i) + "_x", waypoints_[i][0], -1.0); nh.param("fsm/waypoint" + to_string(i) + "_y", waypoints_[i][1], -1.0); nh.param("fsm/waypoint" + to_string(i) + "_z", waypoints_[i][2], -1.0); } /* initialize main modules */ planner_manager_.reset(new FastPlannerManager); planner_manager_->initPlanModules(nh); visualization_.reset(new PlanningVisualization(nh)); /* callback */ exec_timer_ = nh.createTimer(ros::Duration(0.01), &KinoReplanFSM::execFSMCallback, this); safety_timer_ = nh.createTimer(ros::Duration(0.05), &KinoReplanFSM::checkCollisionCallback, this); waypoint_sub_ = nh.subscribe("/waypoint_generator/waypoints", 1, &KinoReplanFSM::waypointCallback, this); odom_sub_ = nh.subscribe("/odom_world", 1, &KinoReplanFSM::odometryCallback, this); replan_pub_ = nh.advertise<std_msgs::Empty>("/planning/replan", 10); new_pub_ = nh.advertise<std_msgs::Empty>("/planning/new", 10); bspline_pub_ = nh.advertise<plan_manage::Bspline>("/planning/bspline", 10); }
我们首先来看两个定时器的回调函数。第一个是KinoReplanFSM::execFSMCallback(),这个函数的触发时间是每0.01秒。首先,每1秒打印一次当前执行状态。然后,根据执行状态变量 exec_state_进入switch循环。
如果状态是INIT,则判断有没有收到odometry 和 plan的 ttrigger是否被触发。 have_odom 和 trigger 这两个bool变量分别在KinoReplanFSM::odometryCallback()和KinoReplanFSM::waypointCallback() 这两个消息订阅的回调函数中被改变。如果都通过,改变当前状态为WAIT_TARGET,并跳出当前循环。
如果状态是WAIT_TARGET, 则判断是否有target,如果有,则改变状态为GEN_NEW_TRAJ,并跳出当前循环。
void KinoReplanFSM::execFSMCallback(const ros::TimerEvent& e) { static int fsm_num = 0; fsm_num++; if (fsm_num == 100) { printFSMExecState(); if (!have_odom_) cout << "no odom." << endl; if (!trigger_) cout << "wait for goal." << endl; fsm_num = 0; } switch (exec_state_) { case INIT: { if (!have_odom_) { return; } if (!trigger_) { return; } changeFSMExecState(WAIT_TARGET, "FSM"); break; } case WAIT_TARGET: { if (!have_target_) return; else { changeFSMExecState(GEN_NEW_TRAJ, "FSM"); } break; } case GEN_NEW_TRAJ: { start_pt_ = odom_pos_; start_vel_ = odom_vel_; start_acc_.setZero(); Eigen::Vector3d rot_x = odom_orient_.toRotationMatrix().block(0, 0, 3, 1); start_yaw_(0) = atan2(rot_x(1), rot_x(0)); start_yaw_(1) = start_yaw_(2) = 0.0; bool success = callKinodynamicReplan(); if (success) { changeFSMExecState(EXEC_TRAJ, "FSM"); } else { // have_target_ = false; // changeFSMExecState(WAIT_TARGET, "FSM"); changeFSMExecState(GEN_NEW_TRAJ, "FSM"); } break; } case EXEC_TRAJ: { /* determine if need to replan */ LocalTrajData* info = &planner_manager_->local_data_; ros::Time time_now = ros::Time::now(); double t_cur = (time_now - info->start_time_).toSec(); t_cur = min(info->duration_, t_cur); Eigen::Vector3d pos = info->position_traj_.evaluateDeBoorT(t_cur); /* && (end_pt_ - pos).norm() < 0.5 */ if (t_cur > info->duration_ - 1e-2) { have_target_ = false; changeFSMExecState(WAIT_TARGET, "FSM"); return; } else if ((end_pt_ - pos).norm() < no_replan_thresh_) { // cout << "near end" << endl; return; } else if ((info->start_pos_ - pos).norm() < replan_thresh_) { // cout << "near start" << endl; return; } else { changeFSMExecState(REPLAN_TRAJ, "FSM"); } break; } case REPLAN_TRAJ: { LocalTrajData* info = &planner_manager_->local_data_; ros::Time time_now = ros::Time::now(); double t_cur = (time_now - info->start_time_).toSec(); start_pt_ = info->position_traj_.evaluateDeBoorT(t_cur); start_vel_ = info->velocity_traj_.evaluateDeBoorT(t_cur); start_acc_ = info->acceleration_traj_.evaluateDeBoorT(t_cur); start_yaw_(0) = info->yaw_traj_.evaluateDeBoorT(t_cur)[0]; start_yaw_(1) = info->yawdot_traj_.evaluateDeBoorT(t_cur)[0]; start_yaw_(2) = info->yawdotdot_traj_.evaluateDeBoorT(t_cur)[0]; std_msgs::Empty replan_msg; replan_pub_.publish(replan_msg); bool success = callKinodynamicReplan(); if (success) { changeFSMExecState(EXEC_TRAJ, "FSM"); } else { changeFSMExecState(GEN_NEW_TRAJ, "FSM"); } break; } } }
如果状态是GEN_NEW_TRAJ,则调用callKinodynamicReplan()函数进行规划。成功则改变执行状态为EXEC_TRAJ,失败则改变执行状态为GEN_NEW_TRAJ。这里我们来看callKinodynamicReplan()函数。如果规划成功的话,就通过Bspline_pub把相应的B样条轨迹发布出去,否则返回FALSE。
bool KinoReplanFSM::callKinodynamicReplan() { bool plan_success = planner_manager_->kinodynamicReplan(start_pt_, start_vel_, start_acc_, end_pt_, end_vel_); if (plan_success) { planner_manager_->planYaw(start_yaw_); auto info = &planner_manager_->local_data_; /* publish traj */ plan_manage::Bspline bspline; bspline.order = 3; bspline.start_time = info->start_time_; bspline.traj_id = info->traj_id_; Eigen::MatrixXd pos_pts = info->position_traj_.getControlPoint(); for (int i = 0; i < pos_pts.rows(); ++i) { geometry_msgs::Point pt; pt.x = pos_pts(i, 0); pt.y = pos_pts(i, 1); pt.z = pos_pts(i, 2); bspline.pos_pts.push_back(pt); } Eigen::VectorXd knots = info->position_traj_.getKnot(); for (int i = 0; i < knots.rows(); ++i) { bspline.knots.push_back(knots(i)); } Eigen::MatrixXd yaw_pts = info->yaw_traj_.getControlPoint(); for (int i = 0; i < yaw_pts.rows(); ++i) { double yaw = yaw_pts(i, 0); bspline.yaw_pts.push_back(yaw); } bspline.yaw_dt = info->yaw_traj_.getInterval(); bspline_pub_.publish(bspline); /* visulization */ auto plan_data = &planner_manager_->plan_data_; visualization_->drawGeometricPath(plan_data->kino_path_, 0.075, Eigen::Vector4d(1, 1, 0, 0.4)); visualization_->drawBspline(info->position_traj_, 0.1, Eigen::Vector4d(1.0, 0, 0.0, 1), true, 0.2, Eigen::Vector4d(1, 0, 0, 1)); return true; } else { cout << "generate new traj fail." << endl; return false; } }
如果状态为EXEC_TRAJ的话,则判断是否需要进行重规划。如果当前时间距离起始时间已经超过当前执行轨迹的时长,则将have_target置于false,将执行状态变为WAIT_TARGET。如果当前距离与终点距离小于不需要重规划的阈值,或者当前距离与起点距离小于horizon,那么直接返回。反之,则进入重规划阶段。
如果状态为REPLAN_TRAJ的话,则利用当前的位置,速度,状态,进行callKinodynamicReplan重规划,如果成功,则将执行状态更改为EXEC_TRAJ,否则将执行状态更改为GEN_NEW_TRAJ.
再来看KinoReplanFSM::checkCollisionCallback()。这一执行器回调函数是用来判断目标点是否有障碍物或者是轨迹执行过程中是否有障碍物的。如果目标点有障碍物,就在目标点周围通过离散的半径及角度循环来寻找新的安全的目标点。如果找到了,就直接改变状态进入REPLAN_TRAJ。如果目标点周围没有障碍物且目前状态是EXEX_TRAJ,则利用planner_manager的checkTrajCollision函数进行轨迹检查,如果轨迹不发生碰撞,则无事发生,如果轨迹碰撞,则状态变为REPLAN_TRAJ,进行重规划。
void KinoReplanFSM::checkCollisionCallback(const ros::TimerEvent& e) { LocalTrajData* info = &planner_manager_->local_data_; if (have_target_) { auto edt_env = planner_manager_->edt_environment_; double dist = planner_manager_->pp_.dynamic_ ? edt_env->evaluateCoarseEDT(end_pt_, /* time to program start + */ info->duration_) : edt_env->evaluateCoarseEDT(end_pt_, -1.0); if (dist <= 0.3) { /* try to find a max distance goal around */ bool new_goal = false; const double dr = 0.5, dtheta = 30, dz = 0.3; double new_x, new_y, new_z, max_dist = -1.0; Eigen::Vector3d goal; for (double r = dr; r <= 5 * dr + 1e-3; r += dr) { for (double theta = -90; theta <= 270; theta += dtheta) { for (double nz = 1 * dz; nz >= -1 * dz; nz -= dz) { new_x = end_pt_(0) + r * cos(theta / 57.3); new_y = end_pt_(1) + r * sin(theta / 57.3); new_z = end_pt_(2) + nz; Eigen::Vector3d new_pt(new_x, new_y, new_z); dist = planner_manager_->pp_.dynamic_ ? edt_env->evaluateCoarseEDT(new_pt, /* time to program start+ */ info->duration_) : edt_env->evaluateCoarseEDT(new_pt, -1.0); if (dist > max_dist) { /* reset end_pt_ */ goal(0) = new_x; goal(1) = new_y; goal(2) = new_z; max_dist = dist; } } } } if (max_dist > 0.3) { cout << "change goal, replan." << endl; end_pt_ = goal; have_target_ = true; end_vel_.setZero(); if (exec_state_ == EXEC_TRAJ) { changeFSMExecState(REPLAN_TRAJ, "SAFETY"); } visualization_->drawGoal(end_pt_, 0.3, Eigen::Vector4d(1, 0, 0, 1.0)); } else { // have_target_ = false; // cout << "Goal near collision, stop." << endl; // changeFSMExecState(WAIT_TARGET, "SAFETY"); cout << "goal near collision, keep retry" << endl; changeFSMExecState(REPLAN_TRAJ, "FSM"); std_msgs::Empty emt; replan_pub_.publish(emt); } } } /* ---------- check trajectory ---------- */ if (exec_state_ == FSM_EXEC_STATE::EXEC_TRAJ) { double dist; bool safe = planner_manager_->checkTrajCollision(dist); if (!safe) { // cout << "current traj in collision." << endl; ROS_WARN("current traj in collision."); changeFSMExecState(REPLAN_TRAJ, "SAFETY"); } } }
至此,kino_dynamic_replan的代码流程就结束了。我们大致从头梳理一下整个系统的工作顺序。
至此,关于这篇文章的代码就给大家分析完毕,笔者能力所限,出错在所难免,欢迎大家批评指正。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。