Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.58 KB | None | 0 0
  1. #pragma config(I2C_Usage, I2C1, i2cSensors)
  2. #pragma config(Sensor, dgtl1, button1, sensorTouch)
  3. #pragma config(Sensor, dgtl2, button2, sensorTouch)
  4. #pragma config(Sensor, dgtl3, limitS, sensorTouch)
  5. #pragma config(Sensor, dgtl4, limitS2, sensorTouch)
  6. #pragma config(Sensor, I2C_1, , sensorQuadEncoderOnI2CPort, , AutoAssign )
  7. #pragma config(Motor, port1, motor1, tmotorVex393_HBridge, openLoop, encoderPort, I2C_1)
  8. #pragma config(Motor, port10, motor2, tmotorVex393_HBridge, openLoop)
  9. #pragma config(Motor, port2, motor3, tmotorVex393_HBridge, openLoop)
  10. #pragma config(Sensor, in1, InfraCollector, sensorReflection)
  11. #pragma config(Sensor, dgtl5, RedLED, sensorDigitalOut)
  12. #pragma config(Sensor, dgtl11, StateLED, sensorDigitalOut)
  13. //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
  14.  
  15. bool button1_pushed; //flag to store button1 input
  16. bool button2_pushed; //flag to store button2 input
  17. bool limitS_pushed;
  18. bool limitS2_pushed;//flag to store limitS input
  19.  
  20. bool isLost;
  21. int light_threshold;
  22. // initial light threshold
  23.  
  24. void monitorInput(){
  25.  
  26.     if(SensorValue(button1) && !button1_pushed){
  27.         button1_pushed = true;
  28.     }//if
  29.  
  30.     if(SensorValue(button2) && !button2_pushed){
  31.         button2_pushed = true;
  32.     }//if
  33.  
  34.     if(SensorValue(limitS) && !limitS_pushed){
  35.         limitS_pushed = true;
  36.     }//if
  37.  
  38.     if(SensorValue(limitS2) && !limitS2_pushed){
  39.         limitS2_pushed = true;
  40.     }//if
  41. }//monitorInput
  42.  
  43. void exercise(){
  44.  
  45.     enum T_state {
  46.         MOTOR_IDLE = 0,
  47.         MOTOR_FORWARD,
  48.         MOTOR_FORWARD2,
  49.         MOTOR_FINDING,
  50.         MOTOR_FINDING2,
  51.         RESEARCH,
  52.         MOTOR_CONNECTING,
  53.         WALL,
  54.         COMPLETION
  55.     };//enum
  56.  
  57.     T_state state = MOTOR_IDLE;
  58.  
  59.     while(true){
  60.         // Monitor buttons
  61.         monitorInput();
  62.  
  63.         switch(state){
  64.  
  65.         case MOTOR_IDLE:
  66.             // MOTOR_IDLE state: motor off waiting to search
  67.             motor[motor1] = 0;
  68.             motor[motor2] = 0;
  69.             motor[motor3] = 0;
  70.  
  71.             //swapping to searching state when button pushed, resetting light threshold for each new search
  72.             if ( button1_pushed ){
  73.                 state = MOTOR_FINDING;
  74.                 light_threshold = 90;
  75.                 button1_pushed = false;
  76.             }//if
  77.         break;
  78.  
  79.         case MOTOR_FINDING:
  80.             // MOTOR_FINDING state: turn motors on in opposite directions, search for the beacon.
  81.             resetMotorEncoder(motor1);
  82.             motor[motor1] = 50;
  83.             motor[motor2] = 50;
  84.  
  85.             //loop to search for beacon
  86.             while(SensorValue(InfraCollector)>light_threshold){
  87.                 //reset motor encoder and increment threshold after every 360 degree turn
  88.                 if(getMotorEncoder(motor1) > 2200){
  89.                     light_threshold = light_threshold + 15;
  90.                     resetMotorEncoder(motor1);
  91.                 }//if
  92.             }//while
  93.  
  94.             //light has been found, go towards it
  95.             state = MOTOR_FORWARD;
  96.  
  97.             //if the limit switches are pressed, go into the states to back out
  98.             if(limitS_pushed || limitS2_pushed){
  99.                 state = WALL;
  100.                 limitS_pushed = false;
  101.                 limitS2_pushed = false;
  102.             }//if
  103.         break;
  104.         /*
  105.         case MOTOR_FINDING2:
  106.             // MOTOR_FINDING state: turn motors on in opposite directions, search for the beacon.
  107.             resetMotorEncoder(motor1);
  108.             motor[motor1] = -30;
  109.             motor[motor2] = -30;
  110.  
  111.             //loop to search for beacon
  112.             while(SensorValue(InfraCollector) > light_threshold){
  113.                 //reset motor encoder and increment threshold after every 360 degree turn
  114.                 if(getMotorEncoder(motor1) < -2200){
  115.                     light_threshold = light_threshold + 15;
  116.                     resetMotorEncoder(motor1);
  117.                 }//if
  118.             }//while
  119.  
  120.             //light has been found, go towards it
  121.             state = MOTOR_FORWARD;
  122.  
  123.             //if the limit switches are pressed, go into the states to back out
  124.             if(limitS_pushed || limitS2_pushed){
  125.                 state = WALL;
  126.                 limitS_pushed = false;
  127.                 limitS2_pushed = false;
  128.             }//if
  129.         break;
  130.     */
  131.  
  132.         case RESEARCH:
  133.             resetMotorEncoder(motor1);
  134.  
  135.             while(getMotorEncoder(motor1) < 100){
  136.                 if(SensorValue(InfraCollector) < light_threshold + 5){
  137.                     isLost = false;
  138.                     break;
  139.                 }
  140.                 else{
  141.                     isLost = true;
  142.                 }
  143.             if(limitS_pushed || limitS2_pushed){
  144.                 state = WALL;
  145.                 limitS_pushed = false;
  146.                 limitS2_pushed = false;
  147.             }//if
  148.             }
  149.  
  150.             state = MOTOR_FORWARD;
  151.         break;
  152.  
  153.         case MOTOR_FORWARD:
  154.             //MOTOR_FORWARD state: turn on motor, search for any abnormalities
  155.             motor[motor1] = 60;
  156.             motor[motor2] = -60;
  157.             if(SensorValue(InfraCollector) < 80){
  158.                 state = MOTOR_FORWARD2;
  159.             }
  160.  
  161.             if(getMotorEncoder(motor1) > 200){
  162.                 state = RESEARCH;
  163.                 isLost = false;
  164.             }
  165.  
  166.             if(isLost){
  167.                 state = MOTOR_FINDING;
  168.             }
  169.             //if the limit switches are pressed, go into the states to back out
  170.             if(limitS_pushed || limitS2_pushed){
  171.                 state = WALL;
  172.                 limitS_pushed = false;
  173.                 limitS2_pushed = false;
  174.             }//if
  175.         break;
  176.                    
  177.         case MOTOR_FORWARD2:
  178.             //MOTOR_FORWARD2 state: slow motors, search for any abnormalities
  179.             motor[motor1] = 10;
  180.             motor[motor2] = -10;
  181.             if(SensorValue(InfraCollector) < 50){
  182.                 state = MOTOR_CONNECTING;
  183.             }
  184.  
  185.             if(getMotorEncoder(motor1) > 200){
  186.                 state = RESEARCH;
  187.                 isLost = false;
  188.             }
  189.  
  190.             if(isLost){
  191.                 state = MOTOR_FINDING;
  192.             }
  193.             //if the limit switches are pressed, go into the states to back out
  194.             if(limitS_pushed || limitS2_pushed){
  195.                 state = WALL;
  196.                 limitS_pushed = false;
  197.                 limitS2_pushed = false;
  198.             }//if
  199.         break;
  200.        
  201.         case WALL:
  202.             //WALL state: after collsion with wall, moves back and searches for beacon again
  203.             resetMotorEncoder(motor1);
  204.  
  205.             //move back from wall and search for beacon again
  206.             while(getMotorEncoder(motor1) > -300){
  207.                 motor[motor1] = -60;
  208.                 motor[motor2] = 60;
  209.             }//while
  210.                                
  211.                     light_threshold = light_threshold - 15;
  212.                 state = MOTOR_FINDING;
  213.                 motor[motor1] = 0;
  214.                 motor[motor2] = 0;
  215.         break;
  216.  
  217.         case MOTOR_CONNECTING:
  218.             // MOTOR_CONNECTING state: lower connector to attch cable attaching our cable, raise connector to original position
  219.             wait1Msec(250);
  220.             motor[motor1] = 0;
  221.             motor[motor2] = 0;
  222.             motor[motor3] = -22;
  223.             wait1Msec(2500);
  224.             motor[motor3] = 25;
  225.             wait1Msec(2400);
  226.             state = COMPLETION;
  227.             motor[motor3] = 0;
  228.         break;
  229.  
  230.         case COMPLETION:
  231.             //COMPLETION state: connection has succeeded, back away and turn on red LED
  232.             resetMotorEncoder(motor1);
  233.             //back away
  234.             while(getMotorEncoder(motor1) > -300){
  235.                 motor[motor1] = -60;
  236.                 motor[motor2] = 60;
  237.             }//while
  238.  
  239.             resetMotorEncoder(motor1);
  240.             //turn 90 degrees
  241.             while(getMotorEncoder(motor1) < 550){
  242.                 motor[motor1] = 60;
  243.                 motor[motor2] = 60;
  244.             }//while
  245.  
  246.             resetMotorEncoder(motor1);
  247.             //move off cable
  248.             while(getMotorEncoder(motor1) < 300){
  249.                 motor[motor1] = 60;
  250.                 motor[motor2] = -60;
  251.             }//while
  252.  
  253.             motor[motor1] = 0;
  254.             motor[motor2] = 0;
  255.             SensorValue(StateLED) = 1;
  256.  
  257.             button1_pushed = false;
  258.             state = MOTOR_IDLE;
  259.         break;
  260.  
  261.         default:
  262.         } //switch(state)
  263.     } //while(1)
  264. } //end exercise
  265.  
  266. task main(){
  267.     button1_pushed = button2_pushed = false;
  268.     exercise();
  269. }//end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement