Advertisement
sockra

Slutgiltig version för mBot linjeföljare

Oct 21st, 2019
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.86 KB | None | 0 0
  1. #include "scheduler.h"
  2. #include "semphr.h"
  3. #include <MeAurigaLab.h>
  4.  
  5. MeLineFollower lineSensor(PORT_9);
  6. MeUltrasonicSensor opticalSensor(PORT_10);
  7.  
  8. MeEncoderOnBoard rightMotor(SLOT1);
  9. MeEncoderOnBoard leftMotor(SLOT2);
  10.  
  11. /*//// INIT MOTOR ////*/
  12. void isr_process_leftMotor(void)
  13. {
  14. if(digitalRead(leftMotor.getPortB()) == 0)
  15. {
  16. leftMotor.pulsePosMinus();
  17. }
  18. else
  19. {
  20. leftMotor.pulsePosPlus();
  21. }
  22. }
  23.  
  24. void isr_process_rightMotor(void)
  25. {
  26. if(digitalRead(rightMotor.getPortB()) == 0){
  27. rightMotor.pulsePosMinus();
  28. }
  29. else
  30. {
  31. rightMotor.pulsePosPlus();
  32. }
  33. }
  34.  
  35.  
  36.  
  37. /*//// TASKS ////*/
  38. TaskHandle_t xHandle1 = NULL;
  39. TaskHandle_t xHandle2 = NULL;
  40. TaskHandle_t xHandle3 = NULL;
  41. TaskHandle_t xHandle4 = NULL;
  42. TaskHandle_t xHandle5 = NULL;
  43. TaskHandle_t xHandle6 = NULL;
  44.  
  45. /*//// SEMAPHORES ////*/
  46. SemaphoreHandle_t ReadOpticalSensorData;
  47. SemaphoreHandle_t ReadLineSensorData;
  48. SemaphoreHandle_t SetMotorSpeedData;
  49.  
  50. /*//// GLOBAL VARIABLES ////*/
  51. bool obstacleFound = false;
  52. bool obstacleWasFound = false;
  53. bool makeArch = false;
  54. bool lineFound = false;
  55. int lineSensorData;
  56. int leftMotorSpeed;
  57. int rightMotorSpeed;
  58.  
  59. /*//// FIND-LINE ////*/
  60. bool forward = true;
  61. bool left = false;
  62. int count = 30;
  63.  
  64. /*//// READ-TASKS ////*/
  65. void TaskReadOpticalSensor(void *a)
  66. {
  67. if(xSemaphoreTake(ReadOpticalSensorData, 5) == pdTRUE)
  68. {
  69. if(opticalSensor.distanceCm() <= 20)
  70. {
  71. obstacleFound = true;
  72. }
  73. else
  74. {
  75. obstacleFound = false;
  76. }
  77. xSemaphoreGive(ReadOpticalSensorData);
  78. }
  79. //vTaskDelay(1); // one tick delay (15ms) in between reads for stability
  80. }
  81.  
  82. void TaskReadLineSensor(void *a)
  83. {
  84. if(xSemaphoreTake(ReadLineSensorData, 5) == pdTRUE)
  85. {
  86. lineSensorData = lineSensor.readSensors();
  87. xSemaphoreGive(ReadLineSensorData);
  88. }
  89. //vTaskDelay(1); // one tick delay (15ms) in between reads for stability
  90. }
  91.  
  92. /*//// MOTOR-TASK ////*/
  93. void TaskApplyMotorSpeed(void *a)
  94. {
  95. if(xSemaphoreTake(SetMotorSpeedData, 5) == pdTRUE)
  96. {
  97. leftMotor.setMotorPwm(leftMotorSpeed);
  98. rightMotor.setMotorPwm(-rightMotorSpeed);
  99. xSemaphoreGive(SetMotorSpeedData);
  100. }
  101. }
  102.  
  103. /*//// OBSTACLE-AVOIDANCE ////*/
  104. void TaskAvoidObstacle(void *a)
  105. {
  106. if(xSemaphoreTake(ReadOpticalSensorData, 5) == pdTRUE)
  107. {
  108. if(!lineFound && obstacleFound)
  109. {
  110. if(xSemaphoreTake(SetMotorSpeedData, 5) == pdTRUE)
  111. {
  112. leftMotorSpeed = -100;
  113. rightMotorSpeed = -100;
  114. xSemaphoreGive(SetMotorSpeedData);
  115. }
  116. }
  117.  
  118. //Go here until their is no obstacle in sight
  119. if(obstacleFound && lineFound)
  120. {
  121.  
  122. //Reverse
  123. obstacleWasFound = true;
  124. if(xSemaphoreTake(SetMotorSpeedData, 5) == pdTRUE)
  125. {
  126. leftMotorSpeed = -100;
  127. rightMotorSpeed = -100;
  128. xSemaphoreGive(SetMotorSpeedData);
  129. }
  130. }
  131. //After the robot has reversed we need to perfom the "left-arch manuver"
  132. else if(obstacleWasFound && lineFound)
  133. {
  134. //Make left-turn
  135. if(xSemaphoreTake(SetMotorSpeedData, 5) == pdTRUE)
  136. {
  137. leftMotorSpeed = -180;
  138. rightMotorSpeed = 180;
  139. xSemaphoreGive(SetMotorSpeedData);
  140. makeArch = true;
  141. obstacleWasFound = false;
  142. }
  143. delay(400);
  144. }
  145.  
  146. else if(makeArch && lineFound)
  147. {
  148. if(lineSensorData == 3)
  149. {
  150. if(xSemaphoreTake(SetMotorSpeedData, 5) == pdTRUE)
  151. {
  152. leftMotorSpeed = 150;
  153. rightMotorSpeed = 90;
  154. xSemaphoreGive(SetMotorSpeedData);
  155. }
  156. }
  157. else
  158. {
  159. makeArch = false;
  160. }
  161. }
  162. xSemaphoreGive(ReadOpticalSensorData);
  163. }
  164. }
  165.  
  166. /*//// LINE-TASKS ////*/
  167. void TaskFollowTheLine(void *a)
  168. {
  169. static int lastTurn;
  170.  
  171. if(xSemaphoreTake(ReadOpticalSensorData, 5) == pdTRUE)
  172. {
  173. if(lineFound && !obstacleFound && !makeArch)
  174. {
  175. if(xSemaphoreTake(ReadLineSensorData, 5) == pdTRUE)
  176. {
  177. if(xSemaphoreTake(SetMotorSpeedData, 5) == pdTRUE)
  178. {
  179. switch(lineSensorData)
  180. {
  181. case 0: //Forward
  182. leftMotorSpeed = 120;
  183. rightMotorSpeed = 120;
  184. lastTurn = 0;
  185. obstacleWasFound = false;
  186. break;
  187.  
  188. case 1: //Left
  189. leftMotorSpeed = -120;
  190. rightMotorSpeed = 120;
  191. lastTurn = 1;
  192. obstacleWasFound = false;
  193. break;
  194.  
  195. case 2: //Right
  196. leftMotorSpeed = 120;
  197. rightMotorSpeed = -120;
  198. lastTurn = 2;
  199. obstacleWasFound = false;
  200. break;
  201.  
  202. case 3: //No line
  203. switch(lastTurn)
  204. {
  205. case 0: //Reverse
  206. leftMotorSpeed = -90;
  207. rightMotorSpeed = -90;
  208. break;
  209.  
  210. case 1: //Left
  211. leftMotorSpeed = -160;
  212. rightMotorSpeed = 160;
  213. break;
  214.  
  215. case 2: //Right
  216. leftMotorSpeed = 160;
  217. rightMotorSpeed = -160;
  218. break;
  219. }
  220. break;
  221. }
  222. xSemaphoreGive(SetMotorSpeedData);
  223. }
  224. xSemaphoreGive(ReadLineSensorData);
  225. }
  226. }
  227. xSemaphoreGive(ReadOpticalSensorData);
  228. }
  229. }
  230.  
  231. void TaskFindTheLine(void *a)
  232. {
  233. if(xSemaphoreTake(ReadLineSensorData, 5) == pdTRUE)
  234. {
  235. if(lineSensorData != 3) //3 == no line
  236. {
  237. lineFound = true; //This is always true when the initial line is found.
  238. }
  239. else if(!lineFound)
  240. {
  241. Serial.print(count);
  242. Serial.print(":");
  243. //Search for the initial line. This happens at the start of the program
  244. if(forward && !left && count > 18)
  245. {
  246. Serial.println("Forward");
  247. if(xSemaphoreTake(SetMotorSpeedData, 5) == pdTRUE)
  248. {
  249. //Forward
  250. leftMotorSpeed = 120;
  251. rightMotorSpeed = 120;
  252. xSemaphoreGive(SetMotorSpeedData);
  253. }
  254. delay(100);
  255. count--;
  256. if(count <= 18)
  257. forward = false;
  258. }
  259. else if(!forward && count > 6 && count <= 18)
  260. {
  261. Serial.println("Reverse");
  262. if(xSemaphoreTake(SetMotorSpeedData, 5) == pdTRUE)
  263. {
  264. //Reverse
  265. leftMotorSpeed = -120;
  266. rightMotorSpeed = -120;
  267. xSemaphoreGive(SetMotorSpeedData);
  268. }
  269. delay(100);
  270. count--;
  271.  
  272. if(count <= 6)
  273. {
  274. left = true;
  275. forward = true;
  276. }
  277. }
  278.  
  279. else if(left && count > 0 && count <= 6)
  280. {
  281. Serial.println("Left");
  282. if(xSemaphoreTake(SetMotorSpeedData, 5) == pdTRUE)
  283. {
  284. //left-turn
  285. leftMotorSpeed = -180;
  286. rightMotorSpeed = 180;
  287. xSemaphoreGive(SetMotorSpeedData);
  288. }
  289. delay(100);
  290. count--;
  291.  
  292. if(count <= 0)
  293. {
  294. left = false;
  295. count = 30;
  296. }
  297. }
  298. }
  299. xSemaphoreGive(ReadLineSensorData);
  300. }
  301. }
  302.  
  303.  
  304.  
  305. void setup() {
  306.  
  307. attachInterrupt(leftMotor.getIntNum(), isr_process_leftMotor, RISING);
  308. attachInterrupt(rightMotor.getIntNum(), isr_process_rightMotor, RISING);
  309.  
  310. Serial.begin(9600);
  311.  
  312. TCCR1A = _BV(WGM10);
  313. TCCR1B = _BV(CS11) | _BV(WGM12);
  314.  
  315. TCCR2A = _BV(WGM21) | _BV(WGM20);
  316. TCCR2B = _BV(CS21);
  317.  
  318. /*//// SEMAPHORES ////*/
  319. ReadOpticalSensorData = xSemaphoreCreateMutex();
  320. ReadLineSensorData = xSemaphoreCreateMutex();
  321. SetMotorSpeedData = xSemaphoreCreateMutex();
  322.  
  323. vSchedulerInit();
  324.  
  325. /*//// TASKS ////*/
  326. vSchedulerPeriodicTaskCreate(TaskReadOpticalSensor, "t1", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle1, pdMS_TO_TICKS(0),
  327. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  328.  
  329. vSchedulerPeriodicTaskCreate(TaskReadLineSensor, "t2", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle2, pdMS_TO_TICKS(0),
  330. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  331.  
  332. vSchedulerPeriodicTaskCreate(TaskApplyMotorSpeed, "t3", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle3, pdMS_TO_TICKS(0),
  333. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  334.  
  335. vSchedulerPeriodicTaskCreate(TaskAvoidObstacle, "t4", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle4, pdMS_TO_TICKS(0),
  336. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  337.  
  338. vSchedulerPeriodicTaskCreate(TaskFollowTheLine, "t5", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle5, pdMS_TO_TICKS(0),
  339. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  340.  
  341. vSchedulerPeriodicTaskCreate(TaskFindTheLine, "t6", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle6, pdMS_TO_TICKS(0),
  342. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  343.  
  344.  
  345. vSchedulerStart();
  346.  
  347. }
  348.  
  349. void loop() {
  350. /*if(work){
  351. work();
  352. ´work = (work()--)++;
  353. }
  354. */
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement