Advertisement
Trainlover08

3.1 (V2 Drive)

Oct 31st, 2023 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.54 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. // Brain should be defined by default
  17. brain Brain;
  18.  
  19.  
  20. // START V5 MACROS
  21. #define waitUntil(condition)                                                   \
  22.   do {                                                                         \
  23.     wait(5, msec);                                                             \
  24.   } while (!(condition))
  25.  
  26. #define repeat(iterations)                                                     \
  27.   for (int iterator = 0; iterator < iterations; iterator++)
  28. // END V5 MACROS
  29.  
  30.  
  31. // Robot configuration code.
  32. controller Controller1 = controller(primary);
  33. distance Distance1 = distance(PORT7);
  34. motor leftMotorA = motor(PORT4, ratio6_1, true);
  35. motor leftMotorB = motor(PORT8, ratio6_1, true);
  36. motor_group LeftDriveSmart = motor_group(leftMotorA, leftMotorB);
  37. motor rightMotorA = motor(PORT2, ratio6_1, false);
  38. motor rightMotorB = motor(PORT7, ratio6_1, false);
  39. motor_group RightDriveSmart = motor_group(rightMotorA, rightMotorB);
  40. drivetrain Drivetrain = drivetrain(LeftDriveSmart, RightDriveSmart, 219.44, 295, 40, mm, 1);
  41.  
  42. motor cataMotorA = motor(PORT1, ratio36_1, false);
  43. motor cataMotorB = motor(PORT10, ratio36_1, true);
  44. motor_group cata = motor_group(cataMotorA, cataMotorB);
  45.  
  46. //Should say that port 13 is unplugged, this is by design cause im lazy
  47. motor MotorGroup7MotorA = motor(PORT12, ratio36_1, true);
  48. motor MotorGroup7MotorB = motor(PORT13, ratio36_1, false);
  49. motor_group MotorGroup7 = motor_group(MotorGroup7MotorA, MotorGroup7MotorB);
  50.  
  51. digital_out DigitalOutB = digital_out(Brain.ThreeWirePort.B);
  52. digital_out DigitalOutD = digital_out(Brain.ThreeWirePort.D);
  53.  
  54.  
  55.  
  56. // define variable for remote controller enable/disable
  57. bool RemoteControlCodeEnabled = true;
  58. // define variables used for controlling motors based on controller inputs
  59. bool DrivetrainLNeedsToBeStopped_Controller1 = true;
  60. bool DrivetrainRNeedsToBeStopped_Controller1 = true;
  61.  
  62. // define a task that will handle monitoring inputs from Controller1
  63. int rc_auto_loop_function_Controller1() {
  64.   // process the controller input every 20 milliseconds
  65.   // update the motors based on the input values
  66.   while(true) {
  67.     if(RemoteControlCodeEnabled) {
  68.  
  69.       // calculate the drivetrain motor velocities from the controller joystick axies
  70.       // left = Axis3 + Axis1
  71.       // right = Axis3 - Axis1
  72.       double y = 0;
  73. double x = Controller1.Axis3.position();
  74.  
  75. x = x * 1.28;
  76.  
  77. x = fabs(x);
  78.  
  79. if(0<=x&&x<60){
  80.     y = pow(1.04029, x * 1.284467);
  81. }else if(60<=x&&x<80){
  82.     y = 0.75 * x -25;
  83. }else if(80<=x&&x<=108){
  84.     y = 1.0357 * x - 47.85714857;
  85. }else if(x > 108){
  86.     y = 63 + pow(1.232099, x - 108);
  87. }
  88.  
  89. y = y/1.28;
  90.                  
  91. if(Controller1.Axis3.position() < 0){
  92.     y = -y;
  93. }
  94. double sy = 0;
  95. double sx = Controller1.Axis1.position();
  96.  
  97. sx = sx * 1.28;
  98.  
  99. sx = fabs(sx);
  100.  
  101. if(0<=sx&&sx<60){
  102.     sy = pow(1.04029, sx * 1.284467);
  103. }else if(60<=sx&&sx<80){
  104.     sy = 0.75 * sx -25;
  105. }else if(80<=sx&&sx<=108){
  106.     sy = 1.0357 * sx - 47.85714857;
  107. }else if(sx > 108){
  108.     sy = 63 + pow(1.232099, sx - 108);
  109. }
  110.  
  111. sy = sy/1.28;
  112.                  
  113. if(Controller1.Axis1.position() < 0){
  114.     sy = -sy;
  115. }
  116.  
  117.       int drivetrainLeftSideSpeed = y + sy;
  118.       int drivetrainRightSideSpeed = y - sy;
  119.  
  120.       // check if the value is inside of the deadband range
  121.       if (drivetrainLeftSideSpeed < 5 && drivetrainLeftSideSpeed > -5) {
  122.         // check if the left motor has already been stopped
  123.         if (DrivetrainLNeedsToBeStopped_Controller1) {
  124.           // stop the left drive motor
  125.           LeftDriveSmart.stop();
  126.           // tell the code that the left motor has been stopped
  127.           DrivetrainLNeedsToBeStopped_Controller1 = false;
  128.         }
  129.       } else {
  130.         // reset the toggle so that the deadband code knows to stop the left motor nexttime the input is in the deadband range
  131.         DrivetrainLNeedsToBeStopped_Controller1 = true;
  132.       }
  133.       // check if the value is inside of the deadband range
  134.       if (drivetrainRightSideSpeed < 5 && drivetrainRightSideSpeed > -5) {
  135.         // check if the right motor has already been stopped
  136.         if (DrivetrainRNeedsToBeStopped_Controller1) {
  137.           // stop the right drive motor
  138.           RightDriveSmart.stop();
  139.           // tell the code that the right motor has been stopped
  140.           DrivetrainRNeedsToBeStopped_Controller1 = false;
  141.         }
  142.       } else {
  143.         // reset the toggle so that the deadband code knows to stop the right motor next time the input is in the deadband range
  144.         DrivetrainRNeedsToBeStopped_Controller1 = true;
  145.       }
  146.  
  147.       // only tell the left drive motor to spin if the values are not in the deadband range
  148.       if (DrivetrainLNeedsToBeStopped_Controller1) {
  149.         LeftDriveSmart.setVelocity(drivetrainLeftSideSpeed, percent);
  150.         LeftDriveSmart.spin(forward);
  151.       }
  152.       // only tell the right drive motor to spin if the values are not in the deadband range
  153.       if (DrivetrainRNeedsToBeStopped_Controller1) {
  154.         RightDriveSmart.setVelocity(drivetrainRightSideSpeed, percent);
  155.         RightDriveSmart.spin(forward);
  156.       }
  157.     }
  158.     // wait before repeating the process
  159.     wait(20, msec);
  160.   }
  161.   return 0;
  162. }
  163.  
  164.  
  165. task rc_auto_loop_task_Controller1(rc_auto_loop_function_Controller1);
  166.  
  167. #pragma endregion VEXcode Generated Robot Configuration
  168.  
  169.  
  170. // Include the V5 Library
  171. #include "vex.h"
  172.  
  173. // Allows for easier use of the VEX Library
  174. using namespace vex;
  175.  
  176. // "when started" hat block
  177. int whenStarted1() {
  178.   Drivetrain.setDriveVelocity(600.0, rpm);
  179.   Drivetrain.setTurnVelocity(100.0, percent);
  180.   MotorGroup7.setMaxTorque(100, percent);
  181.   MotorGroup7.setVelocity(100, percent);
  182.   cata.setMaxTorque(100, percent);
  183.   cata.setVelocity(100, percent);
  184.   bool wingState = false;
  185.   bool digitalB1 = false;
  186.   bool digitalB2 = false;
  187.   bool pl = false;
  188.   bool ytoggle = false;
  189.   float catapos = 0;
  190.   int turng = 0;
  191.   bool matchload = false;
  192.   int i = 0;
  193.   int c = 0;
  194.   while (true) {
  195.     cata.setStopping(hold);
  196.     cata.stop();
  197.     if(Controller1.ButtonR1.pressing()){
  198.       if(Controller1.Axis3.position() < -15.0){
  199.         MotorGroup7.spin(reverse);
  200.       } else {
  201.         MotorGroup7.spin(forward);
  202.       }
  203.     } else {
  204.       MotorGroup7.spin(forward);
  205.     }
  206.     turng = floor(cata.position(turns));
  207.     if(Controller1.ButtonDown.pressing()){
  208.       cata.spin(reverse);
  209.     }
  210.     if(Controller1.ButtonUp.pressing()){
  211.       cata.spin(forward);
  212.     }
  213.     if(catapos <= 60 || catapos >= 330){
  214.       digitalB2 = false;
  215.     } else {
  216.       digitalB2 = true;
  217.     }
  218.     if(Controller1.ButtonL1.pressing()){
  219.       wingState = !wingState;
  220.     }
  221.     if (Controller1.ButtonL1.pressing()){
  222.       DigitalOutD.set(true);
  223.       wait(150, msec);
  224.     } else{
  225.       DigitalOutD.set(false);
  226.       wait(150, msec);
  227.     }
  228.     if(Controller1.ButtonLeft.pressing()){
  229.       pl = !pl;
  230.       wait(150, msec);
  231.     }
  232.     catapos = cata.position(degrees) - (turng * 360);
  233.     //if(pl == true){
  234.       //DigitalOutB.set(true);
  235.     //} else if((digitalB1 == true || digitalB2 == true)||(digitalB1 == true & digitalB2 == true)){
  236.         //DigitalOutB.set(true);
  237.       //} else {
  238.         //DigitalOutB.set(false);
  239.       //}
  240.      if(Controller1.ButtonY.pressing()){
  241.          ytoggle = !ytoggle;
  242.      }
  243.      if(ytoggle == true){
  244.        //DigitalOutB.set(true);
  245.      }else{
  246.        //DigitalOutB.set(false);
  247.      }
  248.  
  249.   if(Controller1.ButtonA.pressing()){
  250.       matchload = !matchload;
  251.       wait(350, msec);
  252.       }
  253.   if(matchload == true){
  254.     DigitalOutB.set(true);
  255.     MotorGroup7.stop();
  256.     printf("%.1f\n", (Distance1.objectDistance(inches)));
  257.     if(i == 0){
  258.       cata.spinFor(forward, 355, degrees);
  259.       c = c + 355;
  260.       ++i;
  261.     }
  262.     if(Distance1.objectDistance(inches) < 2.5){
  263.       wait(25, msec);
  264.       cata.spinFor(forward, 30, degrees);
  265.       c = c + 30;
  266.       if(Distance1.objectDistance(inches) > 2.5){
  267.           c = c + 330;
  268.           cata.setposition(c, degrees);
  269.         }
  270.     }
  271.   } else{
  272.     DigitalOutB.set(false);
  273.     MotorGroup7.setVelocity(100, percent);
  274.     if(i == 1){
  275.       cata.spinFor(reverse, 355, degrees);
  276.       c = c - 355;
  277.     }
  278.     i = 0;
  279.   }
  280.  
  281.   wait(5, msec);
  282.   }
  283.   return 0;
  284. }
  285.  
  286. int onauton_autonomous_0() {
  287.   MotorGroup7.setvelocity(100, percent);
  288.   MotorGroup7.spin(forward);
  289.   Drivetrain.forward();
  290.   wait(500, msec);
  291.   Drivetrain.reverse();
  292.   wait(75, msec);
  293.   Drivetrain.stop();
  294.   return 0;
  295. }
  296.  
  297. int ondriver_drivercontrol_0() {
  298.   whenStarted1();
  299.   return 0;
  300. }
  301.  
  302. void VEXcode_driver_task() {
  303.   // Start the driver control tasks....
  304.   vex::task drive0(ondriver_drivercontrol_0);
  305.   while(Competition.isDriverControl() && Competition.isEnabled()) {this_thread::sleep_for(10);}
  306.   drive0.stop();
  307.   return;
  308. }
  309.  
  310. void VEXcode_auton_task() {
  311.   // Start the auton control tasks....
  312.   vex::task auto0(onauton_autonomous_0);
  313.   while(Competition.isAutonomous() && Competition.isEnabled()) {this_thread::sleep_for(10);}
  314.   auto0.stop();
  315.   return;
  316. }
  317.  
  318.  
  319. int main() {
  320.   // post event registration
  321.  
  322.   // set default print color to black
  323.   printf("\033[30m");
  324.  
  325.   vex::competition::bStopTasksBetweenModes = false;
  326.   Competition.autonomous(VEXcode_auton_task);
  327.   Competition.drivercontrol(VEXcode_driver_task);
  328.  
  329.   // wait for rotation sensor to fully initialize
  330.   wait(30, msec);
  331.  
  332.  
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement