Advertisement
Trainlover08

Vex Transmission 0.2

Jun 2nd, 2023 (edited)
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. #include <iostream>
  2. #include "vex.h"
  3.  
  4. using namespace vex;
  5.  
  6. int Laxle_speed = 0;
  7. int Raxle_speed = 0;
  8. int Left_Bank_rpm = 0;
  9. int Right_Bank_rpm = 0;
  10. int Left_Bank_power = 0;
  11. int Right_Bank_power = 0;
  12. int gear = 1;
  13. bool PTOtoggle = false;
  14.  
  15. vex::solenoid Solenoid[] = {vex::solenoid(A), vex::solenoid(B), vex::solenoid(C), vex::solenoid(D), vex::solenoid(E), vex::solenoid(F)};
  16. bool solenoidStates[] = {false, false, false, false, false, false};
  17.  
  18. while(1) {
  19.     Brain.Screen.printAt( 5, 230, "frame %3d", gif.getFrameIndex() );
  20.     this_thread::sleep_for(10);
  21.  
  22. // Create a drive base with two motors
  23. motor_group driveBase = motor_group(motor(PORT1), motor(PORT2));
  24.  
  25.  
  26.  
  27. void Gear1() {
  28.   solenoidStates[0] = true;
  29.   solenoidStates[1] = true;
  30.   gear = 1;
  31. }
  32.  
  33. void Gear2() {
  34.   solenoidStates[1] = true;
  35.   solenoidStates[2] = true;
  36.   gear = 2;
  37. }
  38.  
  39. void Gear3() {
  40.   solenoidStates[2] = true;
  41.   solenoidStates[3] = true;
  42.   gear = 3;
  43. }
  44.  
  45. void Gear4() {
  46.   solenoidStates[3] = true;
  47.   solenoidStates[4] = true;
  48.   gear = 4;
  49. }
  50.  
  51. void Aux1() {
  52.   solenoidStates[4] = true;
  53.   solenoidStates[5] = true;
  54.   gear = -1;
  55. }
  56.  
  57. void Aux2() {
  58.   solenoidStates[5] = true;
  59.   gear = -2;
  60. }
  61.  
  62. void Aux3() {
  63.   for (int i = 0; i < 6; i++) {
  64.     solenoidStates[i] = false;
  65.   }
  66.   gear = -3;
  67. }
  68.  
  69. void UpShift() {
  70.   if (PTOtoggle == false) {
  71.     if (gear == 1) {
  72.       Gear2();
  73.     } else if (gear == 2) {
  74.       Gear3();
  75.     } else if (gear == 3) {
  76.       Gear4();
  77.     }
  78.   } else {
  79.     if (gear == -3) {
  80.       Aux2();
  81.     } else if (gear == -2) {
  82.       Aux1();
  83.     }
  84.   }
  85. }
  86.  
  87. void DownShift() {
  88.   if (PTOtoggle == false) {
  89.     if (gear == 2) {
  90.       Gear1();
  91.     } else if (gear == 3) {
  92.       Gear2();
  93.     } else if (gear == 4) {
  94.       Gear3();
  95.     }
  96.   } else {
  97.     if (gear == -2) {
  98.       Aux3();
  99.     } else if (gear == -1) {
  100.       Aux2();
  101.     }
  102.   }
  103. }
  104.  
  105. int main() {
  106.   // Initializing the VEX Brain
  107.   vex::brain Brain;
  108.  
  109.   while (true) {
  110.     if (Brain.Controller1.ButtonR1.pressing()) {
  111.       UpShift();
  112.     }
  113.  
  114.     if (Brain.Controller1.ButtonR2.pressing()) {
  115.       DownShift();
  116.     }
  117.  
  118.     if (Brain.Controller1.ButtonB.pressing()) {
  119.       PTOtoggle = !PTOtoggle;
  120.     }
  121.  
  122.     // Configure the controller
  123.     controller Controller = controller(primary);
  124.      vex::Gif gif("world.gif", 120, 0 );
  125.    
  126.       LeftMotor.setStopping(vex::brakeType::hold);
  127.       RightMotor.setStopping(vex::brakeType::hold);
  128.  
  129.         // Get joystick positions
  130.         int RightStickY = Controller.Axis2.position(percentUnits::pct);
  131.         int RightStickX = Controller.Axis1.position(percentUnits::pct);
  132.  
  133.         // Calculate motor velocities based on joystick inputs
  134.         double y = 0;
  135.         double x = RightStickX;
  136.  
  137.         if (x < 0) {
  138.             x = -x;
  139.         }
  140.  
  141.         if (0 <= x && x < 60) {
  142.             y = pow(1.04029, x * 1.284467);
  143.         } else if (60 <= x && x < 80) {
  144.             y = 0.75 * x - 25;
  145.         } else if (80 <= x && x <= 108) {
  146.             y = 1.0357 * x - 47.85714857;
  147.         } else if (x > 108) {
  148.             y = 63 + pow(1.232099, x - 108);
  149.         } else if (x >= 128) {
  150.             y = 128;
  151.         }
  152.  
  153.         if (RightStickX < 0) {
  154.             y = -y;
  155.         }
  156.  
  157.         double steerX = RightStickX;
  158.         double rightWheels = y - steerX;
  159.         double leftWheels = y + steerX;
  160.  
  161.         // Set the motor velocities
  162.         driveBase.setVelocity(rightWheels, percentUnits::pct);
  163.         driveBase.setVelocity(leftWheels, percentUnits::pct);
  164.  
  165.         // Run the motors
  166.         driveBase.spin(forward);
  167.  
  168.         task::sleep(20);
  169.     }
  170.   }
  171. }
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement