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 5.0KB

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