Advertisement
Guest User

Untitled

a guest
Mar 8th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.62 KB | None | 0 0
  1. #include "sensors_com.h"
  2. #include "udp_interface.h"
  3. #include "communication.h"
  4. extern "C" {
  5. #include "log.h"
  6. }
  7.  
  8. #include <opencv2/objdetect.hpp>
  9. #include <opencv2/highgui.hpp>
  10. #include <opencv2/imgproc.hpp>
  11. #include <opencv2/aruco.hpp>
  12.  
  13. #include <iostream>
  14. #include <stdio.h>
  15. #include <ctime>
  16. #include <math.h>
  17. #include <signal.h>
  18. #include <pthread.h>
  19.  
  20. using namespace std;
  21. using namespace cv;
  22.  
  23. #define IMG_PREVIEW
  24. #define FRAME_WIDTH   320
  25. #define FRAME_HEIGHT  240
  26.  
  27.  
  28.  
  29. void interrupt_handler(int sig);
  30.  
  31.  
  32. VideoCapture stream(0);
  33. pthread_t communication_thread;
  34. Point the_center(0, 0);
  35. extern object_position_t object_pos;
  36.  
  37. int main( void )
  38. {
  39.   char buf[256];
  40.   sprintf(buf, "OpenCV version %d.%d", CV_MAJOR_VERSION, CV_MINOR_VERSION);
  41.  
  42.   log_info(buf);
  43.  
  44.   signal(SIGINT, interrupt_handler);
  45.   Mat frame_cam(FRAME_HEIGHT, FRAME_WIDTH, CV_8UC3);
  46.   Ptr<aruco::Dictionary> marker_dictionary = aruco::getPredefinedDictionary(0);
  47.   aruco::DetectorParameters parameters;
  48.   vector<int> marker_ids, rejected_candidates;
  49.   vector<vector<Point_<float>>> marker_corners;  // Point2f
  50.   vector<vector<Point3f>> rotation_vectors;
  51.   vector<vector<Point3f>> translation_vectors;
  52.  
  53.   stream.set(CV_CAP_PROP_FRAME_WIDTH, FRAME_WIDTH);
  54.   stream.set(CV_CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT);
  55.   stream.set(CV_CAP_PROP_FORMAT, CV_8UC3);
  56.  
  57.   if (!stream.isOpened())
  58.   {
  59.     log_error("Failed to open video stream...exiting");
  60.     return 1;
  61.   }
  62.   else
  63.   {
  64.     log_info("Video stream has been opened");
  65.   }
  66.  
  67.   const char serial_port[] = "/dev/ttyS0";
  68.   if (sensors_data_init(serial_port, 921600) == -1)
  69.   {
  70.     log_error("Failed to open serial port: %s, exiting", serial_port);
  71.     //return 1;
  72.   }
  73.   else
  74.   {
  75.     log_info("Serial port %s has been opened", serial_port);
  76.   }
  77.  
  78.   if (udp_interface_init() == -1)
  79.   {
  80.     log_error("Error initilising UDP interface...exiting");
  81.     return 1;
  82.   }
  83.   else
  84.   {
  85.     log_info("UDP inteface initilised", serial_port);
  86.   }
  87.  
  88.   pthread_create(
  89.     &communication_thread,
  90.     NULL,
  91.     communication_handler_thread,
  92.     NULL
  93.   );
  94.  
  95.   while (true)
  96.   {
  97.     stream.grab();
  98.     stream.retrieve(frame_cam);
  99.     if (frame_cam.empty() == true)
  100.     {
  101.       log_info("Couldn't read new image...exiting");
  102.       return 1;
  103.     }
  104.  
  105.     /*
  106.      * FRAME PROCESSING
  107.      */
  108.  
  109.     aruco::detectMarkers(
  110.       frame_cam,
  111.       marker_dictionary,
  112.       marker_corners,
  113.       marker_ids,
  114.       &parameters,
  115.       rejected_candidates
  116.     );
  117.     aruco::drawDetectedMarkers(frame_cam, marker_corners, marker_ids);
  118.  
  119.     /*
  120.     // pose estimation requires properly calibrated cam
  121.     aruco::estimatePoseSingleMarkers(
  122.       marker_corners,
  123.       0.05,                                  // size of markers side in meters
  124.       camera_matrix, dist_coeffs,            // cam calibration params
  125.       rotation_vectors, translation_vectors  // rotation, translation vectors
  126.     );
  127.     for (uint8_t i = 0; i < marker_ids.size(); ++i) {
  128.       aruco::drawAxis(
  129.         frame_cam,
  130.         camera_matrix, dist_coeffs,
  131.         rotation_vectors[i], translation_vectors[i],
  132.         0.1                                  // length of the axis
  133.       );
  134.     }
  135.     */
  136.  
  137. #ifdef IMG_PREVIEW
  138.     imshow("Video Capture", frame_cam);
  139.  
  140.     if (waitKey(10) >= 0)
  141.     {
  142.       communication_handler_thread_stop();
  143.       stream.release();
  144.       break;
  145.     }
  146. #endif
  147.  
  148.   }
  149.   return 0;
  150. }
  151.  
  152.  
  153. void interrupt_handler(int sig)
  154. {
  155.   log_warn("SIGINT...exiting");
  156.   communication_handler_thread_stop();
  157.   pthread_join(communication_thread, NULL);
  158.   sensors_data_cleanup();
  159.   stream.release();
  160.   exit(0);
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement