Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. #include <NewPing.h>
  2. #include <Servo.h>
  3. #include <I2Cdev.h>
  4. #include <MPU6050.h>
  5.  
  6. int right1 = 4; // in1 of right tire
  7. int rightEN = 3; // enA of right tire
  8. int right2 = 2; // in2 of right tire
  9. int left1 = 5; // in1 of left tire
  10. int leftEN = 6; // enA of left tire
  11. int left2 = 7; // in2 of right tire
  12.  
  13. int rightLED = 8;
  14. int leftLED = 9;
  15.  
  16. int trig = 12; // trigger PIN
  17. int echo = 11; // echo PIN
  18. int max_dist = 300; // max distance of ultrasonic
  19. int usReading; // reading of ultrasonic
  20. int usReadingThreshold = 25; // max distance between car and walls
  21. NewPing US (trig,echo,max_dist); // NewPing initialization
  22. Servo servoMotor; // Servo initialization
  23.  
  24. int MAXSPDR = 125; // maximum speed of right tire
  25. int MAXSPDL = 140; // maximum speed of left tire
  26. int leftDistance;
  27. int rightDistance;
  28.  
  29. int16_t gz;
  30. MPU6050 accelgyro; // SCL -> A5 ------ SDA -> A4
  31. float theta;
  32. float oldTheta;
  33. unsigned int oldtime = 0;
  34. unsigned int now;
  35.  
  36. void setup() {
  37.  
  38. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  39. Wire.begin();
  40. #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  41. Fastwire::setup(400, true);
  42. #endif
  43.  
  44. Serial.println("Initializing I2C devices...");
  45. accelgyro.initialize();
  46.  
  47. int offset = 0;
  48. bool calibrated = false;
  49.  
  50. while (calibrated == false) {
  51. accelgyro.setZGyroOffset(offset);
  52. for (int i = 0; i < 100; i++) {
  53. gz = accelgyro.getRotationZ() + gz;
  54. }
  55. gz = gz / 100;
  56.  
  57. if ((gz > -1) && (gz < 1)) {
  58. calibrated = true;
  59. Serial.print("\nGyro offset: ");
  60. Serial.println(offset);
  61. delay(2000);
  62. gz = 0;
  63. }
  64. if (gz <= -2) {
  65. offset++;
  66. }
  67. if (gz >= 2) {
  68. offset--;
  69. }
  70. gz = 0;
  71.  
  72. }
  73.  
  74. pinMode(rightLED,OUTPUT);
  75. pinMode(leftLED,OUTPUT);
  76. pinMode(right1,OUTPUT);
  77. pinMode(right2,OUTPUT);
  78. pinMode(rightEN,OUTPUT);
  79. pinMode(left1,OUTPUT);
  80. pinMode(left2,OUTPUT);
  81. pinMode(leftEN,OUTPUT);
  82. Serial.begin(115200);
  83. servoMotor.attach(10); // Servo PIN
  84. servoMotor.write(86); // sets initial servo angle
  85. delay(200);
  86.  
  87. moveForward(); // moves the car forward
  88. }
  89.  
  90. void loop() {
  91. delay(10);
  92. usReading = US.ping_cm();
  93. printReading();
  94. if(usReading < usReadingThreshold && usReading !=0){ // checks if the reading is below the threshold
  95. moveStop();
  96. char vacantDirection = checkDirections(); // checks vacant directions left & right
  97. carTurn(vacantDirection); // turns the car in the vacant direction
  98. }
  99. }
  100.  
  101. void printReading(){
  102. Serial.print("Distance is currently ");
  103. Serial.println(usReading);
  104. }
  105.  
  106. void moveRightTire(int pwm){
  107. digitalWrite(right1, HIGH);
  108. digitalWrite(right2, LOW);
  109. analogWrite(rightEN, pwm); // moves the right tire forward at pwm speed
  110. }
  111.  
  112. void reverseRightTire(int pwm){
  113. digitalWrite(right1, LOW);
  114. digitalWrite(right2, HIGH);
  115. analogWrite(rightEN, pwm); // moves the right tire forward at pwm speed in opposite direction
  116. }
  117.  
  118. void moveLeftTire(int pwm){
  119. digitalWrite(left1, HIGH);
  120. digitalWrite(left2, LOW);
  121. analogWrite(leftEN, pwm); // moves the left tire forward at pwm speed
  122. }
  123.  
  124. void reverseLeftTire(int pwm){
  125. digitalWrite(left1, LOW);
  126. digitalWrite(left2, HIGH);
  127. analogWrite(leftEN, pwm); // moves the left tire forward at pwm speed in opposite direction
  128. }
  129.  
  130. void moveForward(){
  131. moveRightTire(MAXSPDR); // moves right tire at max speed
  132. moveLeftTire(MAXSPDL); // moves left tire at max speed
  133. }
  134.  
  135. void moveStop(){
  136. moveRightTire(0); // stops right tire
  137. moveLeftTire(0); // stops left tire
  138. }
  139.  
  140. char checkDirections(){
  141. servoMotor.write(180); //looks left
  142. delay(200);
  143. leftDistance = US.ping_cm(); //sets the left distance to the current reading
  144. servoMotor.write(0); //looks right
  145. delay(350);
  146. rightDistance = US.ping_cm(); //sets the right distance to the current reading
  147. servoMotor.write(86);
  148. delay(200);
  149. if(rightDistance>leftDistance){
  150. return 'R';
  151. }else{
  152. return 'L';
  153. }
  154. }
  155.  
  156. void carTurn(char directn){
  157. theta = 0;
  158. if(directn == 'R'){
  159. digitalWrite(rightLED,HIGH);
  160. reverseRightTire(MAXSPDR);
  161. moveLeftTire(MAXSPDL);
  162.  
  163. while (theta > -87) {
  164. getTheta();
  165. }
  166.  
  167. moveForward();
  168.  
  169. digitalWrite(rightLED,LOW);
  170.  
  171. }else if(directn == 'L'){
  172. digitalWrite(leftLED,HIGH);
  173. moveRightTire(MAXSPDR);
  174. reverseLeftTire(MAXSPDL);
  175.  
  176. while (theta < 87) {
  177. getTheta();
  178. }
  179. moveForward();
  180.  
  181. digitalWrite(leftLED,LOW);
  182. }
  183. }
  184.  
  185. void getTheta() {
  186. gz = accelgyro.getRotationZ();
  187.  
  188. // display tab-separated accel/gyro x/y/z values
  189. now = millis();
  190.  
  191. if (oldtime > now) {
  192. oldtime = now;
  193. Serial.print("Angle: \t");
  194. Serial.print(theta);
  195. Serial.print("\t");
  196. }
  197. else {
  198. theta = ((((float)gz / 131)) * (((float)now - oldtime) / 1000)) + theta;
  199. oldtime = now;
  200. if (abs(theta - oldTheta) <= 0.01) {
  201. theta = oldTheta;
  202. }
  203. oldTheta = theta;
  204. Serial.print("Angle: \t");
  205. Serial.print(theta);
  206. Serial.print("\t");
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement