Advertisement
lamiastella

PMDCamera.cpp

Jun 9th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.82 KB | None | 0 0
  1. #include "PMDCamera.h"
  2.  
  3. /***
  4. Private constructor for the PMD depth sensor
  5. ***/
  6. PMDCamera::PMDCamera(bool use_live_sensor)
  7. {
  8.     CONFIDENCE_THRESHHOLD = (60.0f / 255.0f*500.0f);
  9.     INVALID_FLAG_VALUE = PMD_FLAG_INVALID;
  10.     X_DIMENSION = 176;
  11.     Y_DIMENSION = 120;
  12.  
  13.     if (!use_live_sensor) {
  14.         return;
  15.     }
  16.  
  17.     int res;
  18.     std::cout << "Trying to open pmd\n";
  19.     res = pmdOpen(&hnd, SOURCE_PLUGIN, SOURCE_PARAM, PROC_PLUGIN, PROC_PARAM); //Open the PMD sensor
  20.     if (res != PMD_OK)
  21.     {
  22.         pmdGetLastError(0, err, 128);
  23.         fprintf(stderr, "Could not connect: %s\n", err);
  24.         return;
  25.     }
  26.     printf("opened sensor\n");
  27.  
  28.     // Updating the sensor is necessary before any data can be retrieved
  29.     res = pmdUpdate(hnd);
  30.     if (res != PMD_OK)
  31.     {
  32.         pmdGetLastError(hnd, err, 128);
  33.         fprintf(stderr, "Couldn't transfer data: %s\n", err);
  34.         pmdClose(hnd);
  35.         return;
  36.     }
  37.     printf("acquired image\n");
  38.  
  39.     // res: Structure which contains various meta-information about the data delivered by your Nano.
  40.     // It is advisabe to always use the data delivered by this struct (for example the width and height of the imager
  41.     // and the image format). Please refer to the PMSDSK documentation for more information
  42.     res = pmdGetSourceDataDescription(hnd, &dd);
  43.     if (res != PMD_OK)
  44.     {
  45.         pmdGetLastError(hnd, err, 128);
  46.         fprintf(stderr, "Couldn't get data description: %s\n", err);
  47.         pmdClose(hnd);
  48.         return;
  49.     }
  50.  
  51.     printf("retrieved source data description\n");
  52.     if (dd.subHeaderType != PMD_IMAGE_DATA)
  53.     {
  54.         fprintf(stderr, "Source data is not an image!\n");
  55.         pmdClose(hnd);
  56.         return;
  57.     }
  58.  
  59.     numPixels = dd.img.numRows * dd.img.numColumns; // Number of pixels in camera
  60.     dists = new float[3 * numPixels]; // Dists contains XYZ values. needs to be 3x the size of numPixels
  61.     amps = new float[numPixels];
  62.     frame.create(dd.img.numRows, dd.img.numColumns, CV_8UC3);
  63.     /*
  64.     KF.init(6, 3, 0);
  65.     KF.transitionMatrix = (cv::Mat_<float>(6, 6) << 1, 0, 0, 1, 0, 0,
  66.                                                     0, 1, 0, 0, 1, 0,
  67.                                                     0, 0, 1, 0, 0, 1,
  68.                                                     0, 0, 0, 1, 0, 0,
  69.                                                     0, 0, 0, 0, 1, 0,
  70.                                                     0, 0, 0, 0, 0, 1);
  71.     measurement = cv::Mat_<float>::zeros(3, 1);
  72.  
  73.     //Initaite Kalman
  74.     KF.statePre.setTo(0);
  75.     setIdentity(KF.measurementMatrix);
  76.     setIdentity(KF.processNoiseCov, cv::Scalar::all(.001)); // Adjust this for faster convergence - but higher noise
  77.     setIdentity(KF.measurementNoiseCov, cv::Scalar::all(1e-1));
  78.     setIdentity(KF.errorCovPost, cv::Scalar::all(.1));
  79.     */
  80.    
  81. }
  82.  
  83. /***
  84. Public deconstructor for he PMD depth sensor
  85. ***/
  86. PMDCamera::~PMDCamera() {}
  87.  
  88. void PMDCamera::destroyInstance()
  89. {
  90.     printf("closing sensor\n");
  91.     pmdClose(hnd);
  92.     printf("sensor closed\n");
  93. }
  94.  
  95. /***
  96. Create xyzMap, zMap, ampMap, and flagMap from sensor input
  97. ***/
  98. void PMDCamera::update()
  99. {
  100.     initilizeImages();
  101.    
  102.     fillInAmps();
  103.     fillInZCoords();
  104.     // Flags. Helps with denoising.
  105.    
  106.     unsigned *flags = new unsigned[ampMap.cols*ampMap.rows];
  107.     int res = pmdGetFlags(hnd, flags, numPixels * sizeof(unsigned));
  108.     if (res != PMD_OK) {
  109.         pmdGetLastError(hnd, err, 128);
  110.         fprintf(stderr, "Couldn't get the flags: %s\n", err);
  111.         pmdClose(hnd);
  112.         return;
  113.     }
  114.     flagMap.data = (uchar *)flags;
  115.  
  116.     res = pmdUpdate(hnd);
  117.     if (res != PMD_OK) {
  118.         pmdGetLastError(hnd, err, 128);
  119.         fprintf(stderr, "Couldn't update the PMD camera: %s\n", err);
  120.         pmdClose(hnd);
  121.         return;
  122.     }
  123.     delete(flags);
  124.    
  125. }
  126.  
  127.  
  128. /***
  129. Reads the depth data from the sensor and fills in the matrix
  130. ***/
  131. void PMDCamera::fillInZCoords()
  132. {
  133.     int res = pmdGet3DCoordinates(hnd, dists, 3 * numPixels * sizeof(float)); //store x,y,z coordinates dists (type: float*)
  134.     //float * zCoords = new float[1]; //store z-Coordinates of dists in zCoords
  135.     if (res != PMD_OK) {
  136.         pmdGetLastError(hnd, err, 128);
  137.         fprintf(stderr, "Couldn't get 3D coordinates: %s\n", err);
  138.         pmdClose(hnd);
  139.         return;
  140.     }
  141.     xyzMap = cv::Mat(xyzMap.size(), xyzMap.type(), dists);
  142.     //std::cout << "nrows: " << xyzMap.rows << std::endl;
  143.     //std::cout << "ncols: " << xyzMap.cols << std::endl;
  144. }
  145.  
  146. /***
  147. Reads the amplitude data from the sensor and fills in the matrix
  148. ***/
  149. void PMDCamera::fillInAmps()
  150. {
  151.     int res = pmdGetAmplitudes(hnd, amps, numPixels * sizeof(float));
  152.     //float * dataPtr = amps;
  153.     if (res != PMD_OK) {
  154.         pmdGetLastError(hnd, err, 128);
  155.         fprintf(stderr, "Couldn't get amplitudes: %s\n", err);
  156.         pmdClose(hnd);
  157.         return;
  158.     }
  159.     ampMap.data = (uchar *)amps;
  160. }
  161.  
  162. /***
  163. Returns the X value at (i, j)
  164. ***/
  165. float PMDCamera::getX(int i, int j) const
  166. {
  167.     int flat = j * dd.img.numColumns * 3 + i * 3;
  168.     return dists[flat];
  169. }
  170.  
  171. /***
  172. Returns the Y value at (i, j)
  173. ***/
  174. float PMDCamera::getY(int i, int j) const
  175. {
  176.     int flat = j * dd.img.numColumns * 3 + i * 3;
  177.     return dists[flat + 1];
  178. }
  179.  
  180. /***
  181. Returns the Z value at (i, j)
  182. ***/
  183. float PMDCamera::getZ(int i, int j) const
  184. {
  185.     int flat = j * dd.img.numColumns * 3 + i * 3;
  186.     return dists[flat + 2];
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement