Advertisement
sockra

Untitled

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