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.

autonomous_mode_main.cpp 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "lfr.h"
  2. #include <opencv2/core/utils/logger.hpp>
  3. int main(void)
  4. {
  5. //Disable opencv logging messages
  6. //cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_WARNING);
  7. const int thresholdBinary = 140;
  8. const int videoHeight = 720;
  9. const int videoWidth = 1280;
  10. const int gaussKernelSize = 11;
  11. std::mutex mutex;
  12. LFR lfr(videoHeight, videoWidth, thresholdBinary, gaussKernelSize, [&](std::exception const &ex)
  13. {
  14. std::unique_lock<std::mutex> lock(mutex);
  15. std::cerr<<"camera exception:"<<ex.what()<<std::endl;
  16. return false;
  17. });
  18. //To calculate the frame rate
  19. std::chrono::milliseconds last = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
  20. std::chrono::milliseconds now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
  21. cv::Mat img;
  22. lfr.addListener([&](LFR_Result result)
  23. {
  24. std::unique_lock<std::mutex> lock(mutex);
  25. if (!result.rawImage.empty())
  26. {
  27. img = result.rawImage;
  28. //Resize to minimize latency using raspi over ssh
  29. //cv::resize(result.rawImage, img, cv::Size(128, 64+32));
  30. //Calculate frame rate
  31. now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
  32. unsigned int deltaMs = static_cast<unsigned int>((now-last).count());
  33. double delta = static_cast<double>(deltaMs) / 1000.0;
  34. double frameRate = 1.0 / static_cast<double>(delta);
  35. std::cout << "Frame rate: " << frameRate << std::endl;
  36. last = now;
  37. }
  38. }, &mutex);
  39. lfr.startLoop();
  40. for(int finished = false; finished != 'q';){
  41. finished = std::tolower(cv::waitKey(1));
  42. std::unique_lock<std::mutex> lock(mutex);
  43. if(!img.empty()){
  44. cv::imshow("frame", img);
  45. }
  46. }
  47. }