Advertisement
ossipee

Birdmun_teaches_oss

Feb 23rd, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.29 KB | None | 0 0
  1.    
  2.  
  3.     // All code learned and modded from Jayden "ChickenParmis" fantastic site
  4.     // http://www.duino-robotics.com/
  5.     // So Lets Make Robots! here's the place to do it http://letsmakerobots.com/
  6.     //many thanks to all I welcome all idea to improve ossipee
  7.      
  8.     // add some libraries for ping and servo, OddBot recomends I just do a function,
  9.     //planning on it
  10.     #include <Servo.h>
  11.     #include <NewPing.h>
  12.      
  13.     //some programming etiquette suggests constants, which #defines are, should be
  14.     //all CAPS. It assists in reading the code. Also, having values defined at/near
  15.     //the top of your program lets you make adjustments without having to search for
  16.     //the "magic numbers" hidden in your code.
  17.     #define USTrigger 7       //picks ping trigger pin
  18.     #define USEcho 8          //picks ping recievor pin, signal in
  19.     #define MaxDistance 100   // sets pings maximum range
  20.     #define MINDISTANCE 20    //sets min distance for avoidance
  21.     #define LED 13            // pin 13 for included led on board unused right now
  22.     #define LEFTREV 0         // left servo full reverse
  23.     #define LEFTFWD 180       // left servo full forward
  24.     #define LEFTSTOP 90       // left servo stop
  25.     #define RIGHTREV 180      // right servo full reverse
  26.     #define RIGHTFWD 0        // right servo full forward
  27.     #define RIGHTSTOP 90      // right servo stop
  28.     #define DEADBAND 5        // value above/below center that will back robot out of corners
  29.     #define SWEEPLEFT 130     // sweep servo face left
  30.     #define SWEEPRIGHT 50     // sweep servo face right
  31.     #define SWEEPCENTER 90    // center sweep servo
  32.      
  33.     //Create Left Servo object, lets arduino program know it has a left servo
  34.     Servo leftServo;
  35.     //Create Right Servo object, lets arduino program know it has right servo
  36.     Servo rightServo;
  37.     //Create sweep Servo object,
  38.     //lets arduino program know it has a servo to sweep ping
  39.     Servo servo;
  40.     //Create ultrasonic object, arduino now know's it's got a ping
  41.     NewPing sonar(USTrigger, USEcho, MaxDistance);
  42.      
  43.     //Below we are creating unsigned integer variables which we will use later on in
  44.     //the code. They are unsigned as they will only have postive values
  45.     //if you sign them they can have positive and negitive values, thanks 6677,
  46.     //Birdmun also says that just using positive ones saves program space
  47.     //this is going to be time between sending and getting signal back from ping,
  48.     //gonna use it to make a distance
  49.     //~ unsigned int duration; //no longer required
  50.     //***variables moved to local functions
  51.     //this is going to be the distance in cm we get from ping
  52.     //~ unsigned int distance; //doesn't need to be global
  53.     //~ //this is the number we get from a ping reading looking straight ahead
  54.     //~ unsigned int FrontDistance;
  55.     //~ //this is the number we get from a ping reading looking to the left,
  56.     //~ //check servo is really looking left
  57.     //~ unsigned int LeftDistance;
  58.     //~ // same as above but looking right
  59.     //~ unsigned int RightDistance;
  60.      
  61.     void setup() {          //This part of the program runs 1 time at start up
  62.       Serial.begin(9600); //required if you are going to send data via serial
  63.       leftServo.attach(9);   //Left servo connected to pin 9
  64.       leftServo.write(LEFTSTOP);  //Write the neutral position to that servo
  65.       rightServo.attach(10); //Right servo connected to pin 10
  66.       rightServo.write(RIGHTSTOP);  //Write the neutral position to that servo
  67.       servo.attach (6);      // puts ping servo on pin 6
  68.     }
  69.      
  70.     //This block repeats itself while the Arduino is turned on
  71.     void loop() {
  72.       //this is the number we get from a ping reading looking straight ahead
  73.       unsigned int FrontDistance;
  74.       servo.write(SWEEPCENTER); //position the ping servo to face ahead could have done above but like it here                
  75.       //Set the variable FrontDistance to
  76.       //the value of the distance returned from the scan function
  77.       FrontDistance = scan();
  78.       Serial.println("Front distance = ");
  79.       Serial.print(FrontDistance);
  80.       //If there is nothing infront of the robot within 20cm or the distance value
  81.       //is 0 (which for the newping libary means no ping was returned) then.
  82.       if(FrontDistance > MINDISTANCE || FrontDistance == 0) {
  83.          moveRobot(100, 100, 500); //move robot forward for 0.5 seconds
  84.          //moveRobot(100, 100, 0); //delay may not be needed try both ways
  85.       } else { //Else (if there is something infront of the robot within 40cm) then
  86.         moveRobot(0, 0, 0); //Go to the moveStop function
  87.         navigate(); //Go to navigate function
  88.       }
  89.     }
  90.      
  91.     //procedures can accept values. They must be declared properly.
  92.     void moveRobot(int leftServoSpeed, int rightServoSpeed, int dlay) {
  93.       //the sevos are opposite on bot thus forwardon 1 = reverse on other
  94.       //~ rightServo.write(180);                      //reverses right servo
  95.       //~ leftServo.write(0);                         //reverses left servo
  96.       int leftSpeed, rightSpeed;
  97.       if (leftServoSpeed == 0) {
  98.         leftSpeed = LEFTSTOP; //stop
  99.       } else if (leftServoSpeed < 0) {
  100.         leftSpeed = map(leftServoSpeed, -100, -1, LEFTREV, LEFTSTOP - 1); //reverse
  101.       } else {
  102.         leftSpeed = map(leftServoSpeed, 1, 100, LEFTSTOP + 1, LEFTFWD); //forward
  103.       }
  104.       if (rightServoSpeed == 0) {
  105.         rightSpeed = RIGHTSTOP; //stop
  106.       } else if (rightServoSpeed < 0) {
  107.         rightSpeed = map(rightServoSpeed, -100, -1, RIGHTREV, RIGHTSTOP -1); //rev
  108.       } else {
  109.         rightSpeed = map(rightServoSpeed, 1, 100, RIGHTSTOP +1, RIGHTFWD); //forward
  110.       }
  111.       rightServo.write(rightSpeed);
  112.       leftServo.write(leftSpeed);
  113.       delay(dlay);
  114.     }
  115.      
  116.     //functions can accept values as well as return values. The unsigned int means
  117.     //it will return an unsigned int to the calling function. void means it returns
  118.     //nothing.
  119.     unsigned int scan() { //This function determines the distance things are away from the ultrasonic sensor
  120.       unsigned int distance; //moved here. Does not need to be a global variable.
  121.       Serial.println("");
  122.       Serial.println("Scanning");
  123.       //~ Time = sonar.ping();
  124.       //~ distance = Time / US_ROUNDTRIP_CM;
  125.       //newping lib will return cm, in, mSec
  126.       //http://playground.arduino.cc/Code/NewPing#Methods
  127.       distance = sonar.ping_cm();
  128.       delay(500); //why the delay?
  129.       return distance;
  130.     }
  131.     void navigate() {
  132.       //this is the number we get from a ping reading looking to the left,
  133.       //check servo is really looking left
  134.       unsigned int LeftDistance;
  135.       // same as above but looking right
  136.       unsigned int RightDistance;
  137.         Serial.println("There's an obstacle!");
  138.         servo.write(SWEEPLEFT); //Move the servo to the left (my little servos didn't like going to 180 so I played around with the value until it worked nicely)
  139.         delay(1000); //delay(1000) = 1000 millaseconds, 1 second
  140.         //scan(); //Go to the scan function
  141.         LeftDistance = scan(); //Set the variable LeftDistance to the distance on the left
  142.         Serial.println("Left distance = ");
  143.         Serial.print(LeftDistance);
  144.         servo.write(SWEEPRIGHT);                           //Move the servo to the right
  145.         delay(1000);                                       //Wait a second for the servo to get there
  146.         //scan();                                           //Go to the scan function called above
  147.         RightDistance = scan();                         //Set the variable RightDistance to the distance on the right
  148.         Serial.println("Right distance = ");
  149.         Serial.print(RightDistance);
  150.         if(abs(RightDistance - LeftDistance) < DEADBAND) { //DEADBAND can be changed above
  151.           //~ moveBackward();                                  //Go to the moveBackward function
  152.           //~ delay(200);                                      //Pause the program for 200 milliseconds to let the robot reverse
  153.           //~ moveRight();                                     //Go to the moveRight function
  154.           //~ delay(100);                                      //Pause the program for 200 milliseconds to let the robot turn right
  155.           moveRobot(-100, -100, 200); //reverse for 200 mS
  156.           moveRobot(100, -100, 200); //rotate right for 200 mS
  157.         } else if(RightDistance < LeftDistance) {                 //If the distance on the right is less than that on the left then...
  158.           //~ moveLeft();                                      //Go to the moveLeft function
  159.           //~ delay(100);                                      //Pause the program for half a second to let the robot turn
  160.           moveRobot(-100, 100, 500); //rotate left for 500 mS
  161.         } else { // you have already checked for the other two possibilities
  162.         //~ else if(LeftDistance < RightDistance)             //Else if the distance on the left is less than that on the right then...
  163.         //~ {
  164.          //~ moveRight();                                     //Go to the moveRight function
  165.          //~ delay(100);                                      //Pause the program for half a second to let the robot turn
  166.           moveRobot(100, -100, 500); //rotate right for 500 mS
  167.         }
  168.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement