Advertisement
sockra

Untitled

Oct 21st, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 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 = -50;
  115. rightMotorSpeed = 150;
  116. xSemaphoreGive(SetMotorSpeedData);
  117. }
  118. }
  119. else
  120. {
  121. if(xSemaphoreTake(SetMotorSpeedData, 5) == pdTRUE)
  122. {
  123. //Hard-right
  124. leftMotorSpeed = 150;
  125. rightMotorSpeed = -50;
  126. xSemaphoreGive(SetMotorSpeedData);
  127. }
  128. }
  129. }
  130. xSemaphoreGive(ReadOpticalSensorData);
  131. }
  132. }
  133.  
  134. /*//// LINE-TASKS ////*/
  135. void TaskFollowTheLine(void *a)
  136. {
  137. static int lastTurn;
  138.  
  139. if(xSemaphoreTake(ReadOpticalSensorData, 5) == pdTRUE)
  140. {
  141. if(lineFound && !obstacleFound)
  142. {
  143. if(xSemaphoreTake(ReadLineSensorData, 5) == pdTRUE)
  144. {
  145. if(xSemaphoreTake(SetMotorSpeedData, 5) == pdTRUE)
  146. {
  147. switch(lineSensorData)
  148. {
  149. case 0: //Forward
  150. leftMotorSpeed = 125;
  151. rightMotorSpeed = 125;
  152. lastTurn = 0;
  153. obstacleWasFound = false;
  154. leftTurn = 0;
  155. rightTurn = 0;
  156. break;
  157.  
  158. case 1: //Left
  159. leftMotorSpeed = -150;
  160. rightMotorSpeed = 150;
  161. lastTurn = 1;
  162. leftTurn++;
  163. obstacleWasFound = false;
  164. break;
  165.  
  166. case 2: //Right
  167. leftMotorSpeed = 150;
  168. rightMotorSpeed = -150;
  169. lastTurn = 2;
  170. rightTurn++;
  171. obstacleWasFound = false;
  172. break;
  173.  
  174. case 3: //No line
  175. if(obstacleWasFound)
  176. {
  177. leftMotorSpeed = 125;
  178. rightMotorSpeed = 125;
  179. }
  180. else
  181. {
  182. switch(lastTurn)
  183. {
  184. case 0: //Reverse
  185. leftMotorSpeed = -150;
  186. rightMotorSpeed = -150;
  187. break;
  188.  
  189. case 1: //Left
  190. leftMotorSpeed = -100;
  191. rightMotorSpeed = 100;
  192. break;
  193.  
  194. case 2: //Right
  195. leftMotorSpeed = 100;
  196. rightMotorSpeed = -100;
  197. break;
  198. }
  199. }
  200. break;
  201. }
  202. xSemaphoreGive(SetMotorSpeedData);
  203. }
  204. xSemaphoreGive(ReadLineSensorData);
  205. }
  206. }
  207. xSemaphoreGive(ReadOpticalSensorData);
  208. }
  209. }
  210.  
  211. void TaskFindTheLine(void *a)
  212. {
  213. if(xSemaphoreTake(ReadLineSensorData, 5) == pdTRUE)
  214. {
  215. if(lineSensorData != 3) //3 == no line
  216. {
  217. lineFound = true; //This is always true when the initial line is found.
  218. }
  219. else
  220. {
  221. //Search for the initial line. This happens at the start of the program
  222. }
  223. xSemaphoreGive(ReadLineSensorData);
  224. }
  225. }
  226.  
  227.  
  228.  
  229. void setup() {
  230.  
  231. attachInterrupt(leftMotor.getIntNum(), isr_process_leftMotor, RISING);
  232. attachInterrupt(rightMotor.getIntNum(), isr_process_rightMotor, RISING);
  233.  
  234. Serial.begin(9600);
  235.  
  236. TCCR1A = _BV(WGM10);
  237. TCCR1B = _BV(CS11) | _BV(WGM12);
  238.  
  239. TCCR2A = _BV(WGM21) | _BV(WGM20);
  240. TCCR2B = _BV(CS21);
  241.  
  242. /*//// SEMAPHORES ////*/
  243. ReadOpticalSensorData = xSemaphoreCreateMutex();
  244. ReadLineSensorData = xSemaphoreCreateMutex();
  245. SetMotorSpeedData = xSemaphoreCreateMutex();
  246.  
  247. vSchedulerInit();
  248.  
  249. /*//// TASKS ////*/
  250. vSchedulerPeriodicTaskCreate(TaskReadOpticalSensor, "t1", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle1, pdMS_TO_TICKS(0),
  251. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  252.  
  253. vSchedulerPeriodicTaskCreate(TaskReadLineSensor, "t2", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle2, pdMS_TO_TICKS(0),
  254. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  255.  
  256. vSchedulerPeriodicTaskCreate(TaskApplyMotorSpeed, "t3", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle3, pdMS_TO_TICKS(0),
  257. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  258.  
  259. vSchedulerPeriodicTaskCreate(TaskAvoidObstacle, "t4", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle4, pdMS_TO_TICKS(0),
  260. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  261.  
  262. vSchedulerPeriodicTaskCreate(TaskFollowTheLine, "t5", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle5, pdMS_TO_TICKS(0),
  263. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  264.  
  265. vSchedulerPeriodicTaskCreate(TaskFindTheLine, "t6", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle6, pdMS_TO_TICKS(0),
  266. pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  267.  
  268.  
  269. vSchedulerStart();
  270.  
  271. }
  272.  
  273. void loop() {
  274. // put your main code here, to run repeatedly:
  275.  
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement