Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. /*
  2. * Demo line-following code for the Pololu Zumo Robot
  3. *
  4. * This code will follow a black line on a white background, using a
  5. * PID-based algorithm. It works decently on courses with smooth, 6"
  6. * radius curves and has been tested with Zumos using 30:1 HP and
  7. * 75:1 HP motors. Modifications might be required for it to work
  8. * well on different courses or with different motors.
  9. *
  10. * https://www.pololu.com/catalog/product/2506
  11. * https://www.pololu.com
  12. * https://forum.pololu.com
  13. */
  14.  
  15. //Inlcuding the nessesary librarys and defining classes
  16. #include <Zumo32U4.h>
  17. #include <Wire.h>
  18. #include <ZumoShield.h>
  19.  
  20. Zumo32U4LineSensors lineSensor;
  21. Zumo32U4LCD lcd;
  22. Zumo32U4Motors motors;
  23. Zumo32U4ButtonA buttonA;
  24.  
  25. ZumoBuzzer buzzer;
  26. ZumoReflectanceSensorArray reflectanceSensors;
  27. ZumoMotors motors;
  28. Pushbutton button(ZUMO_BUTTON);
  29.  
  30. int lastError = 0;
  31.  
  32. //Arrays
  33. unsigned int lineSensorValues[5];
  34.  
  35. //Defining global constants
  36. const int motor_speeds = 200;
  37.  
  38. //Millis() varibles
  39. unsigned long time_now;
  40. const int terskel = 2000;
  41.  
  42.  
  43. // This is the maximum speed the motors will be allowed to turn.
  44. // (400 lets the motors go at top speed; decrease to impose a speed limit)
  45. const int MAX_SPEED = 400;
  46.  
  47. void lcd_startup(){
  48. lcd.clear();
  49. lcd.gotoXY(0,0);
  50. lcd.print("A to");
  51. lcd.gotoXY(0,1);
  52. lcd.print("calibr8");
  53. }
  54.  
  55.  
  56. void setup()
  57. {
  58.  
  59. // Play a little welcome song
  60. buzzer.play(">g32>>c32");
  61.  
  62. //calibration start
  63. lineSensor.initFiveSensors();
  64. lcd_startup();
  65. buttonA.waitForButton();
  66. calibrate_IR();
  67.  
  68. // Initialize the reflectance sensors module
  69. reflectanceSensors.init();
  70.  
  71. // Play music and wait for it to finish before we start driving.
  72. buzzer.play("L16 cdegreg4");
  73. while(buzzer.isPlaying());
  74. }
  75.  
  76. void loop()
  77. {
  78. unsigned int sensors[6];
  79.  
  80. // Get the position of the line. Note that we *must* provide the "sensors"
  81. // argument to readLine() here, even though we are not interested in the
  82. // individual sensor readings
  83. int position = reflectanceSensors.readLine(sensors);
  84.  
  85. // Our "error" is how far we are away from the center of the line, which
  86. // corresponds to position 2500.
  87. int error = position - 2500;
  88.  
  89. // Get motor speed difference using proportional and derivative PID terms
  90. // (the integral term is generally not very useful for line following).
  91. // Here we are using a proportional constant of 1/4 and a derivative
  92. // constant of 6, which should work decently for many Zumo motor choices.
  93. // You probably want to use trial and error to tune these constants for
  94. // your particular Zumo and line course.
  95. int speedDifference = error / 4 + 6 * (error - lastError);
  96.  
  97. lastError = error;
  98.  
  99. // Get individual motor speeds. The sign of speedDifference
  100. // determines if the robot turns left or right.
  101. int m1Speed = MAX_SPEED + speedDifference;
  102. int m2Speed = MAX_SPEED - speedDifference;
  103.  
  104. // Here we constrain our motor speeds to be between 0 and MAX_SPEED.
  105. // Generally speaking, one motor will always be turning at MAX_SPEED
  106. // and the other will be at MAX_SPEED-|speedDifference| if that is positive,
  107. // else it will be stationary. For some applications, you might want to
  108. // allow the motor speed to go negative so that it can spin in reverse.
  109. if (m1Speed < 0)
  110. m1Speed = 0;
  111. if (m2Speed < 0)
  112. m2Speed = 0;
  113. if (m1Speed > MAX_SPEED)
  114. m1Speed = MAX_SPEED;
  115. if (m2Speed > MAX_SPEED)
  116. m2Speed = MAX_SPEED;
  117.  
  118. motors.setSpeeds(m1Speed, m2Speed);
  119. }
  120.  
  121.  
  122.  
  123. void calibrate_IR(){ //Function that makes car spin and calibrates the IR sensors
  124. delay(1000); //Delay, so that the car doesn't begin calibrating right away
  125. motors.setSpeeds(motor_speeds, -motor_speeds); //Makes car spin
  126. time_now = millis(); //Takes timestamp
  127.  
  128. for(int i = 0; i < 180; i++){
  129. if(i > 30 && i < 120){
  130. motors.setSpeeds(motor_speeds,-motor_speeds);
  131. lineSensor.calibrate();
  132. }
  133. else{
  134. motors.setSpeeds(-motor_speeds,motor_speeds);
  135. lineSensor.calibrate();
  136. }
  137. }
  138. motors.setSpeeds(0,0);
  139. buzzer.play(">g32>>c32");
  140. }
  141.  
  142. void lcd_startup(){
  143. lcd.clear(); //Clears lcd screen
  144. lcd.gotoXY(0,0); //Sets cursor to 0,0
  145. lcd.print("A to"); //Writes text
  146. lcd.gotoXY(0,1); //Sets crisor to 1,0
  147. lcd.print("calibr8"); //Prints text
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement