filter-differentiator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-, Rohan Budhiraja LAAS-CNRS
3  *
4  * This file is part of sot-torque-control.
5  * sot-torque-control 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-torque-control 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-torque-control. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #define LOGFILE "/tmp/fd_log.dat"
18 
19 #define LOG(x) \
20  { \
21  std::ofstream LogFile; \
22  LogFile.open(LOGFILE, std::ofstream::app); \
23  LogFile << x << std::endl; \
24  LogFile.close(); \
25  }
26 
28 #include <dynamic-graph/factory.h>
29 
30 #include <sot/core/debug.hh>
32 // #include <sot/torque_control/motor-model.hh>
33 #include <Eigen/Dense>
34 
35 namespace dynamicgraph {
36 namespace sot {
37 
38 #define ALL_INPUT_SIGNALS m_xSIN
39 
40 #define ALL_OUTPUT_SIGNALS m_x_filteredSOUT << m_dxSOUT << m_ddxSOUT
41 
42 namespace dynamicgraph = ::dynamicgraph;
43 using namespace dynamicgraph;
44 using namespace dynamicgraph::command;
45 using namespace Eigen;
46 
50 
51 /* --- DG FACTORY --------------------------------------------------- */
53  "FilterDifferentiator");
54 
55 /* --- CONSTRUCTION ------------------------------------------------- */
56 /* --- CONSTRUCTION ------------------------------------------------- */
57 /* --- CONSTRUCTION ------------------------------------------------- */
59  : Entity(name),
61  CONSTRUCT_SIGNAL_OUT(x_filtered, dynamicgraph::Vector, m_x_dx_ddxSINNER),
62  CONSTRUCT_SIGNAL_OUT(dx, dynamicgraph::Vector, m_x_dx_ddxSINNER),
63  CONSTRUCT_SIGNAL_OUT(ddx, dynamicgraph::Vector, m_x_dx_ddxSINNER),
64  CONSTRUCT_SIGNAL_INNER(x_dx_ddx, dynamicgraph::Vector, m_xSIN) {
66 
67  /* Commands. */
68  addCommand(
69  "getTimestep",
70  makeDirectGetter(*this, &m_dt,
71  docDirectGetter("Control timestep [s ]", "double")));
72  addCommand("getSize", makeDirectGetter(*this, &m_x_size,
73  docDirectGetter("Size of the x signal",
74  "size_type")));
75  addCommand("init",
77  docCommandVoid4("Initialize the filter.",
78  "Control timestep [s].",
79  "Size of the input signal x",
80  "Numerator of the filter",
81  "Denominator of the filter")));
82  addCommand("switch_filter",
85  docCommandVoid2("Switch Filter.", "Numerator of the filter",
86  "Denominator of the filter")));
87 }
88 
89 /* --- COMMANDS ------------------------------------------------------ */
90 /* --- COMMANDS ------------------------------------------------------ */
91 /* --- COMMANDS ------------------------------------------------------ */
92 void FilterDifferentiator::init(const double &timestep, const size_type &xSize,
93  const Eigen::VectorXd &filter_numerator,
94  const Eigen::VectorXd &filter_denominator) {
95  m_x_size = xSize;
96  m_dt = timestep;
97  m_filter =
98  new CausalFilter(timestep, xSize, filter_numerator, filter_denominator);
99 
100  LOG("Filtering started with "
101  << "Numerator " << filter_numerator << std::endl
102  << "Denominator" << filter_denominator << std::endl);
103  return;
104 }
105 
107  const Eigen::VectorXd &filter_numerator,
108  const Eigen::VectorXd &filter_denominator) {
109  LOG("Filter switched with "
110  << "Numerator " << filter_numerator << std::endl
111  << "Denominator" << filter_denominator << std::endl
112  << "at time" << m_xSIN.getTime());
113  m_filter->switch_filter(filter_numerator, filter_denominator);
114 }
115 
116 /* --- SIGNALS ------------------------------------------------------ */
117 /* --- SIGNALS ------------------------------------------------------ */
118 /* --- SIGNALS ------------------------------------------------------ */
119 
121  sotDEBUG(15) << "Compute x_dx inner signal " << iter << std::endl;
122  if (s.size() != 3 * m_x_size) s.resize(3 * m_x_size);
123  // read encoders
124  const dynamicgraph::Vector &base_x = m_xSIN(iter);
125  assert(base_x.size() == m_x_size);
126  m_filter->get_x_dx_ddx(base_x, s);
127  return s;
128 }
129 
135 
137  sotDEBUG(15) << "Compute x_filtered output signal " << iter << std::endl;
138 
139  const dynamicgraph::Vector &x_dx_ddx = m_x_dx_ddxSINNER(iter);
140  if (s.size() != m_x_size) s.resize(m_x_size);
141  s = x_dx_ddx.head(m_x_size);
142  return s;
143 }
144 
146  sotDEBUG(15) << "Compute dx output signal " << iter << std::endl;
147 
148  const dynamicgraph::Vector &x_dx_ddx = m_x_dx_ddxSINNER(iter);
149  if (s.size() != m_x_size) s.resize(m_x_size);
150  s = x_dx_ddx.segment(m_x_size, m_x_size);
151  return s;
152 }
153 
155  sotDEBUG(15) << "Compute ddx output signal " << iter << std::endl;
156 
157  const dynamicgraph::Vector &x_dx_ddx = m_x_dx_ddxSINNER(iter);
158  if (s.size() != m_x_size) s.resize(m_x_size);
159  s = x_dx_ddx.tail(m_x_size);
160  return s;
161 }
162 
163 void FilterDifferentiator::display(std::ostream &os) const {
164  os << "FilterDifferentiator " << getName() << ":\n";
165  try {
166  getProfiler().report_all(3, os);
167  } catch (const ExceptionSignal &e) {
168  }
169 }
170 
171 } // namespace sot
172 } // namespace dynamicgraph
Eigen
makeCommandVoid4
CommandVoid4< E, T1, T2, T3, T4 > * makeCommandVoid4(E &entity, boost::function< void(E *, const T1 &, const T2 &, const T3 &, const T4 &)> function, const std::string &docString)
dynamicgraph::sot::FilterDifferentiator::m_x_size
size_type m_x_size
sampling timestep of the input signal
Definition: filter-differentiator.hh:87
CONSTRUCT_SIGNAL_IN
#define CONSTRUCT_SIGNAL_IN(name, type)
filter-differentiator.hh
dynamicgraph
dynamicgraph::command
dynamicgraph::Entity
dynamicgraph::sot::FilterDifferentiator::init
void init(const double &timestep, const size_type &xSize, const Eigen::VectorXd &filter_numerator, const Eigen::VectorXd &filter_denominator)
Definition: filter-differentiator.cpp:92
dynamicgraph::sot::CausalFilter
Definition: causal-filter.hh:45
dynamicgraph::sot::DEFINE_SIGNAL_OUT_FUNCTION
DEFINE_SIGNAL_OUT_FUNCTION(x_filtered, dynamicgraph::Vector)
Definition: filter-differentiator.cpp:136
dynamicgraph::sot::EntityClassName
FilterDifferentiator EntityClassName
Definition: filter-differentiator.cpp:49
dynamicgraph::sot::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(FeaturePosture, "FeaturePosture")
dynamicgraph::Entity::getName
const std::string & getName() const
debug.hh
dynamicgraph::sot::DEFINE_SIGNAL_INNER_FUNCTION
DEFINE_SIGNAL_INNER_FUNCTION(x_dx_ddx, dynamicgraph::Vector)
Definition: filter-differentiator.cpp:120
makeDirectGetter
DirectGetter< E, T > * makeDirectGetter(E &entity, T *ptr, const std::string &docString)
s
s
dynamicgraph::sot::FilterDifferentiator
Definition: filter-differentiator.hh:58
dynamicgraph::sot::FilterDifferentiator::switch_filter
void switch_filter(const Eigen::VectorXd &filter_numerator, const Eigen::VectorXd &filter_denominator)
Definition: filter-differentiator.cpp:106
docCommandVoid4
std::string docCommandVoid4(const std::string &doc, const std::string &type1, const std::string &type2, const std::string &type3, const std::string &type4)
ALL_INPUT_SIGNALS
#define ALL_INPUT_SIGNALS
Definition: filter-differentiator.cpp:38
dynamicgraph::sot::FilterDifferentiator::display
virtual void display(std::ostream &os) const
Definition: filter-differentiator.cpp:163
x
x
dynamicgraph::size_type
Matrix::Index size_type
CONSTRUCT_SIGNAL_INNER
#define CONSTRUCT_SIGNAL_INNER(name, type, dep)
dynamicgraph::Vector
Eigen::VectorXd Vector
makeCommandVoid2
CommandVoid2< E, T1, T2 > * makeCommandVoid2(E &entity, boost::function< void(const T1 &, const T2 &)> function, const std::string &docString)
getProfiler
Stopwatch & getProfiler()
Definition: stop-watch.cpp:46
docCommandVoid2
std::string docCommandVoid2(const std::string &doc, const std::string &type1, const std::string &type2)
ALL_OUTPUT_SIGNALS
#define ALL_OUTPUT_SIGNALS
Definition: filter-differentiator.cpp:40
dynamicgraph::ExceptionSignal
docDirectGetter
std::string docDirectGetter(const std::string &name, const std::string &type)
dynamicgraph::sot::FilterDifferentiator::m_filter
CausalFilter * m_filter
polynomial-fitting filters
Definition: filter-differentiator.hh:90
CONSTRUCT_SIGNAL_OUT
#define CONSTRUCT_SIGNAL_OUT(name, type, dep)
dynamicgraph::sot::CausalFilter::switch_filter
void switch_filter(const Eigen::VectorXd &filter_numerator, const Eigen::VectorXd &filter_denominator)
Definition: causal-filter.cpp:107
dynamicgraph::Entity::addCommand
void addCommand(const std::string &name, command::Command *command)
all-commands.h
LOG
#define LOG(x)
Definition: filter-differentiator.cpp:19
dynamicgraph::Entity::signalRegistration
void signalRegistration(const SignalArray< sigtime_t > &signals)
dynamicgraph::sot::FilterDifferentiator::m_dt
double m_dt
Definition: filter-differentiator.hh:86
Stopwatch::report_all
void report_all(int precision=2, std::ostream &output=std::cout)
Definition: stop-watch.cpp:185
compile.name
name
Definition: compile.py:23
sotDEBUG
#define sotDEBUG(level)
Definition: debug.hh:168
dynamicgraph::sot::FilterDifferentiator::FilterDifferentiator
EIGEN_MAKE_ALIGNED_OPERATOR_NEW FilterDifferentiator(const std::string &name)
Definition: filter-differentiator.cpp:58


sot-core
Author(s): Olivier Stasse, ostasse@laas.fr
autogenerated on Tue Oct 24 2023 02:26:31