Projektarbeit Line Following Robot bei Prof. Chowanetz im WS22/23
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lfr.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "lfr.h"
  2. #define right false
  3. #define left true
  4. LFR::LFR(int videoHeight, int videoWidth, int thresholdBinary, int gaussKernelSize, ExceptionCallback cb):
  5. stop(false),
  6. input(videoHeight, videoWidth),
  7. processing(),
  8. controlModule(),
  9. interpreter(),
  10. intersectionHandler(),
  11. cb(cb)
  12. {
  13. this->thresholdBinary = thresholdBinary;
  14. this->gaussKernelSize = gaussKernelSize;
  15. }
  16. LFR::~LFR()
  17. {
  18. endLoop();
  19. thread->join();
  20. }
  21. void LFR::removeListener(LFR::ListenerKey key)
  22. {
  23. std::lock_guard<std::mutex> lock(mutex);
  24. auto it = std::find_if(std::begin(listeners), std::end(listeners), [&](auto const &val){
  25. return val.first == key;
  26. });
  27. if(it != std::end(listeners))
  28. {
  29. listeners.erase(it);
  30. }
  31. }
  32. void LFR::addListener(LFR::ListenerCallback cb, LFR::ListenerKey key)
  33. {
  34. std::lock_guard<std::mutex> lock(mutex);
  35. listeners.emplace_back(key, std::move(cb));
  36. }
  37. void LFR::setStop(bool val)
  38. {
  39. std::lock_guard<std::mutex> lock(mutex);
  40. stop = val;
  41. }
  42. void LFR::createThread()
  43. {
  44. thread = std::make_unique<std::thread>([this](){
  45. while(true)
  46. {
  47. LFR_Result result;
  48. if(!stop && !listeners.empty())
  49. {
  50. try
  51. {
  52. std::lock_guard<std::mutex> lock(mutex);
  53. Mat originalImage = input.readWebcam();
  54. Point roiOrigin(0, int(originalImage.rows*(3.25/6.0)));
  55. Rect roi(roiOrigin.x, roiOrigin.y, originalImage.cols, originalImage.rows/6);
  56. Mat processedImage = originalImage;
  57. processing.processImage(processedImage, this->thresholdBinary, this->gaussKernelSize);
  58. FrameData data = processing.calculateLineSegments(processedImage, roi);
  59. processing.filterReflections(data);
  60. processing.calcAngles(data, originalImage.cols, originalImage.rows, left);
  61. controlModule.calcSpeeds(1,1, data.angle);
  62. processedImage = provideOutput(originalImage, processedImage, data, roi);
  63. result.rawImage = originalImage;
  64. result.processedImage = processedImage;
  65. result.data = data;
  66. result.motorSignals = controlModule.readMotors();
  67. }
  68. catch(std::exception const &ex)
  69. {
  70. if(!cb(ex))
  71. {
  72. //callback returned false -> exception not handled -> exit
  73. exit(EXIT_FAILURE);
  74. }
  75. }
  76. //Invoke the callback method (ListenerPair second -> ListenerCallback)
  77. for(auto &val : listeners)
  78. {
  79. val.second(result);
  80. }
  81. }
  82. else
  83. {
  84. break;
  85. }
  86. }
  87. });
  88. }
  89. void LFR::startLoop()
  90. {
  91. if(thread)
  92. {
  93. //Restart thread if it is running
  94. setStop(true);
  95. thread->join();
  96. setStop(false);
  97. }
  98. createThread();
  99. }
  100. void LFR::endLoop()
  101. {
  102. setStop(true);
  103. }
  104. cv::Mat LFR::provideOutput(Mat originalImage, Mat processedImage, const FrameData& frameData, const Rect& roi)
  105. {
  106. for(int i = 0; i < frameData.contours.size(); i++)
  107. {
  108. drawContours(originalImage, frameData.contours, i, Scalar(0,255,255), 1, 8, noArray(), 0, Point(roi.x, roi.y));
  109. rectangle(originalImage, frameData.boundingBoxes[i], Scalar(0,255,0));
  110. Rect center(Point(frameData.middlePoints[i].x-2, frameData.middlePoints[i].y-2), Point(frameData.middlePoints[i].x+2, frameData.middlePoints[i].y+2));
  111. rectangle(originalImage, center, Scalar(0,0,255));
  112. Rect leftRect(Point(frameData.leftEdges[i].x-2, frameData.leftEdges[i].y-2), Point(frameData.leftEdges[i].x+2, frameData.leftEdges[i].y+2));
  113. rectangle(originalImage, leftRect, Scalar(0,0,255));
  114. }
  115. if(frameData.contours.size() > 0)
  116. {
  117. //Draw the Arrow for the check of the angle
  118. int length = 100;
  119. Point P1 = frameData.middlePoints[frameData.index];
  120. Point P2;
  121. P2.x = (int)round(P1.x + length * cos(frameData.angle * CV_PI / 180.0));
  122. P2.y = (int)round(P1.y + length * sin(frameData.angle * CV_PI / 180.0));
  123. cv::arrowedLine(originalImage, P1, P2, Scalar(0,0,255), 2, 8);
  124. }
  125. return originalImage;
  126. }