cartpole_dynamics_solver.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2019, Traiko Dinev
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
31 
33 
34 namespace exotica
35 {
37 {
38  num_positions_ = 2;
39  num_velocities_ = 2;
40  num_controls_ = 1;
42 }
43 
45 {
46  const int num_positions_in = scene_in->GetKinematicTree().GetNumControlledJoints();
47  // TODO: This is a terrible check (not against name etc.), but can stop _some_ mismatches between URDF/model and dynamics
48  if (num_positions_in != 2)
49  ThrowPretty("Robot model may not be a Cartpole.");
50 }
51 
52 Eigen::VectorXd CartpoleDynamicsSolver::f(const StateVector& x, const ControlVector& u)
53 {
54  const double& theta = x(1);
55  const double& xdot = x(2);
56  const double& thetadot = x(3);
57 
58  auto sin_theta = std::sin(theta);
59  auto cos_theta = std::cos(theta);
60  auto theta_dot_squared = thetadot * thetadot;
61 
62  auto x_dot = StateVector(4);
63  x_dot << xdot, thetadot,
64  (u(0) + m_p_ * sin_theta * (l_ * theta_dot_squared + g_ * cos_theta)) /
65  (m_c_ + m_p_ * sin_theta * sin_theta),
66  -(l_ * m_p_ * cos_theta * sin_theta * theta_dot_squared + u(0) * cos_theta +
67  (m_c_ + m_p_) * g_ * sin_theta) /
68  (l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta);
69 
70  return x_dot;
71 }
72 
73 // NOTE: tested in test/test_cartpole_diff.py in this package
74 Eigen::MatrixXd CartpoleDynamicsSolver::fx(const StateVector& x, const ControlVector& u)
75 {
76  const double& theta = x(1);
77  const double& tdot = x(3);
78 
79  auto sin_theta = std::sin(theta);
80  auto cos_theta = std::cos(theta);
81 
82  Eigen::Matrix4d fx;
83  fx << 0, 0, 1, 0,
84  0, 0, 0, 1,
85  //
86  0,
87  -2 * m_p_ * (m_p_ * (g_ * cos_theta + l_ * tdot * tdot) * sin_theta + u(0)) * sin_theta * cos_theta / std::pow(m_c_ + m_p_ * sin_theta * sin_theta, 2) + (-g_ * m_p_ * sin_theta * sin_theta + m_p_ * (g_ * cos_theta + l_ * tdot * tdot) * cos_theta) / (m_c_ + m_p_ * sin_theta * sin_theta),
88  0,
89  2 * l_ * m_p_ * tdot * sin_theta / (m_c_ + m_p_ * sin_theta * sin_theta),
90  //
91  0,
92  -2 * l_ * m_p_ * (-g_ * (m_c_ + m_p_) * sin_theta - l_ * m_p_ * tdot * tdot * sin_theta * cos_theta - u(0) * cos_theta) * sin_theta * cos_theta / std::pow(l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta, 2) + (-g_ * (m_c_ + m_p_) * cos_theta + l_ * m_p_ * tdot * tdot * sin_theta * sin_theta - l_ * m_p_ * tdot * tdot * cos_theta * cos_theta + u(0) * sin_theta) / (l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta),
93  0,
94  -2 * l_ * m_p_ * tdot * sin_theta * cos_theta / (l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta);
95 
96  return fx;
97 }
98 
99 // NOTE: tested in test/test_cartpole_diff.py in this package
100 Eigen::MatrixXd CartpoleDynamicsSolver::fu(const StateVector& x, const ControlVector& u)
101 {
102  const double& theta = x(1);
103 
104  auto sin_theta = std::sin(theta);
105  auto cos_theta = std::cos(theta);
106 
107  Eigen::Vector4d fu;
108  fu << 0, 0, 1 / (m_c_ + m_p_ * sin_theta * sin_theta), -cos_theta / (l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta);
109  return fu;
110 }
111 
112 // NOTE: Code to generate 2nd order dynamics is in scripts/gen_second_order_dynamics.py
113 // fxx -> NX x NX x NX
114 // middle dimension is always NX
115 // NX = 4
116 // NU = 1
117 Eigen::Tensor<double, 3> CartpoleDynamicsSolver::fxx(const StateVector& x, const ControlVector& u)
118 {
119  const double& theta = x(1);
120  const double& tdot = x(3);
121 
122  auto sin_theta = std::sin(theta);
123  auto cos_theta = std::cos(theta);
124 
126  fxx.setValues({{{0, 0, 0, 0},
127  {0, 0, 0, 0},
128  {0, 0, 0, 0},
129  {0, 0, 0, 0}},
130  {{0, 0, 0, 0},
131  {0, 0, 0, 0},
132  {0,
133  8 * m_p_ * m_p_ * (m_p_ * (g_ * cos_theta + l_ * tdot * tdot) * sin_theta + u(0)) * sin_theta * sin_theta * cos_theta * cos_theta /
134  std::pow(m_c_ + m_p_ * sin_theta * sin_theta, 3) -
135  4 * m_p_ * (-g_ * m_p_ * sin_theta * sin_theta + m_p_ * (g_ * cos_theta + l_ * tdot * tdot) * cos_theta) * sin_theta * cos_theta / std::pow(m_c_ + m_p_ * sin_theta * sin_theta, 2) +
136  2 * m_p_ * (m_p_ * (g_ * cos_theta + l_ * tdot * tdot) * sin_theta + u(0)) * sin_theta * sin_theta / std::pow(m_c_ + m_p_ * sin_theta * sin_theta, 2) - 2 * m_p_ * (m_p_ * (g_ * cos_theta + l_ * tdot * tdot) * sin_theta + u(0)) * cos_theta * cos_theta / std::pow(m_c_ + m_p_ * sin_theta * sin_theta, 2) + (-3 * g_ * m_p_ * sin_theta * cos_theta - m_p_ * (g_ * cos_theta + l_ * tdot * tdot) * sin_theta) / (m_c_ + m_p_ * sin_theta * sin_theta),
137  0,
138  -4 * l_ * m_p_ * m_p_ * tdot * sin_theta * sin_theta * cos_theta / std::pow(m_c_ + m_p_ * sin_theta * sin_theta, 2) + 2 * l_ * m_p_ * tdot * cos_theta / (m_c_ + m_p_ * sin_theta * sin_theta)},
139  {0,
140  8 * l_ * l_ * m_p_ * m_p_ * (-g_ * (m_c_ + m_p_) * sin_theta - l_ * m_p_ * tdot * tdot * sin_theta * cos_theta - u(0) * cos_theta) * sin_theta * sin_theta * cos_theta * cos_theta / std::pow(l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta, 3) + 2 * l_ * m_p_ * (-g_ * (m_c_ + m_p_) * sin_theta - l_ * m_p_ * tdot * tdot * sin_theta * cos_theta - u(0) * cos_theta) * sin_theta * sin_theta / std::pow(l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta, 2) - 2 * l_ * m_p_ * (-g_ * (m_c_ + m_p_) * sin_theta - l_ * m_p_ * tdot * tdot * sin_theta * cos_theta - u(0) * cos_theta) * cos_theta * cos_theta / std::pow(l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta, 2) - 4 * l_ * m_p_ * (-g_ * (m_c_ + m_p_) * cos_theta + l_ * m_p_ * tdot * tdot * sin_theta * sin_theta - l_ * m_p_ * tdot * tdot * cos_theta * cos_theta + u(0) * sin_theta) * sin_theta * cos_theta / std::pow(l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta, 2) + (g_ * (m_c_ + m_p_) * sin_theta + 4 * l_ * m_p_ * tdot * tdot * sin_theta * cos_theta + u(0) * cos_theta) / (l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta),
141  0,
142  4 * l_ * l_ * m_p_ * m_p_ * tdot * sin_theta * sin_theta * cos_theta * cos_theta / std::pow(l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta, 2) + 2 * l_ * m_p_ * tdot * sin_theta * sin_theta / (l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta) - 2 * l_ * m_p_ * tdot * cos_theta * cos_theta / (l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta)}},
143  {{0, 0, 0, 0},
144  {0, 0, 0, 0},
145  {0, 0, 0, 0},
146  {0, 0, 0, 0}},
147  {{0, 0, 0, 0},
148  {0, 0, 0, 0},
149  {0,
150  -4 * l_ * m_p_ * m_p_ * tdot * sin_theta * sin_theta * cos_theta / std::pow(m_c_ + m_p_ * sin_theta * sin_theta, 2) + 2 * l_ * m_p_ * tdot * cos_theta / (m_c_ + m_p_ * sin_theta * sin_theta),
151  0, 2 * l_ * m_p_ * sin_theta / (m_c_ + m_p_ * sin_theta * sin_theta)},
152  {0,
153  4 * l_ * l_ * m_p_ * m_p_ * tdot * sin_theta * sin_theta * cos_theta * cos_theta / std::pow(l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta, 2) +
154  (2 * l_ * m_p_ * tdot * sin_theta * sin_theta - 2 * l_ * m_p_ * tdot * cos_theta * cos_theta) / (l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta),
155  0, -2 * l_ * m_p_ * sin_theta * cos_theta / (l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta)}}});
156  return fxx;
157 }
158 
159 Eigen::Tensor<double, 3> CartpoleDynamicsSolver::fxu(const StateVector& x, const ControlVector& u)
160 {
161  const double& theta = x(1);
162 
163  auto sin_theta = std::sin(theta);
164  auto cos_theta = std::cos(theta);
165 
167  fxu.setValues({{{0, 0, 0, 0},
168  {0, 0, 0, 0},
169  {0, -2 * m_p_ * sin_theta * cos_theta / std::pow(m_c_ + m_p_ * sin_theta * sin_theta, 2), 0, 0},
170  {0, 2 * l_ * m_p_ * sin_theta * cos_theta * cos_theta / std::pow(l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta, 2) + sin_theta / (l_ * m_c_ + l_ * m_p_ * sin_theta * sin_theta),
171  0, 0}}});
172  return fxu;
173 }
174 
175 } // namespace exotica
exotica::CartpoleDynamicsSolver::AssignScene
void AssignScene(ScenePtr scene_in) override
Definition: cartpole_dynamics_solver.cpp:44
exotica::CartpoleDynamicsSolver::m_p_
double m_p_
!< Cart mass (kg)
Definition: cartpole_dynamics_solver.h:73
exotica::CartpoleDynamicsSolver::fu
Eigen::MatrixXd fu(const StateVector &x, const ControlVector &u) override
Computes the dynamics derivative w.r.t .the control input u.
Definition: cartpole_dynamics_solver.cpp:100
exotica::AbstractDynamicsSolver::StateVector
Eigen::Matrix< T, NX, 1 > StateVector
exotica::CartpoleDynamicsSolver::fx
Eigen::MatrixXd fx(const StateVector &x, const ControlVector &u) override
Computes the dynamics derivative w.r.t .the state x.
Definition: cartpole_dynamics_solver.cpp:74
exotica::CartpoleDynamicsSolver::fxx
Eigen::Tensor< double, 3 > fxx(const StateVector &x, const ControlVector &u) override
Definition: cartpole_dynamics_solver.cpp:117
exotica::AbstractDynamicsSolver::num_positions_
int num_positions_
REGISTER_DYNAMICS_SOLVER_TYPE
#define REGISTER_DYNAMICS_SOLVER_TYPE(TYPE, DERIV)
gen_second_order_dynamics.xdot
xdot
Definition: gen_second_order_dynamics.py:6
exotica::CartpoleDynamicsSolver::f
StateVector f(const StateVector &x, const ControlVector &u) override
Computes the forward dynamics of the system.
Definition: cartpole_dynamics_solver.cpp:52
exotica
gen_second_order_dynamics.u
u
Definition: gen_second_order_dynamics.py:7
exotica::AbstractDynamicsSolver::has_second_order_derivatives_
bool has_second_order_derivatives_
exotica::ScenePtr
std::shared_ptr< Scene > ScenePtr
cartpole_dynamics_solver.h
test_cartpole_diff.cos
cos
Definition: test_cartpole_diff.py:8
exotica::CartpoleDynamicsSolver::CartpoleDynamicsSolver
CartpoleDynamicsSolver()
Definition: cartpole_dynamics_solver.cpp:36
exotica::AbstractDynamicsSolver::ControlVector
Eigen::Matrix< T, NU, 1 > ControlVector
exotica::CartpoleDynamicsSolver::fxu
Eigen::Tensor< double, 3 > fxu(const StateVector &x, const ControlVector &u) override
Definition: cartpole_dynamics_solver.cpp:159
exotica::CartpoleDynamicsSolver
Definition: cartpole_dynamics_solver.h:43
ThrowPretty
#define ThrowPretty(m)
exotica::CartpoleDynamicsSolver::l_
double l_
!< Pole mass (kg)
Definition: cartpole_dynamics_solver.h:74
gen_second_order_dynamics.theta
theta
Definition: gen_second_order_dynamics.py:4
exotica::CartpoleDynamicsSolver::g_
double g_
Definition: cartpole_dynamics_solver.h:71
exotica::CartpoleDynamicsSolver::m_c_
double m_c_
!< Gravity (m/s^2)
Definition: cartpole_dynamics_solver.h:72
x
double x
exotica::AbstractDynamicsSolver::num_controls_
int num_controls_
test_cartpole_diff.sin
sin
Definition: test_cartpole_diff.py:7
exotica::AbstractDynamicsSolver::num_velocities_
int num_velocities_
gen_second_order_dynamics.tdot
tdot
Definition: gen_second_order_dynamics.py:5


exotica_cartpole_dynamics_solver
Author(s): Traiko Dinev
autogenerated on Fri Aug 2 2024 08:44:05