iCub-main
main.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Department of Robotics Brain and Cognitive Sciences - Istituto Italiano di Tecnologia
3  * Author: Ugo Pattacini
4  * email: 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 
18 #include <yarp/os/all.h>
19 #include <yarp/dev/all.h>
20 #include <yarp/sig/all.h>
21 #include <yarp/math/Math.h>
22 #include <yarp/math/Rand.h>
23 
24 #include <iostream>
25 #include <iomanip>
26 #include <string>
27 
28 using namespace std;
29 using namespace yarp::os;
30 using namespace yarp::dev;
31 using namespace yarp::sig;
32 using namespace yarp::math;
33 
37 class ClientModule: public RFModule
38 {
39 protected:
40  PolyDriver client;
41  ICartesianControl *arm;
42  Vector xdhat;
43 
44 public:
45  /**********************************************************/
46  bool configure(ResourceFinder &rf)
47  {
48  string remote=rf.find("remote").asString();
49  string local=rf.find("local").asString();
50 
51  // Just usual things...
52  Property option("(device cartesiancontrollerclient)");
53  option.put("remote","/"+remote);
54  option.put("local","/"+local);
55 
56  if (!client.open(option))
57  return false;
58 
59  // open the view
60  client.view(arm);
61  arm->setTrajTime(2.0);
62  arm->setInTargetTol(1e-3);
63 
64  Vector dof;
65  arm->getDOF(dof);
66  for (size_t i=0; i<dof.length(); i++)
67  {
68  double min,max;
69  arm->getLimits(i,&min,&max);
70 
71  // these margins are just to prevent the
72  // solver from solving for exactly the
73  // joints bounds
74  min+=1.0; max-=1.0; // [deg]
75  arm->setLimits(i,min,max);
76  }
77 
78  Rand::init();
79 
80  return true;
81  }
82 
83  /**********************************************************/
84  bool close()
85  {
86  if (client.isValid())
87  client.close();
88 
89  return true;
90  }
91 
92  /**********************************************************/
93  bool updateModule()
94  {
95  bool done=false;
96  arm->checkMotionDone(&done);
97  if (done)
98  {
99  Vector xd(3);
100  xd[0]=Rand::scalar(1.0,2.5);
101  xd[1]=Rand::scalar(0.0,2.0);
102  xd[2]=0.0;
103 
104  cout<<endl;
105  cout<<"Solving for: ("<<xd.toString()<<")"<<endl;
106  arm->goToPositionSync(xd);
107  Vector odhat,qdhat;
108  arm->getDesired(xdhat,odhat,qdhat);
109  cout<<"Going to: ("<<xdhat.toString()<<")"<<endl;
110  cout<<"Solved Configuration: ["<<qdhat.toString()<<"]"<<endl;
111  }
112  else
113  {
114  Vector x,o;
115  arm->getPose(x,o);
116  cout<<"Running: ("<<x.toString()<<"); distance to go: "<<norm(xdhat-x)<<endl;
117  }
118 
119  return true;
120  }
121 
122  /**********************************************************/
123  double getPeriod() { return 0.4; }
124 };
125 
126 
127 /**********************************************************/
128 int main(int argc, char *argv[])
129 {
130  Network yarp;
131  if (!yarp.checkNetwork())
132  {
133  cout<<"Error: yarp server does not seem available"<<endl;
134  return 1;
135  }
136 
137  ResourceFinder rf;
138  rf.setDefault("remote","server");
139  rf.setDefault("local","client");
140  rf.configure(argc,argv);
141 
142  ClientModule client;
143  return client.runModule(rf);
144 }
145 
146 
147 
This class implements the client.
Definition: main.cpp:38
PolyDriver client
Definition: main.cpp:40
Vector xdhat
Definition: main.cpp:42
ICartesianControl * arm
Definition: main.cpp:41
bool close()
Definition: main.cpp:84
double getPeriod()
Definition: main.cpp:123
bool configure(ResourceFinder &rf)
Definition: main.cpp:46
bool updateModule()
Definition: main.cpp:93
double norm(const yarp::sig::Matrix &M, int col)
Returns the norm of the vector given in the form: matrix(:,col).
int main(int argc, char *argv[])
Definition: main.cpp:31
bool done
Definition: main.cpp:42
const FSC max
Definition: strain.h:48
const FSC min
Definition: strain.h:49
Copyright (C) 2008 RobotCub Consortium.