Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.78 KB | None | 0 0
  1. /*
  2. Radar Screen Visualisation for SRF-05
  3. Maps out an area of what the SRF-05 sees from a top down view.
  4. Takes and displays 2 readings, one left to right and one right to left.
  5. Displays an average of the 2 readings
  6. Displays motion alert if there is a large difference between the 2 values.
  7. */
  8. import processing.serial.*;         // Importa la libreria serie
  9. Serial myPort;                      // Declara el puerto serie
  10. float x, y;                         // Variable para almacenar las coordenadas
  11. int radius = 350;                   // Establece el radio de los objetos
  12. int w = 300;                        // Establece un valor de anchura arbitraria
  13. int degree = 0;                     // Varible para la posición del servo en grados
  14. int value = 0;                      // Variable para el valor del sensor
  15. int motion = 0;                     // Variable para indicar el sentido del barrido del servo
  16. int[] newValue = new int[181];      // Matriz para almacenar cada valor nuevo de cada posición del servo
  17. int[] oldValue = new int[181];      // Matriz para almacenar cada valor previo de cada posición del servo
  18. PFont myFont;                       // Variable para configuración de fuente
  19. int radarDist = 0;                  // set value to configure Radar distance labels
  20. int firstRun = 0;                   // value to ignore triggering motion on the first 2 servo sweeps
  21. int lf = 10;                        // ASCII retorno de carro
  22. void setup(){
  23. size(750, 450);                     // Establece el tamaño de la ventana
  24. background (0);                     // Establece a negro del fondo de la ventana
  25. myFont = createFont("verdana", 12); // Parametros de la fuente
  26. textFont(myFont);                   // Establece los parametros de la fuente
  27.  
  28. println(Serial.list());              // Lista todos los puertos series
  29. myPort = new Serial(this, Serial.list()[0], 9600); // Establece un puerto serie
  30. myPort.bufferUntil(lf);              // Almacena en el bufer hasta llegar un retorno de carro
  31. }
  32. /* draw the screen */
  33. void draw(){
  34.   fill(0);                              // set the following shapes to be black
  35.   noStroke();                           // set the following shapes to have no outline
  36.   ellipse(radius, radius, 750, 750);    // draw a circle with a width/ height = 750 with its center position (x and y) set by the radius
  37.   rectMode(CENTER);                     // set the following rectangle to be drawn around its center
  38.   rect(350,402,800,100);                // draw rectangle (x, y, width, height)
  39.   if (degree >= 179) {                  // if at the far right then set motion = 1/ true we're about to go right to left
  40.   motion = 1;                           // this changes the animation to run right to left
  41.   }
  42.   if (degree <= 1) {                    // if servo at 0 degrees then we're about to go left to right
  43.   motion = 0;                           // this sets the animation to run left to right
  44.   }
  45.   /* setup the radar sweep */
  46.   /*
  47.   We use trigonmetry to create points around a circle.
  48.   So the radius plus the cosine of the servo position converted to radians
  49.   Since radians 0 start at 90 degrees we add 180 to make it start from the left
  50.   Adding +1 (i) each time through the loops to move 1 degree matching the one degree of servo movement
  51.   cos is for the x left to right value and sin calculates the y value
  52.   since its a circle we plot our lines and vertices around the start point for everything will always be the center.
  53.   */
  54.   strokeWeight(7);                      // set the thickness of the lines
  55.   if (motion == 0) {                    // if going left to right
  56.     for (int i = 0; i <= 20; i++) {     // draw 20 lines with fading colour each 1 degree further round than the last
  57.       stroke(0, (10*i), 0);             // set the stroke colour (Red, Green, Blue) base it on the the value of i
  58.       line(radius, radius, radius + cos(radians(degree+(180+i)))*w, radius + sin(radians(degree+(180+i)))*w); // line(start x, start y, end x, end y)
  59.     }
  60.   } else {                              // if going right to left
  61.     for (int i = 20; i >= 0; i--) {     // draw 20 lines with fading colour
  62.       stroke(0,200-(10*i), 0);          // using standard RGB values, each between 0 and 255
  63.       line(radius, radius, radius + cos(radians(degree+(180+i)))*w, radius + sin(radians(degree+(180+i)))*w);
  64.     }
  65.   }
  66.   /* Setup the shapes made from the sensor values */
  67.   noStroke();                           // no outline
  68.   /* first sweep */
  69.   fill(0,50,0);                         // set the fill colour of the shape (Red, Green, Blue)
  70.   beginShape();                         // start drawing shape
  71.     for (int i = 0; i < 180; i++) {     // for each degree in the array
  72.       x = radius + cos(radians((180+i)))*((oldValue[i])); // create x coordinate
  73.       y = radius + sin(radians((180+i)))*((oldValue[i])); // create y coordinate
  74.       vertex(x, y);                     // plot vertices
  75.     }
  76.   endShape();                           // end shape
  77.   /* second sweep */
  78.   fill(0,110,0);
  79.   beginShape();
  80.     for (int i = 0; i < 180; i++) {
  81.       x = radius + cos(radians((180+i)))*(newValue[i]);
  82.       y = radius + sin(radians((180+i)))*(newValue[i]);
  83.       vertex(x, y);
  84.     }
  85.   endShape();
  86.   /* average */
  87.   fill(0,170,0);
  88.   beginShape();
  89.     for (int i = 0; i < 180; i++) {
  90.       x = radius + cos(radians((180+i)))*((newValue[i]+oldValue[i])/2); // create average
  91.       y = radius + sin(radians((180+i)))*((newValue[i]+oldValue[i])/2);
  92.       vertex(x, y);
  93.     }
  94.   endShape();
  95.   /* if after first 2 sweeps, highlight motion with red circle*/
  96.   if (firstRun >= 360) {
  97.     stroke(150,0,0);
  98.     strokeWeight(1);
  99.     noFill();
  100.       for (int i = 0; i < 180; i++) {
  101.         if (oldValue[i] - newValue[i] > 35 || newValue[i] - oldValue[i] > 35) {
  102.           x = radius + cos(radians((180+i)))*(newValue[i]);
  103.           y = radius + sin(radians((180+i)))*(newValue[i]);
  104.           ellipse(x, y, 10, 10);
  105.         }
  106.       }
  107.   }
  108.   /* set the radar distance rings and out put their values, 50, 100, 150 etc.. */
  109.   for (int i = 0; i <=6; i++){
  110.     noFill();
  111.     strokeWeight(1);
  112.     stroke(0, 255-(30*i), 0);
  113.     ellipse(radius, radius, (100*i), (100*i));
  114.     fill(0, 100, 0);
  115.     noStroke();
  116.     text(Integer.toString(radarDist+50), 380, (305-radarDist), 50, 50);
  117.     radarDist+=50;
  118.   }
  119.   radarDist = 0;
  120.   /* draw the grid lines on the radar every 30 degrees and write their values 180, 210, 240 etc.. */
  121.   for (int i = 0; i <= 6; i++) {
  122.     strokeWeight(1);
  123.     stroke(0, 55, 0);
  124.     line(radius, radius, radius + cos(radians(180+(30*i)))*w, radius + sin(radians(180+(30*i)))*w);
  125.     fill(0, 55, 0);
  126.     noStroke();
  127.     if (180+(30*i) >= 300) {
  128.       text(Integer.toString(180+(30*i)), (radius+10) + cos(radians(180+(30*i)))*(w+10), (radius+10) + sin(radians(180+(30*i)))*(w+10), 25,50);
  129.     } else {
  130.       text(Integer.toString(180+(30*i)), radius + cos(radians(180+(30*i)))*w, radius + sin(radians(180+(30*i)))*w, 60,40);
  131.     }
  132.   }
  133.   /* Write information text and values. */
  134.   noStroke();
  135.   fill(0);
  136.   rect(350,402,800,100);
  137.   fill(0, 100, 0);
  138.   text("Grados: "+Integer.toString(degree), 100, 380, 100, 50);           // use Integet.toString to convert numeric to string as text() only outputs strings
  139.   text("Distancia: "+Integer.toString(value), 100, 400, 100, 50);         // text(string, x, y, width, height)
  140.   text("www.tuelectronica.es", 540, 380, 250, 50);
  141.   fill(0);
  142.   rect(70,60,150,100);
  143.   fill(0, 100, 0);
  144.   text("Screen Key:", 100, 50, 150, 50);
  145.   fill(0,50,0);
  146.   rect(30,53,10,10);
  147.   text("Primer barrido", 115, 70, 150, 50);
  148.   fill(0,110,0);
  149.   rect(30,73,10,10);
  150.   text("Segundo barrido", 115, 90, 150, 50);
  151.   fill(0,170,0);
  152.   rect(30,93,10,10);
  153.   text("Promedio", 115, 110, 150, 50);
  154.   noFill();
  155.   stroke(150,0,0);
  156.   strokeWeight(1);
  157.   ellipse(29, 113, 10, 10);
  158.   fill(150,0,0);
  159.   text("Movimiento", 115, 130, 150, 50);
  160. }
  161. /* get values from serial port */
  162. void serialEvent (Serial myPort) {
  163.   String xString = myPort.readStringUntil(lf);  // read the serial port until a new line
  164.    
  165.     if (xString != null) {  // if theres data in between the new lines
  166.         xString = trim(xString); // get rid of any whitespace just in case
  167.         String getX = xString.substring(1, xString.indexOf("V")); // get the value of the servo position
  168.         String getV = xString.substring(xString.indexOf("V")+1, xString.length()); // get the value of the sensor reading
  169.         degree = Integer.parseInt(getX); // set the values to variables
  170.         value = Integer.parseInt(getV);
  171.         oldValue[degree] = newValue[degree]; // store the values in the arrays.
  172.         newValue[degree] = value;
  173.         /* sets a counter to allow for the first 2 sweeps of the servo */
  174.         firstRun++;
  175.         if (firstRun > 360) {
  176.         firstRun = 360; // keep the value at 360
  177.         }
  178.   }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement