Main.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import lejos.geom.Line;
  2. import lejos.geom.Rectangle;
  3. import lejos.robotics.mapping.LineMap;
  4. import lejos.robotics.navigation.Pose;
  5. import java.io.*;
  6. import java.util.ArrayList;
  7. public class Main {
  8. private LineMap map;
  9. public Main() {
  10. Line[] lines = {
  11. new Line(0f, 0f, 155f, 0f),
  12. new Line(155f, 0f, 110f, 135f),
  13. new Line(110f, 135f, 0f, 132f),
  14. new Line(0f, 132f, 0f, 0f)
  15. };
  16. Rectangle bounds = new Rectangle(0, 0, 1189, 841);
  17. map = new LineMap(lines, bounds);
  18. }
  19. public double expectedSonarRead(Pose p, double angle, int cone) {
  20. /**************** MODELO DO SONAR *******************/
  21. Pose tmppose = new Pose(p.getX(), p.getY(), p.getHeading());
  22. float mindist = Float.POSITIVE_INFINITY;
  23. for (int angulo=-cone/2; angulo <= cone/2; angulo++) {
  24. tmppose.setHeading((float) (p.getHeading() - angulo + angle));
  25. float dist = map.range(tmppose);
  26. if (dist > 0 && dist < mindist)
  27. mindist = dist;
  28. }
  29. return Math.min(mindist, 255);
  30. }
  31. public static void fileCalc (String filename) {
  32. try {
  33. File file = new File(filename);
  34. FileReader fileReader = new FileReader(file);
  35. BufferedReader bufferedReader = new BufferedReader(fileReader);
  36. String line;
  37. bufferedReader.readLine();
  38. ArrayList<Integer> reads = new ArrayList<Integer>();
  39. ArrayList<Integer> headinng = new ArrayList<Integer>();
  40. double x = 0;
  41. double y = 0;
  42. int head = 0;
  43. Main m = new Main();
  44. while ((line = bufferedReader.readLine()) != null) {
  45. String[] data = line.split(",");
  46. x = Double.parseDouble(data[0]);
  47. y = Double.parseDouble(data[1]);
  48. head = (int)Double.parseDouble(data[2]);
  49. int sonar_ang = (int)Double.parseDouble(data[3]);
  50. int read = (int)Double.parseDouble(data[4]);
  51. // if (read != 255) {
  52. reads.add(read);
  53. headinng.add(sonar_ang);
  54. // }
  55. }
  56. // System.out.println("Deletados: "+(181-reads.size()));
  57. fileReader.close();
  58. // for (int j = 0; j < reads.size(); j++)
  59. // if (headinng.get(j) == 0) {
  60. // y = reads.get(j);
  61. // }
  62. // vamos calcular varios cones
  63. Pose p = new Pose((float)x, (float)y, head);
  64. System.out.print(p);
  65. for (int i = 0; i <= 80; i += 2) {
  66. // para cada posicao
  67. double sum = 0;
  68. int count_exclude = 0;
  69. for (int j = 0; j < reads.size(); j++) {
  70. double exp = m.expectedSonarRead(p, -headinng.get(j)-90, i);
  71. double err = Math.abs(reads.get(j)-exp);
  72. if (err >= 10) {
  73. count_exclude++;
  74. continue;
  75. }
  76. err *= err;
  77. // System.out.println(headinng.get(j)+" "+reads.get(j)+" "+exp);
  78. sum += err;
  79. }
  80. // System.out.println();
  81. // System.out.println(count_exclude);
  82. sum /= (reads.size()-count_exclude);
  83. System.out.print(","+sum);
  84. }
  85. System.out.println();
  86. } catch (IOException e) {
  87. e.printStackTrace();
  88. }
  89. }
  90. public static void main(String[] args) {
  91. String dir = "medidas";
  92. File folder = new File(dir);
  93. File[] listOfFiles = folder.listFiles();
  94. for (int i = 0; i < listOfFiles.length; i++) {
  95. if (listOfFiles[i].isFile()) {
  96. // System.out.println("File " + listOfFiles[i].getName());
  97. fileCalc(dir+"/"+listOfFiles[i].getName());
  98. }
  99. }
  100. }
  101. }