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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. if (result.validLane)
  36. {
  37. std::cout << "Frame rate: " << frameRate << " angle: " << result.data.angle
  38. << " motor 1: " << result.motorSignals[0] << " motor 2: " << result.motorSignals[1]
  39. << " motor 3: " << result.motorSignals[2] << " motor 4: " << result.motorSignals[3] <<std::endl;
  40. }
  41. else
  42. {
  43. std::cout << "No lane found." << std::endl;;
  44. }
  45. last = now;
  46. }
  47. }, &mutex);
  48. lfr.startLoop();
  49. for(int finished = false; finished != 'q';){
  50. finished = std::tolower(cv::waitKey(1));
  51. std::unique_lock<std::mutex> lock(mutex);
  52. if(!img.empty()){
  53. cv::imshow("frame", img);
  54. }
  55. }
  56. }