viernes, 10 de mayo de 2019

  • PROYECTO RADAR ARDUINO

  • Pablo Ruiz Ponsoda y Jaime Páramo Benítez
Inspiración para el proyecto aquí


VIDEO de cómo funciona.


HARDWARE:
Arduino-Radar-Circuit-Schematics
Placa Arduino, sensor de ultrasonidos y servo motor.

SOFTWARE:
Hemos utilizado una combinación de C++ y Arduino.
La parte de Arduino controla el servo motor y el ultrasonidos y la parte de C++ lo muestra en pantalla en forma de RADAR.
El programa de Arduino:
  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. }
  50. void drawRadar() {
  51. pushMatrix();
  52. translate(960,1000); // moves the starting coordinats to new location
  53. noFill();
  54. strokeWeight(2);
  55. stroke(98,245,31);
  56. // draws the arc lines
  57. arc(0,0,1800,1800,PI,TWO_PI);
  58. arc(0,0,1400,1400,PI,TWO_PI);
  59. arc(0,0,1000,1000,PI,TWO_PI);
  60. arc(0,0,600,600,PI,TWO_PI);
  61. // draws the angle lines
  62. line(-960,0,960,0);
  63. line(0,0,-960*cos(radians(30)),-960*sin(radians(30)));
  64. line(0,0,-960*cos(radians(60)),-960*sin(radians(60)));
  65. line(0,0,-960*cos(radians(90)),-960*sin(radians(90)));
  66. line(0,0,-960*cos(radians(120)),-960*sin(radians(120)));
  67. line(0,0,-960*cos(radians(150)),-960*sin(radians(150)));
  68. line(-960*cos(radians(30)),0,960,0);
  69. popMatrix();
  70. }
Programa de C++:
  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. }