Advertisement
sockra

Untitled

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