Currently my code works but I have some problems I want to solve :
- I want it to stop at any moment by pressing a screen button, but when it is going back it does not read any button. I have to wait it going straight. How can I solve it ?
- When I want to stop motors, there is a little delay between 2 commands so when robot detect something, it still turn a little on itself. How can I correct this ?
- When it is going back, I use controlled rotation (with angles). But there is a little delay between each commands, I want all done without any interruption. How can I proceed ?
Here is my code commented :
- Code: Select all
package sentinel;
import lejos.nxt.*; // Motors, buttons and LCD
import lejos.robotics.objectdetection.*; // US
import lejos.util.Delay; // msDelay()
import java.util.*; // Random()
/* This programm is a sentinel : the robot should go straight in a direction.
* When it detect something, it go back, turn a 1/3 and go straight.
*
* Notes :
*
* Created on 11.06.2012
* Codeur : CHEN Cedric
*/
public class Sentinel
{
public static void main(String[] args)
{
int distance = 30; // us detects on 30 cm.
int distanceMax = 100;
int speed = 2*360; // 2 round per second.
int angle = 666; // Do not know why, but when weel turn of 666°, the robot turn of 1/3.
int rd; // random.
// Initialisation of speed
Motor.B.setSpeed(speed);
Motor.C.setSpeed(speed);
// ultrasonic constructor
UltrasonicSensor us = new UltrasonicSensor(SensorPort.S4);
FeatureDetector fd = new RangeFeatureDetector(us, distanceMax, 500);
// random constructor
Random random = new Random();
/************************************************************
* Programm
***********************************************************/
LCD.drawString("Timmy !!", 5, 3); // Print rotot's name
Button.waitForAnyPress(); // wait before start
Delay.msDelay(500);
// while we do not press a buton to stop
while(Button.readButtons() == 0)
{
Feature result = fd.scan();
// if it detects nothing
if(result == null || result.getRangeReading().getRange() > distance)
{
// still going straight
Motor.B.forward();
Motor.C.forward();
}
else
{
// we stop motors
Motor.B.stop();
Motor.C.stop(); // Here is the delay I talked about.
// 2 weel rounds to go back
Motor.B.rotate(-2*360, true);
Motor.C.rotate(-2*360, true);
while (Motor.B.isMoving() || Motor.C.isMoving());
// Here is an invisible delay I want to delete.
// random integer we change in +/-1 for the "turn around"
rd = random.nextInt(2);
if (rd == 0) rd = 1;
else rd = -1;
// turn aroud
Motor.B.rotate(rd * angle, true);
Motor.C.rotate(-rd * angle, true);
while (Motor.B.isMoving() || Motor.C.isMoving());
}
}
Motor.B.stop();
Motor.C.stop();
}
}
Thanks you for your help.
