Advertisement
Guest User

#6

a guest
Apr 18th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.25 KB | None | 0 0
  1. /********************************************************
  2.  ****************** CYberNeticS *************************
  3.  *******************************************************/
  4.  
  5. #include <Wire.h>
  6. #include <MMA8653.h>
  7. #include <Arduino.h>
  8. #include <Servo.h>
  9. #include "Studuino.h"
  10.  
  11. boolean REACH_END = false;
  12.  
  13. int LEFT_SPEED = 255;
  14. int RIGHT_SPEED = 210;
  15. int TURNING_OFFSET = 100;
  16.  
  17. int MIDDLE_VALUE_1;
  18. int MAX_VALUE_1 = 10000;
  19. int MIN_VALUE_1 = 0;
  20.  
  21. int MIDDLE_VALUE_2;
  22. int MAX_VALUE_2 = 10000;
  23. int MIN_VALUE_2 = 0;
  24.  
  25. int SF2;
  26. int SF3;
  27. int SF4;
  28. int SF5;
  29.  
  30. int SB1;
  31. int SB2;
  32.  
  33. int TRAVELLING_STRAIGHT;
  34.  
  35. Studuino board;
  36.  
  37. /********************************************************
  38.  ********************************************************
  39.  ********* Setup ( Initialize all tha things ) **********
  40.  ********************************************************
  41.  *******************************************************/
  42.  
  43. void setup()
  44. {
  45.   Serial.begin(9600); // Start Serial Output
  46.   board.InitDCMotorPort(PORT_M1); // Initialize Port M1 ( for motor )
  47.   board.InitDCMotorPort(PORT_M2); // Initialize Port M2 ( for motor )
  48.  
  49.   board.InitSensorPort(PORT_A0, PIDPUSHSWITCH); // Initialize Port A0 ( for button )
  50.  
  51.   board.InitSensorPort(PORT_A6, PIDIRPHOTOREFLECTOR); // Initialize Port A6 ( for IR sensor )
  52.   board.InitSensorPort(PORT_A7, PIDIRPHOTOREFLECTOR); // Initialize Port A7 ( for IR sensor )
  53. }
  54.  
  55. /********************************************************
  56.  ********************************************************
  57.  ******** Functions ( By alphabetical Order ) ***********
  58.  ********************************************************
  59.  *******************************************************/
  60.  
  61. void calibrateSensors()
  62. {
  63.   for (int i = 0; i < 30; i++)
  64.   {
  65.     if (i >= 10 && i < 30)
  66.     {
  67.       spinRight();
  68.       calibrateSensorValues();
  69.     }
  70.     else
  71.     {
  72.       spinLeft();
  73.     }
  74.     delay(100);
  75.   }
  76.  
  77.   while ( SF4 == 0 )
  78.   {
  79.     spinLeft();
  80.     getSensorValues();
  81.   }
  82.  
  83.   moveForward(LEFT_SPEED,RIGHT_SPEED);
  84.   delay(500);
  85. }
  86.  
  87. //*****************************************************
  88.  
  89. void calibrateSensorValues()
  90. {
  91.   int a = board.GetIRPhotoreflectorValue(PORT_A7);
  92.   int b = analogRead(A2);
  93.  
  94.   if ( a < MAX_VALUE_2 )
  95.   {
  96.     MAX_VALUE_1 = a;
  97.   }
  98.   if ( a > MIN_VALUE_2 )
  99.   {
  100.     MIN_VALUE_1 = a;
  101.   }
  102.  
  103.   if ( b < MAX_VALUE_2 )
  104.   {
  105.     MAX_VALUE_2 = b;
  106.   }
  107.   if ( b > MIN_VALUE_2 )
  108.   {
  109.     MIN_VALUE_2 = b;
  110.   }
  111.  
  112.   MIDDLE_VALUE_1 = ( MIN_VALUE_1 + MAX_VALUE_1 ) / 2;
  113.   MIDDLE_VALUE_2 = ( MIN_VALUE_2 + MAX_VALUE_2 ) / 2;
  114.   Serial.println(MIDDLE_VALUE_1);
  115.   Serial.println(MIDDLE_VALUE_2);
  116. }
  117.  
  118. //*****************************************************
  119.  
  120. void countdown()
  121. {
  122.   delay(3000); // Temporally solution until we get a LED
  123. }
  124.  
  125. //*****************************************************
  126.  
  127. void moveForward(int leftSpeed, int rightSpeed)
  128. {
  129.   board.DCMotorPower(PORT_M1, leftSpeed);
  130.   board.DCMotorControl(PORT_M1, REVERSE);
  131.  
  132.   board.DCMotorPower(PORT_M2, rightSpeed);
  133.   board.DCMotorControl(PORT_M2, REVERSE);
  134. }
  135.  
  136. //*****************************************************
  137.  
  138. void finishingMove()
  139. {
  140.  
  141. }
  142.  
  143. //*****************************************************
  144.  
  145. void getSensorValues()
  146. {
  147.  
  148.   if ( board.GetIRPhotoreflectorValue(PORT_A6) < MIDDLE_VALUE_1 )
  149.   {
  150.     SB1 = 1;
  151.   }
  152.   else
  153.   {
  154.     SB1 = 0;
  155.   }
  156.  
  157.   if ( board.GetIRPhotoreflectorValue(PORT_A7) < MIDDLE_VALUE_1 )
  158.   {
  159.     SB2 = 1;
  160.   }
  161.   else
  162.   {
  163.     SB2 = 0;
  164.   }
  165.  
  166.   if ( analogRead(A2) > MIDDLE_VALUE_2 )
  167.   {
  168.     SF2 = 1;
  169.   }
  170.   else
  171.   {
  172.     SF2 = 0;
  173.   }
  174.  
  175.   if ( analogRead(A3) > MIDDLE_VALUE_2 )
  176.   {
  177.     SF3 = 1;
  178.   }
  179.   else
  180.   {
  181.     SF3 = 0;
  182.   }
  183.  
  184.   if ( analogRead(A4) > MIDDLE_VALUE_2 )
  185.   {
  186.     SF4 = 1;
  187.   }
  188.   else
  189.   {
  190.     SF4 = 0;
  191.   }
  192.  
  193.   if ( analogRead(A5) > MIDDLE_VALUE_2 )
  194.   {
  195.     SF5 = 1;
  196.   }
  197.   else
  198.   {
  199.     SF5 = 0;
  200.   }
  201. }
  202.  
  203. //*****************************************************
  204.  
  205. void solveMaze()
  206. {
  207.   while(REACH_END == false)
  208.   {
  209.     getSensorValues();
  210.  
  211.     /*** Stabalizer ***/
  212.     if ((SF2 == 1 || SF5 == 1) && SF3 == 1 && SF4 == 1 && SB1 == 0 && SB2 == 0) // If robot is straight
  213.     {
  214.       moveForward(LEFT_SPEED,RIGHT_SPEED);
  215.       TRAVELLING_STRAIGHT++;
  216.     }
  217.  
  218.     if (SF5 == 0 && SF2 == 1 && SB1 == 0 && SB2 == 0) // If robot is slightly to the left
  219.     {
  220.       moveForward(LEFT_SPEED , RIGHT_SPEED - TURNING_OFFSET);
  221.     }
  222.  
  223.     if (SF5 == 1 && SF2 == 0 && SB1 == 0 && SB2 == 0) // If robot is slightly to the left
  224.     {
  225.       moveForward(LEFT_SPEED - TURNING_OFFSET , RIGHT_SPEED);
  226.     }
  227.  
  228.     if (TRAVELLING_STRAIGHT > 2000 && SF2 == 0 && SF3 == 0 && SF4 == 0 && SF5 == 0 && SB1 == 0 && SB2 == 0)
  229.     {
  230.       delay(100);
  231.       getSensorValues();
  232.       if (SF2 == 0 && SF3 == 0 && SF4 == 0 && SF5 == 0 && SB1 == 0 && SB2 == 0)
  233.       {
  234.         moveForward(0,0);
  235.         delay(100000);
  236.       }
  237.     }
  238.  
  239.  
  240.     if ( SB1 == 1 || SB2 == 1 )
  241.     {
  242.       delay(200);
  243.       getSensorValues();
  244.       TRAVELLING_STRAIGHT == 0;
  245.  
  246.       if (SB1 == 1 && SB2 == 1)
  247.       {
  248.         if (SF5 == 1 || SF4 == 1 || SF3 == 1 || SF2 == 1) // If Robot detects it as + junction
  249.         {
  250.           spinLeft();
  251.           getSensorValues();
  252.           while (SF2 == 0)
  253.           {
  254.             spinLeft();
  255.             getSensorValues();
  256.           }
  257.         }
  258.  
  259.         if (SF5 == 0 || SF4 == 0 || SF3 == 0 || SF2 == 0) // If robot detects it as upright T junction
  260.         {
  261.           spinLeft();
  262.           getSensorValues();
  263.           delay(1000);
  264.           while (SF2 == 0)
  265.           {
  266.             spinLeft();
  267.             getSensorValues();
  268.           }
  269.         }
  270.       }
  271.  
  272.  
  273.  
  274.       if (SB1 == 1 && SB2 == 0)
  275.       {
  276.         if (SF5 == 0 || SF4 == 0 || SF3 == 0 || SF2 == 0) // If robot detects it as left turn
  277.         {
  278.           spinLeft();
  279.           getSensorValues();
  280.           while (SF2 == 0)
  281.           {
  282.             spinLeft();
  283.             getSensorValues();
  284.           }
  285.         }
  286.  
  287.         if (SF5 == 1 || SF4 == 1 || SF3 == 1 || SF2 == 1) // If robot detects it as T junction ( rotated clockwise )
  288.         {
  289.           spinLeft();
  290.           getSensorValues();
  291.           while (SF2 == 0)
  292.           {
  293.             spinLeft();
  294.             getSensorValues();
  295.           }
  296.         }
  297.       }
  298.  
  299.       if (SB1 == 0 && SB2 == 1)
  300.       {
  301.         if (SF5 == 1 || SF4 == 1 || SF3 == 1 || SF2 == 1) // If robot detects it as T junction ( rotated counterclockwise )
  302.         {
  303.           delay(500);
  304.         }
  305.  
  306.         if (SF5 == 0 || SF4 == 0 || SF3 == 0 || SF2 == 0) // If robot detects it as right turn
  307.         {
  308.           spinRight();
  309.           getSensorValues();
  310.           while (SF5 == 0)
  311.           {
  312.             spinRight();
  313.             getSensorValues();
  314.           }
  315.         }
  316.       }
  317.     }
  318.   }
  319. }
  320.  
  321.  
  322.  
  323. //*****************************************************
  324.  
  325. void showRawSensorValues()
  326. {
  327.   Serial.println(analogRead(A2));
  328.   Serial.println(analogRead(A3));
  329.   Serial.println(analogRead(A4));
  330.   Serial.println(analogRead(A5));
  331.   Serial.println("");
  332.   Serial.println(board.GetIRPhotoreflectorValue(PORT_A6));
  333.   Serial.println(board.GetIRPhotoreflectorValue(PORT_A7));
  334.   Serial.println("");
  335.   delay(500);
  336. }
  337.  
  338.  
  339. //*****************************************************
  340.  
  341. void spinLeft()
  342. {
  343.   board.DCMotorPower(PORT_M1, LEFT_SPEED);
  344.   board.DCMotorControl(PORT_M1, NORMAL);
  345.  
  346.   board.DCMotorPower(PORT_M2, RIGHT_SPEED);
  347.   board.DCMotorControl(PORT_M2, REVERSE);
  348.  
  349. }
  350.  
  351. //*****************************************************
  352.  
  353. void spinRight()
  354. {
  355.   board.DCMotorPower(PORT_M1, LEFT_SPEED);
  356.   board.DCMotorControl(PORT_M1, REVERSE);
  357.  
  358.   board.DCMotorPower(PORT_M2, RIGHT_SPEED);
  359.   board.DCMotorControl(PORT_M2, NORMAL);
  360.  
  361. }
  362.  
  363. /********************************************************
  364.  ********************************************************
  365.  ********** Loop ( Where the fun begins ) ***************
  366.  ********************************************************
  367.  *******************************************************/
  368.  
  369. void loop()
  370. {
  371.   int A0 = board.GetPushSwitchValue(PORT_A0);
  372.  
  373.   if ( A0 == 0 )
  374.   {
  375.     countdown();
  376.     calibrateSensors();
  377.     solveMaze();
  378.     finishingMove();
  379.   }
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement