Advertisement
sockra

Untitled

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