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.6KB

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