Advertisement
sockra

senaste

Oct 21st, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 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 lineFound = false;
  54. int lineSensorData;
  55. int leftMotorSpeed;
  56. int rightMotorSpeed;
  57. /*These are used when avoiding potential obstacles */
  58. int leftTurn = 0;
  59. int rightTurn = 0;
  60.  
  61. /*//// READ-TASKS ////*/
  62. void TaskReadOpticalSensor(void *a)
  63. {
  64. if(xSemaphoreTake(ReadOpticalSensorData, 5) == pdTRUE)
  65. {
  66. if(opticalSensor.distanceCm() <= 20)
  67. {
  68. obstacleFound = true;
  69. }
  70. else
  71. {
  72. obstacleFound = false;
  73. }
  74. xSemaphoreGive(ReadOpticalSensorData);
  75. }
  76. //vTaskDelay(1); // one tick delay (15ms) in between reads for stability
  77. }
  78.  
  79. void TaskReadLineSensor(void *a)
  80. {
  81. if(xSemaphoreTake(ReadLineSensorData, 5) == pdTRUE)
  82. {
  83. lineSensorData = lineSensor.readSensors();
  84. xSemaphoreGive(ReadLineSensorData);
  85. }
  86. //vTaskDelay(1); // one tick delay (15ms) in between reads for stability
  87. }
  88.  
  89. /*//// MOTOR-TASK ////*/
  90. void TaskApplyMotorSpeed(void *a)
  91. {
  92. if(xSemaphoreTake(SetMotorSpeedData, 5) == pdTRUE)
  93. {
  94. leftMotor.setMotorPwm(leftMotorSpeed);
  95. rightMotor.setMotorPwm(-rightMotorSpeed);
  96. xSemaphoreGive(SetMotorSpeedData);
  97. }
  98. }
  99.  
  100. /*//// OBSTACLE-AVOIDANCE ////*/
  101. void TaskAvoidObstacle(void *a)
  102. {
  103. if(xSemaphoreTake(ReadOpticalSensorData, 5) == pdTRUE)
  104. {
  105. if(obstacleFound)
  106. {
  107. obstacleWasFound = true;
  108.  
  109. if(leftTurn > rightTurn)
  110. {
  111. if(xSemaphoreTake(SetMotorSpeedData, 5) == pdTRUE)
  112. {
  113. //Hard-left
  114. leftMotorSpeed = -150;
  115. rightMotorSpeed = 150;
  116. xSemaphoreGive(SetMotorSpeedData);
  117. }
  118. delay(800);
  119. }
  120. else
  121. {
  122. if(xSemaphoreTake(SetMotorSpeedData, 5) == pdTRUE)
  123. {
  124. //Hard-right
  125. leftMotorSpeed = 150;
  126. rightMotorSpeed = -150;
  127. xSemaphoreGive(SetMotorSpeedData);
  128. }
  129. delay(800);
  130. }
  131. }
  132. xSemaphoreGive(ReadOpticalSensorData);
  133. }
  134. }
  135.  
  136. /*//// LINE-TASKS ////*/
  137. void TaskFollowTheLine(void *a)
  138. {
  139. static int lastTurn;
  140.  
  141. if(xSemaphoreTake(ReadOpticalSensorData, 0) == pdTRUE)
  142. {
  143. if(lineFound && !obstacleFound)
  144. {
  145. if(xSemaphoreTake(ReadLineSensorData, 0) == pdTRUE)
  146. {
  147. if(xSemaphoreTake(SetMotorSpeedData, 0) == pdTRUE)
  148. {
  149. switch(lineSensorData)
  150. {
  151. case 0: //Forward
  152. leftMotorSpeed = 130;
  153. rightMotorSpeed = 130;
  154. lastTurn = 0;
  155. obstacleWasFound = false;
  156. leftTurn = 0;
  157. rightTurn = 0;
  158. break;
  159.  
  160. case 1: //Left
  161. leftMotorSpeed = -150;
  162. rightMotorSpeed = 150;
  163. lastTurn = 1;
  164. leftTurn++;
  165. obstacleWasFound = false;
  166. break;
  167.  
  168. case 2: //Right
  169. leftMotorSpeed = 150;
  170. rightMotorSpeed = -150;
  171. lastTurn = 2;
  172. rightTurn++;
  173. obstacleWasFound = false;
  174. break;
  175.  
  176. case 3: //No line
  177. if(!obstacleWasFound)
  178. {
  179. switch(lastTurn)
  180. {
  181. case 0: //Reverse
  182. leftMotorSpeed = -150;
  183. rightMotorSpeed = -150;
  184. break;
  185.  
  186. case 1: //Left
  187. leftMotorSpeed = -150;
  188. rightMotorSpeed = 150;
  189. break;
  190.  
  191. case 2: //Right
  192. leftMotorSpeed = 150;
  193. rightMotorSpeed = -150;
  194. break;
  195. }
  196. }
  197. break;
  198. }
  199. xSemaphoreGive(SetMotorSpeedData);
  200. }
  201. xSemaphoreGive(ReadLineSensorData);
  202. }
  203. }
  204. xSemaphoreGive(ReadOpticalSensorData);
  205. }
  206. }
  207.  
  208. void TaskFindTheLine(void *a)
  209. {
  210. if(xSemaphoreTake(ReadLineSensorData, 5) == pdTRUE)
  211. {
  212. if(lineSensorData != 3) //3 == no line
  213. {
  214. lineFound = true; //This is always true when the initial line is found.
  215. }
  216. else
  217. {
  218. //Search for the initial line. This happens at the start of the program
  219. }
  220. xSemaphoreGive(ReadLineSensorData);
  221. }
  222. }
  223.  
  224.  
  225.  
  226. void setup() {
  227.  
  228. attachInterrupt(leftMotor.getIntNum(), isr_process_leftMotor, RISING);
  229. attachInterrupt(rightMotor.getIntNum(), isr_process_rightMotor, RISING);
  230.  
  231. Serial.begin(9600);
  232.  
  233. TCCR1A = _BV(WGM10);
  234. TCCR1B = _BV(CS11) | _BV(WGM12);
  235.  
  236. TCCR2A = _BV(WGM21) | _BV(WGM20);
  237. TCCR2B = _BV(CS21);
  238.  
  239. /*//// SEMAPHORES ////*/
  240. ReadOpticalSensorData = xSemaphoreCreateMutex();
  241. ReadLineSensorData = xSemaphoreCreateMutex();
  242. SetMotorSpeedData = xSemaphoreCreateMutex();
  243.  
  244. vSchedulerInit();
  245.  
  246. /*//// TASKS ////*/
  247. vSchedulerPeriodicTaskCreate(TaskReadOpticalSensor, "t1", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle1, pdMS_TO_TICKS(0),
  248. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  249.  
  250. vSchedulerPeriodicTaskCreate(TaskReadLineSensor, "t2", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle2, pdMS_TO_TICKS(0),
  251. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  252.  
  253. vSchedulerPeriodicTaskCreate(TaskApplyMotorSpeed, "t3", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle3, pdMS_TO_TICKS(0),
  254. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  255.  
  256. vSchedulerPeriodicTaskCreate(TaskAvoidObstacle, "t4", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle4, pdMS_TO_TICKS(0),
  257. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  258.  
  259. vSchedulerPeriodicTaskCreate(TaskFollowTheLine, "t5", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle5, pdMS_TO_TICKS(0),
  260. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  261.  
  262. vSchedulerPeriodicTaskCreate(TaskFindTheLine, "t6", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle6, pdMS_TO_TICKS(0),
  263. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  264.  
  265.  
  266. vSchedulerStart();
  267.  
  268. }
  269.  
  270. void loop() {
  271. // put your main code here, to run repeatedly:
  272.  
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement