summaryrefslogtreecommitdiff
path: root/roombacomm-client/src/com/hackingroomba/roombacomm/BetterRoomba.java
blob: 6d854cd751d2e2bde01ba96fd5dfc0cffce1a8b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.hackingroomba.roombacomm;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;

public class BetterRoomba implements SerialPortEventListener {
	
	private static final String PORT = "/dev/ttyUSB0";
	private static final int TIMEOUT = 2000;
	
	private static final int RATE = 115200;
	private static final int DATABITS = 8;
	private static final int PARITY = SerialPort.PARITY_NONE;
	private static final int STOPBITS = SerialPort.STOPBITS_1;
	
	private SerialPort serialPort;
	private InputStream input;
	private OutputStream output;
	
	private InputStreamReader isr;
	
	public static void main(String[] args) throws Exception {
		System.out.println("Starting...");
		BetterRoomba br = new BetterRoomba();
		
		br.startup();
		br.send(new byte[] {(byte) 136, (byte) 8});
		Thread.sleep(5000);
		br.send(new byte[] {(byte) 148, (byte) 2, (byte) 19,(byte) 20});
		Thread.sleep(5000);
		
		br.send(new byte[] {(byte) 150, (byte) 0});
		br.send(new byte[] {(byte) 136, (byte) 255});
		br.shutdown();
		
		System.out.println("-END-");
	}
	
	@Override
	synchronized public void serialEvent(SerialPortEvent e) {
		try {
			System.out.print("Serial event " + e.getEventType() + ": ");
			
			if (isr.ready()) {
				int c = isr.read();
				if (c == 18) {
					for (int i=0; i<6; i++) {
						System.out.print(isr.read() + " ");
					}
				}
				if (c == 19) {
					System.out.print(c + " ");
					int bytes = isr.read();
					for (int i=0; i<bytes; i++) {
						System.out.print(isr.read() + " ");
					}
					System.out.print("[" + isr.read() + "] ");
				}
			}
			else
				System.out.println("For some weird reason the buffer ain't ready.");
			System.out.println("--");
		} catch (Exception ex) {
			System.out.println("Exception: " + ex.getMessage());
		}
	}
	
	public void startup() throws Exception {
		serialConnect();
	}
	
	public void shutdown() throws Exception {
		try {
            if (input != null)
            	input.close();
            if (output != null) 
            	output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        try {
            if (serialPort != null)
            	serialPort.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	private void send(byte[] arr) throws Exception {
		System.out.print("Sending: ");
		for (byte b : arr) {
			System.out.print(String.valueOf(b) + " ");
		}
		System.out.println("");
		output.write(arr);
	}
	
	private void serialConnect() throws Exception {
		try {
			Enumeration portList = CommPortIdentifier.getPortIdentifiers();
	        while (portList.hasMoreElements()) {
	            CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
	            
	            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
	                System.out.println("Found " + portId.getName());
	                
	                if (portId.getName().equals(PORT)) {
	                	System.out.println("Opening port: "+ portId.getName());
	                    serialPort = (SerialPort) portId.open("roomba serial", TIMEOUT);
	                    
	                    input  = serialPort.getInputStream();
	                    output = serialPort.getOutputStream();
	                    
	                    isr = new InputStreamReader(input);
	                    
	                    serialPort.setSerialPortParams(RATE, DATABITS, STOPBITS, PARITY);
	                    serialPort.addEventListener(this);
	                    serialPort.notifyOnDataAvailable(true);
	                    
	                    System.out.println("Port opened successfully");
	                }
	            }
	        }
	    } catch (Exception e) {
	        System.out.println(e.getMessage());
	        throw e;
	    }
	}
}