16 Haziran 2016 Perşembe

Arduino Radar Projesi

Bu arduino eğitiminde size bu güzel görünümlü radar projesinin Arduino kart ve PDE(İşleme Geliştirme Ortamı) kullanarak nasıl yapılacağını göstereceğim. Daha fazla ayrıntı için aşağıdaki videoyu izleyebilir veya öğretici yazıyı okuyabilirsiniz. Bu arduino projesi için tüm ihtiyacınız olan nesneleri kontrol etmek için bir ulrasonik sensör, sensörü döndürmek için bir mini hobi servo motor ve bunları kontrol etmek için bir arduino kart.

CİHAZ YAPIMI
İlk olarak ultrasonik sensörü Arduino karta bağlamak için bir karton stand yaptım. Aşağdaki resimde göründüğü gibi katladım, yapıştırdım ve bir vida ile servo motora tutturdum.

Ayrıca sensöre bağlamak için üzerine jumper kablo lehimlenmiş olan 4 adet pin başlığı ekledim.

Son olarak servo motoru poşet lastiği kullanarak arduino karta bağladım.


DEVRE ŞEMASI

HC-SR04 ultrasonik sensörü Arduino karrt’da 10 ve 11. pinlere ve servo motoru 12. pine bağladım.

KAYNAK KODU

Şimdi Arduino karta IDE ile Arduino arasındaki etkileşimi sağlamak için bir kod yazıp yüklemek gerekiyor.

İŞTE HER BİR SATIRI AÇIKLAMALI ARDUİNO KAYNAK KODU
  1. // Includes the Servo library
  2. #include <Servo.h>.
  3. // Defines Tirg and Echo pins of the Ultrasonic Sensor
  4. const int trigPin = 10;
  5. const int echoPin = 11;
  6. // Variables for the duration and the distance
  7. long duration;
  8. int distance;
  9. Servo myServo; // Creates a servo object for controlling the servo motor
  10. void setup() {
  11. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  12. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  13. Serial.begin(9600);
  14. myServo.attach(12); // Defines on which pin is the servo motor attached
  15. }
  16. void loop() {
  17. // rotates the servo motor from 15 to 165 degrees
  18. for(int i=15;i<=165;i++){
  19. myServo.write(i);
  20. delay(30);
  21. distance = calculateDistance();// Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
  22. Serial.print(i); // Sends the current degree into the Serial Port
  23. Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  24. Serial.print(distance); // Sends the distance value into the Serial Port
  25. Serial.print("."); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  26. }
  27. // Repeats the previous lines from 165 to 15 degrees
  28. for(int i=165;i>15;i--){
  29. myServo.write(i);
  30. delay(30);
  31. distance = calculateDistance();
  32. Serial.print(i);
  33. Serial.print(",");
  34. Serial.print(distance);
  35. Serial.print(".");
  36. }
  37. }
  38. // Function for calculating the distance measured by the Ultrasonic sensor
  39. int calculateDistance(){
  40. digitalWrite(trigPin, LOW);
  41. delayMicroseconds(2);
  42. // Sets the trigPin on HIGH state for 10 micro seconds
  43. digitalWrite(trigPin, HIGH);
  44. delayMicroseconds(10);
  45. digitalWrite(trigPin, LOW);
  46. duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
  47. distance= duration*0.034/2;
  48. return distance;
  49. }

Seri Port dan veri okuyan SerialEvent() fonksiyonu kullanılarak sensör Arduino kart tarafından ölçülen açı ve mesafe değerlerini IDE içine alırız ve açı, mesafe değerlerini  iAngle ve iDistance değişkenleri içine koyarız. Bu değişkenler radar çizimi, hatlar, nesne kontrolü ve bazı metinler için kullanılıyor olacak.


