Hi!
Um mir selbst etwas Java beizubringen habe ich mich dazu entschieden mit Simbad eher spielerisch damit zu beginnen. Im Moment versuche ich die Position des Roboters auf einer 2 Dimensionalen Karte auszugeben und die Daten des Sonarsensors auf der Karte anzuzeigen. Das funktioniert soweit ja auch schon recht gut, abgesehen von den Sensordaten( siehe Screenshot).
ich hoffe ihr könnt mir helfen, ich denke es liegt daran das mir die Position der Sensoren unbekannt ist und ich vom mittelpunkt des Roboters aus berechne, habe aber auch keine Idee woher ich die Positionsdaten der Sensoren erhalte.
Hier noch meine Roboterklasse...
Um mir selbst etwas Java beizubringen habe ich mich dazu entschieden mit Simbad eher spielerisch damit zu beginnen. Im Moment versuche ich die Position des Roboters auf einer 2 Dimensionalen Karte auszugeben und die Daten des Sonarsensors auf der Karte anzuzeigen. Das funktioniert soweit ja auch schon recht gut, abgesehen von den Sensordaten( siehe Screenshot).
ich hoffe ihr könnt mir helfen, ich denke es liegt daran das mir die Position der Sensoren unbekannt ist und ich vom mittelpunkt des Roboters aus berechne, habe aber auch keine Idee woher ich die Positionsdaten der Sensoren erhalte.
Hier noch meine Roboterklasse...
Java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication7;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.vecmath.Point3d;
import simbad.sim.*;
import javax.vecmath.Vector3d;
import simbad.gui.Simbad;
import java.awt.BasicStroke;
import java.awt.Graphics2D;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
public class MyRobot extends Agent {
public final static int ARENA_WIDTH = 9; //lenght of the arena
public final static int ARENA_HEIGHT = 9; // width of the arena
Point point1 = null, point2 = null;
Point obstacle = null;
Point3d coords;
ArrayList<Line2D.Double> lines;
ArrayList<Line2D.Double> obstacleLines;
RangeSensorBelt sonars, bumpers;
SensorMatrix luminanceMatrix;
CameraSensor camera;
TrackPanel panel;
JInternalFrame statPanel;
Simbad sframe;
public MyRobot(Vector3d position, String name) {
super(position, name);
bumpers = RobotFactory.addBumperBeltSensor(this);
sonars = RobotFactory.addSonarBeltSensor(this, 24);
camera = RobotFactory.addCameraSensor(this);
// prepare a buffer for storing gray level image
luminanceMatrix = camera.createCompatibleSensorMatrix();
panel = new TrackPanel();
Dimension dim = new Dimension(ARENA_WIDTH * 70, ARENA_HEIGHT * 70);
panel.setPreferredSize(dim);
panel.setMinimumSize(dim);
this.statPanel = new JInternalFrame();
statPanel.add(panel);
coords = new Point3d();
getCoords(coords);
point1 = new Point();
point1.setLocation((coords.x + ARENA_WIDTH) * 70 / 2, (coords.z + ARENA_HEIGHT) * 70 / 2);
lines = new ArrayList<>();
obstacleLines = new ArrayList<>();
}
/**
*
* @param frame
*/
public void setTrackViewFrame(JInternalFrame frame) {
frame.add(panel);
frame.setLocation(725, 20);
frame.setSize(ARENA_WIDTH * 70, ARENA_HEIGHT * 70);
}
class TrackPanel extends JPanel {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
int width = ARENA_WIDTH * 70;
int height = ARENA_HEIGHT * 70;
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, width, height);
g2.setStroke(new BasicStroke(15.0f, // Line width
BasicStroke.CAP_ROUND, // End-cap style
BasicStroke.JOIN_ROUND)); // Vertex join style
for (int y = 0; y < lines.size(); y++) {
if (lines.get(0) != null) {
g2.drawLine((int) lines.get(y).x1,
(int) lines.get(y).y1,
(int) lines.get(y).x2,
(int) lines.get(y).y2);
}
g2.setColor(Color.RED);
g2.setStroke(new BasicStroke(5.0f)); // Vertex join style
for (int x = 0; x < obstacleLines.size(); x++) {
if (obstacleLines.get(0) != null) {
g2.drawLine((int) obstacleLines.get(x).x1,
(int) obstacleLines.get(x).y1,
(int) obstacleLines.get(x).x2,
(int) obstacleLines.get(x).y2);
}
}
g2.setStroke(new BasicStroke(15.0f, // Line width
BasicStroke.CAP_ROUND, // End-cap style
BasicStroke.JOIN_ROUND)); // Vertex join style
g2.setColor(Color.WHITE);
}
}
}
//
public void setObstacle() {
getCoords(coords);
for (int i = 0; i < sonars.getNumSensors(); i++) {
double range = sonars.getMeasurement(i);
double angle = sonars.getSensorAngle(i);
boolean hit = sonars.hasHit(i);
System.out.println("Sonar at angle " + angle
+ "measured range =" + range + " has hit something:" + hit);
if (hit ==true) {
obstacle = new Point();
obstacle.setLocation((coords.x + ARENA_WIDTH ) * 70 / 2+ range *cos(angle) , (coords.z + ARENA_HEIGHT) * 70 / 2+ range *sin(angle));
Line2D.Double obstLine = new Line2D.Double();
obstLine.setLine( obstacle, obstacle);
obstacleLines.add(obstLine);
hit = false;
}
}
}
@Override
public void performBehavior() {
if (bumpers.oneHasHit()) {
setTranslationalVelocity(-0.1);
setRotationalVelocity(0.1 * Math.random());
} else // Front left obstacle ?
if (sonars.hasHit(0) && (sonars.hasHit(1))) {
setObstacle();
setRotationalVelocity(-Math.PI / 4);
setTranslationalVelocity(0.1);
} // Front right obstacle ?
else if (sonars.hasHit(0) && (sonars.hasHit(23))) {
setObstacle();
setRotationalVelocity(Math.PI / 4);
setTranslationalVelocity(0.1);
} // left obstacle ?
else if (sonars.hasHit(3) || sonars.hasHit(4)) {
setObstacle();
setRotationalVelocity(-Math.PI / 8);
setTranslationalVelocity(0.1);
} // right obstacle ?
else if (sonars.hasHit(21) || sonars.hasHit(20)) {
setObstacle();
setRotationalVelocity(Math.PI / 8);
setTranslationalVelocity(0.1);
} else if ((getCounter() % 100) == 0) {
setRotationalVelocity(Math.PI / 2 * (0.5 - Math.random()));
setTranslationalVelocity(0.5);
}
camera.copyVisionImage(luminanceMatrix);
if (getCounter() % 10 == 0) {
getCoords(coords);
point2 = new Point();
point2.setLocation((coords.x + ARENA_WIDTH) * 70 / 2, (coords.z + ARENA_HEIGHT) * 70 / 2);
Line2D.Double newLine = new Line2D.Double();
newLine.setLine(point1, point2);
lines.add(newLine);
panel.repaint();
point1.x = point2.x;
point1.y = point2.y;
}
//every 20 frames
if (getCounter() % 20 == 0) {
// print each sonars measurement
for (int i = 0; i < sonars.getNumSensors(); i++) {
double range = sonars.getMeasurement(i);
double angle = sonars.getSensorAngle(i);
boolean hit = sonars.hasHit(i);
System.out.println("Sonar at angle " + angle
+ "measured range =" + range + " has hit something:" + hit);
}
}
}
}