1 |
|
|
/* |
2 |
|
|
* Copyright 2014, Andrea Del Prete, LAAS-CNRS |
3 |
|
|
* |
4 |
|
|
*/ |
5 |
|
|
|
6 |
|
|
#ifndef __sot_torque_control_NumericalDifference_H__ |
7 |
|
|
#define __sot_torque_control_NumericalDifference_H__ |
8 |
|
|
/* --------------------------------------------------------------------- */ |
9 |
|
|
/* --- API ------------------------------------------------------------- */ |
10 |
|
|
/* --------------------------------------------------------------------- */ |
11 |
|
|
|
12 |
|
|
#if defined(WIN32) |
13 |
|
|
#if defined(numerical_difference_EXPORTS) |
14 |
|
|
#define SOTNUMERICALDIFFERENCE_EXPORT __declspec(dllexport) |
15 |
|
|
#else |
16 |
|
|
#define SOTNUMERICALDIFFERENCE_EXPORT __declspec(dllimport) |
17 |
|
|
#endif |
18 |
|
|
#else |
19 |
|
|
#define SOTNUMERICALDIFFERENCE_EXPORT |
20 |
|
|
#endif |
21 |
|
|
|
22 |
|
|
// #define VP_DEBUG 1 /// enable debug output |
23 |
|
|
// #define VP_DEBUG_MODE 20 |
24 |
|
|
|
25 |
|
|
/* --------------------------------------------------------------------- */ |
26 |
|
|
/* --- INCLUDE --------------------------------------------------------- */ |
27 |
|
|
/* --------------------------------------------------------------------- */ |
28 |
|
|
|
29 |
|
|
/* HELPER */ |
30 |
|
|
#include <dynamic-graph/signal-helper.h> |
31 |
|
|
|
32 |
|
|
#include <boost/circular_buffer.hpp> |
33 |
|
|
#include <sot/core/matrix-geometry.hh> |
34 |
|
|
#include <sot/core/stop-watch.hh> |
35 |
|
|
#include <sot/torque_control/utils/vector-conversions.hh> |
36 |
|
|
|
37 |
|
|
/* Polynomial estimators */ |
38 |
|
|
#include <sot/torque_control/utils/lin-estimator.hh> |
39 |
|
|
#include <sot/torque_control/utils/quad-estimator.hh> |
40 |
|
|
|
41 |
|
|
namespace dynamicgraph { |
42 |
|
|
namespace sot { |
43 |
|
|
namespace torque_control { |
44 |
|
|
|
45 |
|
|
/** |
46 |
|
|
* This Entity takes as inputs a signal and estimates its first two time |
47 |
|
|
* derivatives. |
48 |
|
|
*/ |
49 |
|
|
class SOTNUMERICALDIFFERENCE_EXPORT NumericalDifference |
50 |
|
|
: public ::dynamicgraph::Entity { |
51 |
|
|
DYNAMIC_GRAPH_ENTITY_DECL(); |
52 |
|
|
|
53 |
|
|
public: /* --- SIGNALS --- */ |
54 |
|
|
DECLARE_SIGNAL_IN(x, dynamicgraph::Vector); |
55 |
|
|
DECLARE_SIGNAL_OUT(x_filtered, dynamicgraph::Vector); |
56 |
|
|
DECLARE_SIGNAL_OUT(dx, dynamicgraph::Vector); |
57 |
|
|
DECLARE_SIGNAL_OUT(ddx, dynamicgraph::Vector); |
58 |
|
|
|
59 |
|
|
/// The following inner signals are used because this entity has some output |
60 |
|
|
/// signals whose related quantities are computed at the same time by the same |
61 |
|
|
/// algorithm To avoid the risk of recomputing the same things twice, we |
62 |
|
|
/// create an inner signal that groups together all the quantities that are |
63 |
|
|
/// computed together. Then the single output signals will depend on this |
64 |
|
|
/// inner signal, which is the one triggering the computations. Inner signals |
65 |
|
|
/// are not exposed, so that nobody can access them. |
66 |
|
|
|
67 |
|
|
/// This signal contains the estimated positions, velocities and |
68 |
|
|
/// accelerations. |
69 |
|
|
DECLARE_SIGNAL_INNER(x_dx_ddx, dynamicgraph::Vector); |
70 |
|
|
|
71 |
|
|
protected: |
72 |
|
|
double m_dt; /// sampling timestep of the input signal |
73 |
|
|
double m_delay; /// delay introduced by the estimation |
74 |
|
|
int x_size; |
75 |
|
|
/// std::vector to use with the filters |
76 |
|
|
/// All the variables whose name contains 'filter' are outputs of the filters |
77 |
|
|
std::vector<double> m_ddx_filter_std; /// 2nd derivative |
78 |
|
|
std::vector<double> m_dx_filter_std; /// 1st derivative |
79 |
|
|
std::vector<double> m_x_filter_std; /// filtered output |
80 |
|
|
std::vector<double> m_x_std; /// x signal |
81 |
|
|
|
82 |
|
|
/// polynomial-fitting filters |
83 |
|
|
PolyEstimator* m_filter; |
84 |
|
|
|
85 |
|
|
public: |
86 |
|
|
EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
87 |
|
|
|
88 |
|
|
/** --- CONSTRUCTOR ---- */ |
89 |
|
|
NumericalDifference(const std::string& name); |
90 |
|
|
|
91 |
|
|
/** Initialize the NumericalDifference. |
92 |
|
|
* @param timestep Period (in seconds) after which the sensors' data are |
93 |
|
|
* updated. |
94 |
|
|
* @param sigSize Size of the input signal. |
95 |
|
|
* @param delay Delay (in seconds) introduced by the estimation. |
96 |
|
|
* This should be a multiple of timestep. |
97 |
|
|
* @param polyOrder Order of the approximation polynomial. int 1 or 2. |
98 |
|
|
* @note The estimationDelay is half of the length of the window used for the |
99 |
|
|
* polynomial fitting. The larger the delay, the smoother the estimations. |
100 |
|
|
*/ |
101 |
|
|
void init(const double& timestep, const int& sigSize, const double& delay, |
102 |
|
|
const int& polyOrder); |
103 |
|
|
|
104 |
|
|
protected: |
105 |
|
|
void sendMsg(const std::string& msg, MsgType t = MSG_TYPE_INFO, |
106 |
|
|
const char* = "", int = 0) { |
107 |
|
|
logger_.stream(t) << ("[" + name + "] " + msg) << '\n'; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
public: /* --- ENTITY INHERITANCE --- */ |
111 |
|
|
virtual void display(std::ostream& os) const; |
112 |
|
|
|
113 |
|
|
}; // class NumericalDifference |
114 |
|
|
|
115 |
|
|
} // namespace torque_control |
116 |
|
|
} // namespace sot |
117 |
|
|
} // namespace dynamicgraph |
118 |
|
|
|
119 |
|
|
#endif // #ifndef __sot_torque_control_NumericalDifference_H__ |