Advertisement
Mikkel_Serrantes

Untitled

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