Radarı çizmek için bu fonksiyonu arc() ve line()fonksiyonundan oluşan drawRadar() fonksiyonu yaptım.
void drawRadar() {
  pushMatrix();
  translate(960,1000); // moves the starting coordinats to new location
  noFill();
  strokeWeight(2);
  stroke(98,245,31);
  // draws the arc lines
  arc(0,0,1800,1800,PI,TWO_PI);
  arc(0,0,1400,1400,PI,TWO_PI);
  arc(0,0,1000,1000,PI,TWO_PI);
  arc(0,0,600,600,PI,TWO_PI);
  // draws the angle lines
  line(-960,0,960,0);
  line(0,0,-960*cos(radians(30)),-960*sin(radians(30)));
  line(0,0,-960*cos(radians(60)),-960*sin(radians(60)));
  line(0,0,-960*cos(radians(90)),-960*sin(radians(90)));
  line(0,0,-960*cos(radians(120)),-960*sin(radians(120)));
  line(0,0,-960*cos(radians(150)),-960*sin(radians(150)));
  line(-960*cos(radians(30)),0,960,0);
  popMatrix();
}



Radar boyunca hızla hareket eden bir çizgi çizmek için bu fonksiyonu drawLine() yaptım. Dönme merkezi translate() fonksiyonu ile ayarlanır ve line() fonksiyonu içinde kullanılan iAngle değişkeni her bir dereceyi çizmek için bir çizgi olarak kullanılır.
  1. void drawLine() {
  2. pushMatrix();
  3. strokeWeight(9);
  4. stroke(30,250,60);
  5. translate(960,1000); // moves the starting coordinats to new location
  6. line(0,0,950*cos(radians(iAngle)),-950*sin(radians(iAngle))); // draws the line according to the angle
  7. popMatrix();
  8. }

Tespit edilen nesnenin çizimi için drawObject() fonksiyonunu yaptım. Ultrasonik sensörden mesafe alır, piksele dönüştürür ve sensör açısı ile birlikte radara nesne çizer.
  1. void drawObject() {
  2. pushMatrix();
  3. translate(960,1000); // moves the starting coordinats to new location
  4. strokeWeight(9);
  5. stroke(255,10,10); // red color
  6. pixsDistance = iDistance*22.5; // covers the distance from the sensor from cm to pixels
  7. // limiting the range to 40 cms
  8. if(iDistance<40){
  9. // draws the object according to the angle and the distance
  10. line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),950*cos(radians(iAngle)),-950*sin(radians(iAngle)));
  11. }
  12. popMatrix();
  13. }
Ekranda metin için belirli yerlerde metinler çizen drawText() fonksiyonunu yaptım.
Bütün bu fonksiyonlara kendini herzaman tekrarlayan, çizen ve ekrana yazan ana draw() fonksiyonu denir.
Ayrıca burada hareket bulanıklığını taklit ve hareketli hattın yavaş solması için 2 parametreli fill() fonksiyonunu kullandım.
  1. void draw() {
  2. fill(98,245,31);
  3. textFont(orcFont);
  4. // simulating motion blur and slow fade of the moving line
  5. noStroke();
  6. fill(0,4);
  7. rect(0, 0, width, 1010);
  8. fill(98,245,31); // green color
  9. // calls the functions for drawing the radar
  10. drawRadar();
  11. drawLine();
  12. drawObject();
  13. drawText();
  14. }
İşte radarın son görünümü.


İşte tamamlanmış Arduino Kaynak Kodu:
  1. import processing.serial.*; // imports library for serial communication
  2. import java.awt.event.KeyEvent; // imports library for reading the data from the serial port
  3. import java.io.IOException;
  4. Serial myPort; // defines Object Serial
  5. // defubes variables
  6. String angle="";
  7. String distance="";
  8. String data="";
  9. String noObject;
  10. float pixsDistance;
  11. int iAngle, iDistance;
  12. int index1=0;
  13. int index2=0;
  14. PFont orcFont;
  15. void setup() {
  16. size (1920, 1080);
  17. smooth();
  18. myPort = new Serial(this,"COM4", 9600); // starts the serial communication
  19. myPort.bufferUntil('.'); // reads the data from the serial port up to the character '.'. So actually it reads this: angle,distance.
  20. orcFont = loadFont("OCRAExtended-30.vlw");
  21. }
  22. void draw() {
  23. fill(98,245,31);
  24. textFont(orcFont);
  25. // simulating motion blur and slow fade of the moving line
  26. noStroke();
  27. fill(0,4);
  28. rect(0, 0, width, 1010);
  29. fill(98,245,31); // green color
  30. // calls the functions for drawing the radar
  31. drawRadar();
  32. drawLine();
  33. drawObject();
  34. drawText();
  35. }
  36. void serialEvent (Serial myPort) { // starts reading data from the Serial Port
  37. // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data".
  38. data = myPort.readStringUntil('.');
  39. data = data.substring(0,data.length()-1);
  40. index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1"
  41. angle= data.substring(0, index1); // read the data from position "0" to position of the variable index1 or thats the value of the angle the Arduino Board sent into the Serial Port
  42. distance= data.substring(index1+1, data.length()); // read the data from position "index1" to the end of the data pr thats the value of the distance
  43. // converts the String variables into Integer
  44. iAngle = int(angle);
  45. iDistance = int(distance);
  46. }
  47. void drawRadar() {
  48. pushMatrix();
  49. translate(960,1000); // moves the starting coordinats to new location
  50. noFill();
  51. strokeWeight(2);
  52. stroke(98,245,31);
  53. // draws the arc lines
  54. arc(0,0,1800,1800,PI,TWO_PI);
  55. arc(0,0,1400,1400,PI,TWO_PI);
  56. arc(0,0,1000,1000,PI,TWO_PI);
  57. arc(0,0,600,600,PI,TWO_PI);
  58. // draws the angle lines
  59. line(-960,0,960,0);
  60. line(0,0,-960*cos(radians(30)),-960*sin(radians(30)));
  61. line(0,0,-960*cos(radians(60)),-960*sin(radians(60)));
  62. line(0,0,-960*cos(radians(90)),-960*sin(radians(90)));
  63. line(0,0,-960*cos(radians(120)),-960*sin(radians(120)));
  64. line(0,0,-960*cos(radians(150)),-960*sin(radians(150)));
  65. line(-960*cos(radians(30)),0,960,0);
  66. popMatrix();
  67. }
  68. void drawObject() {
  69. pushMatrix();
  70. translate(960,1000); // moves the starting coordinats to new location
  71. strokeWeight(9);
  72. stroke(255,10,10); // red color
  73. pixsDistance = iDistance*22.5; // covers the distance from the sensor from cm to pixels
  74. // limiting the range to 40 cms
  75. if(iDistance<40){
  76. // draws the object according to the angle and the distance
  77. line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),950*cos(radians(iAngle)),-950*sin(radians(iAngle)));
  78. }
  79. popMatrix();
  80. }
  81. void drawLine() {
  82. pushMatrix();
  83. strokeWeight(9);
  84. stroke(30,250,60);
  85. translate(960,1000); // moves the starting coordinats to new location
  86. line(0,0,950*cos(radians(iAngle)),-950*sin(radians(iAngle))); // draws the line according to the angle
  87. popMatrix();
  88. }
  89. void drawText() { // draws the texts on the screen
  90. pushMatrix();
  91. if(iDistance>40) {
  92. noObject = "Out of Range";
  93. }
  94. else {
  95. noObject = "In Range";
  96. }
  97. fill(0,0,0);
  98. noStroke();
  99. rect(0, 1010, width, 1080);
  100. fill(98,245,31);
  101. textSize(25);
  102. text("10cm",1180,990);
  103. text("20cm",1380,990);
  104. text("30cm",1580,990);
  105. text("40cm",1780,990);
  106. textSize(40);
  107. text("Object: " + noObject, 240, 1050);
  108. text("Angle: " + iAngle +" °", 1050, 1050);
  109. text("Distance: ", 1380, 1050);
  110. if(iDistance<40) {
  111. text(" " + iDistance +" cm", 1400, 1050);
  112. }
  113. textSize(25);
  114. fill(98,245,60);
  115. translate(961+960*cos(radians(30)),982-960*sin(radians(30)));
  116. rotate(-radians(-60));
  117. text("30°",0,0);
  118. resetMatrix();
  119. translate(954+960*cos(radians(60)),984-960*sin(radians(60)));
  120. rotate(-radians(-30));
  121. text("60°",0,0);
  122. resetMatrix();
  123. translate(945+960*cos(radians(90)),990-960*sin(radians(90)));
  124. rotate(radians(0));
  125. text("90°",0,0);
  126. resetMatrix();
  127. translate(935+960*cos(radians(120)),1003-960*sin(radians(120)));
  128. rotate(radians(-30));
  129. text("120°",0,0);
  130. resetMatrix();
  131. translate(940+960*cos(radians(150)),1018-960*sin(radians(150)));
  132. rotate(radians(-60));
  133. text("150°",0,0);
  134. popMatrix();
  135. }

Herhangi bir ekran çözünürlüğüne sığdırmak için kodun yeni güncellenmiş versiyonu:

Sadece size() fonksiyonu içindeki değerleri ekran çözünürlüğünüz ile değiştirin.
  1. /* Arduino Radar Project
  2. *
  3. * Updated version. Fits any screen resolution!
  4. * Just change the values in the size() function,
  5. * with your screen resolution.
  6. *
  7. * by Dejan Nedelkovski,
  8. * www.HowToMechatronics.com
  9. *
  10. */
  11. import processing.serial.*; // imports library for serial communication
  12. import java.awt.event.KeyEvent; // imports library for reading the data from the serial port
  13. import java.io.IOException;
  14. Serial myPort; // defines Object Serial
  15. // defubes variables
  16. String angle="";
  17. String distance="";
  18. String data="";
  19. String noObject;
  20. float pixsDistance;
  21. int iAngle, iDistance;
  22. int index1=0;
  23. int index2=0;
  24. PFont orcFont;
  25. void setup() {
  26. size (1920, 1080); // ***CHANGE THIS TO YOUR SCREEN RESOLUTION***
  27. smooth();
  28. myPort = new Serial(this,"COM4", 9600); // starts the serial communication
  29. myPort.bufferUntil('.'); // reads the data from the serial port up to the character '.'. So actually it reads this: angle,distance.
  30. orcFont = loadFont("OCRAExtended-30.vlw");
  31. }
  32. void draw() {
  33. fill(98,245,31);
  34. textFont(orcFont);
  35. // simulating motion blur and slow fade of the moving line
  36. noStroke();
  37. fill(0,4);
  38. rect(0, 0, width, height-height*0.065);
  39. fill(98,245,31); // green color
  40. // calls the functions for drawing the radar
  41. drawRadar();
  42. drawLine();
  43. drawObject();
  44. drawText();
  45. }
  46. void serialEvent (Serial myPort) { // starts reading data from the Serial Port
  47. // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data".
  48. data = myPort.readStringUntil('.');
  49. data = data.substring(0,data.length()-1);
  50. index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1"
  51. angle= data.substring(0, index1); // read the data from position "0" to position of the variable index1 or thats the value of the angle the Arduino Board sent into the Serial Port
  52. distance= data.substring(index1+1, data.length()); // read the data from position "index1" to the end of the data pr thats the value of the distance
  53. // converts the String variables into Integer
  54. iAngle = int(angle);
  55. iDistance = int(distance);
  56. }
  57. void drawRadar() {
  58. pushMatrix();
  59. translate(width/2,height-height*0.074); // moves the starting coordinats to new location
  60. noFill();
  61. strokeWeight(2);
  62. stroke(98,245,31);
  63. // draws the arc lines
  64. arc(0,0,(width-width*0.0625),(width-width*0.0625),PI,TWO_PI);
  65. arc(0,0,(width-width*0.27),(width-width*0.27),PI,TWO_PI);
  66. arc(0,0,(width-width*0.479),(width-width*0.479),PI,TWO_PI);
  67. arc(0,0,(width-width*0.687),(width-width*0.687),PI,TWO_PI);
  68. // draws the angle lines
  69. line(-width/2,0,width/2,0);
  70. line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30)));
  71. line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60)));
  72. line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90)));
  73. line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120)));
  74. line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150)));
  75. line((-width/2)*cos(radians(30)),0,width/2,0);
  76. popMatrix();
  77. }
  78. void drawObject() {
  79. pushMatrix();
  80. translate(width/2,height-height*0.074); // moves the starting coordinats to new location
  81. strokeWeight(9);
  82. stroke(255,10,10); // red color
  83. pixsDistance = iDistance*((height-height*0.1666)*0.025); // covers the distance from the sensor from cm to pixels
  84. // limiting the range to 40 cms
  85. if(iDistance<40){
  86. // draws the object according to the angle and the distance
  87. line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),(width-width*0.505)*cos(radians(iAngle)),-(width-width*0.505)*sin(radians(iAngle)));
  88. }
  89. popMatrix();
  90. }
  91. void drawLine() {
  92. pushMatrix();
  93. strokeWeight(9);
  94. stroke(30,250,60);
  95. translate(width/2,height-height*0.074); // moves the starting coordinats to new location
  96. line(0,0,(height-height*0.12)*cos(radians(iAngle)),-(height-height*0.12)*sin(radians(iAngle))); // draws the line according to the angle
  97. popMatrix();
  98. }
  99. void drawText() { // draws the texts on the screen
  100. pushMatrix();
  101. if(iDistance>40) {
  102. noObject = "Out of Range";
  103. }
  104. else {
  105. noObject = "In Range";
  106. }
  107. fill(0,0,0);
  108. noStroke();
  109. rect(0, height-height*0.0648, width, height);
  110. fill(98,245,31);
  111. textSize(25);
  112. text("10cm",width-width*0.3854,height-height*0.0833);
  113. text("20cm",width-width*0.281,height-height*0.0833);
  114. text("30cm",width-width*0.177,height-height*0.0833);
  115. text("40cm",width-width*0.0729,height-height*0.0833);
  116. textSize(40);
  117. text("Object: " + noObject, width-width*0.875, height-height*0.0277);
  118. text("Angle: " + iAngle +" °", width-width*0.48, height-height*0.0277);
  119. text("Distance: ", width-width*0.26, height-height*0.0277);
  120. if(iDistance<40) {
  121. text(" " + iDistance +" cm", width-width*0.225, height-height*0.0277);
  122. }
  123. textSize(25);
  124. fill(98,245,60);
  125. translate((width-width*0.4994)+width/2*cos(radians(30)),(height-height*0.0907)-width/2*sin(radians(30)));
  126. rotate(-radians(-60));
  127. text("30°",0,0);
  128. resetMatrix();
  129. translate((width-width*0.503)+width/2*cos(radians(60)),(height-height*0.0888)-width/2*sin(radians(60)));
  130. rotate(-radians(-30));
  131. text("60°",0,0);
  132. resetMatrix();
  133. translate((width-width*0.507)+width/2*cos(radians(90)),(height-height*0.0833)-width/2*sin(radians(90)));
  134. rotate(radians(0));
  135. text("90°",0,0);
  136. resetMatrix();
  137. translate(width-width*0.513+width/2*cos(radians(120)),(height-height*0.07129)-width/2*sin(radians(120)));
  138. rotate(radians(-30));
  139. text("120°",0,0);
  140. resetMatrix();
  141. translate((width-width*0.5104)+width/2*cos(radians(150)),(height-height*0.0574)-width/2*sin(radians(150)));
  142. rotate(radians(-60));
  143. text("150°",0,0);
  144. popMatrix();
  145. }

Hiç yorum yok:

Yorum Gönder