Guest User

Untitled

a guest
Jun 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. //Pin defenitions and other essential values
  2. #define pwm_r 5  //PWM control for motor outputs 1 and 2 is on digital pin 5 RIGHT
  3. #define pwm_l 11  //PWM control for motor outputs 3 and 4 is on digital pin 11 LEFT
  4. #define dir_r 12  //dir control for motor outputs 1 and 2 is on digital pin 12 RIGHT Low is forward
  5. #define dir_l 13  //dir control for motor outputs 3 and 4 is on digital pin 13 LEFT High is forward
  6. #define PINGA 9 //Trigger and echo for the Front Sensor
  7. #define PINGB 8 // Echo for the Front Sensor
  8. #define BASE_SPEED 128 // base speed of the robot
  9. #define SETPOINT 10 //setpoint
  10.  
  11. // Variables
  12. long sonica = 0; //Sonic a intermediate distance
  13. long sonicb = 0;// sonic b intermediate distance
  14. volatile int counterL = 0; // Counter for the Left Wheel encoder
  15. volatile int counterR = 0; // Counter for the Right Wheel encoder
  16. double rspeed = 128;
  17. double lspeed = 128;
  18. double SetPoint = 10;
  19. double ratio = 1;
  20. double Kp = .25;
  21. double diff = 0;
  22. double spchange = 0;
  23. double Max = 0;
  24. double Min = 0;
  25. double max_dist = 25;
  26. double integral = 0;
  27. double Ki = .25;
  28.  
  29. void setup()
  30. {
  31.   attachInterrupt(0, AddL, CHANGE); // Intettupt for the Left Wheel encoder
  32.   attachInterrupt(1, AddR, CHANGE); // Interrupt for the Right Wheel encoder
  33.   pinMode(5, OUTPUT);  //Set control PWM pins to be outputs Speed variance
  34.   pinMode(11, OUTPUT);
  35.   pinMode(dir_r, OUTPUT); // Set the direction pins to Output (high or low)
  36.   pinMode(dir_l, OUTPUT);
  37.   Serial.begin(9600); //Opens up a serial port for command rx and data tx
  38.   Max = BASE_SPEED+((max_dist - SetPoint)*Kp)*BASE_SPEED;
  39.   Min = -Max;
  40. }
  41. void AddL(){ // Addition for the left wheel
  42.  counterL++;
  43. }
  44. void AddR(){ //Addition for the right wheel
  45.  counterR++;
  46. }
  47. void loop()
  48. {
  49.  sonica = Sonic(PINGA); // Collect a sample from the front sensor
  50.  sonicb = Sonic(PINGB); // Collect a sample from the right sensor
  51.  
  52.  if (sonica >= 35){  
  53.     if(SETPOINT == sonicb){
  54.         ratio = 1;
  55.     }
  56.     else if (SETPOINT != sonicb){
  57.         diff = SETPOINT - sonicb;
  58.         integral +=diff;
  59.         ratio = (diff*Kp)+(integral*Ki);
  60.     }
  61.     spchange = BASE_SPEED*ratio;
  62.    
  63.     rspeed += spchange;
  64.     lspeed -= spchange;
  65.     rspeed = map(rspeed, Min, Max, 0, 255);
  66.     lspeed = map(lspeed, Min, Max, 0, 255);
  67.    
  68.     if (ratio == 1){
  69.        rspeed = BASE_SPEED;
  70.        lspeed = BASE_SPEED;      
  71.     }
  72.   }
  73.  else{
  74.    rspeed = BASE_SPEED;
  75.    lspeed = 0;
  76. }
  77. if( sonicb <= 6){
  78.   rspeed = BASE_SPEED;
  79.   lspeed = 0;
  80. }
  81. if(rspeed >255)
  82.   rspeed = 255;
  83. if(lspeed > 255)
  84.   lspeed = 255;
  85. if(rspeed < 0)
  86.   rspeed = 0;
  87. if(lspeed < 0)
  88.   lspeed = 0;
  89.  
  90.  SetWheel(pwm_r, rspeed, dir_r, LOW);
  91.  SetWheel(pwm_l, lspeed, dir_l, HIGH);
  92.   delay(100);
  93.  
  94.  //Serial.print(counterR);
  95.  //Serial.print(",");
  96.  //Serial.println(counterL);
  97. // Serial.print(sonica);
  98.  //Serial.print(",");
  99.  //Serial.println(sonicb);
  100. // Serial.print(",");
  101.  Serial.print(rspeed);
  102.  Serial.print(",");
  103.  Serial.println(lspeed);
  104.  counterR = 0;
  105.  counterL = 0;
  106. // rspeed = BASE_SPEED;
  107. // lspeed = BASE_SPEED;
  108.  delay(100);
  109. }
  110. // Methods to control the motors
  111. void SetWheel( int pwmpin, int vel, int dirpin, boolean dir){
  112.   analogWrite(pwmpin, vel);
  113.   digitalWrite(dirpin, dir);
  114. }
  115. //Method to read/write data to and from the sonic sensors, The below is setup per the PING))) Datasheet
  116. long Sonic(int a){
  117. long duration, cm;
  118.   pinMode(a, OUTPUT);
  119.   digitalWrite(a, LOW);
  120.   delayMicroseconds(2);
  121.   digitalWrite(a, HIGH);
  122.   delayMicroseconds(5);
  123.   digitalWrite(a, LOW);
  124.   pinMode(a, INPUT);
  125.   duration = pulseIn(a, HIGH);
  126.   delay(10);
  127.   cm = (duration/29) /2;
  128.   return cm;
  129.   }
Add Comment
Please, Sign In to add comment