- 3.0.2 rigid body dynamics module.
FixBaseNLOC-impl.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4  **********************************************************************************************************************/
5 
6 #pragma once
7 
8 namespace ct {
9 namespace rbd {
10 
11 template <class FIX_BASE_FD_SYSTEM>
14  const typename NLOptConSolver::Settings_t& nlocSettings,
15  std::shared_ptr<FBSystem> system,
16  bool verbose,
17  std::shared_ptr<LinearizedSystem> linearizedSystem)
18  : system_(system),
19  linearizedSystem_(linearizedSystem),
20  costFunction_(costFun),
21  optConProblem_(system_, costFunction_, linearizedSystem_),
22  iteration_(0)
23 {
24  optConProblem_.verify();
25  nlocSolver_ = std::shared_ptr<NLOptConSolver>(new NLOptConSolver(optConProblem_, nlocSettings));
26 }
27 
28 template <class FIX_BASE_FD_SYSTEM>
34  const typename NLOptConSolver::Settings_t& nlocSettings,
35  std::shared_ptr<FBSystem> system,
36  bool verbose,
37  std::shared_ptr<LinearizedSystem> linearizedSystem)
38  : system_(system),
39  linearizedSystem_(linearizedSystem),
40  inputBoxConstraints_(inputBoxConstraints),
41  stateBoxConstraints_(stateBoxConstraints),
42  generalConstraints_(generalConstraints),
43  costFunction_(costFun),
44  optConProblem_(system_, costFunction_, linearizedSystem_),
45  iteration_(0)
46 {
47  if (inputBoxConstraints_ != nullptr)
48  optConProblem_.setInputBoxConstraints(inputBoxConstraints_);
49  if (stateBoxConstraints_ != nullptr)
50  optConProblem_.setStateBoxConstraints(stateBoxConstraints_);
51  if (generalConstraints_ != nullptr)
52  optConProblem_.setGeneralConstraints(generalConstraints_);
53 
54  optConProblem_.verify();
55  nlocSolver_ = std::shared_ptr<NLOptConSolver>(new NLOptConSolver(optConProblem_, nlocSettings));
56 }
57 
58 template <class FIX_BASE_FD_SYSTEM>
60  const core::Time& tf,
61  StateVectorArray x_ref,
62  FeedbackArray u0_fb,
63  ControlVectorArray u0_ff)
64 {
65  typename NLOptConSolver::Policy_t policy(x_ref, u0_ff, u0_fb, getSettings().dt);
66 
67  nlocSolver_->changeTimeHorizon(tf);
68  nlocSolver_->setInitialGuess(policy);
69  nlocSolver_->changeInitialState(x0.toImplementation());
70 }
71 
72 template <class FIX_BASE_FD_SYSTEM>
74  const core::Time& tf,
75  const int N,
76  ControlVector& u_ref,
78 {
79  ControlVector uff_torque = system_->computeIDTorques(x0.joints());
80 
81  if (ACTUATOR_STATE_DIM > 0) // if there are actuator dynamics
82  {
83  u_ref.setZero(); // TODO compute this from act.state
84  }
85  else // actuator dynamics off
86  {
87  u_ref = uff_torque;
88  }
89 
90  // transcribe uff into the feed-forward init guess
91  ControlVectorArray u0_ff(N, u_ref);
92  StateVectorArray x_ref = StateVectorArray(N + 1, x0.toStateVector());
93  FeedbackArray u0_fb(N, K);
94  typename NLOptConSolver::Policy_t policy(x_ref, u0_ff, u0_fb, getSettings().dt);
95 
96  nlocSolver_->changeTimeHorizon(tf);
97  nlocSolver_->setInitialGuess(policy);
98  nlocSolver_->changeInitialState(x_ref.front());
99 }
100 
101 template <class FIX_BASE_FD_SYSTEM>
103  const RobotState_t& xf,
104  const core::Time& tf,
105  const int N,
106  FeedbackMatrix K)
107 {
110 
111  initializeDirectInterpolation(x0, xf, tf, N, uff_array, x_array, K);
112 }
113 
114 
115 template <class FIX_BASE_FD_SYSTEM>
117  const RobotState_t& xf,
118  const core::Time& tf,
119  const int N,
122  FeedbackMatrix K)
123 {
124  uff_array.resize(N);
125  x_array.resize(N + 1);
126 
127  // temporary variables
128  ControlVector uff_torque;
129 
130  for (int i = 0; i < N + 1; i++)
131  {
132  RobotState_t state_temp = x0;
133  state_temp.joints().toImplementation() =
134  x0.joints().toImplementation() +
135  (xf.joints().toImplementation() - x0.joints().toImplementation()) * SCALAR(i) / SCALAR(N);
136 
137  if (i < N)
138  {
139  uff_torque = system_->computeIDTorques(state_temp.joints());
140  if (ACTUATOR_STATE_DIM > 0) // if there are actuator dynamics
141  {
142  uff_array[i].setZero(); // todo compute this from act.state
143  }
144  else // actuator dynamics off
145  {
146  uff_array[i] = uff_torque;
147  }
148  }
149 
150  if (i > 0 && ACTUATOR_STATE_DIM > 0)
151  {
152  // direct interpolation for actuator state may not make sense. Improve using actuator model
153  state_temp.actuatorState() =
154  system_->getActuatorDynamics()->computeStateFromOutput(state_temp.joints(), uff_torque);
155  }
156 
157  x_array[i] = state_temp.toStateVector();
158  }
159 
160  FeedbackArray u0_fb(N, K);
161 
162  typename NLOptConSolver::Policy_t policy(x_array, uff_array, u0_fb, getSettings().dt);
163 
164  std::cout << "init with " << x0.toStateVector() << std::endl;
165 
166  nlocSolver_->changeInitialState(x0.toStateVector());
167  nlocSolver_->changeTimeHorizon(tf);
168  nlocSolver_->setInitialGuess(policy);
169 }
170 
171 template <class FIX_BASE_FD_SYSTEM>
173 {
174  bool foundBetter = nlocSolver_->runIteration();
175 
176  iteration_++;
177  return foundBetter;
178 }
179 
180 
181 template <class FIX_BASE_FD_SYSTEM>
183 {
184  return nlocSolver_->solve();
185 }
186 
187 template <class FIX_BASE_FD_SYSTEM>
189 {
190  return nlocSolver_->getSolution();
191 }
192 
193 template <class FIX_BASE_FD_SYSTEM>
195 {
196  return nlocSolver_->getStateTrajectory().getTimeArray();
197 }
198 
199 template <class FIX_BASE_FD_SYSTEM>
201 {
202  return nlocSolver_->getSolution().K();
203 }
204 
205 template <class FIX_BASE_FD_SYSTEM>
208 {
209  return nlocSolver_->getSolution().uff();
210 }
211 
212 template <class FIX_BASE_FD_SYSTEM>
214 {
215  return nlocSolver_->getSolution().x_ref();
216 }
217 
218 template <class FIX_BASE_FD_SYSTEM>
221 {
222  return nlocSolver_->getSettings();
223 }
224 
225 template <class FIX_BASE_FD_SYSTEM>
226 void FixBaseNLOC<FIX_BASE_FD_SYSTEM>::changeCostFunction(std::shared_ptr<CostFunction> costFunction)
227 {
228  nlocSolver_->changeCostFunction(costFunction);
229 }
230 
231 template <class FIX_BASE_FD_SYSTEM>
232 std::shared_ptr<typename FixBaseNLOC<FIX_BASE_FD_SYSTEM>::NLOptConSolver> FixBaseNLOC<FIX_BASE_FD_SYSTEM>::getSolver()
233 {
234  return nlocSolver_;
235 }
236 
237 } // namespace rbd
238 } // namespace ct
void initializeDirectInterpolation(const RobotState_t &x0, const RobotState_t &xf, const core::Time &tf, const int N, FeedbackMatrix K=FeedbackMatrix::Zero())
initialize fixed-base robot with a directly interpolated state trajectory and corresponding ID torque...
Definition: FixBaseNLOC-impl.h:102
void initialize(const RobotState_t &x0, const core::Time &tf, StateVectorArray x_ref=StateVectorArray(), FeedbackArray u0_fb=FeedbackArray(), ControlVectorArray u0_ff=ControlVectorArray())
Definition: FixBaseNLOC-impl.h:59
const NLOptConSolver::Settings_t & getSettings() const
Definition: FixBaseNLOC-impl.h:220
std::enable_if<(ACT_STATE_DIM==0), T >::type toStateVector() const
conversion from RobotState to dense state vector
Definition: FixBaseRobotState.h:129
bool runIteration()
Definition: FixBaseNLOC-impl.h:172
const StateVectorArray & getStateVectorArray()
Definition: FixBaseNLOC-impl.h:213
NLOCAlgorithm_t::Policy_t Policy_t
std::shared_ptr< NLOptConSolver > getSolver()
Definition: FixBaseNLOC-impl.h:232
void initializeSteadyPose(const RobotState_t &x0, const core::Time &tf, const int N, ControlVector &u_ref, FeedbackMatrix K=FeedbackMatrix::Zero())
initialize fixed-base robot with a steady pose using inverse dynamics torques as feedforward ...
Definition: FixBaseNLOC-impl.h:73
const double dt
typename FBSystem::SCALAR SCALAR
Definition: FixBaseNLOC.h:30
actuator_state_vector_t & actuatorState()
accessor to actuator state
Definition: FixBaseRobotState.h:94
FixBaseNLOC()=default
default constructor
typename core::FeedbackArray< STATE_DIM, CONTROL_DIM, SCALAR > FeedbackArray
Definition: FixBaseNLOC.h:44
bool verbose
Definition: rbdJITtests.h:15
typename core::StateVectorArray< STATE_DIM, SCALAR > StateVectorArray
Definition: FixBaseNLOC.h:42
for i
typename core::StateFeedbackController< STATE_DIM, CONTROL_DIM, SCALAR > StateFeedbackController
Definition: FixBaseNLOC.h:45
const StateFeedbackController & getSolution()
Definition: FixBaseNLOC-impl.h:188
ct::optcon::NLOptConSolver< STATE_DIM, CONTROL_DIM, STATE_DIM/2, STATE_DIM/2, SCALAR > NLOptConSolver
@ todo: introduce templates for P_DIM and V_DIM
Definition: FixBaseNLOC.h:36
bool solve()
Definition: FixBaseNLOC-impl.h:182
const FeedbackArray & getFeedbackArray()
Definition: FixBaseNLOC-impl.h:200
typename core::ControlVector< CONTROL_DIM, SCALAR > ControlVector
Definition: FixBaseNLOC.h:40
whole fix base robot state, i.e. Joint state, Actuator Dynamics (and fix-base pose) ...
Definition: FixBaseRobotState.h:26
static const size_t ACTUATOR_STATE_DIM
Definition: FixBaseNLOC.h:29
joint_state_vector_t & toImplementation()
Definition: JointState.h:159
typename core::FeedbackMatrix< STATE_DIM, CONTROL_DIM, SCALAR > FeedbackMatrix
Definition: FixBaseNLOC.h:41
void changeCostFunction(std::shared_ptr< CostFunction > costFunction)
Definition: FixBaseNLOC-impl.h:226
typename core::ControlVectorArray< CONTROL_DIM, SCALAR > ControlVectorArray
Definition: FixBaseNLOC.h:43
const ControlVectorArray & getControlVectorArray()
Definition: FixBaseNLOC-impl.h:207
JointState_t & joints()
accessor to joint state
Definition: FixBaseRobotState.h:102
double Time
const core::TimeArray & getTimeArray()
Definition: FixBaseNLOC-impl.h:194