Advertisement
thanadol

Robot Contest Code [Extends]

Jul 28th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.71 KB | None | 0 0
  1. #include <ipst.h>
  2.  
  3. //CLASS-----------------------------------------------------------------------------------------
  4.  
  5. class LightSensor
  6. {
  7.   public:
  8.  
  9.   int port;
  10.  
  11.   int LightLimit;
  12.  
  13.   LightSensor(int port_,int LightLimit_)
  14.   {
  15.     port = port_;
  16.     LightLimit = LightLimit_;
  17.   };
  18.    
  19.   int input()
  20.   {
  21.      return analog(port);
  22.   };
  23.  
  24.   int digital()
  25.   {
  26.     return in(port);
  27.   }
  28.  
  29.   boolean isWhite()
  30.   {
  31.     return input() > LightLimit;
  32.   }
  33.  
  34.   boolean isBlack()
  35.   {
  36.     return !isWhite();
  37.   }
  38.  
  39. };
  40.  
  41. //VARIABLE---------------------------------------------------------------------------------------
  42.  
  43. //Motion
  44.  
  45.   //Normal Moving Speed
  46.   const int normalSpeed = 30;
  47.   const int fastSpeed = 40;
  48.   const int slowSpeed = 25;
  49.  
  50.   //Rotation Speed
  51.   const int turnSpeed = 20;
  52.   const int turningOffsetTime = 400;
  53.   const int rotationFixSpeed = 100;
  54.   const int rotationFixTime = 75;
  55.  
  56.   //caning Speed
  57.   const int canHittingSpeed = (fastSpeed * 2) + 10;
  58.   const int canHittingTime = 10;
  59.   const int canBackwardSpeed = slowSpeed;
  60.  
  61.   //Error Of Balance (relative to left)
  62.   const int balanceError = 0;
  63.  
  64.   //motor port
  65.   const int motorLeft = 2;
  66.   const int motorRight = 1;
  67.  
  68. //Sensoring
  69.  
  70.   //LightSensor
  71.   LightSensor leftSensor = LightSensor(4,500);
  72.   LightSensor midLeftSensor = LightSensor(1,100);
  73.   LightSensor midRightSensor = LightSensor(2,100);
  74.   LightSensor rightSensor = LightSensor(5,500);
  75.  
  76. //CODE----------------------------------------------------------------------------------
  77.  
  78. void fw2(int left, int right)
  79. {
  80.   motor(motorLeft, left + balanceError);
  81.   motor(motorRight, right);
  82. }
  83.  
  84. void bw2(int left, int right)
  85. {
  86.   motor(motorLeft, left + balanceError);
  87.   motor(motorRight, right);
  88. }
  89.  
  90. void turnR90()
  91. {
  92.   fw2(turnSpeed,0);
  93.   delay(turningOffsetTime);
  94.   while(midLeftSensor.isWhite() || midRightSensor.isWhite());
  95.  
  96.   /*fw2(rotationFixSpeed,-rotationFixSpeed);
  97.   delay(rotationFixTime);*/
  98.   motor_stop(ALL);
  99. }
  100.  
  101. void turnL90()
  102. {
  103.  
  104.   fw2(0,turnSpeed);
  105.   delay(turningOffsetTime);
  106.   while(midLeftSensor.isWhite() || midRightSensor.isWhite());
  107.  
  108.   /*fw2(rotationFixSpeed,-rotationFixSpeed);
  109.   delay(rotationFixTime);*/
  110.   motor_stop(ALL);
  111. }
  112.  
  113. int i = 5;
  114.  
  115. //General Along-Path Motion Controller
  116. void track(int code)
  117. {
  118.   if(midLeftSensor.isBlack() && midRightSensor.isBlack())
  119.   {
  120.     //glcd(2,1,"Straight                    ");
  121.       if(code == 2)
  122.         fw2(slowSpeed - 7 , slowSpeed - 7);
  123.       else if(code == 3)
  124.         fw2(slowSpeed - 15 , slowSpeed - 15);
  125.       else if(code == -1)
  126.         fw2(-slowSpeed,-slowSpeed);
  127.       else if(code == 4)
  128.         fw2(slowSpeed - 15 , slowSpeed - 15);
  129.        else
  130.         fw2(slowSpeed, slowSpeed);
  131.       i = 5;
  132.   }
  133.   else if(midLeftSensor.isWhite() && midRightSensor.isBlack())// curve on Right
  134.   {
  135.      // glcd(2,1,"Turn Right Bit             ");
  136.      if(code == 2)
  137.        fw2(slowSpeed,0);
  138.      else if(code == 3)
  139.         fw2(slowSpeed + 35,-slowSpeed);
  140.      else if(code == -1)
  141.        fw2(-slowSpeed, -normalSpeed);
  142.      else if(code == 4)
  143.      {
  144.        motor_stop(ALL);
  145.        while(!(midLeftSensor.isBlack() && midRightSensor.isBlack()))
  146.          fw2(slowSpeed,0);
  147.      }
  148.      else
  149.        fw2(normalSpeed, slowSpeed);
  150.       i = 1;
  151.   }
  152.   else if(midLeftSensor.isBlack() && midRightSensor.isWhite())//curve on left
  153.   {
  154.      // glcd(2,1,"Turn Left Bit            ");
  155.      if(code == 2)
  156.        fw2(0,slowSpeed);
  157.      else if(code == 3)
  158.        fw2(0,slowSpeed + 5);
  159.      else if(code == -1)
  160.        fw2(-normalSpeed,-slowSpeed);
  161.      else if(code == 4)
  162.      {
  163.        motor_stop(ALL);
  164.        while(!(midLeftSensor.isBlack() && midRightSensor.isBlack()))
  165.          fw2(0,slowSpeed);
  166.      }
  167.      else
  168.         fw2(slowSpeed, normalSpeed);
  169.      i = 2;
  170.   }
  171.   else if(code != -1)
  172.   {
  173.       switch(i)
  174.       {
  175.         case 1: fw2(fastSpeed, slowSpeed); break;
  176.         case 2: fw2(slowSpeed , fastSpeed); break;
  177.         case 3: fw2(-normalSpeed, normalSpeed); break;
  178.         case 4: fw2(normalSpeed, -normalSpeed); break;
  179.         case 5: fw2(fastSpeed,fastSpeed);break;
  180.       }
  181.   }
  182.   else
  183.   {
  184.         fw2(-slowSpeed,-slowSpeed);
  185.   }
  186. }
  187.  
  188. //A Common Part From Methods trackCan L,R
  189. void trackCan()
  190. {
  191.   //Go on at full speed for 1 second hit a can
  192.    //glcd(1,1,"Bumping!                               ");
  193.    /*fw2(canHittingSpeed,canHittingSpeed);
  194.    while(rightSensor.isWhite());*/
  195.    
  196.    fw2(-slowSpeed,-slowSpeed);
  197.    delay(700);
  198.    
  199.    while(rightSensor.isWhite())
  200.     track(-1);
  201.   turnR90();
  202. }
  203.  
  204. void allignLine()
  205. {
  206.   motor_stop(ALL);
  207.   if(midLeftSensor.isBlack() && midRightSensor.isBlack())
  208.     return;
  209.   while(!( midLeftSensor.isBlack() && midRightSensor.isBlack() ))
  210.   {
  211.     if(leftSensor.isBlack())
  212.       fw2(-10,10);
  213.     else if(rightSensor.isBlack())
  214.       fw2(10,-10);
  215.   }
  216. }
  217.  
  218. void trackCanL()
  219. {
  220.   while(rightSensor.isWhite())
  221.     track(1);
  222.   //glcd(2,1,"Turn Left 90");
  223.   turnL90();
  224.   while(rightSensor.isWhite())
  225.     track(1);
  226.   //glcd(2,1,"Attack Can                ");
  227.   allignLine();
  228.   fw2(slowSpeed,slowSpeed);
  229.   delay(500);
  230.   trackCan();
  231. }
  232.  
  233. void trackCanL2()
  234. {
  235.   while(rightSensor.isWhite())
  236.     track(2);
  237.   //glcd(2,1,"Turn Left 90");
  238.   turnL90();
  239.   fw2(normalSpeed,normalSpeed);
  240.   delay(1000);// Gap DElay
  241.   while(rightSensor.isWhite())
  242.     track(1);
  243.   //glcd(2,1,"Attack Can                ");
  244.   allignLine();
  245.   fw2(slowSpeed,slowSpeed);
  246.   delay(500);
  247.   trackCan();
  248. }
  249.  
  250. void trackCanL3()
  251. {
  252.   while(leftSensor.isWhite() || rightSensor.isWhite())
  253.     track(3);
  254.   //glcd(2,1,"Turn Left 90");
  255.   turnL90();
  256.  
  257.   while(rightSensor.isWhite())
  258.     track(4);
  259.   //glcd(2,1,"Attack Can                ");
  260.  
  261.   while(midLeftSensor.isBlack() || midRightSensor.isBlack())
  262.   {
  263.     fw2(slowSpeed,slowSpeed);
  264.   }
  265.   motor_stop(ALL);
  266.   while(midLeftSensor.isWhite() || midRightSensor.isWhite())
  267.   {
  268.     fw2(slowSpeed,0);
  269.   }
  270.   motor_stop(ALL);
  271.   delay(500);
  272.   fw2(100,100);
  273.   while(rightSensor.isWhite());
  274.   motor_stop(ALL);
  275. }
  276.  
  277. void setup()
  278. {
  279.   setTextSize(2);
  280.   glcd(4,1,"Press OK!");
  281.   sw_OK_press();
  282.   glcdClear();
  283.   setTextSize(1);
  284.   glcdMode(1);
  285.   fw2(slowSpeed,slowSpeed);
  286.   delay(750);
  287.   trackCanL();
  288.   trackCanL2();
  289.   trackCanL3();
  290. }
  291.  
  292. int speeds = 0;
  293.  
  294. void loop()
  295. {
  296.   //debug();
  297.   //fw2(slowSpeed,slowSpeed);
  298. }
  299.  
  300. void debug()
  301. {
  302.   glcd(6,1,">DEBUG MODE");
  303.   glcd(1,1,"%d  %d  %d  %d   ",leftSensor.input(),midLeftSensor.input(),midRightSensor.input(),rightSensor.input());
  304.   glcd(3,1,"%d  ",speeds);
  305.   glcd(2,1,"%d   %d   %d   %d  ",leftSensor.isBlack(),midLeftSensor.isBlack(),midRightSensor.isBlack(),rightSensor.isBlack());
  306.   if(sw_OK())
  307.     speeds++;
  308.   else if(sw1())
  309.     speeds--;
  310.   fw2(speeds,speeds);
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement