JAVA 51
Untitled Guest on 19th November 2018 11:38:26 PM
  1. /**
  2.  * Show GY521 Data.
  3.  *
  4.  * Reads the serial port to get x- and y- axis rotational data from an accelerometer,
  5.  * a gyroscope, and comeplementary-filtered combination of the two, and displays the
  6.  * orientation data as it applies to three different colored rectangles.
  7.  * It gives the z-orientation data as given by the gyroscope, but since the accelerometer
  8.  * can't provide z-orientation, we don't use this data.
  9.  *
  10.  */
  11.  
  12. import processing.serial.*;
  13.  
  14. Serial  myPort;
  15. short   portIndex = 4; // promeni tova
  16. int     lf = 10;       //ASCII linefeed
  17. String  inString;      //String for testing serial communication
  18. int     calibrating;
  19.  
  20. float   dt;
  21. float   x_gyr;  //Gyroscope data
  22. float   y_gyr;
  23. float   z_gyr;
  24. float   x_acc;  //Accelerometer data
  25. float   y_acc;
  26. float   z_acc;
  27. float   x_fil;  //Filtered data
  28. float   y_fil;
  29. float   z_fil;
  30.  
  31.  
  32. void setup()  {
  33. //  size(640, 360, P3D);
  34.   size(1400, 800, P3D);
  35.   noStroke();
  36.   colorMode(RGB, 256);
  37.  
  38. //  println("in setup");
  39.   String portName = Serial.list()[portIndex];
  40.   println(Serial.list());
  41.   println(" Connecting to -> " + Serial.list()[portIndex]);
  42.   myPort = new Serial(this, portName, 38400);
  43.  // myPort.open();
  44.   myPort.clear();
  45.   myPort.bufferUntil(lf);
  46. }
  47.  
  48. void draw_rect_rainbow() {
  49.   scale(90);
  50.   beginShape(QUADS);
  51.  
  52.   fill(0, 1, 1); vertex(-1,  1.5,  0.25);
  53.   fill(1, 1, 1); vertex( 1,  1.5,  0.25);
  54.   fill(1, 0, 1); vertex( 1, -1.5,  0.25);
  55.   fill(0, 0, 1); vertex(-1, -1.5,  0.25);
  56.  
  57.   fill(1, 1, 1); vertex( 1,  1.5,  0.25);
  58.   fill(1, 1, 0); vertex( 1,  1.5, -0.25);
  59.   fill(1, 0, 0); vertex( 1, -1.5, -0.25);
  60.   fill(1, 0, 1); vertex( 1, -1.5,  0.25);
  61.  
  62.   fill(1, 1, 0); vertex( 1,  1.5, -0.25);
  63.   fill(0, 1, 0); vertex(-1,  1.5, -0.25);
  64.   fill(0, 0, 0); vertex(-1, -1.5, -0.25);
  65.   fill(1, 0, 0); vertex( 1, -1.5, -0.25);
  66.  
  67.   fill(0, 1, 0); vertex(-1,  1.5, -0.25);
  68.   fill(0, 1, 1); vertex(-1,  1.5,  0.25);
  69.   fill(0, 0, 1); vertex(-1, -1.5,  0.25);
  70.   fill(0, 0, 0); vertex(-1, -1.5, -0.25);
  71.  
  72.   fill(0, 1, 0); vertex(-1,  1.5, -0.25);
  73.   fill(1, 1, 0); vertex( 1,  1.5, -0.25);
  74.   fill(1, 1, 1); vertex( 1,  1.5,  0.25);
  75.   fill(0, 1, 1); vertex(-1,  1.5,  0.25);
  76.  
  77.   fill(0, 0, 0); vertex(-1, -1.5, -0.25);
  78.   fill(1, 0, 0); vertex( 1, -1.5, -0.25);
  79.   fill(1, 0, 1); vertex( 1, -1.5,  0.25);
  80.   fill(0, 0, 1); vertex(-1, -1.5,  0.25);
  81.  
  82.   endShape();
  83.  
  84.  
  85. }
  86.  
  87. void draw_rect(int r, int g, int b) {
  88.   scale(90);
  89.   beginShape(QUADS);
  90.  
  91.   fill(r, g, b);
  92.   vertex(-1,  1.5,  0.25);
  93.   vertex( 1,  1.5,  0.25);
  94.   vertex( 1, -1.5,  0.25);
  95.   vertex(-1, -1.5,  0.25);
  96.  
  97.   vertex( 1,  1.5,  0.25);
  98.   vertex( 1,  1.5, -0.25);
  99.   vertex( 1, -1.5, -0.25);
  100.   vertex( 1, -1.5,  0.25);
  101.  
  102.   vertex( 1,  1.5, -0.25);
  103.   vertex(-1,  1.5, -0.25);
  104.   vertex(-1, -1.5, -0.25);
  105.   vertex( 1, -1.5, -0.25);
  106.  
  107.   vertex(-1,  1.5, -0.25);
  108.   vertex(-1,  1.5,  0.25);
  109.   vertex(-1, -1.5,  0.25);
  110.   vertex(-1, -1.5, -0.25);
  111.  
  112.   vertex(-1,  1.5, -0.25);
  113.   vertex( 1,  1.5, -0.25);
  114.   vertex( 1,  1.5,  0.25);
  115.   vertex(-1,  1.5,  0.25);
  116.  
  117.   vertex(-1, -1.5, -0.25);
  118.   vertex( 1, -1.5, -0.25);
  119.   vertex( 1, -1.5,  0.25);
  120.   vertex(-1, -1.5,  0.25);
  121.  
  122.   endShape();
  123.  
  124.  
  125. }
  126.  
  127. void draw()  {
  128.  
  129.   background(0);
  130.   lights();
  131.    
  132.   // Tweak the view of the rectangles
  133.   int distance = 50;
  134.   int x_rotation = 90;
  135.  
  136.   //Show gyro data
  137.   pushMatrix();
  138.   translate(width/6, height/2, -50);
  139.   rotateX(radians(-x_gyr - x_rotation));
  140.   rotateY(radians(-y_gyr));
  141.   rotateZ(radians(-z_gyr));
  142.   draw_rect(249, 250, 50);
  143.  
  144.   popMatrix();
  145.  
  146.   //Show accel data
  147.   pushMatrix();
  148.   translate(width/2, height/2, -50);
  149.   rotateX(radians(-x_acc - x_rotation));
  150.   rotateY(radians(-y_acc));
  151.   rotateZ(radians(-z_acc));
  152.   draw_rect(56, 140, 206);
  153.   popMatrix();
  154.  
  155.   //Show combined data
  156.   pushMatrix();
  157.   translate(5*width/6, height/2, -50);
  158.   rotateX(radians(-x_fil - x_rotation));
  159.   rotateY(radians(-y_fil));
  160.   rotateZ(radians(-z_fil));
  161.   draw_rect(93, 175, 83);
  162.   popMatrix();
  163.  
  164.   textSize(24);
  165.   String accStr = "(" + (int) x_acc + ", " + (int) y_acc + ")";
  166.   String gyrStr = "(" + (int) x_gyr + ", " + (int) y_gyr + ")";
  167.   String filStr = "(" + (int) x_fil + ", " + (int) y_fil + ")";
  168.  
  169.  
  170.   fill(249, 250, 50);
  171.   text("Gyroscope", (int) width/6.0 - 60, 25);
  172.   text(gyrStr, (int) (width/6.0) - 40, 50);
  173.  
  174.   fill(56, 140, 206);
  175.   text("Accelerometer", (int) width/2.0 - 50, 25);
  176.   text(accStr, (int) (width/2.0) - 30, 50);
  177.  
  178.   fill(83, 175, 93);
  179.   text("Combination", (int) (5.0*width/6.0) - 40, 25);
  180.   text(filStr, (int) (5.0*width/6.0) - 20, 50);
  181.  
  182. }
  183.  
  184. void serialEvent(Serial p) {
  185.  
  186.   inString = (myPort.readString());
  187.   //println(inString);
  188.  
  189.   try {
  190.     // Parse the data
  191.     String[] dataStrings = split(inString, '#');
  192.     for (int i = 0; i < dataStrings.length; i++) {
  193.       String type = dataStrings[i].substring(0, 4);
  194.       String dataval = dataStrings[i].substring(4);
  195.     if (type.equals("DEL:")) {
  196.         dt = float(dataval);
  197.         /*
  198.         print("Dt:");
  199.         println(dt);
  200.         */
  201.        
  202.       } else if (type.equals("ACC:")) {
  203.         String data[] = split(dataval, ',');
  204.         x_acc = float(data[0]);
  205.         y_acc = float(data[1]);
  206.         z_acc = float(data[2]);
  207.         /*
  208.         print("Acc:");
  209.         print(x_acc);
  210.         print(",");
  211.         print(y_acc);
  212.         print(",");
  213.         println(z_acc);
  214.         */
  215.       } else if (type.equals("GYR:")) {
  216.         String data[] = split(dataval, ',');
  217.         x_gyr = float(data[0]);
  218.         y_gyr = float(data[1]);
  219.         z_gyr = float(data[2]);
  220.       } else if (type.equals("FIL:")) {
  221.         String data[] = split(dataval, ',');
  222.         x_fil = float(data[0]);
  223.         y_fil = float(data[1]);
  224.         z_fil = float(data[2]);
  225.       }
  226.     }
  227.   } catch (Exception e) {
  228.       println("Caught Exception");
  229.   }
  230. }

RSO cPaste е място за публикуване на код или текст за по-лесно отстраняване на грешки.

Влез или се Регистрай за да редактираш, изтриваш или преглеждаш хронология на твоето публикувано съдържание

Необработен текст

Влез или се Регистрирай за да редактираш или задържиш това съдържание.