Advertisement
sockra

Untitled

Oct 15th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.39 KB | None | 0 0
  1. #include "scheduler.h"
  2. #include <MeAurigaLab.h>
  3.  
  4. MeLineFollower lineSensor(PORT_9);
  5. MeUltrasonicSensor opticalSensor(PORT_10);
  6.  
  7. TaskHandle_t xHandle1 = NULL;
  8. TaskHandle_t xHandle2 = NULL;
  9. TaskHandle_t xHandle3 = NULL;
  10. TaskHandle_t xHandle4 = NULL;
  11. TaskHandle_t xHandle5 = NULL;
  12.  
  13. MeEncoderOnBoard leftMotor(SLOT1);
  14. MeEncoderOnBoard rightMotor(SLOT2);
  15.  
  16. /*/////////  GLOBAL //////////*/
  17. int lineSensorData;
  18. bool obstacleFound = false;
  19. int leftMotorSpeed;
  20. int rightMotorSpeed;
  21. bool lineFound = false;
  22.  
  23. /*////////////////////////////*/
  24.  
  25. /*////  INIT MOTOR  ////*/
  26. void isr_process_leftMotor(void)
  27. {
  28.   if(digitalRead(leftMotor.getPortB()) == 0)
  29.   {
  30.     leftMotor.pulsePosMinus();
  31.   }
  32.   else
  33.   {
  34.     leftMotor.pulsePosPlus();
  35.   }
  36. }
  37.  
  38. void isr_process_rightMotor(void)
  39. {
  40.   if(digitalRead(rightMotor.getPortB()) == 0){
  41.     rightMotor.pulsePosMinus();
  42.   }
  43.   else
  44.   {
  45.     rightMotor.pulsePosPlus();
  46.   }
  47. }
  48. /*//////////////////////*/
  49.  
  50. /*////  TaskReadLineSensor  ////*/
  51. void TaskReadLineSensor(void *a)
  52. {
  53.   lineSensorData = lineSensor.readSensors();
  54. }
  55. /*//////////////////////////////*/
  56.  
  57. /*////  TaskReadOpticalSensor  ////*/
  58. void TaskReadOpticalSensor(void *a)
  59. {
  60.  
  61.   if(opticalSensor.distanceCm() < 15)
  62.   {
  63.     obstacleFound = true;
  64.   }
  65.   else
  66.   {
  67.     obstacleFound = false;
  68.   }
  69. }
  70. /*//////////////////////////////*/
  71.  
  72. /*////  TaskSetMotorSpeed ////*/
  73. void TaskSetMotorSpeed(void *a)
  74. {
  75.   leftMotor.setMotorPwm(-leftMotorSpeed);
  76.   rightMotor.setMotorPwm(rightMotorSpeed);
  77. }
  78. /*////////////////////////////*/
  79.  
  80. /*////  TaskFollowLine  ////*/
  81. void TaskFollowLine(void *a)
  82. {
  83.   static int lastTurn;
  84.  
  85.   if(!obstacleFound && lineFound) //Go in here only if no obstacle is found and the initial line has been found atleast once! check TaskSearchForLine
  86.   {
  87.     switch(lineSensorData)
  88.     {
  89.       case 0: //Forward
  90.       leftMotorSpeed =  50;
  91.       rightMotorSpeed = 50;
  92.       lastTurn = 0;
  93.       break;
  94.  
  95.       case 1: //Left
  96.       leftMotorSpeed =  50;
  97.       rightMotorSpeed = -50;
  98.       lastTurn = 1;
  99.       break;
  100.  
  101.       case 2: //Right
  102.       leftMotorSpeed =  -50;
  103.       rightMotorSpeed = 50;
  104.       lastTurn = 2;
  105.       break;
  106.  
  107.       case 3: //NoLine
  108.         switch(lastTurn)  //Make correction if it was about to turn but then went outside the line
  109.         {
  110.           case 0: //It went forward
  111.           leftMotorSpeed =  50;
  112.           rightMotorSpeed = 50;
  113.           break;
  114.    
  115.           case 1: //It went left
  116.           leftMotorSpeed =  50;
  117.           rightMotorSpeed = -50;
  118.           break;
  119.    
  120.           case 2: //It went right
  121.           leftMotorSpeed =  -50;
  122.           rightMotorSpeed = 50;
  123.           break;
  124.         }
  125.       break;
  126.     }
  127.   }
  128. }
  129. /*//////////////////////////*/
  130.  
  131. /*////  TaskSearchForLine ////*/
  132. void TaskSearchForLine(void *a)
  133. {
  134.   if(lineSensorData == 3 && !lineFound)
  135.   {
  136.     //Searching
  137.     Serial.println("Searching");  
  138.   }
  139.   else
  140.   {
  141.     //Found it
  142.     lineFound = true;
  143.   }
  144. }
  145. /*////////////////////////////*/
  146.  
  147.  
  148.  
  149. void setup() {
  150.  
  151.   attachInterrupt(leftMotor.getIntNum(), isr_process_leftMotor, RISING);
  152.   attachInterrupt(rightMotor.getIntNum(), isr_process_rightMotor, RISING);
  153.  
  154.   Serial.begin(9600);
  155.  
  156.   TCCR1A = _BV(WGM10);
  157.   TCCR1B = _BV(CS11) | _BV(WGM12);
  158.  
  159.   TCCR2A = _BV(WGM21) | _BV(WGM20);
  160.   TCCR2B = _BV(CS21);
  161.  
  162.   vSchedulerInit();
  163.  
  164.   vSchedulerPeriodicTaskCreate(TaskReadOpticalSensor, "opticalSensorData", configMINIMAL_STACK_SIZE, NULL , 1, &xHandle1, pdMS_TO_TICKS(0),
  165.   pdMS_TO_TICKS(10), pdMS_TO_TICKS(10), pdMS_TO_TICKS(10));
  166.  
  167.   vSchedulerPeriodicTaskCreate(TaskReadLineSensor, "lineSensorData", configMINIMAL_STACK_SIZE, NULL , 2, &xHandle2, pdMS_TO_TICKS(0),
  168.   pdMS_TO_TICKS(10), pdMS_TO_TICKS(10), pdMS_TO_TICKS(10));
  169.  
  170.   vSchedulerPeriodicTaskCreate(TaskSetMotorSpeed, "setMotorSpeed", configMINIMAL_STACK_SIZE, NULL , 3, &xHandle3, pdMS_TO_TICKS(0),
  171.   pdMS_TO_TICKS(10), pdMS_TO_TICKS(10), pdMS_TO_TICKS(10));
  172.  
  173.   vSchedulerPeriodicTaskCreate(TaskFollowLine, "followLine", configMINIMAL_STACK_SIZE, NULL , 4, &xHandle4, pdMS_TO_TICKS(0),
  174.   pdMS_TO_TICKS(10), pdMS_TO_TICKS(10), pdMS_TO_TICKS(10));
  175.  
  176.   vSchedulerPeriodicTaskCreate(TaskSearchForLine, "searchForLine", configMINIMAL_STACK_SIZE, NULL , 5, &xHandle4, pdMS_TO_TICKS(0),
  177.   pdMS_TO_TICKS(10), pdMS_TO_TICKS(10), pdMS_TO_TICKS(10));
  178.  
  179.  
  180.  
  181.   vSchedulerStart();
  182. }
  183.  
  184. void loop() {
  185.   // put your main code here, to run repeatedly:
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement