Skip to content

Commit f6e10c0

Browse files
Reimplement SoTUniversalRobotDevice::integrate.
Device version integrates state as it it was a freeflyer.
1 parent cd525f3 commit f6e10c0

File tree

2 files changed

+82
-5
lines changed

2 files changed

+82
-5
lines changed

src/device.cc

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,46 @@
2323
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2424
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525

26+
#define ENABLE_RT_LOG
2627
#include "device.hh"
2728
#include <dynamic-graph/factory.h>
2829
#include <dynamic-graph/all-commands.h>
30+
#include <dynamic-graph/real-time-logger.h>
2931

3032
#include <sot/core/debug.hh>
3133

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+
3266
const double SoTUniversalRobotDevice::TIMESTEP_DEFAULT = 0.001;
3367

3468
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(SoTUniversalRobotDevice,
@@ -88,7 +122,7 @@ void SoTUniversalRobotDevice::setSensors(map<string,SensorValues> &SensorsIn)
88122

89123
// Implements force recollection.
90124
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);
92126
int K = (int)forcesIn.size() / 6;
93127
for(int i=0;i<K;++i)
94128
{
@@ -177,10 +211,7 @@ void SoTUniversalRobotDevice::getControl(map<string,ControlValues> &controlOut)
177211

178212
// Integrate control
179213
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;
184215
previousState_ = state_;
185216

186217
// Specify the joint values for the controller.
@@ -191,3 +222,46 @@ void SoTUniversalRobotDevice::getControl(map<string,ControlValues> &controlOut)
191222
controlOut["control"].setValues(anglesOut);
192223
sotDEBUGOUT(25) ;
193224
}
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+
}

src/device.hh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ protected:
7373
closedLoop_ = closedLoop;
7474
};
7575

76+
/// Integrate control signal to update internal state.
77+
virtual void integrate(const double &dt);
78+
7679
/// \brief Whether the control of the base should be expressed in odometry
7780
/// frame of base frame.
7881
bool closedLoop_;

0 commit comments

Comments
 (0)