Advertisement
OllyR

Cyclops 8

Dec 30th, 2012
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.42 KB | None | 0 0
  1.  
  2. /*
  3. Cyclops8: As Cyclops6, moves towards light. Due to laziness, Forward movement run time depends on angle of light, however left and right turns are reduced to simply 200 m/s rather than calculated.
  4. At the start of the loop, it checks whether any of the front edge sensor pins are over the edge, and if so, backs one wheel up to bring it back on.
  5. If both edge sensors are on the desk, then carries out the usual sweep, and movement, however while moving continues to monitor the edge sensors.
  6. If either edge sensor pass over the edge, the motors are stopped for the remainder of the movement time, although the clock keeps going until the timer has expired.
  7. .... Or least thats what i think it does. It certainly appears to work from this angle.
  8.  */
  9.  
  10. #include <AFMotor.h>       // Adafruit MotorShield
  11. #include <VarSpeedServo.h> // VarSpeedServo library gives more options, but variable speed isnt used yet
  12.  
  13. const int PR = A1; // connect PR to A1
  14. int Array[125];    // array to store sweep values
  15.  
  16. VarSpeedServo Hori;
  17. int HoriPos = 90; //  centre is 90. hardware modififed to prevent clashes. 30 (left) to 150 (right)
  18. int CWHoriDir = 0;
  19. int CCWHoriDir = 0;
  20.  
  21. VarSpeedServo Vert;
  22. int VertPos = 90; // 130 = level, 0 is back on itself, 30 is straight up
  23. int UpVertDir = 0;
  24. int DownVertDir = 0;
  25.  
  26. AF_DCMotor motorL(3, MOTOR12_1KHZ);  
  27. AF_DCMotor motorR(2, MOTOR12_1KHZ);
  28. const int RLED = 13; // indicator LED: 13 and 2 are the only properly unused digi pins on the motorshield.
  29. const int LLED = 2;  // indicator LED
  30.  
  31. int VertDelay;    // integer used to calculate "distance" to travel. Shallow angle on Vert indicates distance to light is greater,and increases tim
  32. int HoriDelay;    // Steep (vertical) angle indicates closer to light and therefore moves in small steps to help prevent over run
  33.  
  34. // front sensors and integers for calibration
  35. const int LeftPR = A2;    // pin that the sensor is attached to
  36. const int CentrePR = A3;    // pin that the sensor is attached to
  37. const int RightPR = A4;    // pin that the sensor is attached to
  38. int LeftValue = 0;         // the sensor value
  39. int LeftMin = 1023;        // minimum sensor value
  40. int LeftMax = 0;           // maximum sensor value
  41. int CentreValue = 0;         // the sensor value
  42. int CentreMin = 1023;        // minimum sensor value
  43. int CentreMax = 0;           // maximum sensor value
  44. int RightValue = 0;         // the sensor value
  45. int RightMin = 1023;        // minimum sensor value
  46. int RightMax = 0;           // maximum sensor value
  47.  
  48. unsigned long previous = 0;
  49. unsigned long interval = 0;
  50.  
  51. void setup() {
  52.  
  53.   Serial.begin(9600);  // Serial at 9600 bps
  54.   Serial.println("Hello!");
  55.   Hori.attach(9);           // 9 is "Servo2"
  56.   Vert.attach(10);          // 10 is "Servo1"
  57.   motorL.setSpeed(250);     // 0 is stop, 255 is full speed:
  58.   motorR.setSpeed(250);     //
  59.   pinMode (RLED,OUTPUT);    // indicator LEDS. HIGH is on, LOW is off
  60.   pinMode (LLED,OUTPUT);
  61.   randomSeed(analogRead(0));
  62.  
  63.   Serial.println("Calibrate...");
  64.   {  
  65.     while (millis() < random(3000, 7000)) { // For between 3 and 7 seconds, Calibrate (random time allows for random starting facing direction)
  66.  
  67.       //Spin on the spot
  68.       motorL.run(FORWARD);  
  69.       motorR.run(BACKWARD);  
  70.       digitalWrite(LLED, LOW);
  71.       digitalWrite(RLED, HIGH);
  72.  
  73.       //Calibrate
  74.       LeftValue = analogRead(LeftPR);
  75.       // record the maximum sensor value
  76.       if (LeftValue > LeftMax) {
  77.         LeftMax = LeftValue;
  78.       }
  79.       // record the minimum sensor value
  80.       if (LeftValue < LeftMin) {
  81.         LeftMin = LeftValue;
  82.       }
  83.       CentreValue = analogRead(CentrePR);
  84.       // record the maximum sensor value
  85.       if (CentreValue > CentreMax) {
  86.         CentreMax = CentreValue;
  87.       }
  88.       // record the minimum sensor value
  89.       if (CentreValue < CentreMin) {
  90.         CentreMin = CentreValue;
  91.       }
  92.       RightValue = analogRead(RightPR);
  93.       // record the maximum sensor value
  94.       if (RightValue > RightMax) {
  95.         RightMax = RightValue;
  96.       }
  97.       // record the minimum sensor value
  98.       if (RightValue < RightMin) {
  99.         RightMin = RightValue;
  100.       }
  101.     }
  102.     motorL.run(RELEASE);  // End of Calibration
  103.     motorR.run(RELEASE);  
  104.     digitalWrite(LLED, LOW);
  105.     digitalWrite(RLED, LOW);
  106.  
  107.     BlindSpot();         //BlindSpot Check
  108.   }
  109. }
  110.  
  111. void loop() {
  112.  
  113.   HoriPos = constrain (HoriPos, 30, 150); // constrain servos, Just in case.
  114.   VertPos = constrain (VertPos, 30, 130);
  115.  
  116.   //LEFT SENSOR
  117.   LeftValue = analogRead(LeftPR);
  118.   LeftValue = map(LeftValue, LeftMin, LeftMax, 0, 100);
  119.   LeftValue = constrain(LeftValue, 0, 200);
  120.   // RIGHT SENSOR
  121.   RightValue = analogRead(RightPR);
  122.   RightValue = map(RightValue, RightMin, RightMax, 0, 100);
  123.   RightValue = constrain(RightValue, 0, 200);
  124.  
  125.   unsigned long current = millis(); // timer for reversing at edge.
  126.   previous = current; // timer for reversing at edge.
  127.   interval = 500; // fixed half second of single wheel backing up.
  128.  
  129.  
  130.   if (LeftValue > 100){ // if the Left edge sensor is over an edge, back up Right wheel
  131.     while (millis() - previous < interval){  
  132.       motorL.run(RELEASE);  
  133.       motorR.run(BACKWARD);  
  134.       digitalWrite(LLED, HIGH);
  135.       digitalWrite(RLED, LOW);
  136.     }
  137.     motorL.run(RELEASE);  
  138.     motorR.run(RELEASE);  
  139.     digitalWrite(LLED, LOW);
  140.     digitalWrite(RLED, LOW);
  141.   }
  142.  
  143.   if (RightValue > 100){ // if the Right edge sensor is over an edge, back up Left wheel
  144.     while (millis() - previous < interval){  
  145.       motorL.run(BACKWARD);  
  146.       motorR.run(RELEASE);  
  147.       digitalWrite(LLED, LOW);
  148.       digitalWrite(RLED, HIGH);
  149.     }
  150.     motorL.run(RELEASE);  
  151.     motorR.run(RELEASE);  
  152.     digitalWrite(LLED, LOW);
  153.     digitalWrite(RLED, LOW);
  154.   }    
  155.  
  156.   else { // Assuming both edge sensors are on the table:
  157.  
  158.     HoriSweep();        //Horizontal Scan
  159.     VertSweep();        // Vertical Scan
  160.     VertDelay = map (VertPos, 30, 130, 500, 10000);
  161.  
  162.     if((HoriPos >= 70) && (HoriPos <= 110)){
  163.       interval = VertDelay;
  164.     }
  165.     else{
  166.       interval = 200;
  167.     }
  168.    unsigned long current = millis(); // timer for motors running
  169.    previous = current;
  170.  
  171.     if(VertPos < 40){     //Stop if under light
  172.       motorL.run(RELEASE);  
  173.       motorR.run(RELEASE);  
  174.       digitalWrite(LLED, LOW);
  175.       digitalWrite(RLED, LOW);
  176.       delay (99999999); // lazy mans ending
  177.     }
  178.  
  179.     else {
  180.       Serial.print("Interval: ");
  181.       Serial. println (interval);
  182.  
  183.       while (millis() - previous < interval){   // while the movement timer allows:
  184.  
  185.         //LEFT SENSOR // monitor the Left Sensor
  186.         LeftValue = analogRead(LeftPR);
  187.         LeftValue = map(LeftValue, LeftMin, LeftMax, 0, 100);
  188.         LeftValue = constrain(LeftValue, 0, 200);
  189.         // RIGHT SENSOR // monitor the Right Sensor
  190.         RightValue = analogRead(RightPR);
  191.         RightValue = map(RightValue, RightMin, RightMax, 0, 100);
  192.         RightValue = constrain(RightValue, 0, 200);
  193.  
  194.         // RIGHT // Spin right if servo sweep has detected light to the right
  195.         if((HoriPos > 110) && (HoriPos <= 150)){
  196.           motorL.run(FORWARD);  
  197.           motorR.run(BACKWARD);  
  198.           digitalWrite(LLED, LOW);
  199.           digitalWrite(RLED, HIGH);
  200.         }
  201.         //LEFT // Spin left if servo sweep has detected light to the left
  202.         if((HoriPos < 70) && (HoriPos >= 30)){
  203.           motorL.run(BACKWARD);  
  204.           motorR.run(FORWARD);  
  205.           digitalWrite(LLED, HIGH);
  206.           digitalWrite(RLED, LOW);
  207.         }
  208.      
  209.         // FORWARD run forward: Window of 20 degrees in the centre results in running forward
  210.           if((HoriPos >= 70) && (HoriPos <= 110)&& (RightValue < 100) && (LeftValue < 100)){ // Both edge sensors must be over desk
  211.           motorL.run(FORWARD);  
  212.           motorR.run(FORWARD);  
  213.           digitalWrite(LLED, HIGH);
  214.           digitalWrite(RLED, HIGH);
  215.         }
  216.         if((HoriPos >= 70) && (HoriPos <= 110) && ((RightValue > 100) || (LeftValue > 100))){ // One of edge sensore not over desk, stop for the remainder of the movement timers allowance
  217.           motorL.run(RELEASE);  
  218.           motorR.run(RELEASE);  
  219.           digitalWrite(LLED, LOW);
  220.           digitalWrite(RLED, LOW);
  221.         }
  222.       }
  223.       {
  224.         motorL.run(RELEASE);  // This loop has ended, stop the motors and go back to the begining.
  225.         motorR.run(RELEASE);  
  226.         digitalWrite(LLED, LOW);
  227.         digitalWrite(RLED, LOW);
  228.       }
  229.     }
  230.   }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement