Advertisement
Trainlover08

???

Dec 5th, 2023 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.88 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. competition Competition;
  15.  
  16. //define global variables
  17. int axis3Drive = 0;
  18. int axis1Drive = 0;
  19. float coef = 1.28;
  20.  
  21. // Brain should be defined by default
  22. brain Brain;
  23.  
  24.  
  25. // START V5 MACROS
  26. #define waitUntil(condition)                                                   \
  27.   do {                                                                         \
  28.     wait(5, msec);                                                             \
  29.   } while (!(condition))
  30.  
  31. #define repeat(iterations)                                                     \
  32.   for (int iterator = 0; iterator < iterations; iterator++)
  33. // END V5 MACROS
  34.  
  35.  
  36. // Robot configuration code.
  37. controller Controller1 = controller(primary);
  38. distance Distance1 = distance(PORT2);
  39. motor leftMotorA = motor(PORT5, ratio6_1, false);
  40. motor leftMotorB = motor(PORT7, ratio6_1, false);
  41. motor_group LeftDriveSmart = motor_group(leftMotorA, leftMotorB);
  42. motor rightMotorA = motor(PORT9, ratio6_1, true);
  43. motor rightMotorB = motor(PORT4, ratio6_1, true);
  44. motor_group RightDriveSmart = motor_group(rightMotorA, rightMotorB);
  45. drivetrain Drivetrain = drivetrain(LeftDriveSmart, RightDriveSmart, 219.44, 295, 40, mm, 1);
  46.  
  47. motor cataMotorA = motor(PORT14, ratio36_1, true);
  48. motor cataMotorB = motor(PORT10, ratio36_1, false);
  49. motor_group cata = motor_group(cataMotorA, cataMotorB);
  50.  
  51. motor intake = motor(PORT11, ratio36_1, true);
  52.  
  53. digital_out DigitalOutB = digital_out(Brain.ThreeWirePort.B);
  54. digital_out DigitalOutH = digital_out(Brain.ThreeWirePort.H);
  55.  
  56. digital_in DigitalInD = digital_in(Brain.ThreeWirePort.D);
  57.  
  58. // define variable for remote controller enable/disable
  59. bool RemoteControlCodeEnabled = true;
  60. // define variables used for controlling motors based on controller inputs
  61. bool DrivetrainLNeedsToBeStopped_Controller1 = true;
  62. bool DrivetrainRNeedsToBeStopped_Controller1 = true;
  63.  
  64. int axis1Position = 0;
  65. int axis3Posititon = 0;
  66.  
  67. int axis3calc() {
  68.     while(true){
  69.     //get controller info and prep for processing
  70.    
  71.     //initalize variables first
  72.     double y = 0;
  73.    
  74.     int x = fabs(axis3Position * coef);
  75.    
  76.     switch(x){
  77.         case 0 ... 60:
  78.             y = pow(1.04029, x * 1.284467);
  79.             break;
  80.         case 61 ... 80:
  81.             y = 0.75 * x -25;
  82.             break;
  83.         case 81 ... 108:
  84.             y = 1.0357 * x - 47.85714857;
  85.             break;
  86.         case 109 ... 127:
  87.             y = 63 + pow(1.232099, x - 108);
  88.             break;
  89.         default:
  90.             y = 128;
  91.             break;
  92.     }
  93.     y = y / coef;
  94.    
  95.     if(Controller1.Axis3.position() < 0){
  96.         y = -y;
  97.     }
  98.    
  99.     axis3Drive = -y;
  100.     wait(2.5, msec);
  101.     }
  102.     return 0;
  103. }
  104. int axis1calc(){
  105.     while(true){
  106.     //get controller info and prep for processing
  107.    
  108.     //initalize variables first
  109.     double y = 0;
  110.    
  111.     int x = fabs(axis3Position * coef);
  112.    
  113.     switch(x){
  114.         case 0 ... 60:
  115.             y = pow(1.04029, x * 1.284467);
  116.             break;
  117.         case 61 ... 80:
  118.             y = 0.75 * x -25;
  119.             break;
  120.         case 81 ... 108:
  121.             y = 1.0357 * x - 47.85714857;
  122.             break;
  123.         case 109 ... 127:
  124.             y = 63 + pow(1.232099, x - 108);
  125.             break;
  126.         default:
  127.             y = 128;
  128.             break;
  129.     }
  130.     y = y / coef;
  131.    
  132.     if(Controller1.Axis1.position() < 0){
  133.         y = -y;
  134.     }
  135.     axis1Drive = y;
  136.     wait(2.5, msec);
  137.     }
  138.     return 0;
  139. }
  140.  
  141. // define a task that will handle monitoring inputs from Controller1
  142. int rc_auto_loop_function_Controller1() {
  143.   // process the controller input every 20 milliseconds
  144.   // update the motors based on the input values
  145.   while(true) {
  146.  
  147.     axis3Position = Controller1.Axis3.position();
  148.     axis1Position = Controller1.Axis1.position();
  149.      
  150.     vex::task ws4(axis1calc);
  151.  
  152.     vex::task ws3(axis3calc);
  153.  
  154.     if(RemoteControlCodeEnabled) {
  155.       int drivetrainLeftSideSpeed = axis3Drive - axis1Drive;
  156.       int drivetrainRightSideSpeed = axis3Drive + axis1Drive;
  157.  
  158.       // check if the value is inside of the deadband range
  159.       if (drivetrainLeftSideSpeed < 5 && drivetrainLeftSideSpeed > -5) {
  160.         // check if the left motor has already been stopped
  161.         if (DrivetrainLNeedsToBeStopped_Controller1) {
  162.           // stop the left drive motor
  163.           LeftDriveSmart.stop();
  164.           // tell the code that the left motor has been stopped
  165.           DrivetrainLNeedsToBeStopped_Controller1 = false;
  166.         }
  167.       } else {
  168.         // reset the toggle so that the deadband code knows to stop the left motor nexttime the input is in the deadband range
  169.         DrivetrainLNeedsToBeStopped_Controller1 = true;
  170.       }
  171.       // check if the value is inside of the deadband range
  172.       if (drivetrainRightSideSpeed < 5 && drivetrainRightSideSpeed > -5) {
  173.         // check if the right motor has already been stopped
  174.         if (DrivetrainRNeedsToBeStopped_Controller1) {
  175.           // stop the right drive motor
  176.           RightDriveSmart.stop();
  177.           // tell the code that the right motor has been stopped
  178.           DrivetrainRNeedsToBeStopped_Controller1 = false;
  179.         }
  180.       } else {
  181.         // reset the toggle so that the deadband code knows to stop the right motor next time the input is in the deadband range
  182.         DrivetrainRNeedsToBeStopped_Controller1 = true;
  183.       }
  184.  
  185.       // only tell the left drive motor to spin if the values are not in the deadband range
  186.       if (DrivetrainLNeedsToBeStopped_Controller1) {
  187.         LeftDriveSmart.setVelocity(drivetrainLeftSideSpeed, percent);
  188.         LeftDriveSmart.spin(forward);
  189.       }
  190.       // only tell the right drive motor to spin if the values are not in the deadband range
  191.       if (DrivetrainRNeedsToBeStopped_Controller1) {
  192.         RightDriveSmart.setVelocity(drivetrainRightSideSpeed, percent);
  193.         RightDriveSmart.spin(forward);
  194.       }
  195.     }
  196.     // wait before repeating the process
  197.     wait(5, msec);
  198.   }
  199.   return 0;
  200. }
  201.  
  202. task rc_auto_loop_task_Controller1(rc_auto_loop_function_Controller1);
  203.  
  204. #pragma endregion VEXcode Generated Robot Configuration
  205. bool intakeInterlock = false;
  206. bool cataInterlock = false;
  207. int switchpoint = 0;
  208.  
  209.  
  210. int Interlock(){
  211.     while(true){
  212.         if(DigitalInD.value() == 0){
  213.             intakeInterlock = true;
  214.         } else {
  215.             intakeInterlock = false;
  216.         }
  217.         if(DigitalOutB.value() == 1){
  218.             cataInterlock = false;
  219.         } else {
  220.             cataInterlock = true;
  221.         }
  222.     wait(5, msec);
  223.     }
  224.     return 0;
  225. }
  226.  
  227. int Intake(){
  228.     bool intakeToggle = false;
  229.       while(true){
  230.         intake.setMaxTorque(100, percent);
  231.         intake.spin(forward);
  232.         if(Controller1.ButtonR1.pressing()){
  233.             intakeToggle = !intakeToggle;
  234.             wait(250, msec);
  235.         }
  236.         if(Distance1.objectDistance(inches) < 4.0){
  237.         intake.setStopping(hold);
  238.           intake.stop();
  239.           intake.setVelocity(0, percent);
  240.         } else {
  241.             intake.setVelocity(60, percent);  
  242.         }
  243.     wait(5, msec);
  244.     }
  245.     return 0;
  246. }
  247.  
  248. int cataFire(){
  249.   cata.setMaxTorque(100, percent);
  250.   cata.setVelocity(100, percent);
  251.   cata.setStopping(hold);
  252.   float i = 0;
  253.   while(true){
  254.     i = cata.position(degrees) - (floor(cata.position(turns)) * 360);
  255.     if(cataInterlock == false && !Controller1.ButtonR1.pressing()){
  256.       if(Distance1.objectDistance(inches) < 3.0 || Controller1.ButtonA.pressing()){
  257.       intake.setVelocity(0, percent);
  258.       cata.spin(forward);
  259.       wait(250, msec);
  260.       while(DigitalInD.value() == 1){
  261.         cata.spin(forward);
  262.       }
  263.       cata.stop();
  264.       } else {
  265.       cata.stop();
  266.       intake.setVelocity(60, percent);
  267.     }
  268.    }
  269.     wait(1, msec);
  270.   }
  271.   return 0;
  272. }
  273.  
  274. int wings(){
  275.   while(true){
  276.     if(Controller1.ButtonL1.pressing()){
  277.       DigitalOutH.set(true);
  278.     }else{
  279.       DigitalOutH.set(false);
  280.     }
  281.     wait(5, msec);
  282.     }
  283.   return 0;
  284. }
  285.  
  286. int encoder(){
  287.   int counter = 0;
  288.   int tempA = 0;
  289.   int tempB = 0;
  290.   int tempL1 = 0;
  291.   int tempR1 = 0;
  292.   int tempU = 0;
  293.   int tempD = 0;
  294.   int tempR = 0;
  295.   int tempL = 0;
  296.   int temp3 = 0;
  297.   int temp1 = 0;
  298.   while(true){
  299.     if(Controller1.ButtonA.pressing()){
  300.         tempA = 1;  
  301.     } else{
  302.         tempA = 0;
  303.     }
  304.     if(Controller1.ButtonB.pressing()){
  305.         tempB = 1;  
  306.     } else{
  307.         tempB = 0;
  308.     }
  309.     if(Controller1.ButtonL1.pressing()){
  310.         tempL1 = 1;  
  311.     } else{
  312.         tempL1 = 0;
  313.     }
  314.     if(Controller1.ButtonR1.pressing()){
  315.         tempR1 = 1;  
  316.     } else{
  317.         tempR1 = 0;
  318.     }
  319.     if(Controller1.ButtonUp.pressing()){
  320.         tempU = 1;  
  321.     } else{
  322.         tempU = 0;
  323.     }
  324.     if(Controller1.ButtonDown.pressing()){
  325.         tempD = 1;  
  326.     } else{
  327.         tempD = 0;
  328.     }
  329.     if(Controller1.ButtonRight.pressing()){
  330.         tempR = 1;  
  331.     } else{
  332.         tempR = 0;
  333.     }
  334.     if(Controller1.ButtonLeft.pressing()){
  335.         tempL = 1;  
  336.     } else{
  337.         tempL = 0;
  338.     }
  339.     temp3 = Controller1.Axis3.position();
  340.     temp1 = Controller1.Axis1.position();
  341.     printf("%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\n",tempA,tempB,tempL1,tempR1,tempU,tempD,tempR,tempL,temp3,temp1);
  342.     wait(5, msec);
  343.   }
  344. }
  345.  
  346. int onauton_autonomous(){
  347. //array setup
  348. float values[] = {};
  349.  
  350. //the y-int for the algebra
  351. int mathA = -10;
  352. int mathB = -9;
  353. int mathL1 = -8;
  354. int mathR1 = -7;
  355. int mathU = -6;
  356. int mathD = -5;
  357. int mathR = -4;
  358. int mathL = -3;
  359. int math3 = -2;
  360. int math1 = -1;
  361.  
  362. int outA = 0;
  363. int outB = 0;
  364. int outL1 = 0;
  365. int outR1 = 0;
  366. int outU = 0;
  367. int outD = 0;
  368. int outR = 0;
  369. int outL = 0;
  370. int out3 = 0;
  371. int out1 = 0;
  372.  
  373. bool buttonA = false;
  374. bool buttonB = false;
  375. bool buttonL1 = false;
  376. bool buttonR1 = false;
  377. bool buttonUp = false;
  378. bool buttonDown = false;
  379. bool buttonRight = false;
  380. bool buttonLeft = false;
  381.  
  382.   while (true) {
  383.     //regular code goes here
  384.     //make sure to replace Controller1.ButtonA.pressing() with “buttonA”…
  385.  
  386.     //math at the start before reading the values
  387.     mathA = mathA + 10;
  388.     mathB = mathB + 10;
  389.     mathL1 = mathL1 + 10;
  390.     mathR1 = mathR1 + 10;
  391.     mathU = mathU + 10;
  392.     mathD = mathD + 10;
  393.     mathR = mathR + 10;
  394.     mathL = mathL + 10;
  395.     math3 = math3 + 10;
  396.     math1 = math1 + 10;
  397.    
  398.     //read the values off of the array and set the values accordingly
  399.     outA = values[mathA];
  400.     outB = values[mathB];
  401.     outL1 = values[mathL1];
  402.     outR1 = values[mathR1];
  403.     outU =  values[mathU];
  404.     outD = values[mathD];
  405.     outR = values[mathR];
  406.     outL = values[mathL];
  407.     out3 = values[math3];
  408.     out1 = values[math1];
  409.    
  410.     //do stuff on the controller
  411.     if(outA == 1){
  412.         buttonA = true;
  413.     } else {
  414.         buttonA = false;
  415.     }
  416.     if(outB == 1){
  417.         buttonB = true;
  418.     } else {
  419.         buttonB = false;
  420.     }
  421.     if(outL1 == 1){
  422.         buttonL1 = true;
  423.     } else {
  424.         buttonL1 = false;
  425.     }
  426.     if(outR1 == 1){
  427.         buttonR1 = true;
  428.     } else {
  429.         buttonR1 = false;
  430.     }
  431.     if(outU == 1){
  432.         buttonUp = true;
  433.     } else {
  434.         buttonUp = false;
  435.     }
  436.     if(outD == 1){
  437.         buttonDown = true;
  438.     } else {
  439.         buttonDown = false;
  440.     }
  441.     if(outL == 1){
  442.         buttonLeft = true;
  443.     } else {
  444.         buttonLeft = false;
  445.     }
  446.     int axis3 = out3;
  447.     int axis1 = out1;
  448.    
  449.     axis1Position = axis1;
  450.     axis3Position = axis3;
  451.     vex::task ws3;
  452.     vex::task  
  453.     RightDriveSmart.setVelocity(axis3Drive - axis1Drive, percent);
  454.     LeftDriveSmart.setVelocity(axis3Drive + axis1Drive, percent);
  455.     RightDriveSmart.spin(forward);
  456.     LeftDriveSmart.spin(forward);
  457.  
  458.   axis3 = out3;
  459.   axis1 = out1;
  460.     cata.setStopping(hold);
  461.     cata.stop();
  462.  if(wingState == true){
  463.     set.DigitalOutD(true);
  464.  }
  465.  
  466.     wait(5, msec);
  467.   }
  468.  
  469.   return 0;
  470. }
  471. }
  472.  
  473. void whenStarted(){
  474.     DigitalOutB.set(true);
  475.   cata.setVelocity(40, percent);
  476.     while(DigitalInD.value() == 1){
  477.       cata.spin(forward);
  478.     }
  479.     cata.setStopping(hold);
  480.     cata.stop();
  481.     //cata.setVelocity(30, percent);
  482.     //cata.setMaxTorque(100, percent);
  483.     //while(DigitalInD.value() == 1){
  484.         //cata.spin(forward);
  485.     //}
  486.     //cata.setVelocity(0, percent);
  487.     //cata.setStopping(hold);
  488.     //cata.stop();
  489. }
  490.  
  491. int main() {
  492.   // post event registration
  493.  
  494.   // set default print color to black
  495.   printf("\033[30m");
  496.  
  497.   // wait for rotation sensor to fully initialize
  498.   wait(30, msec);
  499.  
  500.   //run the important things
  501.   whenStarted();
  502.   vex::task ws5(Interlock);
  503.   vex::task ws6(wings);
  504.   vex::task ws1(Intake);
  505.   vex::task ws2(cataFire);
  506.   vex::task ws7(encoder);
  507. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement