I just got to play with this LeJOS and its great!
I love java, and being able to combine it with my NXT is so cool.
But i have a problem, i have been trying to implement a SegWay model, much as NXTWay and some of the other examples, but my program seems to randomly reboot the nxt while running.
- Code: Select all
import lejos.nxt.*;
public class JSegway {
private int value, delta, old, center, offset;
private LightSensor light;
public JSegway() {
//Create light sensor attached to port S3
light = new LightSensor(SensorPort.S3, true);
value = 0;
old = 0;
delta = 0;
//Wait 0.5 sec then read off center position
try {
Thread.sleep(500);
center = light.readNormalizedValue();
LCD.clear();
LCD.drawInt(center,0,0);
LCD.refresh();
Thread.sleep(500);
}
catch(InterruptedException e) {
System.exit(-1);
}
//Disable smooth acceleration on the motors
Motor.A.smoothAcceleration(false);
Motor.C.smoothAcceleration(false);
Motor.A.regulateSpeed(false);
Motor.C.regulateSpeed(false);
}
public void loop() throws InterruptedException {
while(!Button.ENTER.isPressed()) {
value = light.readNormalizedValue();
delta = old-value;
offset = center - value;
int speed = Math.abs(offset) * 10;
if(speed > 100) {
speed = 100;
}
if(speed < 0) {
speed = 0;
}
if( offset < 0 ) {
Motor.A.setPower(speed);
Motor.C.setPower(speed);
Motor.A.backward();
Motor.C.backward();
}
else if( offset > 0 ) {
Motor.A.setPower(speed);
Motor.C.setPower(speed);
Motor.A.forward();
Motor.C.forward();
}
else {
Motor.A.flt();
Motor.C.flt();
}
old = value;
Thread.sleep(5);
}
System.exit(0);
}
public static void main(String args[]) throws InterruptedException {
new JSegway().loop();
}
}
I know this probabely is a very bad attempt at a segway program, but i cant really tune it until i can actually make the thing run all the time instead of rebooting.
Btw, the main lejos menu seems to indicate that my nxt has between 6.8-7.2v while this is going on. (The batteries were new 30min ago)
Any ideas / help would be greatly appriciated.
/Gof

