d4c
main.cpp
1 /*
2  * Copyright (C) 2013 iCub Facility - Istituto Italiano di Tecnologia
3  * Author: Ilaria Gori, Ugo Pattacini
4  * email: ilaria.gori@iit.it, ugo.pattacini@iit.it
5  * Permission is granted to copy, distribute, and/or modify this program
6  * under the terms of the GNU General Public License, version 2 or any
7  * later version published by the Free Software Foundation.
8  *
9  * A copy of the license can be found at
10  * http://www.robotcub.org/icub/license/gpl.txt
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15  * Public License for more details
16 */
17 
57 #include <string>
58 #include <cstdio>
59 
60 #include <yarp/os/Network.h>
61 #include <yarp/os/RFModule.h>
62 #include <yarp/sig/Vector.h>
63 #include <yarp/sig/Matrix.h>
64 #include <yarp/math/Math.h>
65 #include <yarp/dev/Drivers.h>
66 #include <yarp/dev/PolyDriver.h>
67 #include <yarp/dev/CartesianControl.h>
68 
69 #include <iCub/d4c/d4c_client.h>
70 
71 using namespace std;
72 using namespace yarp::os;
73 using namespace yarp::sig;
74 using namespace yarp::math;
75 using namespace iCub::d4c;
76 
77 
78 /************************************************************************/
79 class ClientModule: public RFModule
80 {
81 protected:
82  D4CClient client;
83  Property targetOptRx,ObsOptRx;
84  bool init;
85  bool closing;
86  yarp::dev::PolyDriver dCtrl;
87  yarp::dev::ICartesianControl *iCtrl;
88  int store_context_id;
89  int target;
90  int obstacle;
91  Vector xTg;
92  Vector o0;
93  double dist;
94  string arm;
95 
96 public:
97 
98  /************************************************************************/
99  bool configure(ResourceFinder &rf)
100  {
101  int verbosity=rf.check("verbosity",Value(0)).asInt();
102  string remote=rf.check("remote",Value("d4c_server")).asString().c_str();
103  string local=rf.check("local",Value("d4c_client")).asString().c_str();
104  string robot=rf.check("robot",Value("icub")).asString().c_str();
105  string part=rf.check("part",Value("left_arm")).asString().c_str();
106  arm=(part=="left_arm"?"left":"right");
107 
108  Property options;
109  options.put("verbosity",verbosity);
110  options.put("remote",("/"+remote).c_str());
111  options.put("local",("/"+local).c_str());
112 
113  init=true;
114  closing=false;
115 
116  Property optCtrl;
117  optCtrl.put("device","cartesiancontrollerclient");
118  optCtrl.put("remote",("/"+robot+"/cartesianController/"+part).c_str());
119  optCtrl.put("local",("/"+local+"/cartesian").c_str());
120 
121  if (dCtrl.open(optCtrl))
122  dCtrl.view(iCtrl);
123  else
124  return false;
125 
126  iCtrl->storeContext(&store_context_id);
127 
128  Vector dof;
129  iCtrl->getDOF(dof);
130  Vector newDof=dof;
131  newDof[0]=1.0;
132  newDof[2]=1.0;
133 
134  iCtrl->setDOF(newDof,dof);
135  iCtrl->setLimits(7,-70.0,70.0);
136 
137  return client.open(options);
138  }
139 
140  /************************************************************************/
141  bool close()
142  {
143  if (!closing)
144  {
145  client.disableControl();
146  client.disableField();
147 
148  iCtrl->restoreContext(store_context_id);
149  dCtrl.close();
150 
151  client.clearItems();
152  }
153 
154  client.close();
155  return true;
156  }
157 
158  /************************************************************************/
159  bool updateModule()
160  {
161  if (init)
162  {
163  Vector x(3),o;
164  x[0]=-0.2;
165  x[1]=(arm=="left"?-0.05:0.05);
166  x[2]=0.1;
167  Matrix R(3,3); R=0.0;
168  R(0,0)=-1.0; R(2,1)=-1.0; R(1,2)=-1.0;
169  o0=dcm2axis(R);
170  iCtrl->goToPoseSync(x,o0,2.0);
171  iCtrl->waitMotionDone();
172  iCtrl->getPose(x,o);
173  iCtrl->setTrajTime(1.0);
174  iCtrl->setInTargetTol(1e-3);
175 
176  xTg=x;
177  xTg[0]-=0.2;
178  dist=norm(xTg-x);
179  Value centerTg; centerTg.fromString(("("+string(xTg.toString().c_str())+")").c_str());
180  Value radiusTg; radiusTg.fromString("(0.01 0.01 0.01)");
181 
182  Property targetOpt;
183  targetOpt.put("type","target_msd");
184  targetOpt.put("active","on");
185  targetOpt.put("K",2.0);
186  targetOpt.put("D",2.5);
187  targetOpt.put("name","target");
188  targetOpt.put("center",centerTg);
189  targetOpt.put("radius",radiusTg);
190  client.addItem(targetOpt,target);
191 
192  Vector xOb=x;
193  xOb[0]=(x[0]+xTg[0])/2.0;
194  xOb[1]+=(arm=="left"?0.05:-0.05);
195  Value centerOb; centerOb.fromString(("("+string(xOb.toString().c_str())+")").c_str());
196  Value radiusOb; radiusOb.fromString("(0.1 0.1 0.1)");
197 
198  Property obstacleOpt;
199  obstacleOpt.put("type","obstacle_gaussian");
200  obstacleOpt.put("active","on");
201  obstacleOpt.put("G",5.0);
202  obstacleOpt.put("name","obstacle");
203  obstacleOpt.put("center",centerOb);
204  obstacleOpt.put("radius",radiusOb);
205  obstacleOpt.put("cut_tails","on");
206  client.addItem(obstacleOpt,obstacle);
207 
208  client.setActiveIF(arm);
209  client.setPointStateToTool();
210  client.enableControl();
211  client.enableField();
212 
213  init=false;
214  return true;
215  }
216  else
217  {
218  Vector x,o,xdot,odot;
219  client.getPointState(x,o,xdot,odot);
220  double d1=norm(xTg-x);
221 
222  Vector xee,oee;
223  iCtrl->getPose(xee,oee);
224  double d2=norm(x-xee);
225 
226  Vector zero(4); zero=0.0;
227  Vector o1=zero;
228  o1[0]=(arm=="left"?-1.0:1.0); o1[3]=M_PI/2.0*(1.0-d1/dist);
229  o=dcm2axis(axis2dcm(o1)*axis2dcm(o0));
230  client.setPointOrientation(o,zero);
231 
232  fprintf(stdout,"|xTg-x|=%g [m], |x-xee|=%g[m]\n",d1,d2);
233  if ((d1<2e-3) && (d2<2e-3))
234  {
235  client.disableControl();
236  client.disableField();
237 
238  fprintf(stdout,"job accomplished\n");
239  iCtrl->restoreContext(store_context_id);
240 
241  dCtrl.close();
242  client.clearItems();
243  closing=true;
244 
245  return false;
246  }
247  else
248  return true;
249  }
250  }
251 
252  /************************************************************************/
253  double getPeriod()
254  {
255  return 0.1;
256  }
257 };
258 
259 
260 /************************************************************************/
261 int main(int argc, char *argv[])
262 {
263  Network yarp;
264  if (!yarp.checkNetwork())
265  {
266  fprintf(stdout,"YARP server not available!\n");
267  return -1;
268  }
269 
270  ResourceFinder rf;
271  rf.setVerbose(true);
272  rf.configure(argc,argv);
273 
274  ClientModule mod;
275  return mod.runModule(rf);
276 }
277 
278 
279 
Definition: d4c.h:46