icub-client
main.cpp
Go to the documentation of this file.
1 /*
2 * Copyright(C) 2015 WYSIWYD Consortium, European Commission FP7 Project ICT - 612139
3 * Authors: Gregoire Pointeau
4 * email : gregoire.pointeau@inserm.fr
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 * icub-client / 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 <csignal>
19 #include <vector>
20 #include <string>
21 
22 #include <yarp/os/all.h>
23 #include <yarp/sig/all.h>
26 
27 using namespace std;
28 using namespace yarp::os;
29 using namespace yarp::sig;
30 using namespace icubclient;
31 
32 namespace
33 {
34  volatile std::sig_atomic_t gSignalStatus;
35 }
36 
37 void signal_handler(int signal)
38 {
39  gSignalStatus=signal;
40 }
41 
42 
43 int main(int argc, char *argv[])
44 {
45  Network yarp;
46  if (!yarp.checkNetwork())
47  {
48  yError()<<"YARP network seems unavailable!";
49  return 1;
50  }
51 
52  ResourceFinder rf;
53  rf.configure(argc,argv);
54 
55  ICubClient icub("test_cartesian_sim","icubClient","test_cartesian_sim.ini");
56  if (!icub.connect())
57  {
58  yError()<<"One or more functionalities seem unavailable!";
59  return 1;
60  }
61 
62  double X_obj = -0.4;
63  double Y_obj = 0.5;
64  double Z_obj = 0.1;
65  double X_ag = -0.4;
66  double Y_ag = 0.5;
67  double Z_ag = 0.5;
68 
69  // populate OPC: begin
70  icub.opc->clear();
71 
72  Object* obj1 = icub.opc->addOrRetrieveEntity<Object>("bottom_left");
73  obj1->m_ego_position[0] = X_obj;
74  obj1->m_ego_position[1] = -1.* Y_obj;
75  obj1->m_ego_position[2] = Z_obj;
76  obj1->m_present = 1.0;
77  obj1->m_color[0] = Random::uniform(0, 80);
78  obj1->m_color[1] = Random::uniform(80, 180);
79  obj1->m_color[2] = Random::uniform(180, 250);
80 
81  Object* obj2 = icub.opc->addOrRetrieveEntity<Object>("top_left");
82  obj2->m_ego_position[0] = X_ag;
83  obj2->m_ego_position[1] = -1.* Y_ag;
84  obj2->m_ego_position[2] = Z_ag;
85  obj2->m_present = 1.0;
86  obj2->m_color[0] = Random::uniform(0, 180);
87  obj2->m_color[1] = Random::uniform(0, 80);
88  obj2->m_color[2] = Random::uniform(180, 250);
89 
90  Object* obj3 = icub.opc->addOrRetrieveEntity<Object>("top_right");
91  obj3->m_ego_position[0] = X_ag;
92  obj3->m_ego_position[1] = Y_ag;
93  obj3->m_ego_position[2] = Z_ag;
94  obj3->m_present = 1.0;
95  obj3->m_color[0] = Random::uniform(100, 180);
96  obj3->m_color[1] = Random::uniform(80, 180);
97  obj3->m_color[2] = Random::uniform(0, 80);
98 
99  Object* obj4 = icub.opc->addOrRetrieveEntity<Object>("bottom_right");
100  obj4->m_ego_position[0] = X_obj;
101  obj4->m_ego_position[1] = Y_obj;
102  obj4->m_ego_position[2] = Z_obj;
103  obj4->m_present = 1.0;
104  obj4->m_color[0] = Random::uniform(100, 180);
105  obj4->m_color[1] = Random::uniform(0, 80);
106  obj4->m_color[2] = Random::uniform(180, 250);
107 
108  vector<Object*> vObject;
109  vObject.push_back(obj1);
110  vObject.push_back(obj2);
111  vObject.push_back(obj3);
112  vObject.push_back(obj4);
113  icub.opc->commit();
114  // populate OPC: end
115 
116  if (rf.check("populate-only"))
117  return 0;
118 
119  // catch ctrl-c sequence
120  std::signal(SIGINT,signal_handler);
121 
122  // loop pointing
123  for (int i = 0; i < 5; i++)
124  {
125  for (vector<Object*>::iterator itOb = vObject.begin(); itOb != vObject.end(); itOb++)
126  {
127  string obj_name=(*itOb)->name();
128  string obj_pos=(*itOb)->m_ego_position.toString(3,3).c_str();
129 
130  yInfo() << "Object " << obj_name << " is in position (" << obj_pos << ") [m]";
131 
132  if (gSignalStatus==SIGINT)
133  {
134  yWarning("SIGINT detected: closing ...");
135  break;
136  }
137 
138  yInfo() << "\t\t" << "looking at it for a while ...";
139  icub.look(obj_name);
140  Time::delay(2.0);
141 
142  if (gSignalStatus==SIGINT)
143  {
144  yWarning("SIGINT detected: closing ...");
145  break;
146  }
147 
148  yInfo() << "\t\t" << "pointing initiated";
149  icub.point(obj_name);
150  yInfo() << "\t\t" << "pointing complete";
151 
152  if (gSignalStatus==SIGINT)
153  {
154  yWarning("SIGINT detected: closing ...");
155  break;
156  }
157 
158  yInfo() << "\t\t" << "homing initiated";
159  icub.home();
160  yInfo() << "\t\t" << "homing complete";
161  }
162 
163  if (gSignalStatus==SIGINT)
164  break;
165  }
166 
167  return 0;
168 }
169 
170 
yarp::sig::VectorOf< double > m_color
Mean color of the object (r,g,b) used mainly for debugging/displaying purposes in the iCubGUI...
Definition: object.h:61
bool point(const yarp::sig::VectorOf< double > &target, const yarp::os::Bottle &options=yarp::os::Bottle())
Point at a specified location from the iCub.
Grants access to high level motor commands (grasp, touch, look, goto, etc) of the robot as well as it...
Definition: icubClient.h:66
bool connect(const std::string &opcName="OPC")
Try to connect all functionalities.
Definition: icubClient.cpp:132
double m_present
Is the object present in the scene A value of 1.0 means that the object currently is in the scene A v...
Definition: object.h:69
void commit()
Commit all the entities & relations stored locally, erasing the server copies.
Definition: opcClient.cpp:835
T * addOrRetrieveEntity(const std::string &name)
Obtains an entity with the given name.
Definition: opcClient.h:119
void signal_handler(int signal)
Definition: main.cpp:37
STL namespace.
Represent any physical entity (including objects and agents) that can be stored within the OPC...
Definition: object.h:35
yarp::sig::VectorOf< double > m_ego_position
Position of the Object, in the initial ego-centered reference frame of the agent mainting the OPC (in...
Definition: object.h:46
bool home(const std::string &part="all")
Go in home position.
int main()
Definition: main.cpp:32
bool look(const std::string &target, const yarp::os::Bottle &options=yarp::os::Bottle())
Look at a specified target object.
void clear()
Clear the OPC content.
Definition: opcClient.cpp:80