@@ -36,18 +36,18 @@ namespace simple_mpc
3636 auto space = MultibodyPhaseSpace (model_handler_.getModel ());
3737 auto rcost = CostStack (space, nv_);
3838
39- rcost.addCost (" state_cost" , QuadraticStateCost (space, nv_, model_handler_. getReferenceState () , settings_.w_x ));
39+ rcost.addCost (" state_cost" , QuadraticStateCost (space, nv_, x0_ , settings_.w_x ));
4040 rcost.addCost (" control_cost" , QuadraticControlCost (space, Eigen::VectorXd::Zero (nv_), settings_.w_u ));
4141
4242 FrameTranslationResidual frame_residual =
4343 FrameTranslationResidual (space.ndx (), nv_, model_handler_.getModel (), reach_pose, ee_id_);
4444 if (reaching)
4545 {
46- rcost.addCost (settings_. ee_name + " _cost " , QuadraticResidualCost (space, frame_residual, settings_.w_frame ));
46+ rcost.addCost (" frame_cost " , QuadraticResidualCost (space, frame_residual, settings_.w_frame ));
4747 }
4848 else
4949 {
50- rcost.addCost (settings_. ee_name + " _cost " , QuadraticResidualCost (space, frame_residual, settings_.w_frame ), 0 .);
50+ rcost.addCost (" frame_cost " , QuadraticResidualCost (space, frame_residual, settings_.w_frame ), 0 .);
5151 }
5252
5353 MultibodyFreeFwdDynamics ode = MultibodyFreeFwdDynamics (space, Eigen::MatrixXd::Identity (nv_, nv_));
@@ -63,8 +63,14 @@ namespace simple_mpc
6363 }
6464 if (settings_.kinematics_limits )
6565 {
66+ std::vector<int > state_id;
67+ for (int i = 0 ; i < nv_; i++)
68+ {
69+ state_id.push_back (i);
70+ }
6671 StateErrorResidual state_fn = StateErrorResidual (space, nv_, space.neutral ());
67- stm.addConstraint (state_fn, BoxConstraint (settings_.qmin , settings_.qmax ));
72+ FunctionSliceXpr state_slice = FunctionSliceXpr (state_fn, state_id);
73+ stm.addConstraint (state_slice, BoxConstraint (settings_.qmin , settings_.qmax ));
6874 }
6975
7076 return stm;
@@ -81,18 +87,43 @@ namespace simple_mpc
8187 return cs;
8288 }
8389
90+ CostStack * ArmDynamicsOCP::getTerminalCostStack ()
91+ {
92+ CostStack * cs = dynamic_cast <CostStack *>(&*problem_->term_cost_ );
93+
94+ return cs;
95+ }
96+
8497 void ArmDynamicsOCP::setReferencePose (const std::size_t t, const Eigen::Vector3d & pose_ref)
8598 {
8699 CostStack * cs = getCostStack (t);
87- QuadraticResidualCost * qrc = cs->getComponent <QuadraticResidualCost>(settings_. ee_name + " _cost " );
100+ QuadraticResidualCost * qrc = cs->getComponent <QuadraticResidualCost>(" frame_cost " );
88101 FrameTranslationResidual * cfr = qrc->getResidual <FrameTranslationResidual>();
89102 cfr->setReference (pose_ref);
90103 }
91104
92105 const Eigen::Vector3d ArmDynamicsOCP::getReferencePose (const std::size_t t)
93106 {
94107 CostStack * cs = getCostStack (t);
95- QuadraticResidualCost * qrc = cs->getComponent <QuadraticResidualCost>(settings_.ee_name + " _cost" );
108+ QuadraticResidualCost * qrc = cs->getComponent <QuadraticResidualCost>(" frame_cost" );
109+ FrameTranslationResidual * cfr = qrc->getResidual <FrameTranslationResidual>();
110+ Eigen::Vector3d ref = cfr->getReference ();
111+
112+ return ref;
113+ }
114+
115+ void ArmDynamicsOCP::setTerminalReferencePose (const Eigen::Vector3d & pose_ref)
116+ {
117+ CostStack * cs = getTerminalCostStack ();
118+ QuadraticResidualCost * qrc = cs->getComponent <QuadraticResidualCost>(" frame_cost" );
119+ FrameTranslationResidual * cfr = qrc->getResidual <FrameTranslationResidual>();
120+ cfr->setReference (pose_ref);
121+ }
122+
123+ const Eigen::Vector3d ArmDynamicsOCP::getTerminalReferencePose ()
124+ {
125+ CostStack * cs = getTerminalCostStack ();
126+ QuadraticResidualCost * qrc = cs->getComponent <QuadraticResidualCost>(" frame_cost" );
96127 FrameTranslationResidual * cfr = qrc->getResidual <FrameTranslationResidual>();
97128 Eigen::Vector3d ref = cfr->getReference ();
98129
@@ -119,6 +150,30 @@ namespace simple_mpc
119150 return qc->getTarget ();
120151 }
121152
153+ void ArmDynamicsOCP::setTerminalWeight (const std::string key, double weight)
154+ {
155+ CostStack * cs = getTerminalCostStack ();
156+ cs->setWeight (key, weight);
157+ }
158+
159+ double ArmDynamicsOCP::getTerminalWeight (const std::string key)
160+ {
161+ CostStack * cs = getTerminalCostStack ();
162+ return cs->getWeight (key);
163+ }
164+
165+ void ArmDynamicsOCP::setWeight (const std::size_t t, const std::string key, double weight)
166+ {
167+ CostStack * cs = getCostStack (t);
168+ cs->setWeight (key, weight);
169+ }
170+
171+ double ArmDynamicsOCP::getWeight (const std::size_t t, const std::string key)
172+ {
173+ CostStack * cs = getCostStack (t);
174+ return cs->getWeight (key);
175+ }
176+
122177 CostStack ArmDynamicsOCP::createTerminalCost ()
123178 {
124179 auto ter_space = MultibodyPhaseSpace (model_handler_.getModel ());
@@ -127,6 +182,10 @@ namespace simple_mpc
127182 term_cost.addCost (
128183 " state_cost" , QuadraticStateCost (ter_space, nv_, model_handler_.getReferenceState (), settings_.w_x ));
129184
185+ FrameTranslationResidual frame_residual =
186+ FrameTranslationResidual (ter_space.ndx (), nv_, model_handler_.getModel (), Eigen::Vector3d::Zero (), ee_id_);
187+ term_cost.addCost (" frame_cost" , QuadraticResidualCost (ter_space, frame_residual, settings_.w_frame ), 0.0 );
188+
130189 return term_cost;
131190 }
132191
0 commit comments