icub-client
touchDetectorThread.h
Go to the documentation of this file.
1 #ifndef RPCLISTENERTHREADH
2 #define RPCLISTENERTHREADH
3 
4 /*
5  * Copyright (C) 2015 iCub Facility - Istituto Italiano di Tecnologia
6  * Author: Bertand HIGY
7  * email: bertrand.higy@iit.it
8  * Permission is granted to copy, distribute, and/or modify this program
9  * under the terms of the GNU General Public License, version 2 or any
10  * later version published by the Free Software Foundation.
11  *
12  * A copy of the license can be found at
13  * http://www.robotcub.org/icub/license/gpl.txt
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
18  * Public License for more details
19 */
20 
21 #include <string>
22 #include <vector>
23 #include <exception>
24 
25 #include <yarp/os/Bottle.h>
26 #include <yarp/os/BufferedPort.h>
27 #include <yarp/os/RateThread.h>
28 
32 class TouchDetectorThread : public yarp::os::RateThread
33 {
34  public:
35  TouchDetectorThread(yarp::os::BufferedPort<yarp::os::Bottle> *torsoPort, yarp::os::BufferedPort<yarp::os::Bottle> *leftArmPort, yarp::os::BufferedPort<yarp::os::Bottle> *rightArmPort, yarp::os::BufferedPort<yarp::os::Bottle> *leftForearmPort, yarp::os::BufferedPort<yarp::os::Bottle> *rightForearmPort, yarp::os::BufferedPort<yarp::os::Bottle> *leftHandPort, yarp::os::BufferedPort<yarp::os::Bottle> *rightHandPort, yarp::os::BufferedPort<yarp::os::Bottle> *touchPort, yarp::os::BufferedPort<yarp::os::Bottle> *touchPortCleaned, int period, std::string clustersConfFilepath, double threshold, int taxelThreshold);
36  void run();
37  bool threadInit();
38 
39  protected:
40  static const int noCluster;
41  static const int nbBodyParts;
42  static const char* bodyParts[7];
43  static const int nbTaxels[7];
44 
46  double threshold;
48  std::string clustersConfFilepath;
49  std::vector<int> taxels2Clusters[7];
50 
51  /* ports */
52  yarp::os::BufferedPort<yarp::os::Bottle> *torsoPort;
53  yarp::os::BufferedPort<yarp::os::Bottle> *leftArmPort;
54  yarp::os::BufferedPort<yarp::os::Bottle> *rightArmPort;
55  yarp::os::BufferedPort<yarp::os::Bottle> *leftForearmPort;
56  yarp::os::BufferedPort<yarp::os::Bottle> *rightForearmPort;
57  yarp::os::BufferedPort<yarp::os::Bottle> *leftHandPort;
58  yarp::os::BufferedPort<yarp::os::Bottle> *rightHandPort;
59  yarp::os::BufferedPort<yarp::os::Bottle> *touchPort;
60  yarp::os::BufferedPort<yarp::os::Bottle> *touchPortCleaned;
61 
67  bool readTaxelsMapping(std::string filename);
68 
74  int getBodyPartId(std::string bodyPartName);
75 
84  void updateMapping(int bodyPart, int firstTaxel, int lastTaxel, int cluster);
85 
92  void countActivations(int bodyPart, yarp::os::Bottle *data, std::vector<int> &activations);
93 
101  void processPort(int portNum, yarp::os::BufferedPort<yarp::os::Bottle> *port, std::vector<int> &activations);
102 };
103 
104 class ParsingException: public std::exception
105 {
106  public:
107  int line;
108 
110  virtual const char* what() const throw();
111 };
112 
113 class BadFormatException: public std::exception
114 {
115 public:
116  std::string expectedType;
117  std::string portName;
118 
120  virtual const char* what() const throw();
121 };
122 
123 #endif
bool readTaxelsMapping(std::string filename)
readTaxelsMapping Read taxels map from configure file, which relates to nbBodyParts and bodyParts ...
int getBodyPartId(std::string bodyPartName)
getBodyPartId Obtain the ID of body parts, which are "torso", "left_arm", "right_arm", "left_forearm", "right_forearm", "left_hand", "right_hand"
yarp::os::BufferedPort< yarp::os::Bottle > * rightHandPort
Yarp BufferedPort of bottle for input right hand activations.
int taxelThreshold
integer value of touched threshold
STL namespace.
void countActivations(int bodyPart, yarp::os::Bottle *data, std::vector< int > &activations)
countActivations Counts number of activations to a body part through a buffered port of that part ...
yarp::os::BufferedPort< yarp::os::Bottle > * leftArmPort
Yarp BufferedPort of bottle for input left arm activations.
yarp::os::BufferedPort< yarp::os::Bottle > * rightForearmPort
Yarp BufferedPort of bottle for input right forearm activations.
yarp::os::BufferedPort< yarp::os::Bottle > * touchPort
Yarp BufferedPort of bottle for output touched activations.
yarp::os::BufferedPort< yarp::os::Bottle > * leftHandPort
Yarp BufferedPort of bottle for input left hand activations.
yarp::os::BufferedPort< yarp::os::Bottle > * torsoPort
Yarp BufferedPort of bottle for input torso activations.
yarp::os::BufferedPort< yarp::os::Bottle > * rightArmPort
Yarp BufferedPort of bottle for input right arm activations.
static const int noCluster
yarp::os::BufferedPort< yarp::os::Bottle > * touchPortCleaned
Yarp BufferedPort of bottle for output touched taxel.
std::string clustersConfFilepath
string value of the path to configure file of clusters
yarp::os::BufferedPort< yarp::os::Bottle > * leftForearmPort
Yarp BufferedPort of bottle for input left forearm activations.
static const int nbBodyParts
void processPort(int portNum, yarp::os::BufferedPort< yarp::os::Bottle > *port, std::vector< int > &activations)
processPort Process port of body part to receive activations to that body part
static const int nbTaxels[7]
void updateMapping(int bodyPart, int firstTaxel, int lastTaxel, int cluster)
updateMapping Change all taxels of a body part to clusterID
std::vector< int > taxels2Clusters[7]
standard vector of integer containing the taxel IDs in form of cluster ID
static const char * bodyParts[7]
TouchDetectorThread(yarp::os::BufferedPort< yarp::os::Bottle > *torsoPort, yarp::os::BufferedPort< yarp::os::Bottle > *leftArmPort, yarp::os::BufferedPort< yarp::os::Bottle > *rightArmPort, yarp::os::BufferedPort< yarp::os::Bottle > *leftForearmPort, yarp::os::BufferedPort< yarp::os::Bottle > *rightForearmPort, yarp::os::BufferedPort< yarp::os::Bottle > *leftHandPort, yarp::os::BufferedPort< yarp::os::Bottle > *rightHandPort, yarp::os::BufferedPort< yarp::os::Bottle > *touchPort, yarp::os::BufferedPort< yarp::os::Bottle > *touchPortCleaned, int period, std::string clustersConfFilepath, double threshold, int taxelThreshold)