30
30
31
31
#include < sot/core/integrator.hh>
32
32
#include < dynamic-graph/factory.h>
33
+ #include < dynamic-graph/real-time-logger.h>
34
+ #include < dynamic-graph/signal-caster.h>
35
+ #include < dynamic-graph/signal.h>
33
36
34
37
namespace dynamicgraph {
35
38
namespace sot {
39
+ namespace internal {
36
40
41
+ Signal::Signal (std::string name) :
42
+ ::dynamicgraph::Signal<Vector, int >(name)
43
+ {
44
+ }
45
+
46
+ /* ------------------------------------------------------------------------ */
47
+
48
+
49
+ void Signal::set (std::istringstream &stringValue) {
50
+ (*this ) = signal_io<Vector>::cast (stringValue);
51
+ }
52
+
53
+
54
+ void Signal::get (std::ostream &os) const {
55
+ signal_io<Vector>::disp (this ->accessCopy (), os);
56
+ }
57
+
58
+
59
+ void Signal::trace (std::ostream &os) const {
60
+ try {
61
+ signal_io<Vector>::trace (this ->accessCopy (), os);
62
+ } catch DG_RETHROW catch (...) {
63
+ DG_THROW ExceptionSignal (ExceptionSignal::SET_IMPOSSIBLE,
64
+ " TRACE operation not possible with this signal. " ,
65
+ " (bad cast while getting value from %s)." ,
66
+ SignalBase<int >::getName ().c_str ());
67
+ }
68
+ }
69
+
70
+ void Signal::setConstant (const Vector &) {
71
+ throw std::runtime_error (" Not implemented." );
72
+ }
73
+
74
+
75
+ void Signal::setReference (const Vector *, Mutex *) {
76
+ throw std::runtime_error (" Not implemented." );
77
+ }
78
+
79
+
80
+ void Signal::setReferenceNonConstant (Vector *, Mutex *) {
81
+ throw std::runtime_error (" Not implemented." );
82
+ }
83
+
84
+
85
+ void Signal::setFunction (boost::function2<Vector &, Vector &, int > t,
86
+ Mutex *mutexref) {
87
+ signalType = ::dynamicgraph::Signal<Vector, int >::FUNCTION;
88
+ Tfunction = t;
89
+ providerMutex = mutexref;
90
+ copyInit = false ;
91
+ setReady ();
92
+ }
93
+
94
+
95
+ const Vector &Signal::accessCopy () const {
96
+ return Tcopy1;
97
+ }
98
+
99
+
100
+ const Vector &Signal::access (const int &t) {
101
+ if (NULL == providerMutex) {
102
+ signalTime = t;
103
+ Tfunction (Tcopy1, t);
104
+ return Tcopy1;
105
+ } else {
106
+ try {
107
+ #ifdef HAVE_LIBBOOST_THREAD
108
+ boost::try_mutex::scoped_try_lock lock (*providerMutex);
109
+ #endif
110
+ signalTime = t;
111
+ Tfunction (Tcopy1, t);
112
+ return Tcopy1;
113
+ } catch (const MutexError &) {
114
+ return accessCopy ();
115
+ }
116
+ }
117
+ }
118
+
119
+
120
+ Signal &Signal::operator =(const Vector &t) {
121
+ throw std::runtime_error (" Output signal cannot be assigned a value." );
122
+ return *this ;
123
+ }
124
+
125
+
126
+ std::ostream &Signal::display (std::ostream &os) const {
127
+ os << " Sig:" << this ->name << " (Type " ;
128
+ switch (this ->signalType ) {
129
+ case Signal::CONSTANT:
130
+ os << " Cst" ;
131
+ break ;
132
+ case Signal::REFERENCE:
133
+ os << " Ref" ;
134
+ break ;
135
+ case Signal::REFERENCE_NON_CONST:
136
+ os << " RefNonCst" ;
137
+ break ;
138
+ case Signal::FUNCTION:
139
+ os << " Fun" ;
140
+ break ;
141
+ }
142
+ return os << " )" ;
143
+ }
144
+
145
+ } // namespace internal
37
146
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN (Integrator, " Integrator" );
38
147
39
148
const double Integrator::dt = 1e-6 ;
40
149
41
150
Integrator::Integrator (const std::string& name) :
42
151
Entity (name),
43
152
velocitySIN_ (0x0 , " Integrator(" + name + " )::input(vector)::velocity" ),
44
- configurationSOUT_ (boost::bind(&Integrator::integrate, this , _1, _2),
45
- velocitySIN_, " Integrator(" + name +
46
- " )::output(vector)::configuration" ), model_(0x0 ),
47
- configuration_ (), lastComputationTime_(-1 )
153
+ configurationSOUT_ (" Integrator(" + name + " )::output(vector)::configuration" ),
154
+ model_ (0x0 ), configuration_(), lastComputationTime_(-1 ), recursivityLevel_(0 )
48
155
{
156
+ configurationSOUT_.setFunction (boost::bind (&Integrator::integrate, this , _1,
157
+ _2));
49
158
signalRegistration (velocitySIN_);
50
159
signalRegistration (configurationSOUT_);
51
160
}
@@ -69,9 +178,25 @@ void Integrator::setInitialConfig(const Vector& initConfig)
69
178
70
179
Vector& Integrator::integrate (Vector& configuration, int time)
71
180
{
181
+ ++recursivityLevel_;
182
+ if (recursivityLevel_ == 2 ){
183
+ configuration = configuration_;
184
+ --recursivityLevel_;
185
+ return configuration;
186
+ }
187
+ configuration.resize (configuration_.size ());
72
188
assert (model_);
73
189
Vector velocity (velocitySIN_ (time));
190
+ // Run Synchronous commands and evaluate signals outside the main
191
+ // connected component of the graph.
192
+ try {
193
+ periodicCallBefore_.run (time);
194
+ } catch (const std::exception &e) {
195
+ dgRTLOG () << " exception caught while running periodical commands (before): "
196
+ << e.what () << std::endl;
197
+ }
74
198
if (lastComputationTime_ == -1 && !velocity.isZero (0 )) {
199
+ recursivityLevel_ = 0 ;
75
200
std::ostringstream os;
76
201
os << " Integrator entity expects zero velocity input for the first "
77
202
" computation. Got instead " << velocity.transpose () << " ." ;
@@ -82,6 +207,13 @@ Vector& Integrator::integrate(Vector& configuration, int time)
82
207
configuration);
83
208
configuration_ = configuration;
84
209
lastComputationTime_ = time;
210
+ try {
211
+ periodicCallAfter_.run (time);
212
+ } catch (const std::exception &e) {
213
+ dgRTLOG () << " exception caught while running periodical commands (after): "
214
+ << e.what () << std::endl;
215
+ }
216
+ --recursivityLevel_;
85
217
return configuration;
86
218
}
87
219
0 commit comments