Commit 5395b9db authored by Mikkel's avatar Mikkel
Browse files

Added all programs for lesson 4 (might still want to do something with the color sensor).

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.lejos.nxt.ldt.LEJOS_LIBRARY_CONTAINER/nxt"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Lesson 4 - 1 (Black White Detection)</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.lejos.nxt.ldt.leJOSBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.lejos.nxt.ldt.leJOSNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
import lejos.nxt.*;
/**
* A sensor that is able to distinguish a black/dark surface
* from a white/bright surface.
*
* Light percent values from an active light sensor and a
* threshold value calculated based on a reading over a
* black/dark surface and a reading over a light/bright
* surface is used to make the distinction between the two
* types of surfaces.
*
* @author Ole Caprani
* @version 20.02.13
*/
public class BlackWhiteSensor {
private LightSensor ls;
private int blackLightValue;
private int whiteLightValue;
private int blackWhiteThreshold;
public BlackWhiteSensor(SensorPort p)
{
ls = new LightSensor(p);
// Use the light sensor as a reflection sensor
ls.setFloodlight(true);
}
private int read(String color){
int lightValue=0;
while (Button.ENTER.isDown());
LCD.clear();
LCD.drawString("Press ENTER", 0, 0);
LCD.drawString("to callibrate", 0, 1);
LCD.drawString(color, 0, 2);
while( !Button.ENTER.isPressed() ){
lightValue = ls.readValue();
LCD.drawInt(lightValue, 4, 10, 2);
LCD.refresh();
}
return lightValue;
}
public void calibrate()
{
blackLightValue = read("black");
whiteLightValue = read("white");
// The threshold is calculated as the median between
// the two readings over the two types of surfaces
blackWhiteThreshold = (blackLightValue+whiteLightValue)/2;
}
public boolean black() {
return (ls.readValue()< blackWhiteThreshold);
}
public boolean white() {
return (ls.readValue()> blackWhiteThreshold);
}
public int light() {
return ls.readValue();
}
}
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.SensorPort;
import lejos.util.Delay;
public class Test {
public static void main(String[] args) {
BlackWhiteSensor sensor = new BlackWhiteSensor(SensorPort.S1);
sensor.calibrate();
LCD.clear();
while(!Button.ESCAPE.isDown()) {
LCD.drawString("Black: " + sensor.black(), 4, 2);
LCD.drawString("White: " + sensor.white(), 4, 4);
Delay.msDelay(5);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.lejos.nxt.ldt.LEJOS_LIBRARY_CONTAINER/nxt"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Lesson 4 - 2 (Line Follower with Calibration)</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.lejos.nxt.ldt.leJOSBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.lejos.nxt.ldt.leJOSNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
import lejos.nxt.*;
/**
* A sensor that is able to distinguish a black/dark surface
* from a white/bright surface.
*
* Light percent values from an active light sensor and a
* threshold value calculated based on a reading over a
* black/dark surface and a reading over a light/bright
* surface is used to make the distinction between the two
* types of surfaces.
*
* @author Ole Caprani
* @version 20.02.13
*/
public class BlackWhiteSensor {
private LightSensor ls;
private int blackLightValue;
private int whiteLightValue;
private int blackWhiteThreshold;
public BlackWhiteSensor(SensorPort p)
{
ls = new LightSensor(p);
// Use the light sensor as a reflection sensor
ls.setFloodlight(true);
}
private int read(String color){
int lightValue=0;
while (Button.ENTER.isDown());
LCD.clear();
LCD.drawString("Press ENTER", 0, 0);
LCD.drawString("to callibrate", 0, 1);
LCD.drawString(color, 0, 2);
while( !Button.ENTER.isPressed() ){
lightValue = ls.readValue();
LCD.drawInt(lightValue, 4, 10, 2);
LCD.refresh();
}
return lightValue;
}
public void calibrate()
{
blackLightValue = read("black");
whiteLightValue = read("white");
// The threshold is calculated as the median between
// the two readings over the two types of surfaces
blackWhiteThreshold = (blackLightValue+whiteLightValue)/2;
}
public boolean black() {
return (ls.readValue()< blackWhiteThreshold);
}
public boolean white() {
return (ls.readValue()> blackWhiteThreshold);
}
public int light() {
return ls.readValue();
}
}
import lejos.nxt.*;
/**
* A locomotion module with methods to drive
* a car with two independent motors. The left motor
* should be connected to port C and the right motor
* to port B.
*
* @author Ole Caprani
* @version 17.4.08
*/
public class Car
{
// Commands for the motors
private final static int forward = 1,
backward = 2,
stop = 3;
private static MotorPort leftMotor = MotorPort.C;
private static MotorPort rightMotor= MotorPort.B;
private Car()
{
}
public static void stop()
{
leftMotor.controlMotor(0,stop);
rightMotor.controlMotor(0,stop);
}
public static void forward(int leftPower, int rightPower)
{
leftMotor.controlMotor(leftPower,forward);
rightMotor.controlMotor(rightPower,forward);
}
public static void backward(int leftPower, int rightPower)
{
leftMotor.controlMotor(leftPower,backward);
rightMotor.controlMotor(rightPower,backward);
}
}
import lejos.nxt.*;
/**
* A simple line follower for the LEGO 9797 car with a light sensor. Before the
* car is started on a line a BlackWhiteSensor is calibrated to adapt to
* different light conditions and colors.
*
* The light sensor should be connected to port 3. The left motor should be
* connected to port C and the right motor to port B.
*
* @author Ole Caprani
* @version 20.02.13
*/
public class LineFollowerCal {
public static void main(String[] aArg) throws Exception {
final int power = 80;
BlackWhiteSensor sensor = new BlackWhiteSensor(SensorPort.S1);
sensor.calibrate();
LCD.clear();
LCD.drawString("Light: ", 0, 2);
while (!Button.ESCAPE.isDown()) {
LCD.drawInt(sensor.light(), 4, 10, 2);
LCD.refresh();
if (sensor.black())
Car.forward(power, 0);
else
Car.forward(0, power);
Thread.sleep(10);
}
Car.stop();
LCD.clear();
LCD.drawString("Program stopped", 0, 0);
LCD.refresh();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.lejos.nxt.ldt.LEJOS_LIBRARY_CONTAINER/nxt"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Lesson 4 - 3 (ThreeColorSensor with Calibration)</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.lejos.nxt.ldt.leJOSBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.lejos.nxt.ldt.leJOSNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
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