sot-talos-balance  2.0.5
Collection of dynamic-graph entities aimed at implementing balance control on talos.
simple-state-integrator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018, Gepetto team, LAAS-CNRS
3  *
4  * This file is part of sot-talos-balance.
5  * sot-talos-balance is free software: you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation, either version 3 of
8  * the License, or (at your option) any later version.
9  * sot-talos-balance is distributed in the hope that it will be
10  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
11  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details. You should
13  * have received a copy of the GNU Lesser General Public License along
14  * with sot-talos-balance. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
18 
19 #include <dynamic-graph/all-commands.h>
20 #include <dynamic-graph/factory.h>
21 
22 #include <Eigen/Core>
23 #include <cmath>
24 #include <pinocchio/multibody/liegroup/special-euclidean.hpp>
25 #include <sot/core/debug.hh>
26 #include <sot/core/stop-watch.hh>
27 // #include "pinocchio/math/quaternion.hpp"
28 
29 namespace dynamicgraph {
30 namespace sot {
31 namespace talos_balance {
32 
33 namespace dg = ::dynamicgraph;
34 using namespace dg;
35 using namespace dg::command;
36 
37 // Size to be aligned "-------------------------------------------------------"
38 #define PROFILE_SIMPLE_STATE_INTEGRATOR_COMPUTATION \
39  "SimpleStateIntegrator computation "
40 
41 #define INPUT_SIGNALS m_controlSIN
42 
43 #define OUTPUT_SIGNALS m_stateSOUT << m_velocitySOUT
44 
47 typedef SimpleStateIntegrator EntityClassName;
48 
49 /* --- DG FACTORY ---------------------------------------------------- */
50 DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(SimpleStateIntegrator,
51  "SimpleStateIntegrator");
52 
53 /* ------------------------------------------------------------------- */
54 /* --- CONSTRUCTION -------------------------------------------------- */
55 /* ------------------------------------------------------------------- */
57  : Entity(name),
58  CONSTRUCT_SIGNAL_IN(control, dynamicgraph::Vector),
59  CONSTRUCT_SIGNAL_OUT(state, dynamicgraph::Vector, INPUT_SIGNALS),
60  CONSTRUCT_SIGNAL_OUT(velocity, dynamicgraph::Vector, NULL) {
61  Entity::signalRegistration(INPUT_SIGNALS << OUTPUT_SIGNALS);
62 
63  /* Commands. */
64 
65  std::string docstring;
66 
67  docstring =
68  "\n"
69  " Set integration timestep value\n"
70  "\n";
71  addCommand("init", new command::Setter<SimpleStateIntegrator, double>(
72  *this, &SimpleStateIntegrator::init, docstring));
73 
74  docstring =
75  "\n"
76  " Set state vector value\n"
77  "\n";
78  addCommand("setState",
79  new command::Setter<SimpleStateIntegrator, Vector>(
80  *this, &SimpleStateIntegrator::setState, docstring));
81 
82  docstring =
83  "\n"
84  " Set velocity vector value\n"
85  "\n";
86  addCommand("setVelocity",
87  new command::Setter<SimpleStateIntegrator, Vector>(
88  *this, &SimpleStateIntegrator::setVelocity, docstring));
89 }
90 
91 void SimpleStateIntegrator::init(const double& step) { timestep_ = step; }
92 
94 
96  velocity_ = vel;
97 }
98 
99 /* ------------------------------------------------------------------- */
100 /* --- SIGNALS ------------------------------------------------------- */
101 /* ------------------------------------------------------------------- */
102 
104  const Vector& control,
105  double dt) {
106  using Eigen::AngleAxisd;
107  using Eigen::Matrix3d;
108  using Eigen::QuaternionMapd;
109  using Eigen::Vector3d;
110 
111  typedef pinocchio::SpecialEuclideanOperationTpl<3, double> SE3;
112  Eigen::Matrix<double, 7, 1> qin, qout;
113  qin.head<3>() = state.head<3>();
114 
115  QuaternionMapd quat(qin.tail<4>().data());
116  quat = AngleAxisd(state(5), Vector3d::UnitZ()) *
117  AngleAxisd(state(4), Vector3d::UnitY()) *
118  AngleAxisd(state(3), Vector3d::UnitX());
119 
120  SE3().integrate(qin, control.head<6>() * dt, qout);
121 
122  Matrix3d rotationMatrix =
123  QuaternionMapd(qout.tail<4>().data()).toRotationMatrix();
124  // Create the Euler angles in good range : [-pi:pi]x[-pi/2:pi/2]x[-pi:pi]
125  Vector3d rollPitchYaw;
126  rotationMatrixToEuler(rotationMatrix, rollPitchYaw);
127  // Update freeflyer state (pose)
128  state.head<3>() = qout.head<3>();
129  state.segment<3>(3) << rollPitchYaw;
130 }
131 
133  const Eigen::Matrix3d& rotationMatrix, Eigen::Vector3d& rollPitchYaw) {
134  double m = sqrt(rotationMatrix(2, 1) * rotationMatrix(2, 1) +
135  rotationMatrix(2, 2) * rotationMatrix(2, 2));
136  double p = atan2(-rotationMatrix(2, 0), m);
137  double r, y;
138  if (fabs(fabs(p) - M_PI / 2.) < 0.001) {
139  r = 0;
140  y = -atan2(rotationMatrix(0, 1), rotationMatrix(1, 1));
141  } else {
142  y = atan2(rotationMatrix(1, 0), rotationMatrix(0, 0)); // alpha
143  r = atan2(rotationMatrix(2, 1), rotationMatrix(2, 2)); // gamma
144  }
145  rollPitchYaw << r, p, y;
146 }
147 
149  m_velocitySOUT.setConstant(velocity_);
150  m_velocitySOUT.setTime(iter + 1);
151 
152  const dynamicgraph::Vector& control = m_controlSIN(iter);
153 
154  const size_t sz = control.size();
155  if ((size_t)(s.size()) != sz) s.resize(sz);
156  if ((size_t)(state_.size()) != sz)
157  throw std::runtime_error("Mismatching state and control size");
158  if ((size_t)(velocity_.size()) != sz)
159  throw std::runtime_error("Mismatching velocity and control size");
160 
161  velocity_ = control;
162 
163  integrateRollPitchYaw(state_, control, timestep_);
164  state_.tail(sz - 6) += control.tail(sz - 6) * timestep_;
165 
166  s = state_;
167 
168  return s;
169 }
170 
172  s = velocity_;
173  return s;
174 }
175 
176 /* --- COMMANDS ---------------------------------------------------------- */
177 
178 /* ------------------------------------------------------------------- */
179 /* --- ENTITY -------------------------------------------------------- */
180 /* ------------------------------------------------------------------- */
181 
182 void SimpleStateIntegrator::display(std::ostream& os) const {
183  os << "SimpleStateIntegrator " << getName();
184  try {
185  getProfiler().report_all(3, os);
186  } catch (ExceptionSignal e) {
187  }
188 }
189 
190 } // namespace talos_balance
191 } // namespace sot
192 } // namespace dynamicgraph
INPUT_SIGNALS
#define INPUT_SIGNALS
Definition: simple-state-integrator.cpp:41
sot_talos_balance.test.appli_admittance_end_effector.sot
sot
Definition: appli_admittance_end_effector.py:117
dynamicgraph
Definition: treeview.dox:24
OUTPUT_SIGNALS
#define OUTPUT_SIGNALS
Definition: simple-state-integrator.cpp:43
dynamicgraph::sot::talos_balance::SimpleStateIntegrator::velocity_
Eigen::VectorXd velocity_
Velocity of the robot wrt pinocchio.
Definition: simple-state-integrator.hh:67
dynamicgraph::sot::talos_balance::SimpleStateIntegrator::SimpleStateIntegrator
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SimpleStateIntegrator(const std::string &name)
Definition: simple-state-integrator.cpp:56
sot_talos_balance.test.script_test_end_effector.y
y
Definition: script_test_end_effector.py:11
dynamicgraph::sot::talos_balance::DEFINE_SIGNAL_OUT_FUNCTION
DEFINE_SIGNAL_OUT_FUNCTION(dq, dynamicgraph::Vector)
Definition: admittance-controller-end-effector.cpp:210
dynamicgraph::sot::talos_balance::SimpleStateIntegrator::state_
Eigen::VectorXd state_
Definition: simple-state-integrator.hh:65
dynamicgraph::sot::talos_balance::math::Vector
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: fwd.hh:36
sot_talos_balance.test.appli_admittance_single_joint.dt
dt
Definition: appli_admittance_single_joint.py:17
dynamicgraph::sot::talos_balance::SimpleStateIntegrator::rotationMatrixToEuler
void rotationMatrixToEuler(const Eigen::Matrix3d &rotationMatrix, Eigen::Vector3d &rollPitchYaw)
Definition: simple-state-integrator.cpp:132
simple-state-integrator.hh
dynamicgraph::sot::talos_balance::SimpleStateIntegrator::display
virtual void display(std::ostream &os) const
Definition: simple-state-integrator.cpp:182
dynamicgraph::sot::talos_balance::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(AdmittanceControllerEndEffector, "AdmittanceControllerEndEffector")
dynamicgraph::sot::talos_balance::SimpleStateIntegrator::setVelocity
void setVelocity(const ::dynamicgraph::Vector &vel)
Definition: simple-state-integrator.cpp:95
dynamicgraph::sot::talos_balance::SimpleStateIntegrator::timestep_
double timestep_
Current integration step.
Definition: simple-state-integrator.hh:60
dynamicgraph::sot::talos_balance::SimpleStateIntegrator::init
void init(const double &step)
Definition: simple-state-integrator.cpp:91
dynamicgraph::sot::talos_balance::SimpleStateIntegrator::integrateRollPitchYaw
void integrateRollPitchYaw(::dynamicgraph::Vector &state, const ::dynamicgraph::Vector &control, double dt)
Definition: simple-state-integrator.cpp:103
dynamicgraph::sot::talos_balance::SimpleStateIntegrator::setState
void setState(const ::dynamicgraph::Vector &st)
Definition: simple-state-integrator.cpp:93
dynamicgraph::sot::talos_balance::EntityClassName
AdmittanceControllerEndEffector EntityClassName
Definition: admittance-controller-end-effector.cpp:46
sot_talos_balance.test.appli_dcm_zmp_control.name
name
Definition: appli_dcm_zmp_control.py:298