gripper-control.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2010,
3  * François Bleibel,
4  * Olivier Stasse,
5  *
6  * CNRS/AIST
7  *
8  */
9 
10 #define ENABLE_RT_LOG
11 
14 
15 #include <sot/core/debug.hh>
16 #include <sot/core/factory.hh>
19 
20 using namespace dynamicgraph::sot;
21 using namespace dynamicgraph;
22 
24 
25 /* --- PLUGIN --------------------------------------------------------------- */
26 /* --- PLUGIN --------------------------------------------------------------- */
27 /* --- PLUGIN --------------------------------------------------------------- */
28 
29 #define SOT_FULL_TO_REDUCED(sotName) \
30  sotName##FullSizeSIN(NULL, "GripperControl(" + name + \
31  ")::input(vector)::" + #sotName + "FullIN"), \
32  sotName##ReduceSOUT( \
33  SOT_INIT_SIGNAL_2(GripperControlPlugin::selector, \
34  sotName##FullSizeSIN, dynamicgraph::Vector, \
35  selectionSIN, Flags), \
36  "GripperControl(" + name + ")::input(vector)::" + #sotName + \
37  "ReducedOUT")
38 
39 const double GripperControl::OFFSET_DEFAULT = 0.9;
40 
41 // TODO: hard coded
42 const double DT = 0.005;
43 
45  : offset(GripperControl::OFFSET_DEFAULT), factor() {}
46 
48  : Entity(name),
49  calibrationStarted(false),
50  positionSIN(NULL,
51  "GripperControl(" + name + ")::input(vector)::position"),
52  positionDesSIN(
53  NULL, "GripperControl(" + name + ")::input(vector)::positionDes"),
54  torqueSIN(NULL, "GripperControl(" + name + ")::input(vector)::torque"),
55  torqueLimitSIN(
56  NULL, "GripperControl(" + name + ")::input(vector)::torqueLimit"),
57  selectionSIN(NULL, "GripperControl(" + name + ")::input(vector)::selec")
58 
59  ,
60  SOT_FULL_TO_REDUCED(position),
61  SOT_FULL_TO_REDUCED(torque),
62  SOT_FULL_TO_REDUCED(torqueLimit),
63  desiredPositionSOUT(
65  positionSIN, dynamicgraph::Vector, positionDesSIN,
66  dynamicgraph::Vector, torqueSIN,
67  dynamicgraph::Vector, torqueLimitSIN,
69  "GripperControl(" + name + ")::output(vector)::reference") {
70  sotDEBUGIN(5);
71 
75 
80  sotDEBUGOUT(5);
81 
82  initCommands();
83 }
84 
86 
88  std::string docstring = "Control of gripper.";
89  return docstring;
90 }
91 
92 /* --- SIGNALS -------------------------------------------------------------- */
93 /* --- SIGNALS -------------------------------------------------------------- */
94 /* --- SIGNALS -------------------------------------------------------------- */
95 
97  const dynamicgraph::Vector &torques,
98  const dynamicgraph::Vector &torqueLimits,
99  const dynamicgraph::Vector &currentNormVel) {
100  const dynamicgraph::Vector::Index SIZE = currentNormVel.size();
101 
102  // initialize factor, if needed.
103  if (factor.size() != SIZE) {
104  factor.resize(SIZE);
105  factor.fill(1.);
106  }
107 
108  // Torque not provided?
109  if (torques.size() == 0) {
110  dgRTLOG() << "torque is not provided " << std::endl;
111  return;
112  }
113 
114  for (int i = 0; i < SIZE; ++i) {
115  // apply a reduction factor if the torque limits are exceeded
116  // and the velocity goes in the same way
117  if ((torques(i) > torqueLimits(i)) && (currentNormVel(i) > 0)) {
118  factor(i) *= offset;
119  } else if ((torques(i) < -torqueLimits(i)) && (currentNormVel(i) < 0)) {
120  factor(i) *= offset;
121  }
122  // otherwise, release smoothly the reduction if possible/needed
123  else {
124  factor(i) /= offset;
125  }
126 
127  // ensure factor is in )0,1(
128  factor(i) = std::min(1., std::max(factor(i), 0.));
129  }
130 }
131 
133  const dynamicgraph::Vector &currentPos,
134  const dynamicgraph::Vector &desiredPos, const dynamicgraph::Vector &torques,
135  const dynamicgraph::Vector &torqueLimits,
136  dynamicgraph::Vector &referencePos) {
137  const dynamicgraph::Vector::Index SIZE = currentPos.size();
138  // if( (SIZE==torques.size()) )
139  // { /* ERROR ... */ }
140 
141  // compute the desired velocity
142  dynamicgraph::Vector velocity = (desiredPos - currentPos) * (1. / DT);
143 
144  computeIncrement(torques, torqueLimits, velocity);
145 
146  sotDEBUG(25) << " velocity " << velocity << std::endl;
147  sotDEBUG(25) << " factor " << factor << std::endl;
148 
149  // multiply the velocity elmt per elmt
150  dynamicgraph::Vector weightedVel(SIZE);
151  weightedVel = velocity * factor;
152  sotDEBUG(25) << " weightedVel " << weightedVel << std::endl;
153 
154  // integrate the desired velocity
155  referencePos.resize(SIZE);
156  referencePos = currentPos + weightedVel * DT;
157  return referencePos;
158 }
159 
161  const dynamicgraph::Vector &fullsize, const Flags &selec,
162  dynamicgraph::Vector &desPos) {
163  int size = 0;
164  for (int i = 0; i < fullsize.size(); ++i) {
165  if (selec(i)) size++;
166  }
167 
168  int curs = 0;
169  desPos.resize(size);
170  for (int i = 0; i < fullsize.size(); ++i) {
171  if (selec(i)) desPos(curs++) = fullsize(i);
172  }
173 
174  return desPos;
175 }
176 
177 /* --- COMMANDLINE ---------------------------------------------------------- */
178 /* --- COMMANDLINE ---------------------------------------------------------- */
179 /* --- COMMANDLINE ---------------------------------------------------------- */
181  namespace dc = ::dynamicgraph::command;
182  addCommand("offset",
183  dc::makeCommandVoid1(*this, &GripperControlPlugin::setOffset,
184  "set the offset (should be in )0, 1( )."));
185 }
186 
187 void GripperControlPlugin::setOffset(const double &value) {
188  if ((value > 0) && (value < 1))
189  offset = value;
190  else
191  throw std::invalid_argument("The offset should be in )0, 1(.");
192 }
GripperControlPlugin(const std::string &name)
dynamicgraph::SignalPtr< Flags, int > selectionSIN
Eigen::VectorXd Vector
dynamicgraph::Vector factor
The multiplication.
int i
virtual std::string getDocString() const
void signalRegistration(const SignalArray< int > &signals)
#define sotDEBUGOUT(level)
Definition: debug.hh:212
dynamicgraph::SignalTimeDependent< dynamicgraph::Vector, int > positionReduceSOUT
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(FeaturePosture, "FeaturePosture")
dynamicgraph::SignalPtr< dynamicgraph::Vector, int > torqueLimitSIN
#define sotDEBUGIN(level)
Definition: debug.hh:211
#define SOT_FULL_TO_REDUCED(sotName)
void computeIncrement(const dynamicgraph::Vector &torques, const dynamicgraph::Vector &torqueLimits, const dynamicgraph::Vector &currentNormVel)
Computes the.
dynamicgraph::SignalTimeDependent< dynamicgraph::Vector, int > torqueReduceSOUT
dynamicgraph::Vector & computeDesiredPosition(const dynamicgraph::Vector &currentPos, const dynamicgraph::Vector &desiredPos, const dynamicgraph::Vector &torques, const dynamicgraph::Vector &torqueLimits, dynamicgraph::Vector &referencePos)
dynamicgraph::SignalTimeDependent< dynamicgraph::Vector, int > torqueLimitReduceSOUT
#define SOT_MEMBER_SIGNAL_4(sotFunction, sotArg1, sotArg1Type, sotArg2, sotArg2Type, sotArg3, sotArg3Type, sotArg4, sotArg4Type)
virtual void plug(SignalBase< Time > *ref)
dynamicgraph::SignalTimeDependent< dynamicgraph::Vector, int > desiredPositionSOUT
static dynamicgraph::Vector & selector(const dynamicgraph::Vector &fullsize, const Flags &selec, dynamicgraph::Vector &desPos)
select only some of the values of the vector fullsize, based on the Flags vector. ...
dynamicgraph::SignalPtr< dynamicgraph::Vector, int > positionFullSizeSIN
#define sotDEBUG(level)
Definition: debug.hh:165
FCL_REAL size() const
#define dgRTLOG()
The goal of this entity is to ensure that the maximal torque will not be exceeded during a grasping t...
std::size_t Index
dynamicgraph::SignalPtr< dynamicgraph::Vector, int > torqueSIN
dynamicgraph::SignalPtr< dynamicgraph::Vector, int > torqueLimitFullSizeSIN
dynamicgraph::SignalPtr< dynamicgraph::Vector, int > torqueFullSizeSIN
void addCommand(const std::string &name, command::Command *command)
dynamicgraph::SignalPtr< dynamicgraph::Vector, int > positionDesSIN
const double DT
dynamicgraph::SignalPtr< dynamicgraph::Vector, int > positionSIN


sot-core
Author(s): Olivier Stasse, ostasse@laas.fr
autogenerated on Wed Jun 21 2023 02:51:26