finger_hand.cpp
Go to the documentation of this file.
00001 #include <agile_grasp/finger_hand.h>
00002 
00003 FingerHand::FingerHand(double finger_width, double hand_outer_diameter,
00004                 double hand_depth) :
00005                 finger_width_(finger_width), hand_outer_diameter_(hand_outer_diameter), hand_depth_(
00006                                 hand_depth)
00007 {
00008         int n = 10; // number of finger placements to consider over a single hand diameter
00009 
00010         Eigen::VectorXd fs_half;
00011         fs_half.setLinSpaced(n, 0.0, hand_outer_diameter - finger_width);
00012         finger_spacing_.resize(2 * fs_half.size());
00013         finger_spacing_
00014                         << (fs_half.array() - hand_outer_diameter_ + finger_width_).matrix(), fs_half;
00015         fingers_ = Eigen::Array<bool, 1, Eigen::Dynamic>::Constant(1, 2 * n, false);
00016 //  std::cout << "fs_half: " << fs_half.transpose() << std::endl;
00017 //  std::cout << "finger spacing: " << finger_spacing_.transpose() << std::endl;
00018 }
00019 
00020 void FingerHand::evaluateFingers(double bite)
00021 {
00022         back_of_hand_ = -1.0 * (hand_depth_ - bite);
00023 //  std::cout << "back_of_hand_: " << back_of_hand_ << std::endl;
00024 
00025         fingers_.setConstant(false);
00026 
00027         // crop points at bite
00028         std::vector<int> cropped_indices;
00029         for (int i = 0; i < points_.cols(); i++)
00030         {
00031                 if (points_(1, i) < bite)
00032                 {
00033                         cropped_indices.push_back(i);
00034 
00035                         // Check that the hand would be able to extend by <bite> onto the object without causing back of hand
00036                         // to collide with points.
00037                         if (points_(1, i) < back_of_hand_)
00038                         {
00039                                 return;
00040                         }
00041                 }
00042         }
00043 //  std::cout << "     -A- " << " " << cropped_indices.size() << " " << points_.cols() << "\n";
00044         Eigen::MatrixXd cropped_points(points_.rows(), cropped_indices.size());
00045         for (int i = 0; i < cropped_indices.size(); i++)
00046         {
00047                 cropped_points.col(i) = points_.col(cropped_indices[i]);
00048         }
00049 //  std::cout << "     -C-\n";
00050 
00051         // identify free gaps
00052         int m = finger_spacing_.size();
00053 //  std::cout << "m: " << m << ", finger_spacing_: " << finger_spacing_.transpose() << " " << finger_spacing_.size() << std::endl;
00054         for (int i = 0; i < m; i++)
00055         {
00056 //    std::cout << "  i: " << i << std::endl;
00057                 // int num_in_gap = (cropped_points.array() > finger_spacing_(i)
00058                 // && cropped_points.array() < finger_spacing_(i) + finger_width_).count();
00059                 int num_in_gap = 0;
00060                 for (int j = 0; j < cropped_indices.size(); j++)
00061                 {
00062                         if (cropped_points(0, j) > finger_spacing_(i)
00063                                         && cropped_points(0, j) < finger_spacing_(i) + finger_width_)
00064                                 num_in_gap++;
00065                 }
00066 //    std::cout << "i: " << i << ", numInGap: " << num_in_gap << " finger_width_: " << finger_width_ << " finger_spacing_: " << finger_spacing_(i) << std::endl;
00067 
00068                 if (num_in_gap == 0)
00069                 {
00070                         int sum;
00071 
00072                         if (i <= m / 2)
00073                         {
00074 //        std::cout << " i <= m/2 " << finger_spacing_(i) << "\n";
00075 //        Eigen::Array<bool, 1, Eigen::Dynamic>
00076                                 sum = (cropped_points.row(0).array()
00077                                                 > finger_spacing_(i) + finger_width_).count();
00078                         }
00079                         else
00080                         {
00081 //        std::cout << " i > m/2\n";
00082                                 sum = (cropped_points.row(0).array() < finger_spacing_(i)).count();
00083                         }
00084 
00085 //      std::cout << "                 i: " << i<< " sum: " << sum << " cropped_points.array(): " << cropped_points.array().rows() << " x " << cropped_points.array().cols() << std::endl;
00086 
00087                         if (sum > 0)
00088                         {
00089 //        std::cout << "      sum larger 0\n";
00090                                 fingers_(i) = true;
00091 //        std::cout << fingers_ << "\n";
00092                         }
00093                 }
00094 //    std::cout << " -----\n";
00095         }
00096 //  std::cout << "     -E-\n";
00097 //  std::cout << " fingers_: " << fingers_ << std::endl;
00098 }
00099 
00100 void FingerHand::evaluateHand()
00101 {
00102 //  std::cout << "evaluateHand()\n";
00103         int n = fingers_.size() / 2;
00104 //  std::cout << " n: " << n;
00105         // hand_ = fingers_.block(0, 0, 1, n) && fingers_.block(0, n, 1, n);
00106         hand_.resize(1, n);
00107         for (int i = 0; i < n; i++)
00108         {
00109                 if (fingers_(i) == 1 && fingers_(n + i) == 1)
00110                         hand_(i) = 1;
00111                 else
00112                         hand_(i) = 0;
00113         }
00114 //  std::cout << " hand_: " << hand_ << std::endl;
00115 }
00116 
00117 void FingerHand::evaluateGraspParameters(double bite)
00118 {
00119 //  double hor_pos = (hand_outer_diameter_ / 2.0)
00120 //      + (finger_spacing_.block(0, 0, 1, hand_.cols()).array() * hand_.cast<double>()).sum() / hand_.sum();
00121 //  std::cout << "hand_: " << hand_ << ", hand_.sum(): " << hand_.sum() << std::endl;
00122 //  std::cout << "finger_spacing_: " << finger_spacing_.block(0, 0, 1, hand_.cols()).array() << std::endl;
00123 //  std::cout << "hand_.cast<double>(): " << hand_.cast<double>() << std::endl;
00124 //  std::cout << finger_spacing_.block(0, 0, 1, hand_.cols()).array() * hand_.cast<double>() << std::endl;
00125 //  std::cout << (finger_spacing_.block(0, 0, 1, hand_.cols()).array() * hand_.cast<double>()).sum() << std::endl;
00126 
00127         double fs_sum = 0.0;
00128         for (int i = 0; i < hand_.size(); i++)
00129         {
00130                 fs_sum += finger_spacing_(i) * hand_(i);
00131         }
00132         double hor_pos = (hand_outer_diameter_ / 2.0) + (fs_sum / hand_.sum());
00133 //      std::cout << "fs_sum: " << fs_sum << ", hor_pos: " << hor_pos << ", hand_: "
00134 //                      << hand_ << "\n";
00135         grasp_bottom << hor_pos, points_.row(1).maxCoeff();
00136         grasp_surface << hor_pos, points_.row(1).minCoeff();
00137 //  std::cout << "  grasp_surface:\n" << grasp_surface << std::endl;
00138 //  std::cout << "  grasp_bottom:\n" << grasp_bottom << std::endl;
00139 //  std::cout << " EGP(1)\n";
00140 //  std::cout << " hand_: " << hand_ << ", " << hand_.sum() << std::endl;
00141 
00142         // calculate handWidth. First find eroded hand. Then take points contained within two fingers of that hand.
00143         std::vector<int> hand_idx;
00144         for (int i = 0; i < hand_.cols(); i++)
00145         {
00146                 if (hand_(i) == true)
00147                         hand_idx.push_back(i);
00148         }
00149 //  std::cout << hand_idx.size() << std::endl;
00150         int hand_eroded_idx = hand_idx[hand_idx.size() / 2];
00151 //  std::cout << "hand_eroded_idx: " << hand_eroded_idx << std::endl;
00152         double left_finger_pos = finger_spacing_(hand_eroded_idx);
00153         double right_finger_pos = finger_spacing_(hand_.cols() + hand_eroded_idx);
00154         double max = -100000.0;
00155         double min = 100000.0;
00156 //  std::cout << " EGP(2) left_finger_pos: " << left_finger_pos << " right_finger_pos: " << right_finger_pos << std::endl;
00157         for (int i = 0; i < points_.cols(); i++)
00158         {
00159                 if (points_(1, i) < bite && points_(0, i) > left_finger_pos
00160                                 && points_(0, i) < right_finger_pos)
00161                 {
00162                         if (points_(0, i) < min)
00163                                 min = points_(0, i);
00164                         if (points_(0, i) > max)
00165                                 max = points_(0, i);
00166                 }
00167         }
00168 //  std::cout << " EGP(3) max: " << max << " min: " << min << std::endl;
00169 
00170         grasp_width_ = max - min;
00171 }
00172 
00173 void FingerHand::deepenHand(double init_deepness, double max_deepness)
00174 {
00175 //  std::cout << "deepenHand begin: " << hand_ << std::endl;
00176 //      std::cout << "deepenHand begin. hand_: " << hand_ << ", fingers_: "
00177 //                      << fingers_ << std::endl;
00178         std::vector<int> hand_idx;
00179 
00180         for (int i = 0; i < hand_.cols(); i++)
00181         {
00182                 if (hand_(i) == true)
00183                         hand_idx.push_back(i);
00184         }
00185 
00186         if (hand_idx.size() == 0)
00187                 return;
00188 
00189         // choose middle hand
00190         int hand_eroded_idx = hand_idx[ceil(hand_idx.size() / 2.0) - 1]; // middle index
00191         Eigen::Array<bool, 1, Eigen::Dynamic> hand_eroded = Eigen::Array<bool, 1,       
00192                 Eigen::Dynamic>::Constant(hand_.cols(), false);
00193         hand_eroded(hand_eroded_idx) = true;
00194 //      std::cout << "hand_eroded_idx: " << hand_eroded_idx << ", hand_eroded: "
00195 //                      << hand_eroded << ", hand_idx.size() / 2: " << hand_idx.size() / 2
00196 //                      << std::endl;
00197 
00198         // attempt to deepen hand
00199         double deepen_step_size = 0.005;
00200         FingerHand new_hand = *this;
00201         FingerHand last_new_hand = new_hand;
00202 //  std::cout << "new_hand.hand_: " << new_hand.getHand() << std::endl;
00203 //  std::cout << "d = " << init_deepness + deepen_step_size << ", max_deepness: " << max_deepness << std::endl;
00204         for (double d = init_deepness + deepen_step_size; d <= max_deepness; d +=       deepen_step_size)
00205         {
00206 //    std::cout << "d(): " << d << std::endl;
00207                 new_hand.evaluateFingers(d);
00208                 new_hand.evaluateHand();
00209 //              std::cout << "d: " << d << ", new_hand.hand_: " << new_hand.getHand()
00210 //                              << std::endl;
00211 //              std::cout << "hand_eroded: " << hand_eroded << std::endl;
00212 //    std::cout << " => last_new_hand.hand_: " << last_new_hand.getHand() << std::endl;
00213 //     if ((new_hand.hand_ && hand_eroded).count() == 0) // hand is so deep that it does not exist any longer
00214 //       break;
00215                 bool is_too_deep = true;
00216                 for (int i = 0; i < hand_eroded.cols(); i++)
00217                 {
00218                         if (new_hand.hand_(i) == 1 && hand_eroded(i) == 1) // hand is not so deep that it does not exist any longer
00219                         {
00220                                 is_too_deep = false;
00221                                 break;
00222                         }
00223                 }
00224                 if (is_too_deep)
00225                         break;
00226                 last_new_hand = new_hand;
00227 //    std::cout << "d[]: " << d << ", last_new_hand.hand: " << last_new_hand.getHand() << ", new_hand.hand: " << new_hand.getHand() << std::endl;
00228         }
00229         *this = last_new_hand; // recover most deep hand
00230         hand_ = hand_eroded;
00231 //      std::cout << "deepenHand done. hand_: " << hand_ << ", fingers_: " << fingers_
00232 //                      << std::endl;
00233 }


agile_grasp
Author(s): Andreas ten Pas
autogenerated on Sat Jun 8 2019 20:08:27