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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. const double maxSpeed = 0.5;
  12. std::mutex mutex;
  13. LFR lfr(videoHeight, videoWidth, thresholdBinary, gaussKernelSize, maxSpeed, [&](std::exception const &ex)
  14. {
  15. std::unique_lock<std::mutex> lock(mutex);
  16. std::cerr<<"camera exception:"<<ex.what()<<std::endl;
  17. return false;
  18. });
  19. //To calculate the frame rate
  20. std::chrono::milliseconds last = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
  21. std::chrono::milliseconds now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
  22. cv::Mat img;
  23. lfr.addListener([&](LFR_Result result)
  24. {
  25. std::unique_lock<std::mutex> lock(mutex);
  26. if (!result.rawImage.empty())
  27. {
  28. img = result.rawImage;
  29. //Resize to minimize latency using raspi over ssh
  30. //cv::resize(result.rawImage, img, cv::Size(128, 64+32));
  31. //Calculate frame rate
  32. now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
  33. unsigned int deltaMs = static_cast<unsigned int>((now-last).count());
  34. double delta = static_cast<double>(deltaMs) / 1000.0;
  35. double frameRate = 1.0 / static_cast<double>(delta);
  36. if (result.validLane)
  37. {
  38. std::cout << "Frame rate: " << frameRate << " angle: " << result.data.angle
  39. << " motor 1: " << result.motorSignals[0] << " motor 2: " << result.motorSignals[1]
  40. << " motor 3: " << result.motorSignals[2] << " motor 4: " << result.motorSignals[3] <<std::endl;
  41. }
  42. else
  43. {
  44. std::cout << "No lane found." << std::endl;;
  45. }
  46. last = now;
  47. }
  48. }, &mutex);
  49. lfr.startLoop();
  50. for(int finished = false; finished != 'q';){
  51. finished = std::tolower(cv::waitKey(1));
  52. std::unique_lock<std::mutex> lock(mutex);
  53. if(!img.empty()){
  54. cv::imshow("frame", img);
  55. }
  56. }
  57. }