Advertisement
Trainlover08

New auton using PID

Mar 11th, 2024 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.61 KB | None | 0 0
  1. #pragma region VEXcode Generated Robot Configuration
  2. // Make sure all required headers are included.
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdbool.h>
  6. #include <math.h>
  7. #include <string.h>
  8.  
  9.  
  10. #include "vex.h"
  11.  
  12. using namespace vex;
  13.  
  14. // Brain should be defined by default
  15. brain Brain;
  16.  
  17.  
  18. // START V5 MACROS
  19. #define waitUntil(condition)                                                   \
  20.   do {                                                                         \
  21.     wait(5, msec);                                                             \
  22.   } while (!(condition))
  23.  
  24. #define repeat(iterations)                                                     \
  25.   for (int iterator = 0; iterator < iterations; iterator++)
  26. // END V5 MACROS
  27.  
  28.  
  29. // Robot configuration code.
  30. motor Left1MotorA = motor(PORT4, ratio6_1, false);
  31. motor Left1MotorB = motor(PORT6, ratio6_1, false);
  32. motor_group Left1 = motor_group(Left1MotorA, Left1MotorB);
  33.  
  34. motor Left2 = motor(PORT9, ratio6_1, false);
  35.  
  36. motor Right1MotorA = motor(PORT16, ratio6_1, true);
  37. motor Right1MotorB = motor(PORT14, ratio6_1, true);
  38. motor_group Right1 = motor_group(Right1MotorA, Right1MotorB);
  39.  
  40. motor Right2 = motor(PORT18, ratio6_1, true);
  41.  
  42. motor Intake = motor(PORT10, ratio6_1, false);
  43.  
  44. digital_out Wings = digital_out(Brain.ThreeWirePort.G);
  45. digital_out ratchetLock = digital_out(Brain.ThreeWirePort.B);
  46. controller Controller1 = controller(primary);
  47. motor Cata = motor(PORT17, ratio36_1, true);
  48.  
  49. pot Pot = pot(Brain.ThreeWirePort.B);
  50. distance Distance1 = distance(PORT19);
  51.  
  52.  
  53.  
  54. // Helper to make playing sounds from the V5 in VEXcode easier and
  55. // keeps the code cleaner by making it clear what is happening.
  56. void playVexcodeSound(const char *soundName) {
  57.   printf("VEXPlaySound:%s\n", soundName);
  58.   wait(5, msec);
  59. }
  60.  
  61.  
  62.  
  63. // define variable for remote controller enable/disable
  64. bool RemoteControlCodeEnabled = true;
  65.  
  66. #pragma endregion VEXcode Generated Robot Configuration
  67.  
  68.  
  69. // Include the V5 Library
  70. #include "vex.h"
  71.  
  72. // Allows for easier use of the VEX Library
  73. using namespace vex;
  74.  
  75. competition Competition;
  76.  
  77. // prepare all the motors and such for operation
  78. int whenStarted1() {
  79.   Left1.setMaxTorque(100.0, percent);
  80.   Left2.setMaxTorque(100.0, percent);
  81.   Right1.setMaxTorque(100.0, percent);
  82.   Right2.setMaxTorque(100.0, percent);
  83.   Intake.setVelocity(100.0, percent);
  84.   Intake.spinFor(forward, 2, turns);
  85.   Cata.setStopping(hold);
  86.   Cata.setVelocity(100.0, percent);
  87.   Cata.setMaxTorque(100.0, percent);
  88.   return 0;
  89. }
  90.  
  91. //thread to only control printing output values
  92. float L1 = 0.0;
  93. float L2 = 0.0;
  94. float L3 = 0.0;
  95. float R1 = 0.0;
  96. float R2 = 0.0;
  97. float R3 = 0.0;
  98. float cata = 0.0;
  99. float intake = 0.0;
  100. int ondriver_drivercontrol_1(){
  101.   while(true){
  102.     L1 = Left1MotorA.position(degrees);
  103.     L2 = Left1MotorB.position(degrees);
  104.     L3 = Left2.position(degrees);
  105.     R1 = Right1MotorA.position(degrees);
  106.     R2 = Right1MotorB.position(degrees);
  107.     R3 = Right2.position(degrees);
  108.     cata = Cata.velocity(rpm);
  109.     intake = Intake.velocity(rpm);
  110.     printf("%f,\t%f,\t%f,\t%f,\t%f,\t%f,\t%f,\t%f,\n",L1,L2,L3,R1,R2,R3,cata,intake);
  111.     wait(10, msec);
  112.   }
  113.   return 0;
  114. }
  115.  
  116. //thread for cata, wings, and intake
  117. int ondriver_drivercontrol_2(){
  118.   while(true){
  119.     //Intake
  120.     if(Controller1.ButtonR1.pressing()){
  121.       Intake.setVelocity(-100.0, percent);
  122.       Intake.spin(forward);
  123.       wait(100, msec);
  124.     } else if(Controller1.ButtonR2.pressing()){
  125.       Intake.setVelocity(100.0, percent);
  126.       Intake.spin(forward);
  127.       wait(100, msec);
  128.     } else {
  129.       Intake.stop();
  130.       wait(100, msec);
  131.     }
  132.     //Wings
  133.     if(Controller1.ButtonL1.pressing()){
  134.       Wings.set(true);
  135.     } else {
  136.       Wings.set(false);
  137.     }
  138.     if(Controller1.ButtonUp.pressing()){
  139.         Cata.spin(reverse);
  140.     } else if(Controller1.ButtonDown.pressing()){
  141.         Cata.spin(forward);
  142.     } else {
  143.         Cata.stop();
  144.     }
  145.     if(!Controller1.ButtonLeft.pressing()){
  146.       ratchetLock.set(true);
  147.     } else {
  148.       ratchetLock.set(false);
  149.     }
  150.   }
  151. }
  152. //variable for setting curve values to desired axis
  153. int SelectorReadOut = 0;
  154.  
  155. //universal output for the drive function
  156. float driveOutput = 0.0;
  157.  
  158. //universal values for each drive factor
  159. float axis1Output = 0.0;
  160. float axis3Output = 0.0;
  161.  
  162.  
  163. //Motor Curve function
  164. void motorCurve(){
  165.     //initialize variables first
  166.     double y = 0;
  167.     float coef = 1.28;
  168.    
  169.     int x = fabs(SelectorReadOut * coef);
  170.    
  171.     switch(x){
  172.         case 0 ... 60:
  173.             y = pow(1.04029, x * 1.284467);
  174.             break;
  175.         case 61 ... 80:
  176.             y = 0.75 * x -25;
  177.             break;
  178.         case 81 ... 108:
  179.             y = 1.0357 * x - 47.85714857;
  180.             break;
  181.         case 109 ... 127:
  182.             y = 63 + pow(1.232099, x - 108);          
  183.             break;
  184.         default:
  185.             y = 128;
  186.             break;
  187.     }
  188.     y = y / coef;
  189.    
  190.     if(SelectorReadOut < 0){
  191.         y = -y;
  192.     }
  193.    
  194.     driveOutput = y;
  195. }
  196.  
  197. int MotorCurve(){
  198.     while(true){
  199.         //get and set the appropriate values for curves function
  200.         SelectorReadOut = Controller1.Axis3.position();
  201.         motorCurve();
  202.         axis3Output = driveOutput;
  203.         SelectorReadOut = Controller1.Axis1.position();
  204.         motorCurve();
  205.         axis1Output = driveOutput;
  206.  
  207.         //set deadband for controller
  208.         if(Controller1.Axis3.position() < 5 && Controller1.Axis3.position() > -5){
  209.             axis3Output = 0.0;
  210.         }
  211.         if(Controller1.Axis1.position() < 5 && Controller1.Axis1.position() > -5){
  212.             axis1Output = 0.0;
  213.         }
  214.         //wait so that the processor doesn’t get overloaded
  215.         wait(5.0, msec);
  216.     }
  217.     return 0;
  218. }
  219.  
  220. //DriveTrain thread
  221. int DriveTrain(){
  222.     while(true){
  223.         //sets the drive based on motor curve outputs
  224.  
  225.         axis1Output = axis1Output * .87;
  226.  
  227.         if(Controller1.ButtonL1.pressing()){
  228.             axis3Output = - axis3Output;
  229.         }
  230.  
  231.         int leftDrive = axis3Output + axis1Output;
  232.         int rightDrive = axis3Output - axis1Output;
  233.  
  234.         Left1.setVelocity(leftDrive, percent);
  235.         Left2.setVelocity(leftDrive, percent);
  236.         Right1.setVelocity(rightDrive, percent);
  237.         Right2.setVelocity(rightDrive, percent);
  238.  
  239.         Left1.spin(forward);
  240.         Left2.spin(forward);
  241.         Right1.spin(forward);
  242.         Right2.spin(forward);
  243.      
  244.         //wait so that the processor doesn’t get overloaded
  245.         wait(5, msec);
  246.     }
  247.     return 0;
  248. }
  249.  
  250.  
  251. //Auton portion begins
  252.  
  253. //global list for threads to pull numbers from
  254. float values[] = {};
  255.  
  256.  
  257. //the 8 threads to decipher the array
  258.  
  259.   float pGain = 0.3;
  260.   float iGain = 0.2;
  261.   float dGain = 0.1;
  262.  
  263.  
  264. int r1(){
  265.   int math = -5;
  266.   float errorSum = 0.0;
  267.   while(true){
  268.     float target = values[math];
  269.     math = math + 8;
  270.     float r1 = Right1MotorA.position(degrees);
  271.     wait(10, msec);
  272.     float r2 = Right1MotorA.position(degrees);
  273.     float error = target - r2;
  274.     errorSum = error + errorSum;
  275.     float deltaV = (r2 - r1) / 0.01;
  276.     Right1MotorA.setVelocity((error * pGain) + (errorSum * iGain) + (deltaV * dGain), percent);
  277.   }
  278.   return 0;
  279. }
  280.  
  281. int r2(){
  282.   int math = -4;
  283.   float errorSum = 0.0;
  284.   while(true){
  285.     float target = values[math];
  286.     math = math + 8;
  287.     float r1 = Right1MotorB.position(degrees);
  288.     wait(10, msec);
  289.     float r2 = Right1MotorB.position(degrees);
  290.     float error = target - r2;
  291.     errorSum = error + errorSum;
  292.     float deltaV = (r2 - r1) / 0.01;
  293.     Right1MotorB.setVelocity((error * pGain) + (errorSum * iGain) + (deltaV * dGain), percent);
  294.   }
  295.   return 0;
  296. }
  297.  
  298. int r3(){
  299.   int math = -3;
  300.   float errorSum = 0.0;
  301.   while(true){
  302.     float target = values[math];
  303.     math = math + 8;
  304.     float r1 = Right2.position(degrees);
  305.     wait(10, msec);
  306.     float r2 = Right2.position(degrees);
  307.     float error = target - r2;
  308.     errorSum = error + errorSum;
  309.     float deltaV = (r2 - r1) / 0.01;
  310.     Right2.setVelocity((error * pGain) + (errorSum * iGain) + (deltaV * dGain), percent);
  311.   }
  312.   return 0;
  313. }
  314.  
  315. int l1(){
  316.   int math = -8;
  317.   float errorSum = 0.0;
  318.   while(true){
  319.     float target = values[math];
  320.     math = math + 8;
  321.     float r1 = Left1MotorA.position(degrees);
  322.     wait(10, msec);
  323.     float r2 = Left1MotorA.position(degrees);
  324.     float error = target - r2;
  325.     errorSum = error + errorSum;
  326.     float deltaV = (r2 - r1) / 0.01;
  327.     Left1MotorA.setVelocity((error * pGain) + (errorSum * iGain) + (deltaV * dGain), percent);
  328.   }
  329.   return 0;
  330. }
  331.  
  332. int l2(){
  333.   int math = -7;
  334.   float errorSum = 0.0;
  335.   while(true){
  336.     float target = values[math];
  337.     math = math + 8;
  338.     float r1 = Left1MotorB.position(degrees);
  339.     wait(10, msec);
  340.     float r2 = Left1MotorB.position(degrees);
  341.     float error = target - r2;
  342.     errorSum = error + errorSum;
  343.     float deltaV = (r2 - r1) / 0.01;
  344.     Left1MotorB.setVelocity((error * pGain) + (errorSum * iGain) + (deltaV * dGain), percent);
  345.   }
  346.   return 0;
  347. }
  348.  
  349. int l3(){
  350.   int math = -6;
  351.   float errorSum = 0.0;
  352.   while(true){
  353.     float target = values[math];
  354.     math = math + 8;
  355.     float r1 = Left2.position(degrees);
  356.     wait(10, msec);
  357.     float r2 = Left2.position(degrees);
  358.     float error = target - r2;
  359.     errorSum = error + errorSum;
  360.     float deltaV = (r2 - r1) / 0.01;
  361.     Left2.setVelocity((error * pGain) + (errorSum * iGain) + (deltaV * dGain), percent);
  362.   }
  363.   return 0;
  364. }
  365.  
  366. int cataAuton(){
  367.   int math = -2;
  368.   while(true){
  369.     math = math + 8;
  370.     Cata.setVelocity(values[math], rpm);
  371.     wait(10, msec);
  372.   }
  373.   return 0;
  374. }
  375.  
  376. int intakeAuton(){
  377.   int math = -1;
  378.   Intake.spin(forward);
  379.   while(true){
  380.     math = math + 8;
  381.     Intake.setVelocity(values[math], rpm);
  382.     wait(10, msec);
  383.   }
  384.   return 0;
  385. }
  386.  
  387. // things to do when auton has been started
  388. int onauton_autonomous_0() {
  389.   Right1.setMaxTorque(100, percent);
  390.   Right2.setMaxTorque(100, percent);
  391.   Left1.setMaxTorque(100, percent);
  392.   Left2.setMaxTorque(100, percent);
  393.   Right1.spin(reverse);
  394.   Right2.spin(reverse);
  395.   Left1.spin(forward);
  396.   Left2.spin(forward);
  397.   Intake.spinFor(reverse, 1, turns);
  398.   Cata.spinFor(forward, 180, degrees);
  399.   return 0;
  400. }
  401.  
  402. void VEXcode_driver_task() {
  403.   // Start the driver control tasks
  404.   vex::task drive0(DriveTrain);
  405.   vex::task drive3(MotorCurve);
  406.   vex::task drive1(ondriver_drivercontrol_1);
  407.     //^enable for auton recording^
  408.   vex::task drive2(ondriver_drivercontrol_2);
  409.   while(Competition.isDriverControl() && Competition.isEnabled()) {this_thread::sleep_for(10);}
  410.   drive0.stop();
  411.   //drive1.stop();
  412.   drive2.stop();
  413.   drive3.stop();
  414.   return;
  415. }
  416.  
  417. void VEXcode_auton_task() {
  418.   // Start the auton control tasks....
  419.   vex::task auto0(onauton_autonomous_0);
  420.   vex::task auto1(r1);
  421.   vex::task auto2(r2);
  422.   vex::task auto3(r3);
  423.   vex::task auto4(l1);
  424.   vex::task auto5(l2);
  425.   vex::task auto6(l3);
  426.   vex::task auto7(cataAuton);
  427.   vex::task auto8(intakeAuton);
  428.   while(Competition.isAutonomous() && Competition.isEnabled()) {this_thread::sleep_for(10);}
  429.   auto0.stop();
  430.   auto1.stop();
  431.   auto2.stop();
  432.   auto3.stop();
  433.   auto4.stop();
  434.   auto5.stop();
  435.   auto6.stop();
  436.   auto7.stop();
  437.   auto8.stop();
  438.   return;
  439. }
  440.  
  441. int main() {
  442.   vex::competition::bStopTasksBetweenModes = false;
  443.   Competition.autonomous(VEXcode_auton_task);
  444.   Competition.drivercontrol(VEXcode_driver_task);
  445.  
  446.   wait(15, msec);
  447.   // post event registration
  448.  
  449.   // set default print color to black
  450.   printf("\033[30m");
  451.  
  452.   // wait for rotation sensor to fully initialize
  453.   wait(30, msec);
  454.  
  455.   whenStarted1();
  456. }
  457.  
  458.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement