RobotHandLocalizationWithFingertips.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package VisionX::Component
17 * @author Kai Welke <kai dot welke at kit dot edu>
18 * @copyright 2011 Humanoids Group, HIS, KIT
19 * @license http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
24 #include "Helpers/helpers.h"
25 #include "../../../vision_algorithms/HandLocalizationWithFingertips/HandLocalisation.h"
26 #include "../../../vision_algorithms/HandLocalizationWithFingertips/Visualization/HandModelVisualizer.h"
27 
28 // Eigen
29 #include <Eigen/Core>
30 
31 // IVT
32 #include <Calibration/StereoCalibration.h>
33 #include <Math/Math3d.h>
34 #include <Image/ImageProcessor.h>
35 
36 // Core
38 
39 // VisionX
42 
43 
44 
45 using namespace armarx;
46 
47 namespace visionx
48 {
49  void RobotHandLocalizationWithFingertips::onInitImageProcessor()
50  {
51  // set desired image provider
52  providerName = getProperty<std::string>("ImageProviderAdapterName").getValue();
53  usingImageProvider(providerName);
54 
55  robotStateProxyName = getProperty<std::string>("RobotStateProxyName").getValue();
56  usingProxy(robotStateProxyName);
57 
58  handFrameName = getProperty<std::string>("HandFrameName").getValue();
59  cameraFrameName = getProperty<std::string>("CameraFrameName").getValue();
60  }
61 
62  void RobotHandLocalizationWithFingertips::onConnectImageProcessor()
63  {
64  // connect to image provider
65  ARMARX_INFO << getName() << " connecting to " << providerName << armarx::flush;
66  visionx::ImageProviderInfo imageProviderInfo = getImageProvider(providerName);
67  imageProviderPrx = getProxy<ImageProviderInterfacePrx>(providerName);
68  imageFormat = imageProviderInfo.imageFormat;
69 
70  cameraImages = new CByteImage*[2];
71  cameraImages[0] = tools::createByteImage(imageProviderInfo);
72  cameraImages[1] = tools::createByteImage(imageProviderInfo);
73 
74  resultImages = new CByteImage*[numResultImages];
75 
76  for (int i = 0; i < numResultImages; i++)
77  {
78  resultImages[i] = tools::createByteImage(imageProviderInfo);
79  }
80 
81  // retrieve stereo information
82  StereoCalibrationProviderInterfacePrx calibrationProviderPrx = StereoCalibrationProviderInterfacePrx::checkedCast(imageProviderPrx);
83 
84  if (!calibrationProviderPrx)
85  {
86  ARMARX_ERROR << "Image provider with name " << providerName << " is not a StereoCalibrationProvider" << std::endl;
87  return;
88  }
89 
90  stereoCalibration = visionx::tools::convert(calibrationProviderPrx->getStereoCalibration());
91 
92 
93  // connect to robot state proxy
94  ARMARX_INFO << getName() << " connecting to " << robotStateProxyName << armarx::flush;
95  robotStateProxy = getProxy<RobotStateComponentInterfacePrx>(robotStateProxyName);
96 
97  this->enableResultImages(numResultImages, imageFormat.dimension, imageFormat.type);
98 
99  // construct hand localizer
100  handLocalization = new CHandLocalisation(600, 2, 2, stereoCalibration, DSHT_HAND_MODEL_PATH); //6000, 2, 2
101  handModelVisualizer = new CHandModelVisualizer(stereoCalibration);
102  }
103 
104  void RobotHandLocalizationWithFingertips::process()
105  {
106  if (!waitForImages(8000))
107  {
108  ARMARX_WARNING << "Timeout or error in wait for images" << armarx::flush;
109  }
110  else
111  {
112  // get images
113  int nNumberImages = getImages(cameraImages);
114  ARMARX_VERBOSE << getName() << " got " << nNumberImages << " images";
115 
116  // get hand pose from robot state
117  armarx::PosePtr handNodePosePtr = armarx::PosePtr::dynamicCast(robotStateProxy->getSynchronizedRobot()->getRobotNode(handFrameName)->getPoseInRootFrame());
118  Eigen::Matrix4f handNodePose = handNodePosePtr->toEigen();
119  armarx::PosePtr cameraNodePosePtr = armarx::PosePtr::dynamicCast(robotStateProxy->getSynchronizedRobot()->getRobotNode(cameraFrameName)->getPoseInRootFrame());
120  Eigen::Matrix4f cameraNodePose = cameraNodePosePtr->toEigen();
121  Eigen::Matrix4f handPoseInCameraFrame = cameraNodePose.inverse() * handNodePose;
122  Vec3d handNodePosition = {handPoseInCameraFrame(0, 3), handPoseInCameraFrame(1, 3), handPoseInCameraFrame(2, 3)};
123  Mat3d handNodeOrientation = {handPoseInCameraFrame(0, 0), handPoseInCameraFrame(0, 1), handPoseInCameraFrame(0, 2),
124  handPoseInCameraFrame(1, 0), handPoseInCameraFrame(1, 1), handPoseInCameraFrame(1, 2),
125  handPoseInCameraFrame(2, 0), handPoseInCameraFrame(2, 1), handPoseInCameraFrame(2, 2)
126  };
127 
128 
129  // TODO: get finger config
130  double* fingerConfig = new double[6];
131  fingerConfig[0] = 90 * M_PI / 180; //100 // palm
132  fingerConfig[1] = 25 * M_PI / 180; // thumb1
133  fingerConfig[2] = 15 * M_PI / 180; // thumb2
134  fingerConfig[3] = 10 * M_PI / 180; // index
135  fingerConfig[4] = 5 * M_PI / 180; // middle
136  fingerConfig[5] = 5 * M_PI / 180; // ring+pinky
137 
138 
139  // localize hand
140  double* estimatedConfig = new double[12];
141  double confidenceRating;
142  handLocalization->LocaliseHand(cameraImages[0], cameraImages[1], handNodePosition, handNodeOrientation, fingerConfig, estimatedConfig, confidenceRating);
143 
144  // draw result images
145  ::ImageProcessor::CopyImage(cameraImages[0], resultImages[0]);
146  ::ImageProcessor::CopyImage(cameraImages[0], resultImages[1]);
147  ::ImageProcessor::CopyImage(cameraImages[0], resultImages[2]);
148  ::ImageProcessor::Zero(resultImages[3]);
149 
150  double* localizationResult = handLocalization->GetResultConfig();
151  handModelVisualizer->UpdateHandModel(localizationResult, drawComplexHandModelInResultImage);
152  delete[] localizationResult;
153  handModelVisualizer->DrawHandModelV2(resultImages[1]);
154  handModelVisualizer->DrawHand(resultImages[2]);
155  handModelVisualizer->DrawSegmentedImage(resultImages[3]);
156  provideResultImages(resultImages);
157  }
158  }
159 
160 
161 
162 
163 
164  void RobotHandLocalizationWithFingertips::onExitImageProcessor()
165  {
166  delete [] cameraImages;
167  }
168 
169 
170 
171  armarx::FramedPoseBasePtr RobotHandLocalizationWithFingertips::getHandPose(const Ice::Current& c)
172  {
173  Eigen::Matrix4f handPose = handLocalization->GetHandPose();
174  armarx::FramedPosePtr ret = new armarx::FramedPose(handPose, cameraFrameName, robotStateProxy->getSynchronizedRobot()->getName());
175  return ret;
176  }
177 
178 
179 
180  visionx::FramedPositionBaseList RobotHandLocalizationWithFingertips::getFingertipPositions(const Ice::Current& c)
181  {
182  visionx::FramedPositionBaseList ret;
183  std::vector<Vec3d> fingertipPositions = handLocalization->GetFingertipPositions();
184 
185  for (size_t i = 0; i < fingertipPositions.size(); i++)
186  {
187  Eigen::Vector3f position;
188  position << fingertipPositions.at(i).x, fingertipPositions.at(i).y, fingertipPositions.at(i).z;
189  armarx::FramedPositionPtr pos = new armarx::FramedPosition(position, cameraFrameName, robotStateProxy->getSynchronizedRobot()->getName());
190  ret.push_back(pos);
191  }
192 
193  return ret;
194  }
195 
196 }
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
armarx::VariantType::FramedPose
const VariantTypeId FramedPose
Definition: FramedPose.h:37
RobotHandLocalizationWithFingertips.h
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
visionx::ImageProviderInfo::imageFormat
ImageFormatInfo imageFormat
Image format struct that contains all necessary image information.
Definition: ImageProcessor.h:496
DSHT_HAND_MODEL_PATH
#define DSHT_HAND_MODEL_PATH
Definition: HandLocalisationConstants.h:62
visionx::tools::createByteImage
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
visionx::ImageProviderInfo
Definition: ImageProcessor.h:466
IceInternal::Handle< Pose >
visionx::tools::convert
CByteImage::ImageType convert(const ImageType visionxImageType)
Converts a VisionX image type into an image type of IVT's ByteImage.
Definition: TypeMapping.cpp:95
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
GfxTL::Vec3d
VectorXD< 3, double > Vec3d
Definition: VectorXD.h:695
visionx::CHandModelVisualizer
Definition: HandModelVisualizer.h:43
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
visionx::CHandLocalisation
Definition: HandLocalisation.h:72
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
ImageUtil.h
TypeMapping.h
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:39
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
Exception.h