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_state_machine.cpp 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "lfr_state_machine.h"
  2. // for string delimiter
  3. vector<string> LFR_StateMachine::split (string s, string delimiter) const {
  4. size_t pos_start = 0, pos_end, delim_len = delimiter.length();
  5. string token;
  6. vector<string> res;
  7. while ((pos_end = s.find (delimiter, pos_start)) != string::npos) {
  8. token = s.substr (pos_start, pos_end - pos_start);
  9. pos_start = pos_end + delim_len;
  10. res.push_back (token);
  11. }
  12. res.push_back (s.substr (pos_start));
  13. return res;
  14. }
  15. void LFR_StateMachine::sanitize (string& s) const
  16. {
  17. char r = '\r', n = '\n';
  18. s.erase(std::remove(s.begin(), s.end(), n), s.end());
  19. s.erase(std::remove(s.begin(), s.end(), r), s.end());
  20. }
  21. bool LFR_StateMachine::checkStringValidity (const std::vector<std::string>& s) const
  22. {
  23. if(s.size() != 5)
  24. {
  25. return false;
  26. }
  27. try
  28. {
  29. for(int i = 0; i < 4; i ++)
  30. {
  31. if(stod(s[i]) > 1.0 || stod(s[i]) < -1.0)
  32. {
  33. return false;
  34. }
  35. }
  36. int i = stoi(s[4]);
  37. if(i != 0 && i != 1)
  38. {
  39. return false;
  40. }
  41. }
  42. catch(const std::exception& e) {return false;}
  43. return true;
  44. }
  45. void LFR_StateMachine::parseString(string s)
  46. {
  47. sanitize(s);
  48. std::vector<std::string> splitStr = split(s, ";");
  49. if(!checkStringValidity(splitStr))
  50. {
  51. std::cout<<"Invalid String" << std::endl;
  52. return;
  53. }
  54. double wheels[4] = {0.0, 0.0, 0.0, 0.0};
  55. int mode = std::stoi(splitStr[4]);
  56. for(int i = 0; i < 3; i++)
  57. {
  58. wheels[i] = std::stod(splitStr[i]);
  59. }
  60. if(mode == 0) {
  61. setState(State::Manual::getInstance());
  62. uartCommunicator.sendTelegram(wheels[0], wheels[1], wheels[2], wheels[3]);
  63. }
  64. else if (mode == 1) {setState(State::Autonomous::getInstance());}
  65. return;
  66. }
  67. LFR_StateMachine::LFR_StateMachine():
  68. autonomousMode(videoHeight, videoWidth, thresholdBinary, gaussKernelSize, [&](std::exception const &ex)
  69. {
  70. std::unique_lock<std::mutex> lock(mutex);
  71. std::cerr<<"camera exception:"<<ex.what()<<std::endl;
  72. return false;
  73. }),
  74. uartCommunicator(),
  75. socket([&](std::exception const &ex)
  76. {
  77. std::unique_lock<std::mutex> lock(mutex);
  78. std::cerr<<"socket exception:"<<ex.what()<<std::endl;
  79. return false;
  80. })
  81. {
  82. // Connect String parser to socket
  83. socket.addListener([&](LFR_Socket::LFR_Telegram telegram)
  84. {
  85. std::unique_lock<std::mutex> lock(mutex);
  86. parseString(std::string(telegram));
  87. }, &mutex);
  88. socket.startLoop();
  89. currentState = &State::Idle::getInstance();
  90. currentState->enter(this);
  91. //Start the permanent loop
  92. char input;
  93. std::cout << "press q to quit" << std::endl;
  94. std::cin >> input;
  95. std::cout << "binned" << std::endl;
  96. while (input != 'q')
  97. {
  98. std::cin >> input;
  99. std::cout << "binned" << std::endl;
  100. }
  101. std::cout << "Exiting central" << std::endl;
  102. }
  103. void LFR_StateMachine::setState(LFR_IState& newState)
  104. {
  105. if (&newState != currentState)
  106. {
  107. currentState->exit(this);
  108. currentState = &newState;
  109. currentState->enter(this);
  110. }
  111. else
  112. {
  113. std::cout << "no switch" << std::endl;
  114. }
  115. }
  116. void LFR_StateMachine::enterAutonomous()
  117. {
  118. //cv::Mat img;
  119. autonomousMode.addListener([&](LFR_Result result)
  120. {
  121. std::unique_lock<std::mutex> lock(mutex);
  122. if (!result.rawImage.empty())
  123. {
  124. /*
  125. img = result.rawImage;
  126. //Resize to minimize latency using raspi over ssh/adapt to 3.5in screen
  127. cv::resize(result.rawImage, img, cv::Size(480, 320));
  128. //Calculate frame rate
  129. now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
  130. unsigned int deltaMs = static_cast<unsigned int>((now-last).count());
  131. double delta = static_cast<double>(deltaMs) / 1000.0;
  132. double frameRate = 1.0 / static_cast<double>(delta);*/
  133. double frameRate = -1.0;
  134. if (result.validLane)
  135. {
  136. std::cout << "Frame rate: " << frameRate << " angle: " << result.data.angle
  137. << " motor 1: " << result.motorSignals[0] << " motor 2: " << result.motorSignals[1]
  138. << " motor 3: " << result.motorSignals[2] << " motor 4: " << result.motorSignals[3] <<std::endl;
  139. uartCommunicator.sendTelegram(result.motorSignals[0], result.motorSignals[1], result.motorSignals[2], result.motorSignals[3]);
  140. }
  141. else
  142. {
  143. std::cout << "No lane found." << std::endl;;
  144. }
  145. //last = now;
  146. }
  147. }, &mutex);
  148. autonomousMode.startLoop();
  149. }
  150. void LFR_StateMachine::exitAutonomous()
  151. {
  152. autonomousMode.removeListener(&mutex);
  153. autonomousMode.endLoop();
  154. }