Advertisement
sockra

Senaste versionen av labben

Oct 15th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.08 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. TaskHandle_t xHandle1 = NULL;
  9. TaskHandle_t xHandle2 = NULL;
  10. TaskHandle_t xHandle3 = NULL;
  11. TaskHandle_t xHandle4 = NULL;
  12. TaskHandle_t xHandle5 = NULL;
  13. TaskHandle_t xHandle6 = NULL;
  14.  
  15. MeEncoderOnBoard leftMotor(SLOT1);
  16. MeEncoderOnBoard rightMotor(SLOT2);
  17.  
  18. /*////  Global variables  ////*/
  19. int lineSensorData;
  20. bool obstacleFound = false;
  21. int leftMotorSpeed;
  22. int rightMotorSpeed;
  23. bool lineFound = false;
  24. bool moveOn = false;
  25. /*////////////////////////////*/
  26.  
  27. /*////  Semaphore ////*/
  28. SemaphoreHandle_t ReadLineSensor;
  29. SemaphoreHandle_t ApplyMotorSpeed;
  30. SemaphoreHandle_t ReadOpticalSensor;
  31. /*////////////////////*/
  32.  
  33.  
  34. /*////  INIT MOTOR  ////*/
  35. void isr_process_leftMotor(void)
  36. {
  37.   if(digitalRead(leftMotor.getPortB()) == 0)
  38.   {
  39.     leftMotor.pulsePosMinus();
  40.   }
  41.   else
  42.   {
  43.     leftMotor.pulsePosPlus();
  44.   }
  45. }
  46.  
  47. void isr_process_rightMotor(void)
  48. {
  49.   if(digitalRead(rightMotor.getPortB()) == 0){
  50.     rightMotor.pulsePosMinus();
  51.   }
  52.   else
  53.   {
  54.     rightMotor.pulsePosPlus();
  55.   }
  56. }
  57. /*//////////////////////*/
  58.  
  59. /*////  TaskReadLineSensor  ////*/
  60. void TaskReadLineSensor(void *a)
  61. {
  62.   if(xSemaphoreTake(ReadLineSensor, 5) == pdTRUE)
  63.   {
  64.     lineSensorData = lineSensor.readSensors();
  65.     xSemaphoreGive(ReadLineSensor);
  66.   }
  67. }
  68. /*//////////////////////////////*/
  69.  
  70. /*////  TaskReadOpticalSensor  ////*/
  71. void TaskReadOpticalSensor(void *a)
  72. {
  73.   if(xSemaphoreTake(ReadOpticalSensor, 5) == pdTRUE)
  74.   {
  75.     if(opticalSensor.distanceCm() < 15)
  76.     {
  77.       obstacleFound = true;
  78.     }
  79.     else
  80.     {
  81.       obstacleFound = false;
  82.     }
  83.  
  84.     xSemaphoreGive(ReadOpticalSensor);
  85.   }
  86. }
  87. /*//////////////////////////////*/
  88.  
  89. /*////  TaskSetMotorSpeed ////*/
  90. void TaskSetMotorSpeed(void *a)
  91. {
  92.   if(xSemaphoreTake(ApplyMotorSpeed, 5) == pdTRUE)
  93.   {
  94.     leftMotor.setMotorPwm(-leftMotorSpeed);
  95.     rightMotor.setMotorPwm(rightMotorSpeed);
  96.     xSemaphoreGive(ApplyMotorSpeed);
  97.   }
  98. }
  99. /*////////////////////////////*/
  100.  
  101. /*////  TaskFollowLine  ////*/
  102. void TaskFollowLine(void *a)
  103. {
  104.   static int lastTurn;
  105.  
  106.   if(!obstacleFound && lineFound && xSemaphoreTake(ReadLineSensor, 5) == pdTRUE && xSemaphoreTake(ApplyMotorSpeed, 5) == pdTRUE && xSemaphoreTake(ReadOpticalSensor, 5) == pdTRUE) //Go in here only if no obstacle is found and the initial line has been found atleast once! check TaskSearchForLine
  107.   {
  108.     switch(lineSensorData)
  109.     {
  110.       case 0: //Forward
  111.       leftMotorSpeed =  100;
  112.       rightMotorSpeed = 100;
  113.       lastTurn = 0;
  114.       break;
  115.  
  116.       case 1: //Left
  117.       leftMotorSpeed =  150;
  118.       rightMotorSpeed = -150;
  119.       lastTurn = 1;
  120.       break;
  121.  
  122.       case 2: //Right
  123.       leftMotorSpeed =  -150;
  124.       rightMotorSpeed = 150;
  125.       lastTurn = 2;
  126.       break;
  127.  
  128.       case 3: //NoLine
  129.       switch(lastTurn)  //Make correction if it was about to turn but then went outside the line
  130.       {
  131.         case 0: //It went forward
  132.         leftMotorSpeed =  -100;
  133.         rightMotorSpeed = -100;
  134.         break;
  135.  
  136.         case 1: //It went left
  137.         leftMotorSpeed =  150;
  138.         rightMotorSpeed = -150;
  139.         break;
  140.  
  141.         case 2: //It went right
  142.         leftMotorSpeed =  -150;
  143.         rightMotorSpeed = 150;
  144.         break;
  145.       }
  146.       break;
  147.     }
  148.     xSemaphoreGive(ReadLineSensor);
  149.     xSemaphoreGive(ReadOpticalSensor);
  150.     xSemaphoreGive(ApplyMotorSpeed);
  151.   }
  152. }
  153. /*//////////////////////////*/
  154.  
  155. /*////  TaskSearchForLine ////*/
  156. void TaskSearchForLine(void *a)
  157. {
  158.   if(xSemaphoreTake(ReadLineSensor, 5) == pdTRUE)
  159.   {
  160.     if(lineSensorData == 3)
  161.     {
  162.       //Searching
  163.       Serial.println("Searching");  
  164.     }
  165.     else
  166.     {
  167.       //Found it
  168.       lineFound = true;
  169.     }
  170.  
  171.     xSemaphoreGive(ReadLineSensor);
  172.   }
  173. }
  174. /*////////////////////////////*/
  175.  
  176. void TaskObstacleAvoidance(void *a)
  177. {
  178.   Serial.println(objective);
  179.   switch(objective)
  180.   {
  181.     case 0: //Reverse
  182.     if(obstacleFound)
  183.     {
  184.        if(xSemaphoreTake(ApplyMotorSpeed, 5) == pdTRUE)
  185.        {
  186.           leftMotorSpeed = 0;
  187.           rightMotorSpeed = 0;
  188.           xSemaphoreGive(ApplyMotorSpeed);
  189.        }
  190.        moveOn = true;
  191.     }
  192.     else if(moveOn && !obstacleFound)
  193.     {
  194.         objective = 1;
  195.     }
  196.     break;
  197.   }
  198. }
  199.  
  200.  
  201.  
  202. void setup() {
  203.  
  204.   attachInterrupt(leftMotor.getIntNum(), isr_process_leftMotor, RISING);
  205.   attachInterrupt(rightMotor.getIntNum(), isr_process_rightMotor, RISING);
  206.  
  207.   Serial.begin(9600);
  208.  
  209.   TCCR1A = _BV(WGM10);
  210.   TCCR1B = _BV(CS11) | _BV(WGM12);
  211.  
  212.   TCCR2A = _BV(WGM21) | _BV(WGM20);
  213.   TCCR2B = _BV(CS21);
  214.  
  215.   //Semaphores
  216.   ReadLineSensor = xSemaphoreCreateMutex();
  217.   ApplyMotorSpeed = xSemaphoreCreateMutex();
  218.   ReadOpticalSensor = xSemaphoreCreateMutex();
  219.  
  220.   vSchedulerInit();
  221.  
  222.   vSchedulerPeriodicTaskCreate(TaskReadOpticalSensor, "opticalSensorData", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle1, pdMS_TO_TICKS(0),
  223.   pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  224.  
  225.   vSchedulerPeriodicTaskCreate(TaskObstacleAvoidance, "obstacleAvoidance", configMINIMAL_STACK_SIZE, NULL , 2, &xHandle2, pdMS_TO_TICKS(0),
  226.   pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  227.  
  228.   vSchedulerPeriodicTaskCreate(TaskSetMotorSpeed, "setMotorSpeed", configMINIMAL_STACK_SIZE, NULL , 3, &xHandle3, pdMS_TO_TICKS(0),
  229.   pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  230.  
  231.   vSchedulerPeriodicTaskCreate(TaskReadLineSensor, "lineSensorData", configMINIMAL_STACK_SIZE, NULL , 4, &xHandle4, pdMS_TO_TICKS(0),
  232.   pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  233.  
  234.   vSchedulerPeriodicTaskCreate(TaskFollowLine, "followLine", configMINIMAL_STACK_SIZE, NULL , 5, &xHandle5, pdMS_TO_TICKS(0),
  235.   pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  236.  
  237.   vSchedulerPeriodicTaskCreate(TaskSearchForLine, "searchForLine", configMINIMAL_STACK_SIZE, NULL , 6, &xHandle6, pdMS_TO_TICKS(0),
  238.   pdMS_TO_TICKS(20), pdMS_TO_TICKS(20), pdMS_TO_TICKS(20));
  239.  
  240.  
  241.  
  242.   vSchedulerStart();
  243. }
  244.  
  245. void loop() {
  246.   // put your main code here, to run repeatedly:
  247.  
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement