dynamics.cpp
Go to the documentation of this file.
00001 #include <mpc/dynamics.h>
00002 
00003 namespace MPC {
00004 
00005         bool setDynamicFunction(Dynamics f, DynamicsD fd) {
00006                 dyn = f;
00007                 dynd = fd;
00008 
00009                 return true;
00010         }
00011 
00012         bool setCostFunction(CostFunction f, CostFunctionD fd) {
00013                 cost = f;
00014                 costd = fd;
00015 
00016                 return true;
00017         }
00018 
00019         float dynamicsAndCost(const VectorT &x, const VectorT &u) {
00020                 // Compute cost only
00021                 return cost(x, u);
00022         }
00023 
00024         void dynamicsAndCost(const VectorT &x, const VectorT &u, VectorT &xNew, float &cNew) {
00025                 dyn(x, u, xNew);
00026                 cNew = cost(x, u);
00027         }
00028 
00029         void dynamicsAndCost(const VectorT &x, const VectorT &u, MatrixT::ColXpr xNew, float &cNew) {
00030                 // TODO: remove this workaround with the temporary vector!
00031                 VectorT tmp;
00032                 dyn(x, u, tmp);
00033                 xNew = tmp;
00034                 cNew = cost(x, u);
00035         }
00036 
00037         void dynamicsAndCost(Trajectory &t) {
00038                 int N = t.x.cols();
00039                 VectorT tmp;
00040                 t.cost.resize(N);
00041                 if (not t.started) {
00042                         // just update trajectory cost
00043                         for (int i = 0; i < N; ++i) {
00044                                 t.cost(i) = cost(t.x.col(i), t.u.col(i));
00045                         }
00046                 } else {
00047                         // if we have some actions also compute dynamics
00048                         for (int i = 0; i < N; ++i) {
00049                                 if (i < N-1) {
00050                                         dyn(t.x.col(i), t.u.col(i), tmp);
00051                                         t.x.col(i+1) = tmp;
00052                                 }
00053                                 t.cost(i) = cost(t.x.col(i), t.u.col(i));
00054                         }
00055                 }
00056         }
00057 
00058         void dynamicsAndCostDerivative(Trajectory &t, TrajectoryDeriv &d) {
00059                 int N = t.u.cols();
00060                 assert(N == (int)d.size() - 1);
00061                 // zero action in the last step
00062                 VectorT uN(VectorT::Zero(t.u.rows()));
00063                 dynd(t.x.col(N), uN, d[N]);
00064                 costd(t.x.col(N), uN, d[N]);
00065                 for (int i = N-1; i >= 0; --i) {
00066                         // compute dynamics derivative first
00067                         dynd(t.x.col(i), t.u.col(i), d[i]);
00068                         costd(t.x.col(i), t.u.col(i), d[i]);
00069                 }
00070         }
00071 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


mpc
Author(s): Jost Tobias Springenberg
autogenerated on Wed Dec 26 2012 16:21:13