Advertisement
Mikkel_Serrantes

Updated Version of Code

Jan 1st, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 24.94 KB | None | 0 0
  1. /*----------------------------------------------------------------------------*/
  2. /*                                                                            */
  3. /*    Module:       main.cpp                                                  */
  4. /*    Author:       VEX                                                       */
  5. /*    Created:      Thu Sep 26 2019                                           */
  6. /*    Description:  Competition Template                                      */
  7. /*                                                                            */
  8. /*----------------------------------------------------------------------------*/
  9.  
  10. // ---- START VEXCODE CONFIGURED DEVICES ----
  11. // Robot Configuration:
  12. // [Name]               [Type]        [Port(s)]
  13. // LeftMotorF           motor         1              
  14. // LeftMotorB           motor         17              
  15. // RightMotorB          motor         12              
  16. // RightMotorF          motor         19              
  17. // Scooper2             motor         2              
  18. // Scooper              motor         6              
  19. // Lift                 motor         8              
  20. // Arm2000              motor         16              
  21. // Controller1          controller                    
  22. // Controller2          controller                    
  23. // P1                   pot           D              
  24. // P2                   pot           G              
  25. // Inertial11           inertial      11              
  26. // LeftEncoder          encoder       A, B            
  27. // RightEncoder         encoder       E, F            
  28. // ---- END VEXCODE CONFIGURED DEVICES ----
  29.  
  30. #include "vex.h"
  31.  
  32.  
  33. using namespace vex;
  34.  
  35.  
  36. int count =0;
  37. int target = 0;
  38. bool driveMode = false;
  39. int PreviousErrorLeft =0;
  40. int PreviousErrorRight =0;
  41. int PreviousErrorArm=0;
  42.  
  43. ////This is problem
  44.  
  45.  
  46.  
  47.  
  48.  
  49. // A global instance of competition
  50. competition Competition;
  51.  
  52. // define your global instances of motors and other devices here
  53.  
  54. /*---------------------------------------------------------------------------*/
  55. /*                          Pre-Autonomous Functions                         */
  56. /*                                                                           */
  57. /*  You may want to perform some actions before the competition starts.      */
  58. /*  Do them in the following function.  You must return from this function   */
  59. /*  or the autonomous and usercontrol tasks will not be started.  This       */
  60. /*  function is only called once after the V5 has been powered on and        */
  61. /*  not every time that the robot is disabled.                               */
  62. /*---------------------------------------------------------------------------*/
  63.  
  64. void pre_auton(void) {
  65.   // Initializing Robot Configuration. DO NOT REMOVE!
  66.   vexcodeInit();
  67.  
  68.   // All activities that occur before the competition starts
  69.   // Example: clearing encoders, setting servo positions, ...
  70. }
  71.  
  72.  
  73. /*---------------------------------------------------------------------------*/
  74. /*                                                                           */
  75. /*                              Autonomous Task                              */
  76. /*                                                                           */
  77. /*  This task is used to control your robot during the autonomous phase of   */
  78. /*  a VEX Competition.                                                       */
  79. /*                                                                           */
  80. /*  You must modify the code to add your own robot specific commands here.   */
  81. /*---------------------------------------------------------------------------*/
  82. void RightMotorFB(double speed) {
  83.   RightMotorF.spin(directionType::fwd,speed,velocityUnits::rpm);
  84.   RightMotorB.spin(directionType::fwd,speed,velocityUnits::rpm);
  85. }
  86.  
  87. void LeftMotorFB(double speed) {
  88.   LeftMotorF.spin(directionType::fwd,speed,velocityUnits::rpm);
  89.   LeftMotorB.spin(directionType::fwd,speed,velocityUnits::rpm);
  90. }
  91.  
  92. void slop(int distance) {
  93.   if(distance <0) {
  94.     LeftMotorFB(-40);
  95.     vex::task::sleep(35);
  96.   }
  97. }
  98.  
  99. const int accel_step =9; ////9
  100. const int deccel_step =100; ///100
  101. static int leftSpeed =0;
  102. static int rightSpeed =0;
  103.  
  104. void LeftMotorFBS(int leftTarget){
  105.     if (leftTarget > 0)
  106.  
  107.         if(abs(leftSpeed) < abs(leftTarget))
  108.             if (abs(leftTarget) > (abs(leftSpeed) + accel_step))
  109.                 leftSpeed += accel_step;
  110.             else
  111.                 leftSpeed = leftTarget;
  112.         else
  113.             if (abs(leftTarget) < (abs(leftSpeed) - deccel_step))
  114.                 leftSpeed -= deccel_step;
  115.             else
  116.                 leftSpeed = leftTarget;
  117.  
  118.     else if (leftTarget < 0)
  119.  
  120.         if(abs(leftSpeed) < abs(leftTarget))
  121.             if (abs(leftTarget) > (abs(leftSpeed) - accel_step))
  122.                 leftSpeed -= accel_step;
  123.             else
  124.                 leftSpeed = leftTarget;
  125.         else
  126.             if (abs(leftTarget) < (abs(leftSpeed) + deccel_step))
  127.                 leftSpeed += deccel_step;
  128.             else
  129.                 leftSpeed = leftTarget;
  130.    
  131.     else leftSpeed = leftTarget;
  132.    
  133.   LeftMotorFB(leftSpeed);
  134. }
  135.  
  136. void RightMotorFBS(int rightTarget){
  137.     if (rightTarget > 0)
  138.  
  139.         if(abs(rightSpeed) < abs(rightTarget))
  140.             if (abs(rightTarget) > (abs(rightSpeed) + accel_step))
  141.                 rightSpeed += accel_step;
  142.             else
  143.                 rightSpeed = rightTarget;
  144.         else
  145.             if (abs(rightTarget) < (abs(rightSpeed) - deccel_step))
  146.                 rightSpeed -= deccel_step;
  147.             else
  148.                 rightSpeed = rightTarget;
  149.  
  150.     else if (rightTarget < 0)
  151.  
  152.         if(abs(rightSpeed) < abs(rightTarget))
  153.             if (abs(rightTarget) > (abs(rightSpeed) - accel_step))
  154.                 rightSpeed -= accel_step;
  155.             else
  156.                 rightSpeed = rightTarget;
  157.         else
  158.             if (abs(rightTarget) < (abs(rightSpeed) + deccel_step))
  159.                 rightSpeed += deccel_step;
  160.             else
  161.                 rightSpeed = rightTarget;
  162.    
  163.     else rightSpeed = rightTarget;
  164.    
  165.   RightMotorFB(rightSpeed);
  166. }
  167.  
  168.  
  169.  
  170. int drivePos() {
  171. return (LeftMotorF.rotation(rotationUnits::deg) + RightMotorF.rotation(rotationUnits::deg))/2;
  172. }
  173. bool isDriving() {
  174.     return(LeftMotorF.isSpinning() || LeftMotorB.isSpinning() || RightMotorF.isSpinning() || RightMotorB.isSpinning());
  175. }
  176.  
  177. void PidBasicAsync(int distance) {
  178.     slop(distance);
  179.     count = 0;
  180.     target = distance;
  181.     driveMode = true;
  182.     LeftMotorF.resetRotation();
  183.     RightMotorF.resetRotation();
  184.     LeftMotorB.resetRotation();
  185.     RightMotorB.resetRotation();
  186.     PreviousErrorLeft = 0;
  187.     PreviousErrorRight = 0;
  188.     LeftMotorFBS(distance);
  189.     RightMotorFBS(distance);
  190.  
  191. }
  192.  
  193. void BigArm (double degrees) {
  194.     double LiftPos= P2.value(rotationUnits::deg);
  195.     double kp = 2.20;
  196.  
  197.     while (LiftPos >= degrees) {
  198.         LiftPos= P2.value(rotationUnits::deg);  
  199.         int power = (LiftPos - degrees) * kp + 10;
  200.         Arm2000.spin(directionType::rev, power, velocityUnits::pct);
  201.         Brain.Screen.printAt(2, 40, "LiftPos: %f", LiftPos);
  202.          wait(0.01, seconds);
  203.     }
  204.  
  205.     Arm2000.stop(brakeType::brake);
  206. }
  207.  
  208.     void PidBasic (int distance) {
  209.     if (driveMode) {
  210.    
  211.     double Kp = 0.16192;///0.2 or .215 .162
  212.     double Kd = .4995;//465 or .52  or .45
  213.    
  214.     int Leftdegrees=LeftMotorF.rotation(rotationUnits::deg);
  215.     int Rightdegrees=RightMotorF.rotation(rotationUnits::deg);
  216.  
  217.     int ErrorLeft = distance - Leftdegrees;
  218.     int ErrorRight = distance - Rightdegrees;
  219.  
  220.     Leftdegrees=LeftMotorF.rotation(rotationUnits::deg);
  221.     Rightdegrees=RightMotorF.rotation(rotationUnits::deg);
  222.  
  223.     ErrorLeft = distance - Leftdegrees;
  224.     ErrorRight = distance - Rightdegrees;
  225.        
  226.     int LeftsideProportionalError = Kp * ErrorLeft;
  227.     int RightsideProportionalError = Kp * ErrorRight;
  228.  
  229.     double LeftsidedDerivativeError = Kd * (ErrorLeft - PreviousErrorLeft);
  230.     double RightsidedDerivativeError = Kd * (ErrorRight - PreviousErrorRight);
  231.        
  232.     LeftMotorFB(LeftsideProportionalError + LeftsidedDerivativeError);
  233.     RightMotorFB(RightsideProportionalError + RightsidedDerivativeError);
  234.        
  235.     PreviousErrorLeft = ErrorLeft;
  236.     PreviousErrorRight = ErrorRight;
  237.        
  238.         /*
  239.         if (abs(ErrorLeft) < 6 && abs(ErrorRight) < 6) {
  240.             break;    
  241.         }
  242.         */
  243.        
  244.         if (!isDriving()) {
  245.             count++;
  246.         }
  247.         if (count > 30) {
  248.             driveMode = false;
  249.         }
  250.        
  251.        
  252.         vex::task::sleep(30);
  253.     }
  254. }
  255.  
  256.  
  257. void Turning (int distance) {
  258.     LeftMotorF.resetRotation();
  259.     RightMotorF.resetRotation();
  260.       LeftMotorB.resetRotation();
  261.     RightMotorB.resetRotation();
  262.    
  263.             LeftMotorF.setStopping(brakeType::brake);
  264.     RightMotorF.setStopping(brakeType::brake);
  265.     LeftMotorB.setStopping(brakeType::brake);
  266.     RightMotorB.setStopping(brakeType::brake);
  267.    
  268.     float Kp = .395; //// best.17 //// ..30 ///4
  269.     float Kd = 0;/// best .35 //// .34 ////next best .32 next best// 25 //smallest movement.1 ///.395
  270.    
  271.     int Leftdegrees=LeftMotorF.rotation(rotationUnits::deg);
  272.     int Rightdegrees=RightMotorF.rotation(rotationUnits::deg);
  273.  
  274.     int ErrorLeft = -1*Leftdegrees - distance;
  275.     int ErrorRight = distance - Rightdegrees;
  276.    
  277.     int PreviousErrorLeft = 0;
  278.     int PreviousErrorRight = 0;
  279.    
  280.     while(1) {
  281.     Leftdegrees=LeftMotorF.rotation(rotationUnits::deg);
  282.     Rightdegrees=RightMotorF.rotation(rotationUnits::deg);
  283.  
  284.      ErrorLeft = -1*Leftdegrees - distance;
  285.      ErrorRight = distance - Rightdegrees;
  286.        
  287.         int LeftsideProportionalError = Kp * ErrorLeft;
  288.         int RightsideProportionalError = Kp * ErrorRight;
  289.  
  290.         double LeftsidedDerivativeError = Kd * (ErrorLeft - PreviousErrorLeft);
  291.         double RightsidedDerivativeError = Kd * (ErrorRight - PreviousErrorRight);
  292.        
  293.         LeftMotorFB(LeftsideProportionalError + LeftsidedDerivativeError);
  294.         RightMotorFB(RightsideProportionalError + RightsidedDerivativeError);
  295.        
  296.         PreviousErrorLeft = ErrorLeft;
  297.         PreviousErrorRight = ErrorRight;
  298.  
  299.         if (abs(ErrorLeft) < 10 && abs(ErrorRight) < 10) {
  300.         LeftMotorFB(0);
  301.         RightMotorFB(0);
  302.      
  303.             break;    
  304.     }
  305.         vex::task::sleep(30);
  306.     }
  307.     }
  308.  
  309.     void PidFoward (int distance) {
  310.     LeftMotorF.resetRotation();
  311.     RightMotorF.resetRotation();
  312.     LeftMotorB.resetRotation();
  313.     RightMotorB.resetRotation();
  314.    
  315.     double Kp = 0.4;///0.2 or .215 //.3  ////0.25 ///0.4
  316.     double Kd = 0; ////.275 or .0325 //.5 ///4375 //////0.438109 ///.7 .54 ////290 /////0.255
  317.  ////0.8 ////0.6
  318.    
  319.     int Leftdegrees=LeftMotorF.rotation(rotationUnits::deg);
  320.     int Rightdegrees=RightMotorF.rotation(rotationUnits::deg);
  321.  
  322.     int ErrorLeft = distance - Leftdegrees;
  323.     int ErrorRight = distance - Rightdegrees;
  324.  
  325.     int PreviousErrorLeft = 0;
  326.     int PreviousErrorRight = 0;
  327.  
  328.     while(1) {
  329.  
  330.     Leftdegrees=LeftMotorF.rotation(rotationUnits::deg);
  331.     Rightdegrees=RightMotorF.rotation(rotationUnits::deg);
  332.  
  333.     ErrorLeft = distance - Leftdegrees;
  334.     ErrorRight = distance - Rightdegrees;
  335.  
  336.     int LeftsideProportionalError = Kp * ErrorLeft;
  337.     int RightsideProportionalError = Kp * ErrorRight;
  338.  
  339.     double LeftsidedDerivativeError = Kd * (ErrorLeft - PreviousErrorLeft);
  340.     double RightsidedDerivativeError = Kd * (ErrorRight - PreviousErrorRight);
  341.  
  342.    int leftPower = LeftsideProportionalError + LeftsidedDerivativeError;
  343.    int rightPower = RightsideProportionalError + RightsidedDerivativeError;
  344.  
  345.  
  346.  LeftMotorFBS(leftPower);
  347.  RightMotorFBS(rightPower);
  348.  
  349.    
  350.         PreviousErrorLeft = ErrorLeft;
  351.         PreviousErrorRight = ErrorRight;
  352.  
  353.         if (abs(ErrorLeft) < 10 && abs(ErrorRight) < 10) {
  354.  
  355.  LeftMotorFB(0);
  356.  RightMotorFB(0);
  357.  
  358.         break;
  359.     }
  360.         vex::task::sleep(20);
  361.  
  362.     }
  363. }
  364.  
  365. void DriveInInches(double distanceinInches) {
  366.  
  367.   double in = 3;
  368.   double out = 2;
  369.   double wheelDiameter = 4; // in inches
  370.   double gearratio = (360 * out) / (wheelDiameter * 3.14 * in);
  371.   double targetDistance = distanceinInches * gearratio;
  372.   PidFoward(targetDistance);
  373. }
  374.  
  375. /////////////////////////////////////////////////////////////////////////////////////////////
  376.  
  377. void StopMotor(vex::brakeType type = brakeType::coast){
  378.     LeftMotorF.stop(type);
  379.     RightMotorF.stop(type);
  380.     LeftMotorB.stop(type);
  381.     RightMotorB.stop(type);
  382. }
  383. void SetMotorBrakingType(brakeType type = brakeType::coast) {
  384.     LeftMotorF.setStopping(type);
  385.     RightMotorF.setStopping(type);
  386.     LeftMotorB.setStopping(type);
  387.     RightMotorB.setStopping(type);
  388. }
  389. int CubeIntake2() {
  390.     int timeout = 4000;        
  391.     Scooper.setStopping(brakeType::coast);    
  392.     int sleepTimeout = 20;
  393.     Scooper.spin(directionType::rev, 100, velocityUnits::pct);
  394.      Scooper.spin(directionType::rev, 100, velocityUnits::pct);
  395.    
  396.     while (timeout > 0) {
  397.         task::sleep(sleepTimeout);
  398.         timeout = timeout - sleepTimeout;
  399.     }
  400.    
  401.     Scooper.stop(brakeType::coast);
  402.    
  403.     return(0);
  404. }
  405. int CubeIntake() {
  406.     int timeout = 4000;        
  407.     Scooper.setStopping(brakeType::coast);  
  408.      Scooper2.setStopping(brakeType::coast);
  409.     int sleepTimeout = 20;
  410.     Scooper.spin(directionType::fwd, 100, velocityUnits::pct);
  411.      Scooper2.spin(directionType::fwd, 100, velocityUnits::pct);
  412.    
  413.     while (timeout > 0) {
  414.         task::sleep(sleepTimeout);
  415.         timeout = timeout - sleepTimeout;
  416.     }
  417.     Scooper.stop(brakeType::coast);
  418.         Scooper2.stop(brakeType::coast);
  419.     return(0);
  420. }
  421. int AutoBallIntakeTakeBall(int timeout = 5000) {          
  422.    
  423.     Scooper.setStopping(brakeType::coast);    
  424.     int sleepTimeout = 20;
  425.     Scooper.spin(directionType::fwd, 60, velocityUnits::pct);
  426.     while (timeout > 0) {
  427.         task::sleep(sleepTimeout);
  428.         timeout = timeout - sleepTimeout;
  429.     }
  430.    
  431.     Scooper.stop(brakeType::coast);
  432.    
  433.     return(0);
  434. }
  435. void Drive(double numOfRevs, int velocity = 50, bool fwd = true, int timeout = 500)
  436. {
  437.     Brain.Screen.printAt(1, 40, "numofrevs: %f, vel: %f", numOfRevs, velocity);
  438.     SetMotorBrakingType();
  439.    
  440.     int sleepTimeout = 20;
  441.    
  442.     if (fwd == false) {
  443.         numOfRevs = numOfRevs * -1;
  444.     }
  445.      
  446.    
  447.     LeftMotorF.startRotateFor(numOfRevs, rotationUnits::rev, velocity, velocityUnits::pct);
  448.     RightMotorF.startRotateFor(numOfRevs, rotationUnits::rev, velocity, velocityUnits::pct);
  449.     LeftMotorB.startRotateFor(numOfRevs, rotationUnits::rev, velocity, velocityUnits::pct);
  450.     RightMotorB.startRotateFor(numOfRevs, rotationUnits::rev, velocity, velocityUnits::pct);
  451.  
  452.     while(LeftMotorF.isSpinning()||RightMotorF.isSpinning()||
  453.           LeftMotorB.isSpinning()||RightMotorB.isSpinning()) {
  454.  
  455.         if (timeout <= 0) {
  456.             break;
  457.         }
  458.         task::sleep(sleepTimeout);
  459.         timeout = timeout - sleepTimeout;
  460.     }
  461.     StopMotor();
  462. }
  463.   void DriveInCM(double distanceInCM, int velocity, bool fwd = true, int timeout = 5000) {
  464.   //  double gearRatio = 1.67;
  465.   //  double wheelDiameter = 10.16;
  466.   //  double circumference = wheelDiameter * M_PI;    
  467.   //  double numOfRevs = (distanceInCM)/ (circumference * gearRatio);
  468.   double numOfRevs = distanceInCM/47.9;////old 53.2 47.9
  469.   Drive(numOfRevs, velocity, fwd, timeout);    
  470. }
  471.  
  472. /////////////////////////////////////////////////////////////////////////////////
  473. void LiftPosition1 () {
  474.  
  475.     int timeout = 5000;
  476.  
  477.     Arm2000.setStopping(brakeType::coast);
  478.  
  479.     int sleepTimeout = 30;
  480.     while (timeout > 0) {
  481.  
  482.     Brain.Screen.clearScreen();
  483.  
  484.     float LiftPos = P2.value(rotationUnits::deg);
  485.  
  486.    Brain.Screen.printAt(2, 40, "LiftPos: %f", LiftPos);
  487.  
  488.         if (LiftPos == 0)
  489.         {
  490.             task::sleep(sleepTimeout);
  491.             continue;
  492.         }
  493.  
  494.         // Lowest position for launch
  495.         if (LiftPos >= 108)
  496.         {
  497.             Brain.Screen.printAt(2, 80, "Stopped: %f", LiftPos);
  498.             break;
  499.         }
  500.  
  501.         Arm2000.spin(directionType::fwd, 100, velocityUnits::pct);
  502.  
  503.         task::sleep(sleepTimeout);
  504.  
  505.         timeout = timeout - sleepTimeout;
  506.  
  507.         Brain.Screen.printAt(2, 20, "Timeout: %d", timeout);
  508.     }
  509.  
  510.     Arm2000.stop(brakeType::coast);    
  511. }
  512.  
  513. void LiftPosition2() {
  514.  
  515.     int timeout = 5000;
  516.  
  517.     Arm2000.setStopping(brakeType::coast);
  518.  
  519.     int sleepTimeout = 30;
  520.  
  521.     while (timeout > 0) {
  522.  
  523.         Brain.Screen.clearScreen();
  524.  
  525.         float LiftPos = P2.value(rotationUnits::deg);  
  526.  
  527.         Brain.Screen.printAt(2, 40, "LiftPos: %f", LiftPos);  
  528.  
  529.         if (LiftPos == 0)
  530.         {
  531.             task::sleep(sleepTimeout);
  532.             continue;
  533.         }
  534.  
  535.         // load position
  536.         if (LiftPos <= 44)
  537.         {
  538.             break;
  539.         }
  540.  
  541.      
  542.         Arm2000.spin(directionType::rev, 45, velocityUnits::pct);
  543.  
  544.         task::sleep(sleepTimeout);
  545.  
  546.         timeout = timeout - sleepTimeout;
  547.         Brain.Screen.printAt(2, 20, "Timeout: %d", timeout);
  548.     }
  549.  
  550.     Arm2000.stop(brakeType::hold);
  551. }
  552.  
  553. void LiftPosition3() {
  554.     int timeout = 2500;
  555.  
  556.     Arm2000.setStopping(brakeType::coast);
  557.  
  558.     int sleepTimeout = 30;
  559.  
  560.     while (timeout > 0) {
  561.        
  562.         Brain.Screen.clearScreen();
  563.  
  564.         float LiftPos = P2.value(rotationUnits::deg);  
  565.  
  566.         Brain.Screen.printAt(2, 40, "LiftPos: %f", LiftPos);  
  567.        
  568.        
  569.          if(P2.value(rotationUnits::deg) <44) {
  570.              while(P2.value(rotationUnits::deg) <44) { ///////////13
  571.                  Arm2000.spin(vex::directionType::fwd, 45, vex::velocityUnits::pct);
  572.                  
  573.          }
  574.            
  575.       break;
  576.            
  577.         }        
  578.             else if
  579.                (P2.value(rotationUnits::deg) > 45) {
  580.                   while(P2.value(rotationUnits::deg) > 45) { /////////16
  581.                  Arm2000.spin(vex::directionType::rev, 45, vex::velocityUnits::pct);
  582.                          }
  583.            
  584.           break;
  585.            
  586.         }
  587.            
  588.         Arm2000.spin(directionType::fwd, 40, velocityUnits::pct);
  589.  
  590.         task::sleep(sleepTimeout);
  591.  
  592.         timeout = timeout - sleepTimeout;
  593.         Brain.Screen.printAt(2, 20, "Timeout: %d", timeout);
  594.     }
  595.  
  596.     Arm2000.stop(brakeType::coast);
  597. }
  598. ///////////////////////////////////////////////??///////////////////////
  599.  
  600. void Lift10() {
  601.     int timeout = 2500;
  602.  
  603.     Lift.setStopping(brakeType::hold);    
  604.  
  605.  
  606.     int sleepTimeout = 30;
  607.  
  608.     while (timeout > 0) {
  609.  
  610.         Brain.Screen.clearScreen();
  611.  
  612.         float LiftCube = P1.value(rotationUnits::deg);  
  613.        
  614.      
  615.         Brain.Screen.printAt(2, 40, "LiftCube %f", LiftCube);  
  616.        
  617.      
  618.    if (LiftCube >= 87)
  619.         {
  620.        
  621.         Brain.Screen.printAt(2, 80, "Stopped: %f", LiftCube);
  622.             break;
  623.         }
  624.        
  625.             Lift.spin(directionType::rev, 127, velocityUnits::pct);
  626.  
  627.         task::sleep(sleepTimeout);
  628.  
  629.         timeout = timeout - sleepTimeout;
  630.         Brain.Screen.printAt(2, 20, "Timeout: %d", timeout);
  631.     }
  632.  
  633.     Lift.stop(brakeType::hold);
  634.    
  635. }
  636. void Lift11() {
  637.     int timeout = 2500;
  638.  
  639.     Lift.setStopping(brakeType::hold);    
  640.    
  641.  
  642.     int sleepTimeout = 30;
  643.  
  644.     while (timeout > 0) {
  645.  
  646.         Brain.Screen.clearScreen();
  647.  
  648.         float LiftCube = P1.value(rotationUnits::deg);  
  649.        
  650.      
  651.         Brain.Screen.printAt(2, 40, "LiftCube %f", LiftCube);  
  652.        
  653.      
  654.    if (LiftCube >= 67)
  655.         {
  656.        
  657.         Brain.Screen.printAt(2, 80, "Stopped: %f", LiftCube);
  658.             break;
  659.         }
  660.        
  661.        
  662.             Lift.spin(directionType::rev, 127, velocityUnits::pct);
  663.  
  664.         task::sleep(sleepTimeout);
  665.  
  666.         timeout = timeout - sleepTimeout;
  667.         Brain.Screen.printAt(2, 20, "Timeout: %d", timeout);
  668.     }
  669.  
  670.     Lift.stop(brakeType::hold);
  671.    
  672. }
  673. void Lift12() {
  674.     int timeout = 2500;
  675.  
  676.     Lift.setStopping(brakeType::hold);    
  677.    
  678.  
  679.     int sleepTimeout = 30;
  680.  
  681.     while (timeout > 0) {
  682.  
  683.         Brain.Screen.clearScreen();
  684.  
  685.         float LiftCube = P1.value(rotationUnits::deg);  
  686.        
  687.      
  688.         Brain.Screen.printAt(2, 40, "LiftCube %f", LiftCube);  
  689.        
  690.      
  691.    if (LiftCube <= 10)
  692.         {
  693.        
  694.         Brain.Screen.printAt(2, 80, "Stopped: %f", LiftCube);
  695.             break;
  696.         }
  697.        
  698.        
  699.             Lift.spin(directionType::fwd, 127, velocityUnits::pct);
  700.  
  701.         task::sleep(sleepTimeout);
  702.  
  703.         timeout = timeout - sleepTimeout;
  704.         Brain.Screen.printAt(2, 20, "Timeout: %d", timeout);
  705.     }
  706.  
  707.     Lift.stop(brakeType::brake);
  708.    
  709. }
  710. /*
  711. LiftPos >= 108*/
  712.  
  713. void CheckLiftPotValues() {
  714.  
  715.     while (1)
  716.     {
  717.         Brain.Screen.clearScreen();
  718.         Brain.Screen.printAt(1, 30, "CheckLiftPot");
  719.         float LiftPos = P2.value(rotationUnits::deg);
  720.         Brain.Screen.printAt(1, 20, "rotation: %f degrees", LiftPos);
  721.  
  722.         //Sleep the task for a short amount of time to prevent wasted resources.    
  723.         task::sleep(20);
  724.     }
  725. }
  726.  
  727. int DriveTask() {
  728.     while (true) {
  729.         if (driveMode) {
  730.             PidBasic(target);
  731.         }
  732.     }
  733. }
  734.  
  735. int LiftTask()
  736. {
  737.     while (true) {
  738.        
  739.            Brain.Screen.clearScreen();
  740.         float LiftPos = P2.value(rotationUnits::deg);
  741.  
  742.         Brain.Screen.printAt(1, 40, "LiftPos: %f", LiftPos);
  743.        
  744.         if (Controller1.ButtonY.pressing()) {
  745.             // Load
  746.            LiftPosition1();
  747.         }
  748.      
  749.          else if (Controller1.ButtonB.pressing()) {
  750.             // Launch
  751.              BigArm(43);
  752.         } else {
  753. Arm2000.stop(brakeType::brake);
  754.          }
  755.        
  756.            if (Controller2.ButtonL1.pressing()) {
  757.             // Launch
  758.             Lift10();
  759.         }
  760.        
  761.              else if (Controller2.ButtonL2.pressing()) {
  762.             // Launch
  763.             Lift11();
  764.         }
  765.        
  766.               else if (Controller2.ButtonDown.pressing()) {
  767.             // Launch
  768.             Lift12();
  769.         }
  770.        
  771.         task::sleep(30);
  772.     }
  773.     return (0);
  774. }
  775.  
  776.    void Turn() {
  777.  
  778.  Inertial11.calibrate();
  779.   // waits for the Inertial Sensor to calibrate
  780.   while (Inertial11.isCalibrating()) {
  781.     wait(100, msec);
  782.   }
  783.   // Turns the robot to the right
  784.   LeftMotorF.spin(forward);
  785.   RightMotorF.spin(reverse);
  786.   RightMotorB.spin(reverse);
  787.     LeftMotorB.spin(forward);
  788.  
  789.  
  790.   // Waits until the motor reaches a 90 degree turn and stops the Left and
  791.   // Right Motors.
  792.   waitUntil((Inertial11.rotation(degrees) >= 90.0));
  793.   LeftMotorF.stop();
  794.   RightMotorB.stop();
  795.    LeftMotorB.stop();
  796.   RightMotorF.stop();
  797.   wait(1, seconds);
  798. }
  799.  
  800. void autonomous(void) {
  801.  
  802. PidFoward(1000);
  803.  
  804.  
  805.  
  806.  
  807.  
  808. return;
  809.  
  810. }
  811.  
  812.  
  813.  
  814.  
  815. /*---------------------------------------------------------------------------*/
  816. /*                                                                           */
  817. /*                              User Control Task                            */
  818. /*                                                                           */
  819. /*  This task is used to control your robot during the user control phase of */
  820. /*  a VEX Competition.                                                       */
  821. /*                                                                           */
  822. /*  You must modify the code to add your own robot specific commands here.   */
  823. /*---------------------------------------------------------------------------*/
  824.  
  825. void usercontrol(void) {
  826.  
  827.   task CalliftTask(LiftTask);
  828.   // User control code here, inside the loop
  829.   while (true) {
  830.  
  831.  
  832.  
  833. RightMotorB.spin(directionType::fwd, Controller1.Axis2.position(percentUnits::pct),velocityUnits::pct);
  834. RightMotorF.spin(directionType::fwd, Controller1.Axis2.position(percentUnits::pct),velocityUnits::pct);
  835. LeftMotorF.spin(directionType::fwd, Controller1.Axis3.position(percentUnits::pct),velocityUnits::pct);
  836. LeftMotorB.spin(directionType::fwd, Controller1.Axis3.position(percentUnits::pct),velocityUnits::pct);
  837.  
  838. if(Controller1.ButtonL2.pressing()) {
  839.   Scooper.spin(vex::directionType::fwd, 100, velocityUnits::pct);
  840.     Scooper2.spin(vex::directionType::fwd, 100, velocityUnits::pct);
  841. }
  842. else if (Controller1.ButtonL1.pressing()) {
  843.    Scooper.spin(vex::directionType::rev, 100, velocityUnits::pct);
  844.     Scooper2.spin(vex::directionType::rev, 100, velocityUnits::pct);
  845. }
  846. else {
  847.   Scooper.stop(brakeType::brake);
  848.   Scooper2.stop(brakeType::brake);
  849. }
  850.  
  851.  
  852.     wait(20, msec); // Sleep the task for a short amount of time to
  853.                     // prevent wasted resources.
  854.   }
  855. }
  856.  
  857. //
  858. // Main will set up the competition functions and callbacks.
  859. //
  860. int main() {
  861.  
  862.  
  863.  
  864.  
  865.   // Set up callbacks for autonomous and driver control periods.
  866.  
  867.   Competition.autonomous(autonomous);
  868.  
  869.   Competition.drivercontrol(usercontrol);
  870.  
  871.   // Run the pre-autonomous function.
  872.   pre_auton();
  873.  
  874.   // Prevent main from exiting with an infinite loop.
  875.   while (true) {
  876.     wait(100, msec);
  877.   }
  878. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement