icub-basic-demos
robot_interfaces.cpp
1 /*
2  * Copyright (C) 2011 Department of Robotics Brain and Cognitive Sciences - Istituto Italiano di Tecnologia
3  * Author: Marco Randazzo
4  * email: marco.randazzo@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 "robot_interfaces.h"
19 #include <string>
20 
21 using namespace yarp::os;
22 using namespace yarp::dev;
23 
24 robot_interfaces::robot_interfaces()
25 {
26  for (int i=0; i<5; i++)
27  {
28  ipos[i]=0;
29  itrq[i]=0;
30  iimp[i]=0;
31  icmd[i]=0;
32  ienc[i]=0;
33  ipid[i]=0;
34  ivel[i]=0;
35  iamp[i]=0;
36  iint[i]=0;
37  dd[i]=0;
38  }
39 }
40 
41 robot_interfaces::~robot_interfaces()
42 {
43  for (int i = 0; i < 5; i++)
44  {
45  if (dd[i] != 0)
46  {
47  dd[i]->close();
48  delete dd[i];
49  dd[i] = 0;
50  }
51  }
52 }
53 
54 bool robot_interfaces::init(std::string robot)
55 {
56  std::string part;
57  std::string localPort;
58  std::string remotePort;
59  bool ok = true;
60 
61  for (int i=0; i<5; i++)
62  {
63  switch (i)
64  {
65  case LEFT_ARM: part = "left_arm"; break;
66  case RIGHT_ARM: part = "right_arm"; break;
67  case LEFT_LEG: part = "left_leg"; break;
68  case RIGHT_LEG: part = "right_leg"; break;
69  case TORSO: part = "torso"; break;
70  }
71 
72  localPort = "/demoForceControl/" + part;
73  remotePort = "/" + robot + "/" + part;
74  options[i].put("robot",robot);
75  options[i].put("part",part);
76  options[i].put("device","remote_controlboard");
77  options[i].put("local",localPort);
78  options[i].put("remote",remotePort);
79 
80  dd[i] = new PolyDriver(options[i]);
81  if(!dd[i] || !(dd[i]->isValid()))
82  {
83  yError("Problems instantiating the device driver %d\n", i);
84  delete dd[i];
85  dd[i] = 0;
86  ok = false;
87  continue;
88  }
89 
90  ok = ok & dd[i]->view(ipos[i]);
91  ok = ok & dd[i]->view(itrq[i]);
92  ok = ok & dd[i]->view(iimp[i]);
93  ok = ok & dd[i]->view(icmd[i]);
94  ok = ok & dd[i]->view(ivel[i]);
95  ok = ok & dd[i]->view(ienc[i]);
96  ok = ok & dd[i]->view(ipid[i]);
97  ok = ok & dd[i]->view(iamp[i]);
98  ok = ok & dd[i]->view(iint[i]);
99  }
100  return ok;
101 }
102 
103