Advertisement
Trainlover08

Untitled

Feb 5th, 2024 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.64 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. controller Controller1 = controller(primary);
  31. digital_out gear1 = digital_out(Brain.ThreeWirePort.A);
  32. digital_out gear2 = digital_out(Brain.ThreeWirePort.B);
  33. motor rightAMotorA = motor(PORT1, ratio18_1, false);
  34. motor rightAMotorB = motor(PORT4, ratio18_1, true);
  35. motor_group rightA = motor_group(rightAMotorA, rightAMotorB);
  36.  
  37. motor rightBMotorA = motor(PORT2, ratio18_1, false);
  38. motor rightBMotorB = motor(PORT11, ratio18_1, true);
  39. motor_group rightB = motor_group(rightBMotorA, rightBMotorB);
  40.  
  41. motor leftAMotorA = motor(PORT10, ratio18_1, false);
  42. motor leftAMotorB = motor(PORT7, ratio18_1, true);
  43. motor_group leftA = motor_group(leftAMotorA, leftAMotorB);
  44.  
  45. motor leftBMotorA = motor(PORT8, ratio18_1, false);
  46. motor leftBMotorB = motor(PORT12, ratio18_1, true);
  47. motor_group leftB = motor_group(leftBMotorA, leftBMotorB);
  48.  
  49.  
  50.  
  51.  
  52. // Helper to make playing sounds from the V5 in VEXcode easier and
  53. // keeps the code cleaner by making it clear what is happening.
  54. void playVexcodeSound(const char *soundName) {
  55.   printf("VEXPlaySound:%s\n", soundName);
  56.   wait(5, msec);
  57. }
  58.  
  59.  
  60.  
  61. // define variable for remote controller enable/disable
  62. bool RemoteControlCodeEnabled = true;
  63.  
  64. #pragma endregion VEXcode Generated Robot Configuration
  65. //Define the many global variables
  66.  
  67. //variable for setting curve values to desired axis
  68. int SelectorReadOut = 0;
  69.  
  70. //universal output for the drive function
  71. float driveOutput = 0.0;
  72.  
  73. //universal values for each drive factor
  74. float axis1Output = 0.0;
  75. float axis3Output = 0.0;
  76.  
  77. //universal Boolean for locking the catapult
  78. bool cataLock = false;
  79.  
  80. //coefficient for output rpm
  81. float rpmCoef = 0.0;
  82.  
  83. //global values for at output rpm for both
  84. float leftRPM = 0.0;
  85. float rightRPM = 0.0;
  86.  
  87. //torque for both sides
  88. float leftTorque = 0.0;
  89. float rightTorque = 0.0;
  90.  
  91. //to lock the shifter when the catapult is going to fire
  92. bool CataFiring = false;
  93.  
  94. //a gear value to make the gears work
  95. int gear = 0;
  96.  
  97. //Motor Curve function
  98. void motorCurve(){
  99.     //get controller info and prep for processing.
  100.    
  101.     //initialize variables first
  102.     double y = 0;
  103.     float coef = 1.28;
  104.    
  105.     int x = fabs(SelectorReadOut * coef);
  106.    
  107.     switch(x){
  108.         case 0 ... 60:
  109.             y = pow(1.04029, x * 1.284467);
  110.             break;
  111.         case 61 ... 80:
  112.             y = 0.75 * x -25;
  113.             break;
  114.         case 81 ... 108:
  115.             y = 1.0357 * x - 47.85714857;
  116.             break;
  117.         case 109 ... 127:
  118.             y = 63 + pow(1.232099, x - 108);          
  119.             break;
  120.         default:
  121.             y = 128;
  122.             break;
  123.     }
  124.     y = y / coef;
  125.    
  126.     if(SelectorReadOut < 0){
  127.         y = -y;
  128.     }
  129.    
  130.     driveOutput = y;
  131.     wait(2.5, msec);
  132.    
  133.  
  134. }
  135.  
  136. //When Started function to initialize everything and set us up for the match
  137. void whenStarted(){
  138.     //intakePneumatics.set(true);
  139.     rightA.setMaxTorque(100, percent);
  140.     rightB.setMaxTorque(100, percent);
  141.     leftA.setMaxTorque(100, percent);
  142.     leftB.setMaxTorque(100, percent);
  143.  
  144.     rightA.setStopping(hold);
  145.     rightB.setStopping(hold);
  146.     leftA.setStopping(hold);
  147.     leftB.setStopping(hold);
  148.  
  149. }
  150.  
  151. //Motor Curve thread
  152. int MotorCurve(){
  153.     while(true){
  154.         //get and set the appropriate values for curves function
  155. SelectorReadOut = Controller1.Axis3.position();
  156. motorCurve();
  157. axis3Output = driveOutput;
  158. SelectorReadOut = Controller1.Axis1.position();
  159. motorCurve();
  160. axis1Output = driveOutput;
  161.  
  162. //set deadband for controller
  163. if(Controller1.Axis3.position() < 5 && Controller1.Axis3.position() > -5){
  164.     axis3Output = 0.0;
  165. }
  166. if(Controller1.Axis1.position() < 5 && Controller1.Axis1.position() > -5){
  167.     axis1Output = 0.0;
  168. }
  169. //wait so that the processor doesn’t get overloaded
  170. wait(1.0, msec);
  171. }
  172. return 0;
  173. }
  174.  
  175. //DriveTrain thread
  176. int DriveTrain(){
  177.     while(true){
  178.         //correct the way the drive is spinning
  179.         if(gear1.value() == 1){
  180.             axis3Output = 0 - axis3Output;
  181.         }
  182.         //psuedo if statement for drivetrain vs cata
  183.  
  184.         //sets the drive based on motor curve outputs
  185.         float leftDrive = axis3Output - axis1Output;
  186.         float rightDrive = axis3Output + axis1Output;
  187.  
  188.         leftA.setVelocity(leftDrive, percent);
  189.         leftB.setVelocity(leftDrive, percent);
  190.         rightA.setVelocity(rightDrive, percent);
  191.         rightB.setVelocity(rightDrive, percent);
  192.  
  193.      
  194.         //wait so that the processor doesn’t get overloaded
  195.         wait(2.5, msec);
  196.   }
  197.   return 0;
  198. }
  199.  
  200. float DriveRPM = 0.0;
  201. float DriveTorque = 0.0;
  202.  
  203. //a thread to do all the miscellaneous math and statistics
  204. int Stats(){
  205.     while(true){
  206.         if(gear1.value() == 1){
  207.             rpmCoef = 1.0;
  208. }
  209. if(gear2.value() == 1){
  210.     rpmCoef = 3.0;
  211. }
  212. if(gear2.value() == 0 && gear1.value() == 0){
  213.     rpmCoef = 0.0;
  214. }
  215. leftRPM = fabs(((leftA.velocity(rpm) + leftB.velocity(rpm)) / 2) * rpmCoef);
  216. rightRPM = fabs(((rightA.velocity(rpm) + rightB.velocity(rpm)) / 2) * rpmCoef);
  217. leftTorque = leftA.torque(Nm) + leftB.torque(Nm);
  218. rightTorque = rightA.torque(Nm) + rightB.torque(Nm);
  219. DriveRPM = (fabs(leftRPM) + fabs(rightRPM))/2;
  220. DriveTorque = (fabs(leftTorque) + fabs(rightTorque)) / 2;
  221. }
  222.     return 0;
  223. }
  224.  
  225. //thread to work the manual shifting
  226.  
  227. int Shifting(){
  228.     while(true){
  229.         if(Controller1.ButtonR1.pressing()){
  230.             gear++;
  231.             wait(250, msec);
  232.         }
  233.         if(Controller1.ButtonL1.pressing()){
  234.             gear--;
  235.             wait(250, msec);
  236.         }
  237.         switch(gear){
  238.             case -1:
  239.                 gear = 0;
  240.                 break;
  241.             case 3:
  242.                 gear = 2;
  243.                 break;
  244.             case 2:
  245.                 gear1.set(false);
  246.                 gear2.set(true);
  247.                 break;
  248.             case 1:
  249.                 gear1.set(true);
  250.                 gear2.set(false);
  251.                 break;
  252.             default:
  253.                 gear1.set(false);
  254.                 gear2.set(false);
  255.                 break;
  256.                
  257.             wait(5.0, msec);
  258.         }
  259.     }
  260.     return 0;
  261. }
  262.  
  263. //UI thread
  264. int UI(){
  265.    
  266.     while(true){
  267.         Controller1.Screen.clearScreen();
  268.         Controller1.Screen.setCursor(1, 1);
  269.         Controller1.Screen.print(DriveRPM);
  270.         Controller1.Screen.print("RPM");
  271.         Controller1.Screen.newLine();
  272.         Controller1.Screen.print(DriveTorque);
  273.         Controller1.Screen.print("NM");
  274.         Controller1.Screen.newLine();
  275.         Controller1.Screen.print("M");
  276.         Controller1.Screen.print(gear);
  277.         Controller1.Screen.setCursor(3, 12);
  278.         Controller1.Screen.print(Brain.Battery.capacity(percent));
  279.         Controller1.Screen.print("%");
  280.         if(DriveRPM > 192 && DriveRPM < 205){
  281.           Controller1.rumble(rumbleShort);
  282.         }
  283.         wait(10, msec);
  284.     }
  285.     return 0;
  286. }
  287.  
  288. //call all functions
  289. int main(){
  290.     //initialize the print color for the console
  291.     printf("\033[30m");
  292.  
  293.     //call threads
  294.     whenStarted();
  295.    
  296.     vex::task ws1(MotorCurve);
  297.     vex::task ws2(DriveTrain);
  298.     vex::task ws5(Stats);
  299.     vex::task ws6(Shifting);
  300.     vex::task ws8(UI);
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement