23
23
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
24
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
25
26
+ #define ENABLE_RT_LOG
26
27
#include " device.hh"
27
28
#include < dynamic-graph/factory.h>
28
29
#include < dynamic-graph/all-commands.h>
30
+ #include < dynamic-graph/real-time-logger.h>
29
31
30
32
#include < sot/core/debug.hh>
31
33
34
+ // Return true if it saturates.
35
+ inline bool saturateBounds (double &val, const double &lower,
36
+ const double &upper) {
37
+ assert (lower <= upper);
38
+ if (val < lower) {
39
+ val = lower;
40
+ return true ;
41
+ }
42
+ if (upper < val) {
43
+ val = upper;
44
+ return true ;
45
+ }
46
+ return false ;
47
+ }
48
+
49
+ #define CHECK_BOUNDS (val, lower, upper, what ) \
50
+ for (int i = 0 ; i < val.size(); ++i) { \
51
+ double old = val (i); \
52
+ if (saturateBounds (val (i), lower (i), upper (i))) { \
53
+ dgRTLOG () << " Robot " what " bound violation at DoF " << i << \
54
+ " : requested " << old << " but set " << val (i) << ' \n ' ; \
55
+ } \
56
+ }
57
+
58
+ #ifdef VP_DEBUG
59
+ class initLog {
60
+ public:
61
+ initLog (void ) { dynamicgraph::sot::DebugTrace::openFile (); }
62
+ };
63
+ initLog log_initiator;
64
+ #endif // #ifdef VP_DEBUG
65
+
32
66
const double SoTUniversalRobotDevice::TIMESTEP_DEFAULT = 0.001 ;
33
67
34
68
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN (SoTUniversalRobotDevice,
@@ -88,7 +122,7 @@ void SoTUniversalRobotDevice::setSensors(map<string,SensorValues> &SensorsIn)
88
122
89
123
// Implements force recollection.
90
124
const std::vector<double >& forcesIn = it->second .getValues ();
91
- assert (std::div (forcesIn.size (), 6 ).rem == 0 );
125
+ assert (std::div (( int ) forcesIn.size (), 6 ).rem == 0 );
92
126
int K = (int )forcesIn.size () / 6 ;
93
127
for (int i=0 ;i<K;++i)
94
128
{
@@ -177,10 +211,7 @@ void SoTUniversalRobotDevice::getControl(map<string,ControlValues> &controlOut)
177
211
178
212
// Integrate control
179
213
increment (timestep_);
180
- sotDEBUG (25 ) << " state = " << state_ << std::endl;
181
- sotDEBUG (25 ) << " diff = " << ((previousState_.size () == state_.size ())?
182
- (state_ - previousState_) : state_ )
183
- << std::endl;
214
+ sotDEBUG (25 ) << " state = " << state_.transpose () << std::endl;
184
215
previousState_ = state_;
185
216
186
217
// Specify the joint values for the controller.
@@ -191,3 +222,46 @@ void SoTUniversalRobotDevice::getControl(map<string,ControlValues> &controlOut)
191
222
controlOut[" control" ].setValues (anglesOut);
192
223
sotDEBUGOUT (25 ) ;
193
224
}
225
+
226
+ void SoTUniversalRobotDevice::integrate (const double &dt)
227
+ {
228
+ using dynamicgraph::Vector;
229
+ const Vector &controlIN = controlSIN.accessCopy ();
230
+
231
+ if (sanityCheck_ && controlIN.hasNaN ()) {
232
+ dgRTLOG () << " Device::integrate: Control has NaN values: " << ' \n '
233
+ << controlIN.transpose () << ' \n ' ;
234
+ return ;
235
+ }
236
+
237
+ if (controlInputType_ == dynamicgraph::sot::CONTROL_INPUT_NO_INTEGRATION) {
238
+ state_ = controlIN;
239
+ return ;
240
+ }
241
+
242
+ if (vel_control_.size () == 0 )
243
+ vel_control_ = Vector::Zero (controlIN.size ());
244
+
245
+ if (controlInputType_ == dynamicgraph::sot::CONTROL_INPUT_TWO_INTEGRATION) {
246
+ // TODO check acceleration
247
+ // Position increment
248
+ vel_control_ = velocity_ + (0.5 * dt) * controlIN;
249
+ // Velocity integration.
250
+ velocity_ += controlIN * dt;
251
+ } else {
252
+ vel_control_ = controlIN;
253
+ }
254
+
255
+ // Velocity bounds check
256
+ if (sanityCheck_) {
257
+ CHECK_BOUNDS (velocity_, lowerVelocity_, upperVelocity_, " velocity" );
258
+ }
259
+
260
+ // Position integration
261
+ state_ += vel_control_ * dt;
262
+
263
+ // Position bounds check
264
+ if (sanityCheck_) {
265
+ CHECK_BOUNDS (state_, lowerPosition_, upperPosition_, " position" );
266
+ }
267
+ }
0 commit comments