123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- import java.awt.BorderLayout;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.event.WindowEvent;
- import java.awt.event.WindowListener;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JSplitPane;
- import javax.swing.SwingUtilities;
- import javax.swing.event.AncestorEvent;
- import javax.swing.event.AncestorListener;
- import config.Map;
- import lejos.pc.comm.NXTComm;
- import lejos.pc.comm.NXTCommException;
- import lejos.pc.comm.NXTCommFactory;
- import lejos.pc.comm.NXTInfo;
- import lejos.robotics.mapping.LineMap;
- import lejos.robotics.navigation.Pose;
- import robots.Robot;
- import robots.VirtualRobot;
- public class MainProgram extends JPanel implements KeyListener, WindowListener {
- private MapImage imap;
-
- private DataOutputStream output;
- private DataInputStream input;
- private Send sendthread;
- private Receiver receivethread;
- public static final byte FORWARD = 0;
- public static final byte STOP = 1;
- public static final byte EXIT = 2;
- public static final byte LEFT = 3;
- public static final byte RIGHT = 4;
- public static final byte BACKWARD = 5;
-
- private class Receiver extends Thread {
- public boolean run = true;
- @Override
- public void run() {
- int bytes_valiable = -1;
-
- while(run) {
- try {
- bytes_valiable = input.available();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- if (bytes_valiable >= 0) {
- try {
- if (input.readByte() != '@') continue;
- int angle = input.readByte();
- float alpha = input.readFloat();
- float x = input.readFloat();
- int distance = input.readByte();
- float y = input.readFloat();
-
- // posicao do robo
- imap.addPoint(x, y, Math.toRadians(alpha+90));
-
- // ponto do ultrasonico
- double sensor_ang = Math.toRadians(alpha+angle);
- double dx = Math.cos(sensor_ang)*distance;
- double dy = Math.sin(sensor_ang)*distance;
- imap.addRead(x+dx, y+dy);
-
- } catch (IOException e1) {
- continue;
- }
- }
- }
- }
- }
- private class Send extends Thread {
- private int before;
- private int command;
- private boolean run = true;
-
- @Override
- public void run() {
- before = -1;
- command = STOP;
-
- while(run) {
- if (before != command) {
- try {
- System.out.println("Send cmd");
- output.write(command);
- output.flush();
- if (command == EXIT) run = false;
- } catch (IOException e1) {
- System.out.println("Erro send cmd");
- before = -1;
- send(command);
- }
- before = command;
- }
-
- try {
- Thread.sleep(50);
- } catch (InterruptedException e) {
- }
- }
- }
- public void send(int cmd) {
- command = cmd;
- }
- }
-
- public MainProgram (LineMap map) {
- JFrame frame = new JFrame("Mapa MAC0318");
- receivethread= new Receiver();
- sendthread = new Send();
-
- // output = out;
- // input = in;
- // receivethread.start();
- // sendthread.start();
-
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLayout(new BorderLayout());
- imap = new MapImage(map);
- ScannerImage scanner = new ScannerImage();
- // frame.add(this.map);
- frame.setSize(800, 800);
- frame.setVisible(true);
-
- frame.setFocusable(true);
- frame.requestFocusInWindow();
- frame.addKeyListener(this);
- frame.addWindowListener(this);
-
- JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scanner, imap);
- splitPane.setOneTouchExpandable(true);
- splitPane.setDividerLocation((int)(frame.getHeight()/2));
- frame.add(splitPane);
-
- //
- // String text = "1,2,3 - Change view mode.\n";
- // text += "s - Save image.\n";
- // text += "l - Show trace.\n";
- // text += "c - Clean map.\n";
- // JOptionPane.showMessageDialog(null, text);
- }
-
- public void addPoint(Pose p) {
- imap.addPoint(p);
- }
- @Override
- public void keyPressed(KeyEvent e) {
- char input = e.getKeyChar();
- if (input == '1') imap.setVisual(0);
- else if (input == '2') imap.setVisual(1);
- else if (input == '3') imap.setVisual(2);
- else if (input == 'l') imap.showLine();
- else if (input == 's') imap.save();
- else if (input == 'c') imap.clean();
-
- switch(e.getKeyCode()) {
- case KeyEvent.VK_UP:
- sendthread.send(FORWARD);
- break;
- case KeyEvent.VK_DOWN:
- sendthread.send(BACKWARD);
- break;
- case KeyEvent.VK_LEFT:
- sendthread.send(LEFT);
- break;
- case KeyEvent.VK_RIGHT :
- sendthread.send(RIGHT);
- break;
- }
- }
- @Override
- public void keyReleased(KeyEvent arg0) {
- sendthread.send(STOP);
- }
- @Override
- public void keyTyped(KeyEvent arg0) {
- }
- public static void main(String[] args) throws NXTCommException, IOException {
- LineMap map = Map.makeMap();
- Robot robot = new VirtualRobot(map);
-
-
- // NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
- // NXTInfo[] nxtInfo = nxtComm.search("NXT8"); //find brick with NXT_ID by doing a Bluetooth inquiry
- // if (nxtInfo.length == 0) { // failed to find a brick with the ID
- // System.err.println("NO NXT found");
- // System.exit(1);
- // }
- // if (!nxtComm.open(nxtInfo[0])) { // the brick was found but a connection could not be establish
- // System.err.println("Failed to open NXT");
- // System.exit(1);
- // }
- //
- // final DataInputStream input = new DataInputStream(nxtComm.getInputStream()); // open data input stream
- // final DataOutputStream output = new DataOutputStream(nxtComm.getOutputStream()); // open data output stream
- //
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- //new Map(input, output);
- new MainProgram(map);
- }
- });
-
- }
- @Override
- public void windowOpened(WindowEvent e) {
-
- }
- @Override
- public void windowClosing(WindowEvent e) {
- System.err.println("Fechando...");
- receivethread.run = false;
- sendthread.send(EXIT);
- try {
- receivethread.join();
- sendthread.join();
- } catch (InterruptedException e1) {
- System.out.println("Nao foi possivel finalizar as threads...");
- }
-
- }
- @Override
- public void windowClosed(WindowEvent e) {
- }
- @Override
- public void windowIconified(WindowEvent e) {
-
- }
- @Override
- public void windowDeiconified(WindowEvent e) {
-
- }
- @Override
- public void windowActivated(WindowEvent e) {
-
- }
- @Override
- public void windowDeactivated(WindowEvent e) {
- }
- }
|