One NXT has the Tilt sensor attached pointing forwards and acts as the controller. You hold the NXT sideways at an angle to the vertical and use it as a steering wheel. Tilting away from you increases the speed. Tilting backwards decreases the speed and then goes into reverse.
I used the Mindsensors ACCL-Nx-3g3x sensor, but am only using the X and Y Tilt values. It should work with any of the mindsensors or HiTechnic acceleration sensors.
The other NXT uses a Pilot class to steer a wheeled vehicle. It works quite well, but it could do with a bit more work on the programs to achive finer contol.
Here is the controller program. You will need to change the name of your NXT, and make sure you have added it to the Bluetooth devices.
- Code: Select all
import java.io.*;
import lejos.nxt.*;
import lejos.nxt.comm.*;
public class TiltController {
/**
* Wii like controller for the NXT.
*
* Needs two NXTs: one for the controller and one to control.
*/
public static void main(String[] args) throws Exception {
TiltSensor controller = new TiltSensor(SensorPort.S1);
int x, y, z;
String name = "NOISY";
LCD.drawString("Connecting ...", 0, 0);
LCD.refresh();
BTRemoteDevice btrd = Bluetooth.getKnownDevice(name);
BTConnection btc = Bluetooth.connect(btrd);
LCD.clear();
LCD.drawString("Connected", 0, 0);
LCD.refresh();
DataInputStream dis = btc.openDataInputStream();
DataOutputStream dos = btc.openDataOutputStream();
while(true) {
x = controller.getXTilt();
y = controller.getYTilt();
z = controller.getZTilt();
LCD.drawInt(x, 3, 0, 1);
LCD.drawInt(y, 3, 0, 2);
LCD.drawInt(z, 3, 0, 3);
LCD.refresh();
dos.writeByte(x);
dos.writeByte(y);
dos.writeByte(z);
dos.flush();
byte ack = dis.readByte();
}
}
}
And here is the code for the robot you are controlling. You will need to change the Pilot constructors parameters for your robot.
- Code: Select all
import java.io.*;
import lejos.navigation.*;
import lejos.nxt.*;
import lejos.nxt.Motor;
import lejos.nxt.comm.*;
public class RemoteSteer {
public static void main(String[] args) throws Exception {
Pilot robot = new Pilot(2.1f,6f,Motor.A, Motor.C,false);
String connected = "Connected";
String waiting = "Waiting";
LCD.drawString(waiting,0,0);
LCD.refresh();
BTConnection btc = Bluetooth.waitForConnection();
LCD.clear();
LCD.drawString(connected,0,0);
LCD.refresh();
DataInputStream dis = new DataInputStream(btc.openInputStream());
DataOutputStream dos = new DataOutputStream(btc.openOutputStream());
while (true) {
int x = dis.readByte();
int y = dis.readByte() & 0xFF;
int z = dis.readByte() & 0xFF;
dos.writeByte((byte) 0xFF); // Ack
dos.flush();
LCD.drawInt(x,4, 0,1);
LCD.drawInt(y,4, 0,2);
LCD.drawInt(z,4, 0,3);
int speed = (180 - y) * 20;
LCD.drawInt(speed, 5, 0, 4);
LCD.refresh();
int ax = (x < 0 ? -x : x);
if (speed > 0) {
robot.setSpeed(speed);
if (ax < 5) robot.forward();
else robot.steer(x);
} else {
speed = - speed;
robot.setSpeed(speed);
robot.backward();
}
}
}
}

