Guest User

Untitled

a guest
Jun 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. int pwm_r = 5;  //PWM control for motor outputs 1 and 2 is on digital pin 5 RIGHT
  2. int pwm_l = 11;  //PWM control for motor outputs 3 and 4 is on digital pin 11 LEFT
  3. int dir_r = 12;  //dir control for motor outputs 1 and 2 is on digital pin 12 RIGHT Low is forward
  4. int dir_l = 13;  //dir control for motor outputs 3 and 4 is on digital pin 13 LEFT High is forward
  5. int triga = 10; //Trigger for the Front Sensor
  6. int trigb = 8; //Trigger for the Right Sensor
  7. int echoa = 9; // Echo for the Front Sensor
  8. int echob = 7; // Echo for the Right Sensor
  9. long sonica = 0; //Sonic a intermediate distance
  10. long sonicb = 0;// sonic b intermediate distance
  11. volatile int counterL = 0; // Counter for the Left Wheel encoder
  12. volatile int counterR = 0; // Counter for the Right Wheel encoder
  13. int Stop = 0;
  14. int full = 255;
  15. int half = 128;
  16. int quart = 64;
  17.  
  18. void setup()
  19. {
  20.   attachInterrupt(0, AddL, CHANGE); // Intettupt for the Left Wheel encoder
  21.   attachInterrupt(1, AddR, CHANGE); // Interrupt for the Right Wheel encoder
  22.   pinMode(pwm_r, OUTPUT);  //Set control PWM pins to be outputs (Speed variance)
  23.   pinMode(pwm_l, OUTPUT);
  24.   pinMode(dir_r, OUTPUT); // Set the direction pins to Output (high or low)
  25.   pinMode(dir_l, OUTPUT);
  26.   Serial.begin(9600); //Opens up a serial port for command rx and data tx
  27.   pinMode(triga, OUTPUT); // Set the trigger pins to Output (High, Low, High)
  28.   pinMode(trigb, OUTPUT);
  29.   pinMode(echoa, INPUT); // Set the echo pins to Input (pulseIn() command usage
  30.   pinMode(echob, INPUT);
  31. }
  32. void AddL(){ // Addition for the left wheel
  33.  counterL++;
  34. }
  35. void AddR(){ //Addition for the right wheel
  36.  counterR++;
  37. }
  38. void loop()
  39. {
  40.   sonica = Sonic(triga,echoa); // Collect a sample from the front sensor
  41.   sonicb = Sonic(trigb,echob); // Collect a sample from the right sensor
  42.  
  43.   if(sonica <30 || sonicb < 14){ // priority loop, the robot CANNOT crash forwards
  44.        SetWheel(pwm_r, half, dir_r, HIGH); // If the robot gets too close to colliding with
  45.        SetWheel(pwm_l, Stop, dir_l, LOW); // a wall OR it gets too close to a wall on the
  46.   }
  47.   else if(sonicb >= 14 && sonicb <= 20) { // If the Robot is within
  48.        SetWheel(pwm_r, half, dir_r, HIGH); // This 6cm distance, it will move forward until
  49.        SetWheel(pwm_l, half, dir_l, LOW);  // it falls out of the range.
  50.   }
  51.   else if(sonicb > 20 && sonica >30){
  52.        SetWheel(pwm_l, half, dir_l,LOW); // if the sensor gets too far from the wall
  53.        SetWheel(pwm_r, Stop, dir_r, HIGH); // on the right side and it is not about to collide
  54.        // with a wall on the front, turn Left to compensate
  55.   }  
  56.        delay(40);
  57.        Serial.print(counterR);
  58.        Serial.print(",");
  59.        Serial.println(counterL);
  60.        counterR = 0;
  61.        counterL = 0;
  62.  }
  63. // Methods to control the motors
  64. void SetWheel( int pwmpin, int vel, int dirpin, boolean dir){
  65.   analogWrite(pwmpin, vel);
  66.   digitalWrite(dirpin, dir);
  67. }
  68. //Method to read/write data to and from the sonic sensors, The below is setup per the HC-SR04 Datasheet
  69. long Sonic(int a, int b){
  70. long duration, cm;
  71.   digitalWrite(a, LOW);
  72.   delayMicroseconds(2);
  73.   digitalWrite(a, HIGH);
  74.   delayMicroseconds(5);
  75.   digitalWrite(a, LOW);
  76.   duration = pulseIn(b, HIGH);
  77.   delay(10);
  78.   cm = (duration/29) /2;
  79.   return cm;
  80.   }
Add Comment
Please, Sign In to add comment