Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.63 KB | None | 0 0
  1. #include <QTRSensors.h>
  2.  
  3. #define Kp 0 // experiment to determine this, start by something small that just makes your bot follow the line at a slow speed
  4. #define Kd 0 // experiment to determine this, slowly increase the speeds and adjust this value. ( Note: Kp < Kd)
  5.  
  6. #define rightMaxSpeed 200 // max speed of the robot
  7. #define leftMaxSpeed 200 // max speed of the robot
  8. #define rightBaseSpeed 150 // this is the speed at which the motors should spin when the robot is perfectly on the line
  9. #define leftBaseSpeed 150  // this is the speed at which the motors should spin when the robot is perfectly on the line
  10.  
  11. #define NUM_SENSORS  8     // number of sensors used
  12. #define TIMEOUT       2500  // waits for 2500 us for sensor outputs to go low
  13. #define EMITTER_PIN   2     // emitter is controlled by digital pin 2
  14.  
  15. #define mRF   9
  16. #define mRB   6  
  17. #define rightMotorPWM // UNUSED
  18. #define mLF   10
  19. #define mLB   11
  20. #define leftMotorPWM // UNUSED
  21. #define motorPower   // UNUSED
  22.  
  23. QTRSensorsRC qtr((unsigned char[]) {
  24.   5, 4, 3, 2, 0, A1, A2, A3
  25. } , NUM_SENSORS);//, TIMEOUT, EMITTER_PIN);
  26.  
  27. unsigned int sensorValues[NUM_SENSORS];
  28.  
  29. void setup()
  30. {
  31.   pinMode(mRF, OUTPUT);
  32.   pinMode(mRB, OUTPUT);
  33.   //pinMode(rightMotorPWM, OUTPUT);
  34.   pinMode(mLF, OUTPUT);
  35.   pinMode(mLB, OUTPUT);
  36.   //pinMode(leftMotorPWM, OUTPUT);
  37.   //pinMode(motorPower, OUTPUT);
  38.  
  39.   int i;
  40.   for (int i = 0; i < 100; i++) // calibrate for sometime by sliding the sensors across the line, or you may use auto-calibration instead
  41.  
  42.     /* comment this part out for automatic calibration
  43.       if ( i  < 25 || i >= 75 ) // turn to the left and right to expose the sensors to the brightest and darkest readings that may be encountered
  44.        turn_right();
  45.       else
  46.        turn_left(); */
  47.     qtr.calibrate();
  48.   delay(20);
  49.   wait();
  50.   delay(2000); // wait for 2s to position the bot before entering the main loop
  51.  
  52.   /* comment out for serial printing
  53.  
  54.     Serial.begin(9600);
  55.     for (int i = 0; i < NUM_SENSORS; i++)
  56.     {
  57.     Serial.print(qtr.calibratedMinimumOn[i]);
  58.     Serial.print(' ');
  59.     }
  60.     Serial.println();
  61.  
  62.     for (int i = 0; i < NUM_SENSORS; i++)
  63.     {
  64.     Serial.print(qtr.calibratedMaximumOn[i]);
  65.     Serial.print(' ');
  66.     }
  67.     Serial.println();
  68.     Serial.println();
  69.   */
  70. }
  71.  
  72. int lastError = 0;
  73.  
  74. void loop()
  75. {
  76.   unsigned int sensors[8];
  77.   int position = qtr.readLine(sensors,QTR_EMITTERS_ON,1); // get calibrated readings along with the line position, refer to the QTR Sensors Arduino Library for more details on line position.
  78.   int error = position - 3500;
  79.  
  80.   int motorSpeed = Kp * error + Kd * (error - lastError);
  81.   lastError = error;
  82.  
  83.   int rightMotorSpeed = rightBaseSpeed + motorSpeed;
  84.   int leftMotorSpeed = leftBaseSpeed - motorSpeed;
  85.  
  86.   if (rightMotorSpeed > rightMaxSpeed ) rightMotorSpeed = rightMaxSpeed; // prevent the motor from going beyond max speed
  87.   if (leftMotorSpeed > leftMaxSpeed ) leftMotorSpeed = leftMaxSpeed; // prevent the motor from going beyond max speed
  88.   if (rightMotorSpeed < 0) rightMotorSpeed = 0; // keep the motor speed positive
  89.   if (leftMotorSpeed < 0) leftMotorSpeed = 0; // keep the motor speed positive
  90.  
  91.   {
  92.     //digitalWrite(motorPower, HIGH); // move forward with appropriate speeds
  93.     analogWrite(mRF, rightMotorSpeed);
  94.     digitalWrite(mRB, LOW);
  95.     //analogWrite(rightMotorPWM, rightMotorSpeed);
  96.     //digitalWrite(motorPower, HIGH);
  97.     analogWrite(mLF, leftMotorSpeed);
  98.     digitalWrite(mLB, LOW);
  99.     //analogWrite(leftMotorPWM, leftMotorSpeed);
  100.   }
  101. }
  102.  
  103. void wait() {
  104.   digitalWrite(mRF, LOW);
  105.   digitalWrite(mRB, LOW);
  106.   digitalWrite(mLF, LOW);
  107.   digitalWrite(mLB, LOW);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement