Advertisement
sockra

Untitled

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