|
@@ -10,156 +10,179 @@
|
|
|
|
|
|
using CppAD::AD;
|
|
|
|
|
|
-size_t K=10;
|
|
|
+size_t K = 10;
|
|
|
size_t nx1 = 0;
|
|
|
size_t ny1 = nx1 + K;
|
|
|
size_t nth1 = ny1 + K;
|
|
|
size_t nv1 = nth1 + K;
|
|
|
size_t nvth1 = nv1 + K;
|
|
|
+size_t nacc1 = nvth1 + K;
|
|
|
+size_t naccth1 = nacc1 + K - 1;
|
|
|
|
|
|
-
|
|
|
-size_t nx2 = nvth1 + K;
|
|
|
+size_t nx2 = naccth1 + K - 1;
|
|
|
size_t ny2 = nx2 + K;
|
|
|
size_t nth2 = ny2 + K;
|
|
|
size_t nv2 = nth2 + K;
|
|
|
size_t nvth2 = nv2 + K;
|
|
|
+size_t nacc2 = nvth2 + K;
|
|
|
+size_t naccth2 = nacc2 + K - 1;
|
|
|
+
|
|
|
+size_t relative_pose_contrains = naccth2 + K - 1;
|
|
|
+
|
|
|
+double Lx1 = -1.3;
|
|
|
+double Ly1 = 0;
|
|
|
+double Lx2 = 1.3;
|
|
|
+double Ly2 = 0;
|
|
|
|
|
|
-double Lx1=-1.3;
|
|
|
-double Ly1=0;
|
|
|
-double Lx2=1.3;
|
|
|
-double Ly2=0;
|
|
|
+double ref_dist = 2.0;
|
|
|
+// double min_dist = 1.5;
|
|
|
+// double max_dist = 3.0;
|
|
|
|
|
|
-class FG_eval_2 {
|
|
|
+class FG_eval_2
|
|
|
+{
|
|
|
public:
|
|
|
// Fitted polynomial coefficients
|
|
|
- Eigen::VectorXd coeffs,coeffs_b;
|
|
|
+ Eigen::VectorXd coeffs, coeffs_b;
|
|
|
Eigen::VectorXd m_data;
|
|
|
- FG_eval_2(Eigen::VectorXd coeffs,Eigen::VectorXd coeffs_b,Eigen::VectorXd P) {
|
|
|
+ FG_eval_2(Eigen::VectorXd coeffs, Eigen::VectorXd coeffs_b, Eigen::VectorXd P)
|
|
|
+ {
|
|
|
this->coeffs = coeffs;
|
|
|
- this->coeffs_b=coeffs_b;
|
|
|
- m_data=P;
|
|
|
+ this->coeffs_b = coeffs_b;
|
|
|
+ m_data = P;
|
|
|
}
|
|
|
|
|
|
typedef CPPAD_TESTVECTOR(AD<double>) ADvector;
|
|
|
- void operator()(ADvector& fg, const ADvector& vars) {
|
|
|
-
|
|
|
- double v1=m_data[0];
|
|
|
- double v2=m_data[1];
|
|
|
- double vth1=m_data[2];
|
|
|
- double vth2=m_data[3];
|
|
|
- double ref_v=m_data[4];
|
|
|
- double dt=m_data[5];
|
|
|
-
|
|
|
- double dx=m_data[6]; /// 两车初始位姿差 前车相对后车坐标系
|
|
|
- double dy=m_data[7];
|
|
|
- double dth=m_data[8]; ///两坐标系角度
|
|
|
-
|
|
|
+ void operator()(ADvector &fg, const ADvector &vars)
|
|
|
+ {
|
|
|
|
|
|
+ // std::cout << "max size: " << naccth2 + K - 1 << std::endl;
|
|
|
+ double v1 = m_data[0];
|
|
|
+ double v2 = m_data[1];
|
|
|
+ double vth1 = m_data[2];
|
|
|
+ double vth2 = m_data[3];
|
|
|
+ double ref_v = m_data[4];
|
|
|
+ double dt = m_data[5];
|
|
|
+
|
|
|
+ /// 两车初始位姿差 前车相对后车坐标系
|
|
|
+ double dx = m_data[6];
|
|
|
+ double dy = m_data[7];
|
|
|
+ double dth = m_data[8]; ///两坐标系角度
|
|
|
|
|
|
///前车 cost weight
|
|
|
- const int y_cost_weight = 1500;
|
|
|
+ const int y_cost_weight = 8000;
|
|
|
const int th_cost_weight = 1000;
|
|
|
const int v_cost_weight = 20;
|
|
|
|
|
|
const int vth_cost_weight = 100;
|
|
|
const int a_cost_weight = 1;
|
|
|
- const int ath_cost_weight=100;
|
|
|
-
|
|
|
- ///将坐标旋转到后车坐标系下
|
|
|
- double new_xt=cos(dth)*Lx1-sin(dth)*Ly1;
|
|
|
- double new_yt=sin(dth)*Lx1+cos(dth)*Ly1;
|
|
|
-
|
|
|
- double distance_x=new_xt-Lx2+dx;
|
|
|
- double distance_y=new_yt-Ly2+dy;
|
|
|
- printf(" Key Point distance xy: %.3f %.3f\n",fabs(distance_x),fabs(distance_y));
|
|
|
+ const int ath_cost_weight = 10;
|
|
|
|
|
|
fg[0] = 0;
|
|
|
///后车 cost weight
|
|
|
- const int back_y_cost_weight = 500;
|
|
|
- const int back_th_cost_weight = 300;
|
|
|
- const int back_v_cost_weight = 1;
|
|
|
+ const int back_y_cost_weight = 8000;
|
|
|
+ const int back_th_cost_weight = 1000;
|
|
|
+ const int back_v_cost_weight = 20;
|
|
|
|
|
|
- const int back_vth_cost_weight = 3;
|
|
|
+ const int back_vth_cost_weight = 100;
|
|
|
const int back_a_cost_weight = 1;
|
|
|
- const int back_ath_cost_weight=5;
|
|
|
+ const int back_ath_cost_weight = 10;
|
|
|
|
|
|
- for (int t = 0; t < K; t++) {
|
|
|
- AD<double> xt=vars[nx1+t];
|
|
|
+ for (int t = 0; t < K; t++)
|
|
|
+ {
|
|
|
+ // 前车坐标系下,y坐标差
|
|
|
+ AD<double> xt = vars[nx1 + t];
|
|
|
AD<double> fx = coeffs[0] + coeffs[1] * xt + coeffs[2] * pow(xt, 2) + coeffs[3] * pow(xt, 3);
|
|
|
- fg[0] += y_cost_weight * CppAD::pow(vars[ny1 + t]-fx, 2);
|
|
|
+ fg[0] += y_cost_weight * CppAD::pow(vars[ny1 + t] - fx, 2);
|
|
|
|
|
|
- AD<double> fth = CppAD::atan(coeffs[1] + 2*coeffs[2]*xt + 3*coeffs[3]*pow(xt,2));
|
|
|
- fg[0] += th_cost_weight * CppAD::pow(vars[nth1 + t]-fth, 2);
|
|
|
- fg[0]+=v_cost_weight*CppAD::pow(vars[nv1+t]-ref_v,2);
|
|
|
-
|
|
|
- //// loss back
|
|
|
- AD<double> xt2=vars[nx2+t];
|
|
|
- AD<double> fx2 = coeffs[0] + coeffs_b[1] * xt2 + coeffs_b[2] * pow(xt2, 2) + coeffs_b[3] * pow(xt2, 3);
|
|
|
- fg[0] += back_y_cost_weight * CppAD::pow(vars[ny2 + t]-fx2, 2);
|
|
|
+ // 前车坐标系下,theta cost & v cost
|
|
|
+ AD<double> fth = CppAD::atan(coeffs[1] + 2 * coeffs[2] * xt + 3 * coeffs[3] * pow(xt, 2));
|
|
|
+ fg[0] += th_cost_weight * CppAD::pow(vars[nth1 + t] - fth, 2);
|
|
|
+ fg[0] += v_cost_weight * CppAD::pow(vars[nv1 + t] - ref_v, 2);
|
|
|
+ fg[0] += vth_cost_weight * CppAD::pow(vars[nvth1 + t], 2);
|
|
|
|
|
|
- AD<double> fth2 = CppAD::atan(coeffs_b[1] + 2*coeffs_b[2]*xt2 + 3*coeffs_b[3]*pow(xt2,2));
|
|
|
- fg[0] += back_th_cost_weight * CppAD::pow(vars[nth2 + t]-fth2, 2);
|
|
|
- fg[0] +=back_v_cost_weight*CppAD::pow(vars[nv2+t]-ref_v,2);
|
|
|
+ // 在前车坐标系下,后车y坐标差
|
|
|
+ AD<double> xt2 = vars[nx2 + t];
|
|
|
+ AD<double> fx2 = coeffs_b[0] + coeffs_b[1] * xt2 + coeffs_b[2] * pow(xt2, 2) + coeffs_b[3] * pow(xt2, 3);
|
|
|
+ fg[0] += back_y_cost_weight * CppAD::pow(vars[ny2 + t] - fx2, 2);
|
|
|
|
|
|
+ // 在前车坐标系下,后车theta cost & v cost
|
|
|
+ AD<double> fth2 = CppAD::atan(coeffs_b[1] + 2 * coeffs_b[2] * xt2 + 3 * coeffs_b[3] * pow(xt2, 2));
|
|
|
+ fg[0] += back_th_cost_weight * CppAD::pow(vars[nth2 + t] - fth2, 2);
|
|
|
+ fg[0] += back_v_cost_weight * CppAD::pow(vars[nv2 + t] - ref_v, 2);
|
|
|
+ fg[0] += back_vth_cost_weight * CppAD::pow(vars[nvth2 + t], 2);
|
|
|
}
|
|
|
//加速度 cost
|
|
|
// Costs for steering (delta) and acceleration (a)
|
|
|
- for (int t = 0; t < K-1; t++) {
|
|
|
- fg[0] += vth_cost_weight * CppAD::pow(vars[nvth1 + t], 2);
|
|
|
- fg[0] += a_cost_weight * CppAD::pow(vars[nv1 + t+1]-vars[nv1+t], 2);
|
|
|
- fg[0] += ath_cost_weight * CppAD::pow(vars[nvth1 + t+1]-vars[nvth1+t], 2);
|
|
|
+ for (int t = 0; t < K - 1; t++)
|
|
|
+ {
|
|
|
+ fg[0] += a_cost_weight * CppAD::pow(vars[nacc1+ t], 2);
|
|
|
+ fg[0] += ath_cost_weight * CppAD::pow(vars[naccth1 + t], 2);
|
|
|
|
|
|
- fg[0] += back_vth_cost_weight * CppAD::pow(vars[nvth2 + t], 2);
|
|
|
- fg[0] += back_a_cost_weight * CppAD::pow(vars[nv2 + t+1]-vars[nv2+t], 2);
|
|
|
- fg[0] += back_ath_cost_weight * CppAD::pow(vars[nvth2 + t+1]-vars[nvth2+t], 2);
|
|
|
+ fg[0] += back_a_cost_weight * CppAD::pow(vars[nacc2 + t], 2);
|
|
|
+ fg[0] += back_ath_cost_weight * CppAD::pow(vars[naccth2 + t], 2);
|
|
|
}
|
|
|
+
|
|
|
///添加两车距离损失
|
|
|
- const int distance_weight_x=10;
|
|
|
- const int distance_weight_y=50;
|
|
|
- const int delta_th_weight=1;
|
|
|
- for(int t=0;t<K;++t)
|
|
|
+ const int distance_weight = 800;
|
|
|
+ const int delta_th_weight = 10;
|
|
|
+ char buf[255]={0};
|
|
|
+ AD<double> dtheta = vars[nth1] - vars[nth2] + dth;
|
|
|
+ for (int t = 0; t < K; ++t)
|
|
|
{
|
|
|
- AD<double> xt=vars[nx1+t]+Lx1;
|
|
|
- AD<double> yt=vars[ny1+t]+Ly1;
|
|
|
+ AD<double> x1_in_front = vars[nx1 + t];
|
|
|
+ AD<double> y1_in_front = vars[ny1 + t];
|
|
|
///将坐标旋转到后车坐标系下
|
|
|
- AD<double> new_xt=CppAD::cos(dth)*xt-CppAD::sin(dth)*yt;
|
|
|
- AD<double> new_yt=CppAD::sin(dth)*xt+CppAD::cos(dth)*yt;
|
|
|
-
|
|
|
- AD<double> distance_x=new_xt-(vars[nx2+t]+Lx2)+dx;
|
|
|
- AD<double> distance_y=new_yt-(vars[ny2+t]+Ly2)+dy;
|
|
|
+ AD<double> x1_in_back = CppAD::cos(dtheta) * x1_in_front - CppAD::sin(dtheta) * y1_in_front + dx;
|
|
|
+ AD<double> y1_in_back = CppAD::sin(dtheta) * x1_in_front + CppAD::cos(dtheta) * y1_in_front + dy;
|
|
|
|
|
|
- fg[0]+=distance_weight_x*CppAD::pow(distance_x,2);//+distance_weight_y*CppAD::pow(distance_y,2);
|
|
|
- fg[0]+=delta_th_weight*CppAD::pow(vars[nth1+t]-vars[nth2+t]+dth,2);
|
|
|
+ AD<double> distance_x = x1_in_back - vars[nx2 + t];
|
|
|
+ AD<double> distance_y = y1_in_back - vars[ny2 + t];
|
|
|
|
|
|
+ fg[0] += distance_weight * CppAD::pow(CppAD::sqrt(CppAD::pow(distance_x, 2) + CppAD::pow(distance_y, 2)) - ref_dist, 2);
|
|
|
+ fg[0] += delta_th_weight * CppAD::pow(CppAD::atan(y1_in_back / x1_in_back), 2);
|
|
|
+ sprintf(buf+strlen(buf), " %.4f ", sqrt(pow(distance_x,2)+pow(distance_y,2)));
|
|
|
+ // // 两车最短距离约束
|
|
|
+ // fg[1 + relative_pose_contrains + t - 4*(K-1)] = CppAD::sqrt(CppAD::pow(distance_x, 2) + CppAD::pow(distance_y, 2));
|
|
|
}
|
|
|
+ std::cout<<buf<<std::endl;
|
|
|
|
|
|
- /////////////////////
|
|
|
- fg[1 + nx1] = vars[nx1]-vars[nv1]*dt;
|
|
|
+ // x, y, theta 路径约束
|
|
|
+ fg[1 + nx1] = vars[nx1];// - vars[nv1] * dt;
|
|
|
fg[1 + ny1] = vars[ny1];
|
|
|
- fg[1 + nth1] = vars[nth1]-vars[nvth1]*dt;
|
|
|
+ fg[1 + nth1] = vars[nth1];// - vars[nvth1] * dt;
|
|
|
+ fg[1 + nv1] = vars[nv1];
|
|
|
+ fg[1 + nvth1] = vars[nvth1];
|
|
|
|
|
|
- fg[1 + nx2] = vars[nx2]-vars[nv2]*dt;
|
|
|
- fg[1 + ny2] = vars[ny2];
|
|
|
- fg[1 + nth2] = vars[nth2]-vars[nvth2]*dt;
|
|
|
+ fg[1 + nx2 - 2*(K-1)] = vars[nx2];// - vars[nv2] * dt;
|
|
|
+ fg[1 + ny2 - 2*(K-1)] = vars[ny2];
|
|
|
+ fg[1 + nth2 - 2*(K-1)] = vars[nth2];// - vars[nvth2] * dt;
|
|
|
+ fg[1 + nv2 - 2*(K-1)] = vars[nv2];
|
|
|
+ fg[1 + nvth2 - 2*(K-1)] = vars[nvth2];
|
|
|
|
|
|
// The rest of the constraints
|
|
|
- for (int t = 1; t < K; t++) {
|
|
|
+ for (int t = 1; t < K; t++)
|
|
|
+ {
|
|
|
/// 位置约束 1
|
|
|
- AD<double> x1 = vars[nx1 + t];
|
|
|
- AD<double> y1 = vars[ny1 + t];
|
|
|
- AD<double> th1 = vars[nth1 + t];
|
|
|
- AD<double> v1 = vars[nv1 + t];
|
|
|
- AD<double> vth1 = vars[nvth1 + t];
|
|
|
-
|
|
|
- AD<double> x0 = vars[nx1 + t -1];
|
|
|
- AD<double> y0 = vars[ny1 + t -1];
|
|
|
- AD<double> th0 = vars[nth1 + t-1];
|
|
|
- AD<double> v0 = vars[nv1 + t-1];
|
|
|
- AD<double> vth0 = vars[nvth1 + t-1];
|
|
|
-
|
|
|
- fg[1 + nx1 + t] = x1 - (x0 + v0 * CppAD::cos(th0) * dt);
|
|
|
- fg[1 + ny1 + t] = y1 - (y0 + v0 * CppAD::sin(th0) * dt);
|
|
|
- fg[1 + nth1 + t] = th1 - (th0 + vth0 * dt);
|
|
|
+ AD<double> fx1 = vars[nx1 + t];
|
|
|
+ AD<double> fy1 = vars[ny1 + t];
|
|
|
+ AD<double> fth1 = vars[nth1 + t];
|
|
|
+ AD<double> fv1 = vars[nv1 + t];
|
|
|
+ AD<double> fvth1 = vars[nvth1 + t];
|
|
|
+
|
|
|
+ AD<double> fx0 = vars[nx1 + t - 1];
|
|
|
+ AD<double> fy0 = vars[ny1 + t - 1];
|
|
|
+ AD<double> fth0 = vars[nth1 + t - 1];
|
|
|
+ AD<double> fv0 = vars[nv1 + t - 1];
|
|
|
+ AD<double> fvth0 = vars[nvth1 + t - 1];
|
|
|
+
|
|
|
+ AD<double> facc0 = vars[nacc1 + t - 1];
|
|
|
+ AD<double> faccth0 = vars[naccth1 + t - 1];
|
|
|
+
|
|
|
+ fg[1 + nx1 + t] = fx1 - (fx0 + fv0 * CppAD::cos(fth0) * dt);
|
|
|
+ fg[1 + ny1 + t] = fy1 - (fy0 + fv0 * CppAD::sin(fth0) * dt);
|
|
|
+ fg[1 + nth1 + t] = fth1 - (fth0 + fvth0 * dt);
|
|
|
+ fg[1 + nv1 + t] = fv1 - (fv0 + facc0 * dt);
|
|
|
+ fg[1 + nvth1 + t] = fvth1 - (fvth0 + faccth0 * dt);
|
|
|
|
|
|
//位置约束2
|
|
|
AD<double> bx1 = vars[nx2 + t];
|
|
@@ -168,115 +191,125 @@ public:
|
|
|
AD<double> bv1 = vars[nv2 + t];
|
|
|
AD<double> bvth1 = vars[nvth2 + t];
|
|
|
|
|
|
- AD<double> bx0 = vars[nx2 + t -1];
|
|
|
- AD<double> by0 = vars[ny2 + t -1];
|
|
|
- AD<double> bth0 = vars[nth2 + t-1];
|
|
|
- AD<double> bv0 = vars[nv2 + t-1];
|
|
|
- AD<double> bvth0 = vars[nvth2 + t-1];
|
|
|
+ AD<double> bx0 = vars[nx2 + t - 1];
|
|
|
+ AD<double> by0 = vars[ny2 + t - 1];
|
|
|
+ AD<double> bth0 = vars[nth2 + t - 1];
|
|
|
+ AD<double> bv0 = vars[nv2 + t - 1];
|
|
|
+ AD<double> bvth0 = vars[nvth2 + t - 1];
|
|
|
|
|
|
- fg[1 + nx2 + t] = bx1 - (bx0 + bv0 * CppAD::cos(bth0) * dt);
|
|
|
- fg[1 + ny2 + t] = by1 - (by0 + bv0 * CppAD::sin(bth0) * dt);
|
|
|
- fg[1 + nth2 + t] = bth1 - (bth0 + bvth0 * dt);
|
|
|
+ AD<double> bacc0 = vars[nacc2 + t - 1];
|
|
|
+ AD<double> baccth0 = vars[naccth2 + t - 1];
|
|
|
|
|
|
+ fg[1 + nx2 + t - 2*(K-1)] = bx1 - (bx0 + bv0 * CppAD::cos(bth0) * dt);
|
|
|
+ fg[1 + ny2 + t - 2*(K-1)] = by1 - (by0 + bv0 * CppAD::sin(bth0) * dt);
|
|
|
+ fg[1 + nth2 + t - 2*(K-1)] = bth1 - (bth0 + bvth0 * dt);
|
|
|
+ fg[1 + nv2 + t - 2*(K-1)] = bv1 - (bv0 + bacc0 * dt);
|
|
|
+ fg[1 + nvth2 + t - 2*(K-1)] = bvth1 - (bvth0 + baccth0 * dt);
|
|
|
}
|
|
|
-
|
|
|
- fg[1 + nv1]=(vars[nv1]-v1)/dt;
|
|
|
- fg[1+nvth1]=(vars[nvth1]-vth1)/dt;
|
|
|
-
|
|
|
- fg[1 + nv2]=(vars[nv2]-v2)/dt;
|
|
|
- fg[1+nvth2]=(vars[nvth2]-vth2)/dt;
|
|
|
- for(int t=1;t<K;++t)
|
|
|
- {
|
|
|
- fg[1+nv1+t]=(vars[nv1+t]-vars[nv1+t-1])/dt;
|
|
|
- fg[1+nvth1+t]=(vars[nvth1+t]-vars[nvth1+t-1])/dt;
|
|
|
- fg[1+nv2+t]=(vars[nv2+t]-vars[nv2+t-1])/dt;
|
|
|
- fg[1+nvth2+t]=(vars[nvth2+t]-vars[nvth2+t-1])/dt;
|
|
|
- }
|
|
|
-
|
|
|
}
|
|
|
};
|
|
|
|
|
|
-bool TowBotMPC::Solve(Eigen::VectorXd state, double max_v,Eigen::VectorXd coeffs, Eigen::VectorXd coeffs_b,std::vector<double> &out)
|
|
|
+bool TowBotMPC::Solve(Eigen::VectorXd state, double max_v, Eigen::VectorXd coeffs, Eigen::VectorXd coeffs_b, std::vector<double> &out)
|
|
|
{
|
|
|
bool ok = true;
|
|
|
typedef CPPAD_TESTVECTOR(double) Dvector;
|
|
|
// State vector holds all current values neede for vars below
|
|
|
|
|
|
- double v1=state[0];
|
|
|
- double v2=state[1];
|
|
|
- double vth1=state[2];
|
|
|
- double vth2=state[3];
|
|
|
- double ref_v=state[4];
|
|
|
- double dt=state[5];
|
|
|
+ // std::cout<<"state: "<<state<<std::endl;
|
|
|
+ double v1 = state[0];
|
|
|
+ double v2 = state[1];
|
|
|
+ double vth1 = state[2];
|
|
|
+ double vth2 = state[3];
|
|
|
+ double ref_v = state[4];
|
|
|
+ double dt = state[5];
|
|
|
|
|
|
- double dx=state[6]; /// 两车初始位姿差 前车相对后车坐标系
|
|
|
- double dy=state[7];
|
|
|
- double dth=state[8]; ///两坐标系角度
|
|
|
+ // double dx = state[6]; /// 两车初始位姿差 前车相对后车坐标系
|
|
|
+ // double dy = state[7];
|
|
|
+ // double dth = state[8]; ///两坐标系角度
|
|
|
|
|
|
- size_t n_vars = K * 5 *2;
|
|
|
- size_t n_constraints = K * 5 *2;
|
|
|
+ size_t n_vars = 2 * (K * 5 + (K - 1) * 2);
|
|
|
+ size_t n_constraints = K * (5 * 2 + 0);
|
|
|
|
|
|
Dvector vars(n_vars);
|
|
|
- for (int i = 0; i < n_vars; i++) {
|
|
|
+ for (int i = 0; i < n_vars; i++)
|
|
|
+ {
|
|
|
vars[i] = 0.0;
|
|
|
}
|
|
|
- for(int i=0;i<K;++i)
|
|
|
+ for (int i = 0; i < K; ++i)
|
|
|
{
|
|
|
- vars[nv1+i]=v1;
|
|
|
- vars[nv2+i]=v2;
|
|
|
+ vars[nv1 + i] = v1;
|
|
|
+ vars[nv2 + i] = v2;
|
|
|
|
|
|
- vars[nvth1+i]=vth1;
|
|
|
- vars[nvth2+i]=vth2;
|
|
|
+ vars[nvth1 + i] = vth1;
|
|
|
+ vars[nvth2 + i] = vth2;
|
|
|
}
|
|
|
|
|
|
Dvector vars_lowerbound(n_vars);
|
|
|
Dvector vars_upperbound(n_vars);
|
|
|
|
|
|
- for (int i = 0; i < n_vars; i++) {
|
|
|
+ for (int i = 0; i < n_vars; i++)
|
|
|
+ {
|
|
|
vars_lowerbound[i] = -1.0e19;
|
|
|
vars_upperbound[i] = 1.0e19;
|
|
|
}
|
|
|
|
|
|
- for (int i = 0; i < K; i++) {
|
|
|
+ for (int i = 0; i < K; i++)
|
|
|
+ {
|
|
|
//// limit v1 v2
|
|
|
- vars_lowerbound[nv1+i] = -max_v;
|
|
|
- vars_upperbound[nv1+i] = max_v;
|
|
|
+ vars_lowerbound[nv1 + i] = -max_v;
|
|
|
+ vars_upperbound[nv1 + i] = max_v;
|
|
|
|
|
|
- vars_lowerbound[nv2+i] = -max_v;
|
|
|
- vars_upperbound[nv2+i] = max_v;
|
|
|
+ vars_lowerbound[nv2 + i] = -max_v;
|
|
|
+ vars_upperbound[nv2 + i] = max_v;
|
|
|
|
|
|
//// limit vth1 2
|
|
|
- vars_lowerbound[nvth1+i] = -0.5;
|
|
|
- vars_upperbound[nvth1+i] = 0.5;
|
|
|
+ vars_lowerbound[nvth1 + i] = -0.5;
|
|
|
+ vars_upperbound[nvth1 + i] = 0.5;
|
|
|
|
|
|
- vars_lowerbound[nvth2+i] = -0.5;
|
|
|
- vars_upperbound[nvth2+i] = 0.5;
|
|
|
+ vars_lowerbound[nvth2 + i] = -0.5;
|
|
|
+ vars_upperbound[nvth2 + i] = 0.5;
|
|
|
}
|
|
|
+ for (int i = 0; i < K - 1; i++)
|
|
|
+ {
|
|
|
+ //// limit acc1 acc2
|
|
|
+ vars_lowerbound[nacc1 + i] = -5.0;
|
|
|
+ vars_upperbound[nacc2 + i] = 5.0;
|
|
|
+
|
|
|
+ //// limit accth1 accth2
|
|
|
+ vars_lowerbound[naccth1 + i] = -0.05;//3度
|
|
|
+ vars_upperbound[naccth2 + i] = 0.05;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
//// constraints
|
|
|
Dvector constraints_lowerbound(n_constraints);
|
|
|
Dvector constraints_upperbound(n_constraints);
|
|
|
- for (int i = 0; i < n_constraints; i++) {
|
|
|
+ for (int i = 0; i < n_constraints; i++)
|
|
|
+ {
|
|
|
constraints_lowerbound[i] = 0;
|
|
|
constraints_upperbound[i] = 0;
|
|
|
}
|
|
|
|
|
|
- for (int i = 0; i < K; i++) {
|
|
|
+ for (int i = 0; i < K; i++)
|
|
|
+ {
|
|
|
////acc v1 v2
|
|
|
- constraints_lowerbound[nv1+i] = -5.0;
|
|
|
- constraints_upperbound[nv1+i] = 5.0;
|
|
|
- constraints_lowerbound[nv2+i] = -5.0;
|
|
|
- constraints_upperbound[nv2+i] = 5.0;
|
|
|
+ constraints_lowerbound[nv1 + i] = -3.0;
|
|
|
+ constraints_upperbound[nv1 + i] = 3.0;
|
|
|
+ constraints_lowerbound[nv2 + i - 2*(K-1)] = -3.0;
|
|
|
+ constraints_upperbound[nv2 + i - 2*(K-1)] = 3.0;
|
|
|
|
|
|
//acc th 1 2
|
|
|
- constraints_lowerbound[nvth1+i] = -2.5;
|
|
|
- constraints_upperbound[nvth1+i] = 2.5;
|
|
|
- constraints_lowerbound[nvth2+i] = -2.5;
|
|
|
- constraints_upperbound[nvth2+i] = 2.5;
|
|
|
+ constraints_lowerbound[nvth1 + i] = -0.1;
|
|
|
+ constraints_upperbound[nvth1 + i] = 0.1;
|
|
|
+ constraints_lowerbound[nvth2 + i - 2*(K-1)] = -0.1;
|
|
|
+ constraints_upperbound[nvth2 + i - 2*(K-1)] = 0.1;
|
|
|
+
|
|
|
+ // constraints_lowerbound[relative_pose_contrains + i - 4*(K-1)] = min_dist;
|
|
|
+ // constraints_upperbound[relative_pose_contrains + i - 4*(K-1)] = max_dist;
|
|
|
}
|
|
|
|
|
|
// object that computes objective and constraintsv
|
|
|
- FG_eval_2 fg_eval(coeffs,coeffs_b,state);
|
|
|
-
|
|
|
+ FG_eval_2 fg_eval(coeffs, coeffs_b, state);
|
|
|
|
|
|
std::string options;
|
|
|
options += "Integer print_level 0\n";
|
|
@@ -286,7 +319,6 @@ bool TowBotMPC::Solve(Eigen::VectorXd state, double max_v,Eigen::VectorXd coeffs
|
|
|
|
|
|
CppAD::ipopt::solve_result<Dvector> solution;
|
|
|
|
|
|
-
|
|
|
CppAD::ipopt::solve<Dvector, FG_eval_2>(
|
|
|
options, vars, vars_lowerbound, vars_upperbound, constraints_lowerbound,
|
|
|
constraints_upperbound, fg_eval, solution);
|
|
@@ -305,23 +337,24 @@ bool TowBotMPC::Solve(Eigen::VectorXd state, double max_v,Eigen::VectorXd coeffs
|
|
|
out.push_back(solution.x[nvth1]);
|
|
|
out.push_back(solution.x[nv2]);
|
|
|
out.push_back(solution.x[nvth2]);
|
|
|
- for (int i = 0; i < K; ++i) {
|
|
|
- tf::Pose pose1,pose2;
|
|
|
- pose1.setOrigin(tf::Vector3(solution.x[nx1+i],solution.x[ny1+i],0));
|
|
|
- pose2.setOrigin(tf::Vector3(solution.x[nx2+i],solution.x[ny2+i],0));
|
|
|
+ for (int i = 0; i < K; ++i)
|
|
|
+ {
|
|
|
+ tf::Pose pose1, pose2;
|
|
|
+ pose1.setOrigin(tf::Vector3(solution.x[nx1 + i], solution.x[ny1 + i], 0));
|
|
|
+ pose2.setOrigin(tf::Vector3(solution.x[nx2 + i], solution.x[ny2 + i], 0));
|
|
|
m_trajectory_f.push_back(pose1);
|
|
|
m_trajectory_b.push_back(pose2);
|
|
|
}
|
|
|
|
|
|
- /*printf("Cost : %.3f ref:%.3f cv : %.3f cvth : %.3f || v2:%.3f th2:%.3f\n",solution.obj_value,
|
|
|
- ref_v,v2,vth2,solution.x[nv2],solution.x[nvth2]);*/
|
|
|
+ // printf("Cost : %.3f ref:%.3f cv : %.3f cvth : %.3f || v2:%.3f th2:%.3f\n",solution.obj_value,
|
|
|
+ // ref_v,v2,vth2,solution.x[nv2],solution.x[nvth2]);
|
|
|
|
|
|
return ok;
|
|
|
-
|
|
|
}
|
|
|
|
|
|
TowBotMPC::TowBotMPC()
|
|
|
-{}
|
|
|
+{
|
|
|
+}
|
|
|
TowBotMPC::~TowBotMPC()
|
|
|
-{}
|
|
|
-
|
|
|
+{
|
|
|
+}
|