Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. float previousError;
  2. int integral;
  3. float turnPID(int angle){
  4. const float Kp = 3;
  5. const float Ki = 0.0;
  6. const float Kd = 0.0;
  7. float error;
  8. int derivative;
  9. int motorSpeed;
  10.  
  11. error = (angle) - avg(Gyro2.value(vex::rotationUnits::deg), Gyro1.value(vex::rotationUnits::deg)); //calculate proportional error
  12. integral += error; //calculate integral of error
  13. derivative = error - previousError; //calculate derivative of drive movement
  14. previousError = error; //current error for next loop
  15. if (error > .5) { integral = 0; }
  16.  
  17. motorSpeed = (Kp*error) + (Ki*integral) + (Kd*derivative); //setting power level for motors
  18.  
  19. if (motorSpeed > 50) { motorSpeed = 50; }
  20. else if (motorSpeed < -50) { motorSpeed = -50; }
  21.  
  22. return -motorSpeed;
  23. }
  24.  
  25. void autonTurn(int angle, int time){
  26. int timeout = time;
  27. while (timeout > 0){
  28.  
  29. frontLeftDrive.spin(vex::directionType::rev,turnPID(angle),vex::velocityUnits::rpm);
  30. midLeftDrive.spin(vex::directionType::rev,turnPID(angle),vex::velocityUnits::rpm);
  31. backLeftDrive.spin(vex::directionType::rev,turnPID(angle),vex::velocityUnits::rpm);
  32. frontRightDrive.spin(vex::directionType::rev,turnPID(angle),vex::velocityUnits::rpm);
  33. midRightDrive.spin(vex::directionType::rev,turnPID(angle),vex::velocityUnits::rpm);
  34. backRightDrive.spin(vex::directionType::rev,turnPID(angle),vex::velocityUnits::rpm);
  35. /*
  36. frontLeftDrive.startRotateFor(-rev(dist),vex::rotationUnits::rev,150,vex::velocityUnits::rpm);
  37. midLeftDrive.startRotateFor(-rev(dist),vex::rotationUnits::rev,150,vex::velocityUnits::rpm);
  38. backLeftDrive.startRotateFor(-rev(dist),vex::rotationUnits::rev,150,vex::velocityUnits::rpm);
  39. frontRightDrive.startRotateFor(-rev(dist),vex::rotationUnits::rev,150,vex::velocityUnits::rpm);
  40. midRightDrive.startRotateFor(-rev(dist),vex::rotationUnits::rev,150,vex::velocityUnits::rpm);
  41. backRightDrive.startRotateFor(-rev(dist),vex::rotationUnits::rev,150,vex::velocityUnits::rpm);
  42. */
  43. vex::task::sleep(10);
  44. timeout -= 10;
  45. }
  46. frontLeftDrive.stop();
  47. midLeftDrive.stop();
  48. backLeftDrive.stop();
  49. frontRightDrive.stop();
  50. midRightDrive.stop();
  51. backRightDrive.stop();
  52. }
  53.  
  54. //to call turn function in auton
  55. autonTurn(90, 2500); //turn 90 degrees counterclockwise and run PID for 1.2 sec
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement