Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.80 KB | None | 0 0
  1. //#include "opencv2/core.hpp"
  2. //#include "opencv2/imgproc.hpp"
  3. //#include "opencv2/highgui.hpp"
  4. #include <stdio.h>
  5. #include <iostream>
  6. #include <cstdio>
  7. #include <fstream>
  8. #include <vector>
  9. #include <math.h>
  10. #include <Eigen/Dense>
  11. #include "pch.h"
  12.  
  13.  
  14. //using namespace cv;
  15. using namespace std;
  16. using namespace Eigen;
  17.  
  18. //Angle, Distance, robot(x,y,a), sensor(x,y,a), unitVector, orthogonal to line (ri)
  19. /*
  20.     Return a vector with four double values (x, y, a, C) that represent the
  21.  
  22.     @param int &iAngle
  23.     @param int &iDistance
  24.     @param Eigen::Vector3d &robotPose
  25.     @param Eigen::Vector3d &sensorPose
  26.     @param Eigen::Vector2d &unitVector
  27.     @param Eigen::Vector2d &ri
  28.     @return
  29. */
  30. Eigen::RowVector4d CoxScanMatch(int &iAngle, int &iDistance, Eigen::Vector3d &robotPose, Eigen::Vector3d &sensorPose, Eigen::Vector2d &unitVector, Eigen::Vector2d &ri)
  31. {
  32.  
  33.     int Rx, Ry, Ra;
  34.     Rx = sensorPose(0); // ?
  35.     Ry = sensorPose(1); // ?
  36.     Ra = sensorPose(2); // ?
  37.     int maxIter = 15;   // ?
  38.     int ddx = 0;
  39.     int ddy = 0;
  40.     int dda = 0;
  41.     double alpha = sensorPose(0);
  42.     double beta = sensorPose(1);
  43.     double gamma = sensorPose(2);
  44.  
  45.     // Repeat until process converge or maximum iterations is reached
  46.     for (int i = 0; i < maxIter; i++)
  47.     {
  48.         // 1)   Transform Laser Points to Cartesian Coordinates
  49.         //      Rotate and translate data from sensor to Robot Coordinate System
  50.         //      Rotate and translate Robot Coordinate System to World Coordinate System
  51.  
  52.         Rx = Rx + ddx;
  53.         Ry = Ry + ddy;
  54.         Ra = Ra + dda;
  55.  
  56.         double x = iDistance * cos(iAngle); // Ska dessa vara doubles?
  57.         double y = iDistance * sin(iAngle);
  58.  
  59.         MatrixXd R1(3, 3);
  60.         R1(0, 0) = cos(gamma);  R1(1, 0) = -sin(gamma); R1(2, 0) = alpha;
  61.         R1(1, 0) = sin(gamma);  R1(1, 1) = cos(gamma);  R1(1, 2) = beta;
  62.         R1(2, 0) = 0;           R1(2, 1) = 0;           R1(2, 2) = 1;
  63.  
  64.         RowVector3d temp;
  65.         temp(0) = x;            temp(1) = y;            temp(2) = 1;
  66.  
  67.         RowVector3d Xs;
  68.         Xs = R1 * temp;
  69.  
  70.         MatrixXd R2(3, 3);
  71.         R2(0, 0) = cos(Ra); R2(1, 0) = -sin(Ra);    R2(2, 0) = Rx;
  72.         R2(1, 0) = sin(Ra); R2(1, 1) = cos(Ra);     R2(1, 2) = Ry;
  73.         R2(2, 0) = 0;       R2(2, 1) = 0;           R2(2, 2) = 1;
  74.  
  75.         temp(0) = Xs.row(0);    temp(1) = Xs.row(1);    temp(2) = 1;
  76.  
  77.         RowVector3d Xw;
  78.         Xw = R2 * temp;
  79.  
  80.         // 2)   Find and assign the closest line to each Laser Point
  81.         //      Calculate the distance between Li and Pi
  82.  
  83.         VectorXd targetT;   // (Should have) size of Xw in matlab code, change so initialized with size?
  84.         VectorXd yTargetT;  // (Should have) size of Xw,
  85.         RowVectorXd Y;      // (Should have) size of U,
  86.  
  87.  
  88.         for (int j = 0; j < Xw.size(); j++)
  89.         {
  90.             for (int k = 0; k < unitVector.size(); k++) // TODO: Fix how many iterations, should be: for i=1:length(U(:,1))
  91.             {
  92.                 Y(k) = ri(k) - unitVector.col(k).dot(Xw.block(0:1, j)); // TODO: is Xw.block correct???
  93.             }
  94.             double minAbsY = Y.cwiseAbs().minCoeff();
  95.             targetT(j) = minAbsY;
  96.             yTargetT(j) = Y(minAbsY);
  97.         }
  98.  
  99.         // Check th and reject outliers
  100.         int sizeYTargetT = yTargetT.size();
  101.         double yTargetTMedian = yTargetT(sizeYTargetT / 2);     // Does this get the median?
  102.         double th = abs(yTargetTMedian * 2);
  103.         vector target <double>;                                 // how to init?
  104.         vector yTarget <double>;                                // how to init?
  105.         vector search_idx <double>;                             // how to init?
  106.  
  107.         for (int j = 0; j < targetT.size(); j++)
  108.         {
  109.             if (abs(yTargetT(j) < th))
  110.             {
  111.                 search_idx.push_back(j);
  112.                 target.push_back(targetT(j));
  113.                 yTarget.push_back(yTargetT(j));
  114.             }
  115.         }
  116.        
  117.  
  118.         // 3)   Set up the matrix A with measured values (distances to line)
  119.         //      Set up matrix Y with lines Li from map. By solving b from Ab = Y
  120.         //      with least square gives an error in match.
  121.  
  122.         RowVectorXd points;
  123.  
  124.         // 4)   Summarize matches (errors)
  125.  
  126.         // 5)   If match is short or too many repetitions:
  127.         //      Go to step 6 and take care of the summarized match
  128.         //      Else go to step 1
  129.  
  130.         // 6)  
  131.  
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement