summaryrefslogtreecommitdiff
path: root/roombacomm-client/src/com/hackingroomba/roombacomm/ArduinoBot.java
blob: d0294131288abdbbfd4f46dcbd3ddf240a8412cd (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * roombacomm.ArduinoClient -- test out the Arduino subsystem without robot motion
 *
 *  Copyright (c) 2009 Paul Bouchier, bouchier@at@classicnet.net
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General
 *  Public License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 *  Boston, MA  02111-1307  USA
 *
 */

package com.hackingroomba.roombacomm;

import com.hackingroomba.roombacomm.RobotConnection.ReadTerminator;
import com.hackingroomba.roombacomm.RobotType.robotTypes;

import java.io.*;

/**
 * The extensions to roombacomm for ArduinoBot operations.
 * 
 * <h2> Overview </h2>
 * This class contains the operations available from an ArduinoBot
**/

public class ArduinoBot extends RoombaComm {
	
	// connection variables
	private Integer compassHeading;
	private String arduinoString;
	private RobotConnection robotConnection;
	private RobotType robotType;
	
	// compass variables
	private int compassOffset = 0;
	private boolean compassOffsetInitialized = false;
	
	// encoder variables
	private int encoder;

	private long startTime;
	public long elapsedTime;
	
	public ArduinoBot() {
		super();
	}
	
	public ArduinoBot(RobotConnection robotConnection) {
		super(robotConnection);
		this.robotConnection = robotConnection;
	}

	/**
	 * Initialize an Arduino robot controller
	 */
	public boolean initArduinoBot(RobotType rt)
	{
		byte[] robotTypeMsg = "robot m\r".getBytes();	// message to Arduino to tell it the robot type
		
		robotType = rt;		// record robotType for later use
		System.out.println("checking for ArduinoBot... \n");
		try {
			Thread.sleep(2000);			
		} catch (Exception e) {System.out.println("Exception in Thread.sleep()"); }	
		
		// send the robot type message to Arduino, so it knows how to read the encoders, and any other robot-specific things
		if (robotType.robotType == RobotType.robotTypes.frankenRoomba) {
			System.out.println("Sending robot r to Arduino");
			robotTypeMsg = "robot r\r".getBytes();
		} else if (robotType.robotType == robotTypes.tankbot) {
			System.out.println("Sending robot t to Arduino");
			robotTypeMsg = "robot t\r".getBytes();
		} 
		send(robotTypeMsg);
		
		printCompass();
		printEncoders();
		return true;
	}
	

    /** 
     * Send a single byte to the ArduinoBot 
     * (defined as int because of stupid java signed bytes). SendToArduino method of RobotConnection
     * is used because this is an ardunobot, which requires a leading m and trailing <LF> to make
     * the parser work right
     * @param b byte of an Arduino command to send
     * @return true on successful send
     */
    public boolean send(int b) {
     	return(robotConnection.sendToArduino(b));
    }
    
   /**
     * Send a byte array to the ArduinoBot. If the first byte is 128 or higher, preface the byte string with m<space>
     * so it's treated by Arduino as a roomba command
     */
    public boolean send(byte[] bytes) {
    	if (bytes[0] < 0)		// must be a roomba command
    		return(robotConnection.sendToArduino(bytes));
    	else
    		return(robotConnection.send(bytes));
    }

	/**
	 * Read a string from Arduino up to a <LF>
	 * @return array of bytes read from ArduinoBot
	 */
    public String readArduinoBot() throws Exception {
    	return(robotConnection.readBotToTerminator(ReadTerminator.LF));
    }

	public void printCompass() {
		int compassReading;
		startTime = System.currentTimeMillis();
		compassReading = getCompass();
		if (compassReading < 0) return;
	    elapsedTime = System.currentTimeMillis() - startTime;
    	System.out.println("Heading: " + compassReading + " in " + elapsedTime + " ms");
	}
	
	public int getCompass() {
		// read the uncorrected heading
		try {
			compassHeading = getUncorrectedCompass();			
		} catch (Exception e) {
			System.out.println("Exception reading uncorrected compass\n" + e.getMessage() + "\n" );
			e.printStackTrace();
			return -1;
		}
		// if we haven't initialized the compass offset yet, try to read an offset from a config file
		// compassOffsetInitialized ensures we only try this once
		try {
			if (!compassOffsetInitialized) {	// get the compass offset from file if it exists
				compassOffset = readConfigInt(new String("compass_offset"));
				compassOffsetInitialized = true;
			}
		} catch (Exception e) {
			System.out.println("No compass_offset file could be opened\n" + e.getMessage());
			compassOffsetInitialized = true;
		}
		
		// offset the uncorrected heading by the compassOffset, which depends on mounting position
    	compassHeading -= compassOffset;
		compassHeading %= 360;		// compensate for offset which can make it exceed 360
    	if (compassHeading < 0) compassHeading += 360;
		return compassHeading;
	}

	private int getUncorrectedCompass() throws Exception {
		byte [] compassCmd = {'c', '\r'};
		int heading;
		
		//System.out.println("getUncorrectedCompass");
		startTime = System.currentTimeMillis();
		if (!send(compassCmd)) {
			System.out.println("Error sending c command");
		}
		try {	
			arduinoString = robotConnection.readBotToTerminator(ReadTerminator.LF);
			if (arduinoString == null) {
				System.out.println("Error: getUncorrectedCompass failed to read heading - null string");
			}
		} catch (Exception e) {
    	    elapsedTime = System.currentTimeMillis() - startTime;
			System.err.println("Exception reading compass in readBotToTerminator in " + elapsedTime + " ms");
            e.printStackTrace();
            throw e;
        }
	    elapsedTime = System.currentTimeMillis() - startTime;

    	//System.out.println("received string: " + arduinoString);
    	String headingString = arduinoString.split("\\s")[0];
    	heading = new Integer(headingString);
    	return heading;
	}
	
	public boolean setCompassOffset(int currentMagHeading)
	{
		int heading;
		
		if ((currentMagHeading < 0) || (currentMagHeading > 360)) {
			System.out.println("currentMagHeading must be between 0 and 360");
			return false;
		}
		try {
			heading = getUncorrectedCompass();			
			if (heading < 0) return false;
			compassOffset = heading - currentMagHeading;
			if (compassOffset < 0) compassOffset += 360;
			writeConfigInt(new String("compass_offset"), compassOffset);
		} catch (Exception e) {
			System.err.println("Error getting compass or writing compass offset");
			e.printStackTrace();
			return false;
		}
		compassOffsetInitialized = true;
		return true;
	}

	/*
	 * Print, Get the current wheel encoders reading
	 */
	public void printEncoders()
	{
		double readEncDistance = 0;
		
		startTime = System.currentTimeMillis();
		try {
			readEncDistance = getEncoders();
		} catch (Exception e) {
			System.err.println("Exception reading robot encoders: " + e.getMessage());
		}
		long elapsedTime = System.currentTimeMillis() - startTime;
		System.out.println("Wheel encoders reading: " + readEncDistance + " in " + elapsedTime + " ms");

	}
	
	public double getEncoders() throws Exception {
		byte [] cmd = {'e', '\r'};
		
		//System.out.println("read encoders");
		startTime = System.currentTimeMillis();		
		if (!send(cmd)) {
			System.out.println("Error sending e command");
		}
		try {	
			arduinoString = robotConnection.readBotToTerminator(ReadTerminator.LF);
		    elapsedTime = System.currentTimeMillis() - startTime;
			if (arduinoString == null) {
				System.err.println("Error: getEncoders failed to read encoders, null returned in " + elapsedTime + " ms");
			}
		} catch (Exception e) {
		    elapsedTime = System.currentTimeMillis() - startTime;
		    System.err.println("Exception reading encoders from readBotToTerminator in " + elapsedTime + " ms");
            e.printStackTrace();
            throw e;
        }

    	//System.out.println("received string: " + arduinoString);
    	String [] encoderStrings = arduinoString.split("\\s");
    	if (encoderStrings.length == 1) {
        	encoder = new Integer(encoderStrings[0]);
    	} else {
    		System.out.println("Error reading encoders - expected 1 string, found:" + encoderStrings.length 
    				+ " in: " + arduinoString + " in " + elapsedTime + " ms");
    		return -1;
    	}
    	
		double encoderDistance = encoder /robotType.countsPerInch;
		return encoderDistance;
	}
	

	/**
	 * Read roomba 26-byte sensor record using robotConnection. Tries once to read valid data, allowing 100ms
	 * timeout on each attempt. 
	 * @return true if read 26 bytes of valid data. Data has been stored in sensor_bytes. False otherwise
	 */
 	public boolean updateSensors()
 	{
 		return updateSensors(SENSORS_ALL);
 	}
 	
 	public boolean updateSensors(int sensorGroup)
 	{
    	int sensorGroupSize;
    	
 		if (robotConnection == null) {
 	 		System.out.println("Error at ArduinoBot.updateSensors(): no connection object for robot");
 	 		return false; 			
 		}

 		switch(sensorGroup) {
 		case SENSORS_ALL: sensorGroupSize = 26; break;
 		case 100: sensorGroupSize = 80; break;
 		default:
 			System.err.println("Invalid sensor group in updateSensors(): " + sensorGroup);
 			return false;
 		}
    	sensors(sensorGroup);
 		return getSensorData(sensorGroupSize); 		
 	}

	/**
	 * Writes an int to the named file
	 * @throws FileNotFoundException If it can't open the file
	 * @throws IOException If it can't write to the file
	 */
	public void writeConfigInt(String filename, int value) throws FileNotFoundException, IOException {
		DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File(filename)));
		dos.writeInt(value);
	}
	
	/**
	 * Read a configuration file containing an int.
	 * @param filename The file to open & read an int from
	 * @return Int read from file
	 * @throws Exception related to attempting to open & read int from file
	 */
	public int readConfigInt(String filename) throws Exception {
		int configInt = -1;
		
		try {
			DataInputStream dis = new DataInputStream(new FileInputStream(new File(filename)));	
			configInt = dis.readInt();

		} catch (Exception e) {
			System.out.println("No config file could be read\n" + e.getMessage());
		}
		return configInt;
	}


	/**
	 * Writes a double to the named file
	 * @throws FileNotFoundException If it can't open the file
	 * @throws IOException If it can't write to the file
	 */
	public void writeConfigDouble(String filename, double value) throws FileNotFoundException, IOException {
		DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File(filename)));
		dos.writeDouble(value);
	}
	
	/**
	 * Read a configuration file containing a double.
	 * @param filename The file to open & read a double from
	 * @return double read from file
	 * @throws Exception related to attempting to open & read double from file
	 */
	public double readConfigDouble(String filename) throws Exception {
		double configDouble = -1;
		
		try {
			DataInputStream dis = new DataInputStream(new FileInputStream(new File(filename)));	
			configDouble = dis.readDouble();

		} catch (Exception e) {
			System.out.println("No config file could be read\n" + e.getMessage());
			throw(e);
		}
		return configDouble;
	}

	public int getEncoder() {
		return encoder;
	}

	/**
	 * Bogus methods which should never be called on ArduinoClient
	 */
 	public String [] listPorts()
 	{
 		System.out.println("Error at ArduinoBot.listPorts(): should never get here");
 		return (new String[] {"Error: listports calledon ArdunoBot; not supported"});
 	}
 	public boolean connect(String s)
 	{
 		System.out.println("Error at ArduinoBot.connect(): should never get here");
 		return false;
 	}
 	public void disconnect()
 	{
 		System.out.println("Error at ArduinoBot.disconnect(): should never get here");
 		return;
 	}

}