Moderators: roger, 99jonathan, imaqine
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.
private int byteArrayToInt(byte[] b)
{
int value = 0;
for(int index = 0;index<b.length;index++)
{
value <<= 8;
value |= b[index] & 0xFF;
}
return value;
} // Wait forever for a RAW connection
connection = Bluetooth.waitForConnection(0, NXTConnection.RAW);
// Use the connection in RAW mode
connection.setIOMode(NXTConnection.RAW);
private int byteArrayToInt(byte[] b)
{
int value = 0;
for(int index = 0;index<b.length;index++)
{
value <<= 8;
value |= b[index] & 0xFF;
}
return value;
}
import lejos.nxt.*;
import lejos.nxt.comm.*;
import lejos.navigation.*;
public class BC2 extends Thread
{
//Text Constant
private final String line = "________________";
//Input Constants
private final int FORWARD = 901;
private final int RIGHT = 902;
private final int BACKWARD = 903;
private final int LEFT = 904;
private final int FORWARD_OFF = 905;
private final int RIGHT_OFF = 906;
private final int BACKWARD_OFF = 907;
private final int LEFT_OFF = 908;
private final int ACTION = 909;
private final int ACTION_OFF = 910;
//Input Variables
private byte[] input;
private int value;
private int previousValue;
int readResult;
private int loopCounter;
//Wheels
private Pilot wheels;
//Bluetooth
BTConnection connection;
public BC2(Pilot newWheels)
{
input = new byte[4];
value = -1;
previousValue = -2;
loopCounter = 0;
wheels = newWheels;
if (!Bluetooth.getPower())
Bluetooth.setPower(true);
System.out.println("BT Ready");
connection = Bluetooth.waitForConnection(0, NXTConnection.RAW);
System.out.println("BT Connected");
connection.openStream();
System.out.println("BT Stream Opened");
}
public void run()
{
while (true)
{
loopCounter++;
readResult = 0;
//Ensures a full read unless error hit
do
{
readResult += connection.read(input, 4, false);
if (Button.ESCAPE.isPressed())
return;
}
while( readResult>=0 && readResult < 4 );
//Good Read
if(readResult==4)
{
value = byteArrayToInt(input);
printState();
if (value!=previousValue)
{
//Set Speed
if(value >= 0 && value <= 900)
{
wheels.setMoveSpeed(value);
wheels.setTurnSpeed(value);
}
//Other Commands
else
{
switch(value)
{
case(FORWARD):
wheels.forward();
System.out.println("Forward");
break;
case(RIGHT):
wheels.steer(200);
System.out.println("Right");
break;
case(BACKWARD):
wheels.backward();
System.out.println("Backward");
break;
case(LEFT):
wheels.steer(-200);
System.out.println("Left");
break;
case(FORWARD_OFF):
wheels.stop();
System.out.println("Stop");
break;
case(RIGHT_OFF):
wheels.stop();
System.out.println("Stop");
break;
case(BACKWARD_OFF):
wheels.stop();
System.out.println("Stop");
break;
case(LEFT_OFF):
wheels.stop();
System.out.println("Stop");
break;
case(ACTION):
wheels.stop();
System.out.println("Action On");
break;
case(ACTION_OFF):
wheels.stop();
System.out.println("Action Off");
break;
}
}
previousValue=value;
}
}
//Bad Read
else
{
System.out.println("ERROR");
printState();
System.out.println("Read Error: " + readResult);
try{Thread.sleep(5000);} catch (InterruptedException e){}
}
}
}
private static int byteArrayToInt(byte[] b)
{
long l = 0;
l |= b[3] & 0xFF;
l <<= 8;
l |= b[2] & 0xFF;
l <<= 8;
l |= b[1] & 0xFF;
l <<= 8;
l |= b[0] & 0xFF;
return (int)l;
}
private void printState()
{
System.out.println(line);
System.out.println("Loop Count: " + loopCounter);
System.out.println("Value: " + value);
}
/*For Debugging
private static void printArray(byte[] array)
{
for (int index = 0;index<array.length;index++)
System.out.println(array[index]);
}
*/
public static void main(String[] args)
{
BC2 me = new BC2(new TachoPilot( (float)5.7, (float)13.3, Motor.A, Motor.C));
me.start();
}
}
/*
* Author: Allon Hadaya
* Date: 06/19/09
*
* Description: -Abstract class that manages bluetooth communications.
* -Expects 32-bit integers.
* -Acts on predefined integer constants.
* -Abstract event methods for robot behavior.
*/
import lejos.nxt.*;
import lejos.nxt.comm.*;
public abstract class BluetoothControl extends Thread
{
//input constants
private static final int FORWARD = 201;
private static final int FORWARD_RIGHT = 202;
private static final int RIGHT = 203;
private static final int BACKWARD_RIGHT = 204;
private static final int BACKWARD = 205;
private static final int BACKWARD_LEFT = 206;
private static final int LEFT = 207;
private static final int FORWARD_LEFT = 208;
private static final int STOP = 209;
private static final int ACTION = 210;
private static final int ACTION_OFF = 211;
private static final int EXIT = 212;
private static final int B_UP_PRESS = 213;
private static final int B_DOWN_PRESS = 214;
private static final int B_UP_RELEASE = 215;
private static final int B_DOWN_RELEASE = 216;
//bluetooth
static BTConnection connection;
//input variables
private static byte[] input;
private static int value;
private static int valueLast;
private static int readResult;
public BluetoothControl()
{
valueLast = 0;
input = new byte[4];
refreshBluetooth();
}
protected abstract void on1To100(int value);
protected abstract void on101To200(int value);
protected abstract void onForward();
protected abstract void onForwardRight();
protected abstract void onRight();
protected abstract void onBackwardRight();
protected abstract void onBackward();
protected abstract void onBackwardLeft();
protected abstract void onLeft();
protected abstract void onForwardLeft();
protected abstract void onStop();
protected abstract void onAction();
protected abstract void onActionOff();
protected abstract void onExit();
protected abstract void onBUpPress();
protected abstract void onBDownPress();
protected abstract void onBUpRelease();
protected abstract void onBDownRelease();
public void run()
{
while (true)
{
readResult = 0;
//Read 4 bytes
do
{
readResult += connection.read(input, 4, false);
if (Button.ESCAPE.isPressed())
return;
}while( readResult>=0 && readResult < 4 );
//Good Read
if(readResult==4)
{
value = byteArrayToInt(input);
//New Value
if (value!=valueLast)
{
//1-100
if(value >= 1 && value <= 100)
{
on1To100(value);
}
//101-200
else if(value >= 101 && value <= 200)
{
on101To200(value);
}
//Other Commands
else
{
switch(value)
{
case(FORWARD):
onForward();
break;
case(FORWARD_RIGHT):
onForwardRight();
break;
case(RIGHT):
onRight();
break;
case(BACKWARD_RIGHT):
onBackwardRight();
break;
case(BACKWARD):
onBackward();
break;
case(BACKWARD_LEFT):
onBackwardLeft();
break;
case(LEFT):
onLeft();
break;
case(FORWARD_LEFT):
onForwardLeft();
break;
case(STOP):
onStop();
break;
case(ACTION):
onAction();
break;
case(ACTION_OFF):
onActionOff();
break;
case(EXIT):
onExit();
return;
case(B_UP_PRESS):
onBUpPress();
break;
case(B_DOWN_PRESS):
onBDownPress();
break;
case(B_UP_RELEASE):
onBUpRelease();
break;
case(B_DOWN_RELEASE):
onBDownRelease();
break;
}
}
valueLast=value;
}
}
//Bad Read
else
{
System.out.println("Disconnected");
//Reset Connection
refreshBluetooth();
}
}
}
private static final void refreshBluetooth()
{
if (!Bluetooth.getPower())
Bluetooth.setPower(true);
System.out.println("BT Standby");
connection = Bluetooth.waitForConnection(0, NXTConnection.RAW);
connection.openStream();
}
private static final int byteArrayToInt(byte[] b)
{
long l = 0;
for(int index=b.length - 1; index > 0; index--)
{
l |= b[index] & 0xFF;
l <<= 8;
}
//Last one not shifted
l |= b[0] & 0xFF;
return (int)l;
}
public static void main(String[] args)
{}
}
Users browsing this forum: No registered users and 2 guests