3 #include "ros/console.h"
11 using drake::solvers::MatrixXDecisionVariable;
19 decision_variables.
position_left = prog.NewContinuousVariables(n_steps + 1, 3);
21 decision_variables.
position_right = prog.NewContinuousVariables(n_steps + 1, 3);
23 decision_variables.
stone_left = prog.NewBinaryVariables(n_steps + 1, n_stones,
"b");
25 decision_variables.
stone_right = prog.NewBinaryVariables(n_steps + 1, n_stones,
"b");
27 drake::solvers::MatrixDecisionVariable<1,1> first_left_M = prog.NewBinaryVariables(1, 1,
"b");
29 decision_variables.
first_left = first_left_M(0,0);
31 return decision_variables;
36 MatrixXDecisionVariable &position_left = decision_variables.
position_left;
38 MatrixXDecisionVariable &position_right = decision_variables.
position_right;
55 assert(position_left.cols() == initial_position_left.rows() && initial_position_left.cols() == 1);
57 prog.AddLinearConstraint(position_left.row(0).transpose() == initial_position_left);
59 assert(position_right.cols() == initial_position_right.rows() && initial_position_right.cols() == 1);
61 prog.AddLinearConstraint(position_right.row(0).transpose() == initial_position_right);
64 assert(position_left.cols() == goal_position_left.rows() && goal_position_left.cols() == 1);
66 prog.AddLinearConstraint(position_left.row(position_left.rows() - 1).transpose() == goal_position_left);
68 assert(position_right.cols() == goal_position_right.rows() && goal_position_right.cols() == 1);
70 prog.AddLinearConstraint(position_right.row(position_right.rows() - 1).transpose() == goal_position_right);
75 MatrixXDecisionVariable &position_left = decision_variables.
position_left;
77 MatrixXDecisionVariable &position_right = decision_variables.
position_right;
81 for (
int t = 0;
t < n_steps; ++
t)
83 prog.AddLinearConstraint(position_left.block(
t + 1, 0, 1, 2).transpose() - position_right.block(
t, 0, 1, 2).transpose() <= Eigen::Vector2d::Constant(
step_span/2));
85 prog.AddLinearConstraint(position_right.block(
t + 1, 0, 1, 2).transpose() - position_left.block(
t, 0, 1, 2).transpose() <= Eigen::Vector2d::Constant(
step_span/2));
87 prog.AddLinearConstraint(position_left(
t + 1, 2) - position_right(
t, 2) <= step_height);
89 prog.AddLinearConstraint(position_right(
t + 1, 2) - position_left(
t, 2) <= step_height);
95 MatrixXDecisionVariable &position_left = decision_variables.
position_left;
97 MatrixXDecisionVariable &position_right = decision_variables.
position_right;
99 drake::solvers::DecisionVariable &first_left = decision_variables.
first_left;
101 drake::symbolic::Expression first_right = 1 - first_left;
103 Eigen::Vector2d step_limit = Eigen::Vector2d::Constant(
step_span);
105 Eigen::Matrix<drake::symbolic::Expression, 3, 1> step_left, step_right;
107 Eigen::Matrix<drake::symbolic::Expression, 3, 1> limit_left, limit_right;
109 for (
int t = 0;
t < n_steps; ++
t)
111 step_left = position_left.row(
t + 1).transpose() - position_left.row(
t).transpose();
113 step_right = position_right.row(
t + 1).transpose() - position_right.row(
t).transpose();
127 prog.AddLinearConstraint(step_left <= limit_left);
129 prog.AddLinearConstraint(step_left >= -limit_left);
131 prog.AddLinearConstraint(step_right <= limit_right);
133 prog.AddLinearConstraint(step_right >= -limit_right);
139 MatrixXDecisionVariable stone_left = decision_variables.
stone_left;
141 MatrixXDecisionVariable stone_right = decision_variables.
stone_right;
143 Eigen::VectorXi one_vec = Eigen::VectorXi::Ones(stone_left.rows());
145 prog.AddLinearConstraint(stone_left*Eigen::VectorXd::Ones(stone_left.cols()) == Eigen::VectorXd::Ones(stone_left.rows()));
147 prog.AddLinearConstraint(stone_right*Eigen::VectorXd::Ones(stone_right.cols()) == Eigen::VectorXd::Ones(stone_right.rows()));
169 MatrixXDecisionVariable &position_left = decision_variables.
position_left;
171 MatrixXDecisionVariable &position_right = decision_variables.
position_right;
173 MatrixXDecisionVariable &stone_left = decision_variables.
stone_left;
175 MatrixXDecisionVariable &stone_right = decision_variables.
stone_right;
181 Eigen::MatrixXd A_ineq, b_ineq, A_eq, b_eq;
183 drake::solvers::DecisionVariable d_l, d_r;
185 for (
int t = 0;
t < n_steps; ++
t)
187 for (
int i = 0; i < n_stones; ++i)
197 d_l = stone_left(
t,i);
199 d_r = stone_right(
t,i);
201 prog.AddLinearConstraint(A_ineq*position_left.row(
t).transpose() <= b_ineq + (1 - d_l)*M);
203 prog.AddLinearConstraint(A_ineq*position_right.row(
t).transpose() <= b_ineq + (1 - d_r)*M);
205 prog.AddLinearConstraint(A_eq*position_left.row(
t).transpose() <= b_eq + (1 - d_l)*M);
207 prog.AddLinearConstraint(A_eq*position_left.row(
t).transpose() >= b_eq - (1 - d_l)*M);
209 prog.AddLinearConstraint(A_eq*position_right.row(
t).transpose() <= b_eq + (1 - d_r)*M);
211 prog.AddLinearConstraint(A_eq*position_right.row(
t).transpose() >= b_eq - (1 - d_r)*M);
220 MatrixXDecisionVariable &position_left = decision_variables.
position_left;
222 MatrixXDecisionVariable &position_right = decision_variables.
position_right;
224 Eigen::Matrix<drake::symbolic::Expression, 3, 1> dist_l;
226 Eigen::Matrix<drake::symbolic::Expression, 3, 1> dist_r;
228 drake::symbolic::Expression res;
230 for (
int t = 0;
t < n_steps; ++
t)
232 dist_l = position_left.row(
t + 1).transpose() - position_left.row(
t).transpose();
234 dist_r = position_right.row(
t + 1).transpose() - position_right.row(
t).transpose();
236 res = (dist_l.transpose()*dist_l + dist_r.transpose()*dist_r).eval()(0);
238 prog.AddQuadraticCost(res);
244 std::ofstream file(filename);
252 ROS_INFO(
"writeMatToFile: Successfully wrote to file");
255 ROS_ERROR(
"could not open file");
257 throw "could not open file " + filename;
265 std::string fend =
".csv";
const Eigen::Vector3d & getCenter() const
const Eigen::Vector3d & getTopRight() const
const SteppingStone & getStoneByName(std::string name) const
const Eigen::Array< SteppingStone, Eigen::Dynamic, 1 > & getSteppingStones() const
void foot_in_stepping_stone(drake::solvers::MathematicalProgram &prog, Terrain &terrain, int n_steps, DecVars &decision_variables)
void relative_position_limits(drake::solvers::MathematicalProgram &prog, int n_steps, double step_span, DecVars &decision_variables)
void set_initial_and_goal_position(drake::solvers::MathematicalProgram &prog, Terrain &terrain, DecVars &decision_variables)
void writeMatToFile(Eigen::MatrixXd &mat, std::string filename)
DecVars add_decision_variables(drake::solvers::MathematicalProgram &prog, Terrain &terrain, int n_steps)
Eigen::Vector4d get_big_M(Terrain terrain)
void minimize_step_length(drake::solvers::MathematicalProgram &prog, int n_steps, DecVars &decision_variables)
void step_sequence(drake::solvers::MathematicalProgram &prog, int n_steps, double step_span, DecVars &decision_variables)
void writeDecVarsToFile(DecVars_res &decision_variables, std::string base_name="footstep_planner")
void one_stone_per_foot(drake::solvers::MathematicalProgram &prog, int n_steps, DecVars &decision_variables)
Eigen::MatrixXd position_left
Eigen::MatrixXd position_right
Eigen::MatrixXd stone_right
Eigen::MatrixXd stone_left
drake::solvers::DecisionVariable first_left
MatrixXDecisionVariable stone_right
MatrixXDecisionVariable position_left
MatrixXDecisionVariable stone_left
MatrixXDecisionVariable position_right