Advertisement
Guest User

LCD autonomous selection

a guest
Nov 15th, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.80 KB | None | 0 0
  1. /** @file opcontrol.c
  2.  * @brief File for operator control code
  3.  *
  4.  
  5. #include "main.h"
  6. #include "coolTeam.h"
  7.  
  8. /*
  9.  * Runs the user operator control code. This function will be started in its own task with the
  10.  * default priority and stack size whenever the robot is enabled via the Field Management System
  11.  * or the VEX Competition Switch in the operator control mode. If the robot is disabled or
  12.  * communications is lost, the operator control task will be stopped by the kernel. Re-enabling
  13.  * the robot will restart the task, not resume it from where it left off.
  14.  *
  15.  * If no VEX Competition Switch or Field Management system is plugged in, the VEX Cortex will
  16.  * run the operator control task. Be warned that this will also occur if the VEX Cortex is
  17.  * tethered directly to a computer via the USB A to A cable without any VEX Joystick attached.
  18.  *
  19.  * Code running in this task can take almost any action, as the VEX Joystick is available and
  20.  * the schedular is operational. However, proper use of delay() or taskDelayUntil() is highly
  21.  * recommended to give other tasks (including system tasks such as updating LCDs) time to run.
  22.  *
  23.  * This task should never exit; it should end with some kind of infinite loop, even if empty.
  24.  */
  25. void operatorControl() {
  26.  
  27.     while(true) {
  28.         const short leftButton = 1;
  29.         const short centerButton = 2;
  30.         const short rightButton = 4;
  31.  
  32.         //Wait for Press--------------------------------------------------
  33.         void waitForPress()
  34.         {
  35.             while(lcdReadButtons(uart1) == 0){
  36.                 wait(2000);
  37.             }
  38.         }
  39.         //----------------------------------------------------------------
  40.  
  41.         //Wait for Release------------------------------------------------
  42.         void waitForRelease()
  43.         {
  44.             while(lcdReadButtons(uart1) != 0){
  45.                 wait(2000);
  46.             }
  47.         }
  48.         //----------------------------------------------------------------
  49.  
  50.  
  51.         //Declare count variable to keep track of our choice
  52.         int count = 0;
  53.  
  54.         //------------- Beginning of User Interface Code ---------------
  55.         //Clear LCD
  56.         lcdClear(uart1);
  57.         //Loop while center button is not pressed
  58.         while(lcdReadButtons(uart1) != centerButton) {
  59.             //Switch case that allows the user to choose from 4 different options
  60.             switch(count){
  61.             case 0:
  62.                 //Display first choice
  63.                 lcdSetText(uart1, 1, "Autonomous 1");
  64.                 lcdSetText(uart1, 2, "<Enter>");
  65.                 waitForPress();
  66.                 //Increment or decrement "count" based on button press
  67.                 if(lcdReadButtons(uart1) == leftButton)
  68.                 {
  69.                     waitForRelease();
  70.                     count = 3;
  71.                 }
  72.                 else if(lcdReadButtons(uart1) == rightButton)
  73.                 {
  74.                     waitForRelease();
  75.                     count++;
  76.                 }
  77.                 break;
  78.             case 1:
  79.                 //Display second choice
  80.                 lcdSetText(uart1, 1, "Autonomous 2");
  81.                 lcdSetText(uart1, 2, "<Enter>");
  82.                 waitForPress();
  83.                 //Increment or decrement "count" based on button press
  84.                 if(lcdReadButtons(uart1) == leftButton)
  85.                 {
  86.                     waitForRelease();
  87.                     count--;
  88.                 }
  89.                 else if(lcdReadButtons(uart1) == rightButton)
  90.                 {
  91.                     waitForRelease();
  92.                     count++;
  93.                 }
  94.                 break;
  95.             case 2:
  96.                 //Display third choice
  97.                 lcdSetText(uart1, 1, "Autonomous 3");
  98.                 lcdSetText(uart1, 2, "<Enter>");
  99.                 waitForPress();
  100.                 //Increment or decrement "count" based on button press
  101.                 if(lcdReadButtons(uart1) == leftButton)
  102.                 {
  103.                     waitForRelease();
  104.                     count--;
  105.                 }
  106.                 else if(lcdReadButtons(uart1) == rightButton)
  107.                 {
  108.                     waitForRelease();
  109.                     count++;
  110.                 }
  111.                 break;
  112.             case 3:
  113.                 //Display fourth choice
  114.                 lcdSetText(uart1, 1, "Autonomous 4");
  115.                 lcdSetText(uart1, 2, "<Enter>");
  116.                 waitForPress();
  117.                 //Increment or decrement "count" based on button press
  118.                 if(lcdReadButtons(uart1) == leftButton)
  119.                 {
  120.                     waitForRelease();
  121.                     count--;
  122.                 }
  123.                 else if(lcdReadButtons(uart1) == rightButton)
  124.                 {
  125.                     waitForRelease();
  126.                     count = 0;
  127.                 }
  128.                 break;
  129.             default:
  130.                 count = 0;
  131.                 break;
  132.  
  133.             }
  134.             //------------- End of User Interface Code ---------------------
  135.  
  136.             //------------- Beginning of Robot Movement Code ---------------
  137.             //Clear LCD
  138.             lcdClear(uart1);
  139.             //Switch Case that actually runs the user choice
  140.             switch(count){
  141.             case 0:
  142.                 //If count = 0, run the code correspoinding with choice 1
  143.                 lcdSetText(uart1, 1, "Autonomous 1");
  144.                 lcdSetText(uart1, 2, "is running");
  145.                 wait(2000);                        // Robot waits for 2000 milliseconds
  146.  
  147.                 // initializes encoders
  148.                 Encoder right = encoderInit(5, 6, false);
  149.  
  150.                 //wait for 0.5 seonds
  151.                 wait(750);
  152.  
  153.                 //robot drives towards the driver while intaking buckyballs
  154.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < 22) {
  155.                     //base
  156.                     motorSet(2, 70);
  157.                     motorSet(3, 80);
  158.                     motorSet(4, 70);
  159.                     motorSet(5, 80);
  160.  
  161.                     //intake
  162.                     motorSet(1, -127);
  163.                     motorSet(10, -127);
  164.                 }
  165.  
  166.                 //wait for 0.2 seconds
  167.                 wait(200);
  168.  
  169.                 //resets the encoder value to zero
  170.                 encoderReset(right);
  171.  
  172.                 //robot drives backwards from the driver
  173.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < 22) {
  174.                     //base
  175.                     motorSet(2, -70);
  176.                     motorSet(3, -80);
  177.                     motorSet(4, -70);
  178.                     motorSet(5, -80);
  179.                 }
  180.                 //stops all the motors
  181.                 motorStopAll();
  182.  
  183.                 //rotate the robot 90 degrees(
  184.                 wait(2000);
  185.  
  186.                 //lowers ball manipulator to the ground for 1.1 seconds
  187.                 motorSet(8, -100);
  188.                 motorSet(9, -100);
  189.                 wait(1100);
  190.                 motorStopAll();
  191.  
  192.                 //wait for 0.2 seconds
  193.                 wait(200);
  194.  
  195.                 //resets the encoder value to zero
  196.                 encoderReset(right);
  197.  
  198.  
  199.                 //robot drives backwards
  200.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < 9) {
  201.                     //base
  202.                     motorSet(2, -70);
  203.                     motorSet(3, -80);
  204.                     motorSet(4, -70);
  205.                     motorSet(5, -80);
  206.                 }
  207.  
  208.                 //lifts ball manipulator for 0.55 seconds
  209.                 motorSet(8, 100);
  210.                 motorSet(9, 100);
  211.                 wait(550);
  212.                 motorStopAll();
  213.  
  214.                 //resets the encoder value to zero
  215.                 encoderReset(right);
  216.  
  217.                 //robot turns left
  218.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < (ROBOT_CURCUMFIRENCE/4) + 4) {
  219.                     motorSet(2, -100);
  220.                     motorSet(3, 100);
  221.                     motorSet(4, -100);
  222.                     motorSet(5, 100);
  223.                 }
  224.                 //wait for 0.2 seconds
  225.                 wait(200);
  226.  
  227.                 //resets the encoder value to zero
  228.                 encoderReset(right);
  229.  
  230.                 //robot drives backwards
  231.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < 30) {
  232.                     motorSet(2, -70);
  233.                     motorSet(3, -80);
  234.                     motorSet(4, -70);
  235.                     motorSet(5, -80);
  236.                 }
  237.  
  238.                 //wait for 0.2 seconds
  239.                 wait(200);
  240.  
  241.                 //resets the encoder value to zero
  242.                 encoderReset(right);
  243.  
  244.                 //robot turns left
  245.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < (ROBOT_CURCUMFIRENCE/2)) {
  246.                     motorSet(2, -70);
  247.                     motorSet(3, 80);
  248.                     motorSet(4, -70);
  249.                     motorSet(5, 80);
  250.                 }
  251.  
  252.                 //releases the buckyballs from the robot
  253.                 motorSet(1, 127);
  254.                 motorSet(10, 127);
  255.  
  256.                 //lifts the big ball manipulator for 0.9 seconds
  257.                 motorSet(8, 127);
  258.                 motorSet(9, 127);
  259.                 wait(900);
  260.                 motorStop(8);
  261.                 motorStop(9);
  262.  
  263.                 //lowers the big ball manipulator for 0.9 seconds
  264.                 motorSet(8, -127);
  265.                 motorSet(9, -127);
  266.                 wait(600);
  267.                 motorStop(8);
  268.                 motorStop(9);
  269.                 //wait(1000);                           // Robot runs previous code for 3000 milliseconds before moving on
  270.                 break;
  271.             case 1:
  272.                 //If count = 1, run the code correspoinding with choice 2
  273.                 lcdSetText(uart1, 1, "Autonomous 2");
  274.                 lcdSetText(uart1, 2, "is running");
  275.                 wait(2000);                        // Robot waits for 2000 milliseconds
  276.  
  277.                 // Move reverse at full power for 3 seconds
  278.                 // initializes encoders
  279.                 // Encoder right = encoderInit(5, 6, false);
  280.  
  281.                 //wait for 0.5 seonds
  282.                 wait(750);
  283.  
  284.  
  285.                 //lifts the ball manipulator for 0.19 seconds
  286.                 motorSet(8, -100);
  287.                 motorSet(9, -100);
  288.                 wait(190);
  289.                 motorStopAll();
  290.  
  291.                 //robot drives towards barrier
  292.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < 28) {
  293.                     //base
  294.                     motorSet(2, 70);
  295.                     motorSet(3, 80);
  296.                     motorSet(4, 70);
  297.                     motorSet(5, 80);
  298.                 }
  299.                 //stops all motors
  300.                 motorStopAll();
  301.  
  302.                 //wait for 0.2 seconds
  303.                 wait(200);
  304.  
  305.                 //resets the encoder value to zero
  306.                 encoderReset(right);
  307.  
  308.                 //releases bucky balls for 1 second
  309.                 motorSet(1, 127);
  310.                 motorSet(10, 127);
  311.                 wait(1000);
  312.                 motorStopAll();
  313.  
  314.  
  315.  
  316.                 //robot drives backwards towards bumpf
  317.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < 28) {
  318.                     motorSet(2, -70);
  319.                     motorSet(3, -80);
  320.                     motorSet(4, -70);
  321.                     motorSet(5, -80);
  322.                 }
  323.                 //stops all motor
  324.                 motorStopAll();
  325.  
  326.                 //rotate the robot about 45 degrees
  327.                 wait(1250);
  328.  
  329.                 //resets the encoder value to zero
  330.                 encoderReset(right);
  331.  
  332.                 //wait 0.2 seconds
  333.                 wait(200);
  334.  
  335.                 //lifts the arm for 1 second
  336.                 motorSet(6, 127);
  337.                 motorSet(7, 127);
  338.                 wait(1000);
  339.                 motorStopAll();
  340.  
  341.                 //robot drives bacwards towards bump
  342.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < 40) {
  343.                     // base
  344.                     motorSet(2, 70);
  345.                     motorSet(3, 80);
  346.                     motorSet(4, 70);
  347.                     motorSet(5, 80);
  348.                 }
  349.                 //wait for 0.2 seconds
  350.                 wait(200);
  351.  
  352.                 //resets the encoder value to zero
  353.                 encoderReset(right);
  354.  
  355.                 //robot drives bacwards towards bump
  356.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < 40) {
  357.                     //base
  358.                     motorSet(2, -70);
  359.                     motorSet(3, -80);
  360.                     motorSet(4, -70);
  361.                     motorSet(5, -80);
  362.                 }
  363.                     // Robot runs previous code for 3000 milliseconds before moving on
  364.                     break;
  365.             case 2:
  366.                 //If count = 2, run the code correspoinding with choice 3
  367.                 lcdSetText(uart1, 1, "Autonomous 3");
  368.                 lcdSetText(uart1, 2, "is running");
  369.                 wait(2000);                        // Robot waits for 2000 milliseconds
  370.  
  371.                 //Turn right for 3seconds
  372.                 // initializes encoders
  373.                 //Encoder right = encoderInit(5, 6, false);
  374.  
  375.                 //wait for 0.5 seonds
  376.                 wait(750);
  377.  
  378.  
  379.                 //lifts the ball manipulator for 0.19 seconds
  380.                 motorSet(8, -100);
  381.                 motorSet(9, -100);
  382.                 wait(190);
  383.                 motorStopAll();
  384.  
  385.                 //robot drives towards barrier
  386.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < 28) {
  387.                     //base
  388.                     motorSet(2, 70);
  389.                     motorSet(3, 80);
  390.                     motorSet(4, 70);
  391.                     motorSet(5, 80);
  392.                 }
  393.                 //stops all motors
  394.                 motorStopAll();
  395.  
  396.                 //wait for 0.2 seconds
  397.                 wait(200);
  398.  
  399.                 //resets the encoder value to zero
  400.                 encoderReset(right);
  401.  
  402.                 //releases bucky balls for 1 second
  403.                 motorSet(1, 127);
  404.                 motorSet(10, 127);
  405.                 wait(1000);
  406.                 motorStopAll();
  407.  
  408.  
  409.  
  410.                 //robot drives backwards towards bumpf
  411.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < 28) {
  412.                     motorSet(2, -70);
  413.                     motorSet(3, -80);
  414.                     motorSet(4, -70);
  415.                     motorSet(5, -80);
  416.                 }
  417.                 //stops all motor
  418.                 motorStopAll();
  419.  
  420.                 //rotate the robot about 45 degrees
  421.                 wait(1250);
  422.  
  423.                 //resets the encoder value to zero
  424.                 encoderReset(right);
  425.  
  426.                 //wait 0.2 seconds
  427.                 wait(200);
  428.  
  429.                 //lifts the arm for 1 second
  430.                 motorSet(6, 127);
  431.                 motorSet(7, 127);
  432.                 wait(1000);
  433.                 motorStopAll();
  434.  
  435.                 //robot drives bacwards towards bump
  436.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < 40) {
  437.                     // base
  438.                     motorSet(2, 70);
  439.                     motorSet(3, 80);
  440.                     motorSet(4, 70);
  441.                     motorSet(5, 80);
  442.                 }
  443.                 //wait for 0.2 seconds
  444.                 wait(200);
  445.  
  446.                 //resets the encoder value to zero
  447.                 encoderReset(right);
  448.  
  449.                 //robot drives bacwards towards bump
  450.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < 40) {
  451.                     //base
  452.                     motorSet(2, -70);
  453.                     motorSet(3, -80);
  454.                     motorSet(4, -70);
  455.                     motorSet(5, -80);
  456.                 }
  457.  
  458.             case 3:
  459.                 //If count = 3, run the code correspoinding with choice 4
  460.                 lcdSetText(uart1, 1, "Autonomous 4");
  461.                 lcdSetText(uart1, 2, "is running");
  462.                 wait(2000);                        // Robot waits for 2000 milliseconds
  463.  
  464.                 // initializes encoders
  465.                 //Encoder right = encoderInit(5, 6, false);
  466.  
  467.                 //wait for 0.5 seonds
  468.                 wait(750);
  469.  
  470.                 //robot drives towards the driver while intaking buckyballs
  471.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < 22) {
  472.                     //base
  473.                     motorSet(2, 70);
  474.                     motorSet(3, 80);
  475.                     motorSet(4, 70);
  476.                     motorSet(5, 80);
  477.  
  478.                     //intake
  479.                     motorSet(1, -127);
  480.                     motorSet(10, -127);
  481.                 }
  482.  
  483.                 //wait for 0.2 seconds
  484.                 wait(200);
  485.  
  486.                 //resets the encoder value to zero
  487.                 encoderReset(right);
  488.  
  489.                 //robot drives backwards from the driver
  490.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < 22) {
  491.                     //base
  492.                     motorSet(2, -70);
  493.                     motorSet(3, -80);
  494.                     motorSet(4, -70);
  495.                     motorSet(5, -80);
  496.                 }
  497.                 //stops all the motors
  498.                 motorStopAll();
  499.  
  500.                 //rotate the robot 90 degrees(
  501.                 wait(2000);
  502.  
  503.                 //lowers ball manipulator to the ground for 1.1 seconds
  504.                 motorSet(8, -100);
  505.                 motorSet(9, -100);
  506.                 wait(1100);
  507.                 motorStopAll();
  508.  
  509.                 //wait for 0.2 seconds
  510.                 wait(200);
  511.  
  512.                 //resets the encoder value to zero
  513.                 encoderReset(right);
  514.  
  515.  
  516.                 //robot drives backwards
  517.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < 9) {
  518.                     //base
  519.                     motorSet(2, -70);
  520.                     motorSet(3, -80);
  521.                     motorSet(4, -70);
  522.                     motorSet(5, -80);
  523.                 }
  524.  
  525.                 //lifts ball manipulator for 0.55 seconds
  526.                 motorSet(8, 100);
  527.                 motorSet(9, 100);
  528.                 wait(550);
  529.                 motorStopAll();
  530.  
  531.                 //resets the encoder value to zero
  532.                 encoderReset(right);
  533.  
  534.                 //robot turns right
  535.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < (ROBOT_CURCUMFIRENCE/4) + 4) {
  536.                     motorSet(2, 100);
  537.                     motorSet(3, -100);
  538.                     motorSet(4, 100);
  539.                     motorSet(5, -100);
  540.                 }
  541.                 //wait for 0.2 seconds
  542.                 wait(200);
  543.  
  544.                 //resets the encoder value to zero
  545.                 encoderReset(right);
  546.  
  547.                 //robot drives backwards
  548.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < 30) {
  549.                     motorSet(2, -70);
  550.                     motorSet(3, -80);
  551.                     motorSet(4, -70);
  552.                     motorSet(5, -80);
  553.                 }
  554.  
  555.                 //wait for 0.2 seconds
  556.                 wait(200);
  557.  
  558.                 //resets the encoder value to zero
  559.                 encoderReset(right);
  560.  
  561.                 //robot turns right
  562.                 while ((abs(encoderGet(right)) / ENCODER_COEFFICIENT) < (ROBOT_CURCUMFIRENCE/2)) {
  563.                     motorSet(2, 70);
  564.                     motorSet(3, -80);
  565.                     motorSet(4, 70);
  566.                     motorSet(5, -80);
  567.                 }
  568.  
  569.                 //releases the buckyballs from the robot
  570.                 motorSet(1, 127);
  571.                 motorSet(10, 127);
  572.  
  573.                 //lifts the big ball manipulator for 0.9 seconds
  574.                 motorSet(8, 127);
  575.                 motorSet(9, 127);
  576.                 wait(900);
  577.                 motorStop(8);
  578.                 motorStop(9);
  579.  
  580.                 //lowers the big ball manipulator for 0.9 seconds
  581.                 motorSet(8, -127);
  582.                 motorSet(9, -127);
  583.                 wait(600);
  584.                 motorStop(8);
  585.                 motorStop(9);
  586.                 //wait(1000);                          // Robot runs previous code for 3000 milliseconds before moving on
  587.                 break;
  588.             default:
  589.                 lcdSetText(uart1, 1, "No valid choice");
  590.                 lcdSetText(uart1, 2, "was made.");
  591.                 break;
  592.                 }
  593. }
  594. }
  595. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement