Advertisement
Guest User

drivetask

a guest
Jan 18th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.23 KB | None | 0 0
  1. task driveTask()
  2. {
  3.     float driveStraightkp;
  4.     float turnSynckp;
  5.     if(lDist == rDist && abs(lDist) > 400)
  6.     {
  7.         driveStraightkp = 0.35;
  8.     }
  9.     else
  10.     {
  11.         driveStraightkp = 0;
  12.     }
  13.     if(lDist == -rDist && abs(lDist) > 30)
  14.     {
  15.         turnSynckp = 1;
  16.     }
  17.     else
  18.     {
  19.         turnSynckp = 0;
  20.     }
  21.  
  22.     float leftDeriv = 0;
  23.     float rightDeriv = 0;
  24.     float dFilter = 0.1;
  25.     prevLeftError = leftDriveTarget  - SensorValue[leftDriveEncoder]; //error values we save to calculate the derivative
  26.     prevRightError = rightDriveTarget - SensorValue[rightDriveEncoder];
  27.     int stoppedCounter = 0; //counter for when the robot has stopped moving
  28.     float leftInt = 0;
  29.     float rightInt = 0;
  30.     driveTargetReached = false;
  31.     while(true)
  32.     {
  33.         leftError = leftDriveTarget - SensorValue[leftDriveEncoder]; //Calculate the error in the difference between leftError and rightError
  34.         rightError = rightDriveTarget - SensorValue[rightDriveEncoder];
  35.  
  36.         leftInt += leftError;
  37.         rightInt += rightError;
  38.  
  39.         if(abs(leftError) > 100)//if the robot is far from the target keep integral at 0
  40.         {
  41.             leftInt = 0;
  42.         }
  43.         if(abs(rightError) > 100)
  44.         {
  45.             rightInt = 0;
  46.         }
  47.         if(sgn(leftError) != sgn(leftInt))//if the robot has crossed the target reset integral
  48.         {
  49.             leftInt = 0;
  50.         }
  51.         if(sgn(rightError) != sgn(rightInt))
  52.         {
  53.             rightInt = 0;
  54.         }
  55.         if(abs(leftInt) > intCap/ki)// if the integral is greater than the cap divided by the integral constant set integral to its maximum
  56.         {
  57.             leftInt = (intCap/ki)*sgn(leftInt);
  58.         }
  59.         if(abs(rightInt) > intCap/ki)
  60.         {
  61.             rightInt = (intCap/ki)*sgn(rightInt);
  62.         }
  63.  
  64.         if(abs(leftError) < 100 || abs(rightError) < 100) //if the error is small don't correct drive difference
  65.         {
  66.             driveStraightkp = 0;
  67.         }
  68.  
  69.         if(reverseDrive)
  70.         {
  71.             if(abs(leftError) < 100 || abs(leftDriveTarget - lDist- SensorValue[leftDriveEncoder]) < 200)
  72.             {
  73.                 speedLimiter = 40;
  74.             }
  75.             else
  76.             {
  77.                 speedLimiter = maxSpeed;
  78.             }
  79.         }
  80.  
  81.         if(lDist == rDist && !reverseDrive)
  82.         {
  83.             if(abs(leftError) < 80)
  84.             {
  85.                 speedLimiter = 35;
  86.             }
  87.             else if(abs(leftError) < 150)
  88.             {
  89.                 speedLimiter = 75;
  90.             }
  91.             else
  92.             {
  93.                 speedLimiter = maxSpeed;
  94.             }
  95.         }
  96.  
  97.         leftDeriv = dFilter * (leftError - prevLeftError) + (1 - dFilter) * leftDeriv;
  98.         rightDeriv = dFilter * (rightError - prevRightError) + (1 - dFilter) * rightDeriv;
  99.  
  100.         float leftDrivePower = kp * leftError + kd * leftDeriv + ki * leftInt;
  101.         float rightDrivePower = kp * rightError + kd * rightDeriv + ki * rightInt;
  102.  
  103.         if(leftDrivePower > speedLimiter)leftDrivePower = speedLimiter;
  104.         if(leftDrivePower < -speedLimiter)leftDrivePower = -speedLimiter;
  105.         if(rightDrivePower > speedLimiter)rightDrivePower = speedLimiter;
  106.         if(rightDrivePower < -speedLimiter)rightDrivePower = -speedLimiter;
  107.  
  108.  
  109.         if(abs(leftError) > 50)
  110.         {
  111.             leftDrivePower += driveStraightkp *(leftError-rightError); //correct drive difference by adjusting power
  112.             rightDrivePower -= driveStraightkp * (leftError-rightError);
  113.  
  114.             leftDrivePower += turnSynckp *(leftError+rightError); //correct drive difference by adjusting power
  115.             rightDrivePower += turnSynckp * (leftError+rightError);
  116.         }
  117.  
  118.         setDrive(leftDrivePower, rightDrivePower);//set the drive motors to their calculated power
  119.  
  120.         if (leftError < 0 || rightError < 0){ //if the robot has passed the target set the target reached value to true
  121.             driveTargetReached = true;
  122.         }
  123.  
  124.         if(abs(leftDeriv) < 1.0 && abs(rightDeriv) < 1 && time1[T4] > 500){ //if the robot isnt moving or is barely moving increase the stopped counter
  125.             stoppedCounter++;
  126.         }
  127.         if(stoppedCounter > accuracy || time1[T4] > driveTimeout){ //if the stopped counter reaches a threshold or the 5 second drive timeout is reached start end of drive code
  128.             if(abs(leftError) < 200 && abs(rightError) < 200){//if error is near target then finish the drive
  129.                 setDrive(0,0);
  130.                 driveFinished = true;
  131.                 driveTargetReached = true;
  132.                 setDrive(0,0);
  133.                 stopTask(driveTask);
  134.             }
  135.             else //if the drive reaches timeout stop the drive and abort auton.
  136.             {
  137.                 setDrive(0,0);
  138.                 driveFinished = true;
  139.                 startTask(abort);//start the abort task
  140.                 stopTask(driveTask);
  141.             }
  142.         }
  143.         prevLeftError = leftError; //reset Previus Error for the derivative
  144.         prevRightError = rightError;
  145.         delay(10);
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement