import java.io.DataInputStream;
import java.io.DataOutputStream;
import lejos.nxt.*;
import lejos.nxt.comm.NXTConnection;
import lejos.nxt.comm.Bluetooth;
public class helloworld {
public static void main(String[] args) throws Exception {
LCD.drawString("waiting for bluetooth", 0, 0);
NXTConnection connection = null;
connection = Bluetooth.waitForConnection();
LCD.drawString("connected", 0, 0);
DataInputStream dis = connection.openDataInputStream();
LCD.clear();
LCD.drawString("stream opened", 0, 0);
Motor.C.setSpeed(900);
Motor.C.rotateTo(0, true); // reset rotation counter
int left_speed;
int right_speed;
int shovel_level;
int last_shovel_level = 0;
while (true) {
while (dis.available() <= 0)
;
left_speed = (dis.read() - 10) * 90;
right_speed = (dis.read() - 10) * 90;
shovel_level = (int) (dis.read() / 100.0 * 360);
dis.close();
if ((shovel_level - last_shovel_level > 20)
|| (shovel_level - last_shovel_level < -20)) {
Motor.C.rotateTo(-shovel_level);
last_shovel_level = shovel_level;
}
if ((left_speed >= 0) && (right_speed >= 0)) {
if (right_speed <= 90)
Motor.A.stop();
else {
Motor.A.forward();
Motor.A.setSpeed(right_speed);
}
if (left_speed <= 90)
Motor.B.stop();
else {
Motor.B.forward();
Motor.B.setSpeed(left_speed);
}
} else {
if (right_speed >= -90)
Motor.A.stop();
else {
Motor.A.backward();
Motor.A.setSpeed(-right_speed);
}
if (left_speed >= -90)
Motor.B.stop();
else {
Motor.B.backward();
Motor.B.setSpeed(-left_speed);
}
}
/*
* dos.writeChars("abcd"); dos.flush(); dos.close();
*/
// LCD.drawString("closed",0,0);
}
}
}
评论8
最新资源