Commit cfe3ef27 authored by Enniwhere's avatar Enniwhere
Browse files

Made a spinning program in order to get the time it takes to spin, made a new...

Made a spinning program in order to get the time it takes to spin, made a new function in private car, upgraded AvoidFigure9_3 and started on the Escape behavior
parent 1ad4c70f
Pipeline #3154 skipped
......@@ -16,8 +16,8 @@ build.dir=build
classes.dir=${build.dir}/classes
docs.dir=${build.dir}/docs
main.class=Vehicle1Dancing
output.basename=Vehicle1Dancing
main.class=AvoidFigure9_3_Upg
output.basename=
main.pc_class=
output.pc_basename=
......
import lejos.nxt.*;
import lejos.util.Delay;
/*
* A simple behavior control program Avoid,
* Figure 9.3 in Jones, Flynn, and Seiger:
* "Mobile Robots, Inspiration to Implementation",
* Second Edition, 1999.
*/
public class AvoidFigure9_3_Upg {
public static void main(String [] args) throws Exception {
PrivateCar car = new PrivateCar();
int power = 70, ms = 500, spin_time = 6500;
UltrasonicSensor sonar = new UltrasonicSensor(SensorPort.S1);
int frontDistance, leftDistance, rightDistance;
int stopThreshold = 30;
// ESCAPE button listener makes it possible to exit the program
// at any time.
Button.ESCAPE.addButtonListener(new ButtonListener() {
public void buttonPressed(Button b) {
System.exit(1);
}
public void buttonReleased(Button b) {
}
});
LCD.drawString("Avoid 9.3 Upgraded", 0, 0);
Button.waitForAnyPress();
while ( true )
{
// Go forward
car.forward(power, power);
// Monitor the distance in front of the car and stop
// when an object gets to close
frontDistance = sonar.getDistance();
while ( frontDistance > stopThreshold )
{
frontDistance = sonar.getDistance();
}
car.stop();
// Get the distance to the left
car.forward(0, power);
Delay.msDelay(ms);
leftDistance = sonar.getDistance();
// Get the distance to the right
car.backward(0, power);
Delay.msDelay(ms);
car.forward(power, 0);
Delay.msDelay(ms);
rightDistance = sonar.getDistance();
if (leftDistance < stopThreshold && rightDistance < stopThreshold && frontDistance < stopThreshold){
// Drive backwards for 2 times ms milliseconds
car.backward(power,power);
Delay.msDelay(ms * 4);
car.stop();
// Turn approximately 180 degrees
car.spin(power);
Delay.msDelay(spin_time/2);
car.stop();
}
// Turn in the direction with most space in front of the car
else if ( leftDistance > rightDistance ){
car.backward(power, 0);
Delay.msDelay(ms);
car.forward(0, power);
Delay.msDelay(ms);
}
}
}
}
import lejos.nxt.*;
import lejos.util.Delay;
/*
* Avoid behavior
*/
class Escape extends Thread
{
private SharedCar car = new SharedCar();
private int power = 70, ms = 500;
UltrasonicSensor sonar = new UltrasonicSensor(SensorPort.S1);
int frontDistance, leftDistance, rightDistance;
int stopThreshold = 30;
public Escape(SharedCar car)
{
this.car = car;
}
public void run()
{
while (true)
{
// Monitor the distance in front of the car and stop
// when an object gets to close
frontDistance = sonar.getDistance();
while ( frontDistance >= stopThreshold )
{
car.noCommand();
frontDistance = sonar.getDistance();
}
car.stop();
Delay.msDelay(ms);
// Get the distance to the left
car.forward(0, power);
Delay.msDelay(ms);
leftDistance = sonar.getDistance();
// Get the distance to the right
car.backward(0, power);
Delay.msDelay(ms);
car.forward(power, 0);
Delay.msDelay(ms);
rightDistance = sonar.getDistance();
// Turn in the direction with most space in front of the car
if ( leftDistance > rightDistance ){
car.backward(power, 0);
Delay.msDelay(ms);
car.forward(0, power);
Delay.msDelay(ms);
}
}
}
}
......@@ -40,4 +40,10 @@ public class PrivateCar implements Car
leftMotor.controlMotor(leftPower,backward);
rightMotor.controlMotor(rightPower,backward);
}
public void spin(int power)
{
leftMotor.controlMotor(power,backward);
rightMotor.controlMotor(power,forward);
}
}
import lejos.nxt.*;
import lejos.util.Delay;
/*
* A simple behavior control program Avoid,
* Figure 9.3 in Jones, Flynn, and Seiger:
* "Mobile Robots, Inspiration to Implementation",
* Second Edition, 1999.
*/
public class Spinner {
public static void main(String [] args) throws Exception {
PrivateCar car = new PrivateCar();
int power = 70;
// ESCAPE button listener makes it possible to exit the program
// at any time.
Button.ESCAPE.addButtonListener(new ButtonListener() {
public void buttonPressed(Button b) {
System.exit(1);
}
public void buttonReleased(Button b) {
}
});
LCD.drawString("Spinner", 0, 0);
Button.waitForAnyPress();
while ( true )
{
// Spin forever
car.forward(power, 0);
car.backward(0, power);
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment