icub-client
touchDetectorModule.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 iCub Facility - Istituto Italiano di Tecnologia
3  * Author: Bertand Higy
4  * email: bertrand.higy@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/LogStream.h>
19 #include "touchDetectorModule.h"
20 
21 using namespace std;
22 using namespace yarp::os;
23 
24 bool TouchDetectorModule::configure(ResourceFinder &rf)
25 {
26  // Initialize parameters
27  initializeParameters(rf);
28 
29  // open ports
30  if (!openPorts())
31  return false;
32 
33  // create the thread and pass pointers to the module parameters
34  thread = new TouchDetectorThread(&torsoPort, &leftArmPort, &rightArmPort, &leftForearmPort, &rightForearmPort, &leftHandPort, &rightHandPort, &touchPort, &touchPortCleaned, period, clustersConfFilepath, threshold, taxelThreshold);
35  // now start the thread to do the work
36  thread->start(); // this calls threadInit() and it if returns true, it then calls run()
37 
38  return true ;
39 }
40 
42 {
43  return true;
44 }
45 
47 {
48  torsoPort.interrupt();
49  leftArmPort.interrupt();
50  rightArmPort.interrupt();
51  leftForearmPort.interrupt();
52  rightForearmPort.interrupt();
53  leftHandPort.interrupt();
54  rightHandPort.interrupt();
55  touchPort.interrupt();
56  touchPortCleaned.interrupt();
57  return true;
58 }
59 
61 {
62  /* stop the thread */
63  thread->stop();
64 
65  torsoPort.close();
66  leftArmPort.close();
67  rightArmPort.close();
68  leftForearmPort.close();
69  rightForearmPort.close();
70  leftHandPort.close();
71  rightHandPort.close();
72  touchPort.close();
73  touchPortCleaned.close();
74 
75  delete thread;
76 
77  return true;
78 }
79 
81 {
82  moduleName = rf.check("name", Value("touchDetector"), "Module name (string)").asString();
83  setName(moduleName.c_str());
84  period = rf.check("period", Value(1000 / 10), "Thread period (string)").asInt();
85  threshold = rf.check("threshold", Value(50.0), "Activation threshold (double)").asDouble();
86  taxelThreshold = rf.check("taxelThreshold", Value(3), "Minimum number of taxels which need to be active (int)").asInt();
87  rf.setDefault("clustersConfFile", Value("clustersConfig.ini"));
88  clustersConfFilepath = rf.findFile("clustersConfFile");
89 
90  // get the name of the input and output ports, automatically prefixing the module name by using getName()
91  torsoPortName = "/";
92  torsoPortName += getName(rf.check("torsoPort", Value("/torso:i"), "Torso input port (string)").asString());
93  leftArmPortName = "/";
94  leftArmPortName += getName(rf.check("leftArmPort", Value("/left_arm:i"), "Left arm input port (string)").asString());
95  rightArmPortName = "/";
96  rightArmPortName += getName(rf.check("rightArmPort", Value("/right_arm:i"), "Right arm input port (string)").asString());
97  leftForearmPortName = "/";
98  leftForearmPortName += getName(rf.check("leftForearmPort", Value("/left_forearm:i"), "Left forearm input port (string)").asString());
99  rightForearmPortName = "/";
100  rightForearmPortName += getName(rf.check("rightForearmPort", Value("/right_forearm:i"), "Right forearm input port (string)").asString());
101  leftHandPortName = "/";
102  leftHandPortName += getName(rf.check("leftHandPort", Value("/left_hand:i"), "Left hand input port (string)").asString());
103  rightHandPortName = "/";
104  rightHandPortName += getName(rf.check("rightHandPort", Value("/right_hand:i"), "Right hand input port (string)").asString());
105  touchPortName = "/";
106  touchPortName += getName(rf.check("touchPort", Value("/touch:o"), "Touch output port (string)").asString());
107  touchPortCleanName = "/";
108  touchPortCleanName += getName(rf.check("touchPortClean", Value("/touchClean:o"), "Touch clean output port (string)").asString());
109 }
110 
112 {
113  if (!torsoPort.open(torsoPortName.c_str())) {
114  yError() << getName() << ": unable to open port " << torsoPortName;
115  return false;
116  }
117  if (!leftArmPort.open(leftArmPortName.c_str())) {
118  yError() << getName() << ": unable to open port " << leftArmPortName;
119  return false;
120  }
121  if (!rightArmPort.open(rightArmPortName.c_str())) {
122  yError() << getName() << ": unable to open port " << rightArmPortName;
123  return false;
124  }
125  if (!leftForearmPort.open(leftForearmPortName.c_str())) {
126  yError() << getName() << ": unable to open port " << leftForearmPortName;
127  return false;
128  }
129  if (!rightForearmPort.open(rightForearmPortName.c_str())) {
130  yError() << getName() << ": unable to open port " << rightForearmPortName;
131  return false;
132  }
133  if (!leftHandPort.open(leftHandPortName.c_str())) {
134  yError() << getName() << ": unable to open port " << leftHandPortName;
135  return false;
136  }
137  if (!rightHandPort.open(rightHandPortName.c_str())) {
138  yError() << getName() << ": unable to open port " << rightHandPortName;
139  return false;
140  }
141  if (!touchPort.open(touchPortName.c_str())) {
142  yError() << getName() << ": unable to open port " << touchPortName;
143  return false;
144  }
145  if (!touchPortCleaned.open(touchPortCleanName.c_str())) {
146  yError() << getName() << ": unable to open port " << touchPortCleanName;
147  return false;
148  }
149  return true;
150 }
151 
STL namespace.
bool configure(yarp::os::ResourceFinder &rf)
void initializeParameters(yarp::os::ResourceFinder &rf)