Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <array>
  4. #include <vector>
  5. #include <thread>
  6. #include <functional>
  7. #include <mutex>
  8. #include <condition_variable>
  9. #include <atomic>
  10. #include <chrono>
  11.  
  12. std::random_device rd; //Will be used to obtain a seed for the random number engine
  13. std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
  14. std::uniform_int_distribution<> dis(1, 500);
  15.  
  16. constexpr int TherholdMin = 10;
  17. constexpr int TherholdMedium = 100;
  18. constexpr int TherholdMax = 250;
  19.  
  20. constexpr int MotorSpeedMin = 180;
  21. constexpr int MotorSpeedMax = 230;
  22.  
  23. constexpr int SensorsCount = 5;
  24.  
  25. std::array<int, SensorsCount> sensorsArray{};
  26.  
  27. std::mutex taskQueueMutex;
  28. std::condition_variable timerExpired;
  29.  
  30. void pinSetup()
  31. {
  32. std::cout << "Executing pin setup" << std::endl;
  33. }
  34.  
  35. void moveForward()
  36. {
  37. std::cout << "Move forward!" << std::endl;
  38. }
  39.  
  40. void moveBackward()
  41. {
  42. std::cout << "Move backward!" << std::endl;
  43. }
  44.  
  45. void moveRight()
  46. {
  47. std::cout << "Move right!" << std::endl;
  48. }
  49.  
  50. void moveLeft()
  51. {
  52. std::cout << "Move left!" << std::endl;
  53. }
  54.  
  55. void stop()
  56. {
  57. std::cout << "Car stopped!" << std::endl;
  58. }
  59.  
  60. void setMotorSpeed(int _motorNumber, size_t _motorSpeed)
  61. {
  62. std::cout << "Motor speed set to: " << _motorSpeed << " for motor number: " << _motorNumber << std::endl;
  63. }
  64.  
  65. namespace Tasks
  66. {
  67. void readHallSensor()
  68. {
  69. int sensorValue = dis(gen);
  70. if (sensorValue < TherholdMin)
  71. {
  72. moveBackward();
  73. moveRight();
  74. }
  75. std::cout << "Read hall sensor: "<< sensorValue << std::endl;
  76. }
  77.  
  78. void readSensorValues()
  79. {
  80. std::for_each(
  81. sensorsArray.begin()
  82. , sensorsArray.end()
  83. , [](int& _sensor)
  84. {
  85. int sensorValue = dis(gen);
  86. _sensor = sensorValue;
  87. }
  88. );
  89. }
  90.  
  91.  
  92. void task3_auto_tarcking()
  93. {
  94. if (sensorsArray[2] >= TherholdMax)
  95. { //The middle sensor is on the black line
  96. if (sensorsArray[1] <= TherholdMedium && sensorsArray[3] <= TherholdMedium)
  97. { //Other sensors are in the white area
  98. moveForward(); //straight
  99. setMotorSpeed( 4, MotorSpeedMin );
  100. }
  101. else if (sensorsArray[1] >= TherholdMax && sensorsArray[3] <= TherholdMin)
  102. { //The second sensor is also on the black line
  103. moveLeft(); //Turn left
  104. setMotorSpeed( MotorSpeedMin,0 );
  105. }
  106. else if (sensorsArray[1] <= TherholdMin && sensorsArray[3] >= TherholdMax)
  107. { //The fourth sensor is also on the black line
  108. moveRight(); //Turn right
  109. setMotorSpeed(3, MotorSpeedMin );
  110. }
  111. }
  112. else if (sensorsArray[2] >= TherholdMax)
  113. { //The middle sensor is in the white area
  114. if (sensorsArray[1] >= TherholdMax && sensorsArray[3] >= TherholdMedium)
  115. { //Turn left
  116. moveLeft();
  117. setMotorSpeed(3, MotorSpeedMax );
  118. }
  119. else if (sensorsArray[1] >= TherholdMedium && sensorsArray[3] >= TherholdMax)
  120. { //Turn right
  121. moveRight();
  122. setMotorSpeed(2, MotorSpeedMin );
  123. }
  124. }
  125. else {
  126. moveBackward();
  127. setMotorSpeed(1, MotorSpeedMin );
  128. }
  129.  
  130. if (sensorsArray[1] >= TherholdMax)
  131. { //The second sensor is on the black line
  132. if (sensorsArray[0] >= TherholdMax && sensorsArray[2] <= TherholdMin)
  133. { //Accelerate left turn
  134. moveLeft();
  135. setMotorSpeed(0, MotorSpeedMax );
  136. }
  137. else
  138. { //Turn left
  139. moveLeft();
  140. setMotorSpeed(0, MotorSpeedMax );
  141. }
  142. }
  143. if (sensorsArray[3] >= TherholdMax)
  144. { //The fourth sensor is on the black line
  145. if (sensorsArray[2] <= TherholdMin && sensorsArray[4] >= TherholdMax)
  146. { //Accelerate right turn
  147. moveRight();
  148. setMotorSpeed( 0, MotorSpeedMax );
  149. }
  150. else
  151. { //Turn right
  152. moveRight();
  153. setMotorSpeed( 0, MotorSpeedMin );
  154. }
  155. }
  156. }
  157.  
  158. };
  159.  
  160. using TasksQueue = std::vector<std::function<void()>>;
  161. TasksQueue tasksQueue{
  162. Tasks::readHallSensor
  163. , Tasks::readSensorValues
  164. , Tasks::task3_auto_tarcking
  165. };
  166.  
  167. void tasksProceeder()
  168. {
  169. size_t taskIndex{};
  170. const size_t tasksCount{ tasksQueue.size() };
  171.  
  172. while(true)
  173. {
  174. std::unique_lock<std::mutex> queueLock( taskQueueMutex );
  175. timerExpired.wait(queueLock);
  176.  
  177. tasksQueue.at( taskIndex )();
  178. taskIndex = taskIndex == tasksCount - 1 ? 0 : ++taskIndex;
  179.  
  180. queueLock.unlock();
  181. timerExpired.notify_one();
  182. }
  183. }
  184.  
  185.  
  186. void timerThread()
  187. {
  188. using namespace std::chrono_literals;
  189. constexpr auto TimerPeriod = 100ms;
  190.  
  191. while (true)
  192. {
  193. timerExpired.notify_one();
  194. std::this_thread::sleep_for( TimerPeriod );
  195. }
  196. }
  197.  
  198. int main()
  199. {
  200.  
  201. std::thread queueWorker(tasksProceeder);
  202. std::thread timer(timerThread);
  203.  
  204. queueWorker.join();
  205. timer.join();
  206.  
  207.  
  208. return 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement