Commit 1a9f89f6 authored by Mikkel's avatar Mikkel
Browse files

All programs from Lesson 1

parent aa9282aa
<?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 1 - Exercise 1 - LineFollower</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 simple line follower for the LEGO 9797 car with a single light sensor.
*
* 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.
*
* Variables initialized with a constant string are used in the LCD.drawString
* calls of the control loop to avoid frequent garbage collection.
*
* @author Ole Caprani
* @version 4.02.12
*/
public class LineFollower {
public static void main(String[] aArg) throws Exception {
String left = "Turn left ";
String right = "Turn right";
LightSensor light = new LightSensor(SensorPort.S3);
final int blackWhiteThreshold = 45; // 26 if "floodlight" is false.
final int sampleInterval = 10; // ms
final int forward = 1;
final int stop = 3;
final int flt = 4;
final int power = 80;
// Use the light sensor as a reflection sensor
light.setFloodlight(true);
LCD.drawString("Light %: ", 0, 0);
// Show light percent until LEFT is pressed
LCD.drawString("Press LEFT", 0, 2);
LCD.drawString("to start", 0, 3);
while (!Button.LEFT.isDown()) {
LCD.drawInt(light.readValue(), 3, 9, 0);
}
// Follow line until ESCAPE is pressed
LCD.drawString("Press ESCAPE", 0, 2);
LCD.drawString("to stop ", 0, 3);
while (!Button.ESCAPE.isDown()) {
if (light.readValue() > blackWhiteThreshold) {
// On white, turn right
LCD.drawString(right, 0, 1);
MotorPort.B.controlMotor(0, stop);
MotorPort.C.controlMotor(power, forward);
} else {
// On black, turn left
LCD.drawString(left, 0, 1);
MotorPort.B.controlMotor(power, forward);
MotorPort.C.controlMotor(0, stop);
}
LCD.drawInt(light.readValue(), 3, 9, 0);
Thread.sleep(sampleInterval);
}
// Stop car gently with free wheel drive
MotorPort.B.controlMotor(0, flt);
MotorPort.C.controlMotor(0, flt);
LCD.clear();
LCD.drawString("Program stopped", 0, 0);
Thread.sleep(2000);
}
}
<?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 1 - Exercise 4 - SLineFollower</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.*;
import java.io.*;
/**
* A simple data logger that can be used to sample a sequence of integer data
* values in a flash file. The file is a text consisting of a sequence of lines
* with a time value and a sample data value separated by a comma.
*
* When data has been sampled the flash file can be transfered to a PC by means
* of the tool nxjbrowse.
*
* @author Ole Caprani
* @version 4.02.13
*/
public class DataLogger {
private File f;
private FileOutputStream fos;
private int startTime;
public DataLogger(String fileName) {
try {
f = new File(fileName);
if (!f.exists()) {
f.createNewFile();
} else {
f.delete();
f.createNewFile();
}
fos = new FileOutputStream(f);
} catch (IOException e) {
LCD.drawString(e.getMessage(), 0, 0);
System.exit(0);
}
startTime = (int) System.currentTimeMillis();
}
public void start() {
startTime = (int) System.currentTimeMillis();
}
public void writeSample(int time, int sample) {
try {
Integer timeInt = new Integer(time);
String timeString = timeInt.toString();
Integer sampleInt = new Integer(sample);
String sampleString = sampleInt.toString();
for (int i = 0; i < timeString.length(); i++) {
fos.write((byte) timeString.charAt(i));
}
// Separate items with comma
fos.write((byte) (','));
for (int i = 0; i < sampleString.length(); i++) {
fos.write((byte) sampleString.charAt(i));
}
// New line
fos.write((byte) ('\r'));
fos.write((byte) ('\n'));
} catch (IOException e) {
LCD.drawString(e.getMessage(), 0, 0);
System.exit(0);
}
}
public void writeSample(int sample) {
writeSample((int) System.currentTimeMillis() - startTime, sample);
}
public void close() {
try {
fos.close();
} catch (IOException e) {
LCD.drawString(e.getMessage(), 0, 0);
System.exit(0);
}
}
}
import lejos.nxt.*;
/**
* A simple line follower for the LEGO 9797 car with a single light sensor.
*
* 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.
*
* Variables initialized with a constant string are used in the LCD.drawString
* calls of the control loop to avoid garbage collection.
*
* @author Ole Caprani
* @version 4.02.13
*/
public class SLineFollower {
public static void main(String[] aArg) throws Exception {
String left = "Turn left ";
String right = "Turn right";
LightSensor light = new LightSensor(SensorPort.S3);
final int blackWhiteThreshold = 45;
final int sampleInterval = 75; // ms
DataLogger dl = new DataLogger("Sample_75.txt");
int lightValue;
final int forward = 1;
final int stop = 3;
final int flt = 4;
final int power = 80;
// Use the light sensor as a reflection sensor
light.setFloodlight(true);
LCD.drawString("Light %: ", 0, 0);
// Show light percent until LEFT is pressed
LCD.drawString("Press LEFT", 0, 2);
LCD.drawString("to start", 0, 3);
while (!Button.LEFT.isDown()) {
LCD.drawInt(light.readValue(), 3, 9, 0);
}
// Follow line until ESCAPE is pressed
LCD.drawString("Press ESCAPE", 0, 2);
LCD.drawString("to stop ", 0, 3);
// Start datalogging
dl.start();
while (!Button.ESCAPE.isDown()) {
lightValue = light.readValue();
if (lightValue > blackWhiteThreshold) {
// On white, turn right
LCD.drawString(right, 0, 1);
MotorPort.B.controlMotor(0, stop);
MotorPort.C.controlMotor(power, forward);
} else {
// On black, turn left
LCD.drawString(left, 0, 1);
MotorPort.B.controlMotor(power, forward);
MotorPort.C.controlMotor(0, stop);
}
LCD.drawInt(lightValue, 3, 9, 0);
dl.writeSample(lightValue);
Thread.sleep(sampleInterval);
}
// Stop car gently with free wheel drive
MotorPort.B.controlMotor(0, flt);
MotorPort.C.controlMotor(0, flt);
LCD.clear();
// Close datalogger
dl.close();
LCD.drawString("Program stopped", 0, 0);
Thread.sleep(2000);
}
}
<?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 1 - Exercise 5 - SensorPortTest</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>
Markdown is supported
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