iCub-main
compensationThread.h
Go to the documentation of this file.
1 
2 /*
3  * Copyright (C) 2009 RobotCub Consortium, European Commission FP6 Project IST-004370
4  * Authors: Andrea Del Prete, Alexander Schmitz
5  * email: andrea.delprete@iit.it, alexander.schmitz@iit.it
6  * website: www.robotcub.org
7  * Permission is granted to copy, distribute, and/or modify this program
8  * under the terms of the GNU General Public License, version 2 or any
9  * later version published by the Free Software Foundation.
10  *
11  * A copy of the license can be found at
12  * http://www.robotcub.org/icub/license/gpl.txt
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17  * Public License for more details
18  */
19 
20 #ifndef __COMPTHREAD_H__
21 #define __COMPTHREAD_H__
22 
23 #include <mutex>
24 #include <iostream>
25 #include <string>
26 #include <sstream>
27 #include <vector>
28 
29 #include <yarp/sig/Vector.h>
30 #include <yarp/os/BufferedPort.h>
31 #include <yarp/os/PeriodicThread.h>
32 #include <yarp/os/ResourceFinder.h>
33 #include <yarp/dev/IAnalogSensor.h>
34 #include <yarp/dev/PolyDriver.h>
35 
38 
39 using namespace std;
40 using namespace yarp::os;
41 using namespace yarp::sig;
42 using namespace yarp::dev;
43 using namespace iCub::skinDynLib;
44 
45 namespace iCub{
46 
47 namespace skinManager{
48 
49 class CompensationThread : public PeriodicThread
50 {
51 public:
52  typedef enum { calibration, compensation} CompensationThreadState;
53 
54  /* class methods */
55 
56  CompensationThread(string name, ResourceFinder* rf, string robotName, double _compensationGain, double _contactCompensationGain,
57  int addThreshold, float minBaseline, bool zeroUpRawData, int period, bool binarization, bool smoothFilter, float smoothFactor);
58  bool threadInit();
59  void threadRelease();
60  void run();
61 
62  void calibrate();
63  void setBinarization(bool value);
64  void setSmoothFilter(bool value);
65  bool setSmoothFactor(float value);
66  bool setAddThreshold(unsigned int thr);
67  bool setCompensationGain(double gain);
68  bool setContactCompensationGain(double gain);
69  bool setMaxNeighborDistance(double dist);
70  bool setTaxelPosition(SkinPart sp, unsigned int taxelId, const Vector &position);
71  bool setTaxelPositions(SkinPart sp, const Vector &positions);
72  bool setTaxelOrientation(SkinPart sp, unsigned int taxelId, const Vector &orientation);
73  bool setTaxelOrientations(SkinPart sp, const vector<Vector> &orientations);
74  bool setTaxelPose(SkinPart sp, unsigned int taxelId, const Vector &pose);
75  bool setTaxelPoses(SkinPart sp, const vector<Vector> &poses);
76  bool setTaxelPoses(SkinPart sp, const Vector &poses);
77 
78  Vector getTouchThreshold();
79  bool getBinarization();
80  bool getSmoothFilter();
81  float getSmoothFactor();
82  unsigned int getAddThreshold();
83  double getCompensationGain();
84  double getContactCompensationGain();
85  double getMaxNeighborDistance(){ return maxNeighDist; }
86  bool isCalibrating();
87 
88  Vector getTaxelPosition(SkinPart sp, unsigned int taxelId);
89  vector<Vector> getTaxelPositions(SkinPart sp=SKIN_PART_ALL);
90  Vector getTaxelOrientation(SkinPart sp, unsigned int taxelId);
91  vector<Vector> getTaxelOrientations(SkinPart sp=SKIN_PART_ALL);
92  Vector getTaxelPose(SkinPart sp, unsigned int taxelId);
93  vector<Vector> getTaxelPoses(SkinPart sp=SKIN_PART_ALL);
94  double getPoseConfidence(SkinPart sp, unsigned int taxelId);
95  Vector getPoseConfidences(SkinPart sp);
96 
97  bool enableSkinPart(SkinPart sp);
98  bool disableSkinPart(SkinPart sp);
99  bool isSkinEnabled(SkinPart sp);
100  vector<SkinPart> getSkinParts();
101  Bottle getInfo();
102 
103 
104 private:
105 
106  /* class constants */
107  static const int CAL_TIME = 5; // calibration time in sec
108 
109  /* class variables */
110  int CAL_SAMPLES; // num of samples needed for calibration
111  int ADD_THRESHOLD; // value added to the touch threshold of every taxel
112  unsigned int SKIN_DIM; // total number of taxels
113  double compensationGain; // the gain of the compensation algorithm
114  double contactCompensationGain; // the gain of the compensation algorithm during contact
115 
116  bool initializationFinished;
117 
118  // calibration variables
119  int calibrationCounter; // count the calibration cycles
120  int readErrorCounter; // it counts the number of successive errors
121 
122  ResourceFinder* rf;
123 
124  // input parameters
125  string moduleName;
126  string robotName;
127  float minBaseline; // if the baseline value is less than this, then a calibration is executed (if allowed)
128  bool zeroUpRawData; // if true the raw data are considered from zero up, otherwise from 255 down
129  bool binarization; // if true binarize the compensated output value (0: no touch, 255: touch)
130  bool smoothFilter; // if true the smooth filter is on, otherwise it is off
131  float smoothFactor; // intensity of the smooth filter action
132  double maxNeighDist; // max neighbor distance used for computing taxel neighbors
133  unsigned int portNum; // number of input ports (that is the same as the number of output ports)
134 
135  vector<Compensator*> compensators;
136  vector<bool> compEnable; // true if the related compensator is enabled, false otherwise
137  vector<bool> compWorking; // true if the related compensator is working, false otherwise
138  unsigned int compensatorCounter; // count the number of compensators that are working
139 
140  // SKIN EVENTS
141  bool skinEventsOn;
142 
143  /* ports */
144  BufferedPort<skinContactList> skinEventsPort; // skin events output port
145  BufferedPort<Vector> monitorPort; // monitoring output port (streaming)
146  BufferedPort<Bottle> infoPort; // info output port
147 
148  CompensationThreadState state; // state of the thread (calibration, compensation)
149  mutex stateSem;
150 
151  /* class private methods */
152  void checkErrors();
153  bool doesBaselineExceed(unsigned int &compInd, unsigned int &taxInd, double &baseline, double &initialBaseline);
154  void sendMonitorData();
155  void sendDebugMsg(string msg);
156  void sendErrorMsg(string msg);
157  void sendSkinEvents();
158 
159 };
160 
161 } //namespace iCub
162 
163 } //namespace skinManager
164 
165 #endif
166 
std::vector< SkinPart > getSkinParts(BodyPart b)
Get the list of skin parts associated to the specified body part.
Definition: common.cpp:17
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.