2031f4bed0b90017124d9f37712eb7c1 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.util.ArrayList;
  4. import java.util.concurrent.Semaphore;
  5. import javax.swing.JPanel;
  6. import lejos.geom.Line;
  7. import lejos.robotics.mapping.LineMap;
  8. import lejos.robotics.navigation.Pose;
  9. public class ScannerImage extends JPanel {
  10. private Pose pose;
  11. private LineMap map;
  12. private ArrayList<SonarRead> lista_leituras;
  13. private Semaphore semaphore;
  14. class SonarRead {
  15. public double distance;
  16. public double ang;
  17. public double expected;
  18. SonarRead(double distance, double ang, double expected) {
  19. this.ang = ang;
  20. this.distance = distance;
  21. this.expected = expected;
  22. }
  23. }
  24. @Override
  25. protected void paintComponent(Graphics g) {
  26. int w = this.getWidth() / 2;
  27. int h = this.getHeight();
  28. int distance = h * 2 / 25;
  29. g.setColor(new Color(0f, 0f, 0f, 0.4f));
  30. for (int i = 1; i <= 18; i++) {
  31. int r = distance * i;
  32. g.drawArc(w - r, h - r, 2 * r, 2 * r, 0, 180);
  33. g.drawString(new Integer(i * 20).toString(), w - 7, h - r);
  34. }
  35. g.setColor(new Color(0f, 0f, 0f, 0.5f));
  36. for (int i = 1; i < 6; i++) {
  37. int lw = (int) (Math.cos(Math.PI / 6.0 * i) * distance * 18);
  38. int lh = (int) (Math.sin(Math.PI / 6.0 * i) * distance * 18);
  39. g.drawLine(w, h, lw + w, h - lh);
  40. }
  41. g.setColor(new Color(0f, 0f, 0f, 0.1f));
  42. for (int i = 1; i < 12; i++) {
  43. int lw = (int) (Math.cos(Math.PI / 12.0 * i) * distance * 18);
  44. int lh = (int) (Math.sin(Math.PI / 12.0 * i) * distance * 18);
  45. g.drawLine(w, h, lw + w, h - lh);
  46. }
  47. if (semaphore.tryAcquire()) {
  48. double d = h * 2.0 / 25.0 / 20.0;
  49. g.setColor(new Color(0f, 1f, 0f));
  50. if (map != null && pose != null) {
  51. Line[] lines = map.getLines();
  52. for (int i = 0; i < lines.length; i++) {
  53. Line l = lines[i];
  54. double sin = Math.sin(-Math.toRadians(pose.getHeading() - 180));
  55. double cos = Math.cos(-Math.toRadians(pose.getHeading() - 180));
  56. double x1 = (l.x1 - pose.getX()) * d;
  57. double y1 = (l.y1 - pose.getY()) * d;
  58. double x2 = (l.x2 - pose.getX()) * d;
  59. double y2 = (l.y2 - pose.getY()) * d;
  60. double xx1 = x1 * cos - y1 * sin;
  61. double yy1 = y1 * cos + x1 * sin;
  62. double xx2 = x2 * cos - y2 * sin;
  63. double yy2 = y2 * cos + x2 * sin;
  64. g.drawLine((int) (w + xx1), (int) (h - yy1), (int) (w + xx2), (int) (h - yy2));
  65. }
  66. }
  67. drawDots(g, w, h);
  68. semaphore.release();
  69. }
  70. }
  71. private void drawDots(Graphics g, int w, int h) {
  72. int oval_size = 16;
  73. int distance = h * 2 / 25;
  74. double d = distance / 20.0;
  75. double a = -oval_size / 4;
  76. double x, y;
  77. g.setColor(new Color(1f, 0f, 0f));
  78. for (SonarRead r : lista_leituras) {
  79. x = a + (d * r.distance) * Math.sin(Math.toRadians(r.ang));
  80. y = a + (d * r.distance) * Math.cos(Math.toRadians(r.ang));
  81. x = w - x;
  82. y = h - y;
  83. g.setColor(new Color(1f, 0f, 0f));
  84. g.fillOval((int) (x - oval_size / 2.0), (int) (y - oval_size / 2.0), oval_size / 2, oval_size / 2);
  85. x = a + (d * r.expected) * Math.sin(Math.toRadians(r.ang));
  86. y = a + (d * r.expected) * Math.cos(Math.toRadians(r.ang));
  87. x = w - x;
  88. y = h - y;
  89. g.setColor(new Color(0f, 0f, 1f));
  90. g.fillOval((int) (x - oval_size / 2.0), (int) (y - oval_size / 2.0), oval_size / 2, oval_size / 2);
  91. }
  92. if (map == null)
  93. return;
  94. }
  95. public Pose getPose() {
  96. return pose;
  97. }
  98. public void setPose (Pose p) {
  99. pose = p;
  100. }
  101. public void addRead(Pose p, double distance, double ang, double expected) {
  102. if (semaphore.tryAcquire()) {
  103. if (pose == null
  104. || !(p.getX() == pose.getX() && p.getY() == pose.getY() && p.getHeading() == pose.getHeading())
  105. || lista_leituras.size() >= 180) {
  106. pose = new Pose(p.getX(), p.getY(), p.getHeading());
  107. lista_leituras.clear();
  108. }
  109. lista_leituras.add(new SonarRead(distance, ang + 90, expected));
  110. semaphore.release();
  111. }
  112. repaint();
  113. }
  114. public ScannerImage(LineMap map) {
  115. this.map = map;
  116. semaphore = new Semaphore(1);
  117. lista_leituras = new ArrayList<SonarRead>();
  118. }
  119. public void clean() {
  120. if (semaphore.tryAcquire()) {
  121. lista_leituras.clear();
  122. semaphore.release();
  123. }
  124. repaint();
  125. }
  126. }