Advertisement
Trainlover08

FINAL CODE (OPTIMIZED & DOCUMENTED)

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