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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. result.validLane = intersectionHandler.foundLane(data);
  62. if (result.validLane)
  63. {
  64. controlModule.calcSpeeds(1,1, data.angle);
  65. processedImage = provideOutput(originalImage, processedImage, data, roi);
  66. }
  67. result.rawImage = originalImage;
  68. result.processedImage = processedImage;
  69. result.data = data;
  70. result.motorSignals = controlModule.readMotors();
  71. }
  72. catch(std::exception const &ex)
  73. {
  74. if(!cb(ex))
  75. {
  76. //callback returned false -> exception not handled -> exit
  77. exit(EXIT_FAILURE);
  78. }
  79. }
  80. //Invoke the callback method (ListenerPair second -> ListenerCallback)
  81. for(auto &val : listeners)
  82. {
  83. val.second(result);
  84. }
  85. }
  86. else
  87. {
  88. break;
  89. }
  90. }
  91. });
  92. }
  93. void LFR::startLoop()
  94. {
  95. if(thread)
  96. {
  97. //Restart thread if it is running
  98. setStop(true);
  99. thread->join();
  100. setStop(false);
  101. }
  102. createThread();
  103. }
  104. void LFR::endLoop()
  105. {
  106. setStop(true);
  107. }
  108. cv::Mat LFR::provideOutput(Mat originalImage, Mat processedImage, const FrameData& frameData, const Rect& roi)
  109. {
  110. for(int i = 0; i < frameData.contours.size(); i++)
  111. {
  112. drawContours(originalImage, frameData.contours, i, Scalar(0,255,255), 1, 8, noArray(), 0, Point(roi.x, roi.y));
  113. rectangle(originalImage, frameData.boundingBoxes[i], Scalar(0,255,0));
  114. Rect center(Point(frameData.middlePoints[i].x-2, frameData.middlePoints[i].y-2), Point(frameData.middlePoints[i].x+2, frameData.middlePoints[i].y+2));
  115. rectangle(originalImage, center, Scalar(0,0,255));
  116. Rect leftRect(Point(frameData.leftEdges[i].x-2, frameData.leftEdges[i].y-2), Point(frameData.leftEdges[i].x+2, frameData.leftEdges[i].y+2));
  117. rectangle(originalImage, leftRect, Scalar(0,0,255));
  118. }
  119. if(frameData.contours.size() > 0)
  120. {
  121. //Draw the Arrow for the check of the angle
  122. int length = 100;
  123. Point P1 = frameData.middlePoints[frameData.index];
  124. Point P2;
  125. P2.x = (int)round(P1.x + length * cos(frameData.angle * CV_PI / 180.0));
  126. P2.y = (int)round(P1.y + length * sin(frameData.angle * CV_PI / 180.0));
  127. cv::arrowedLine(originalImage, P1, P2, Scalar(0,0,255), 2, 8);
  128. }
  129. return originalImage;
  130. }