Commit 79fd83be authored by Frederik Jerløv-Troelsen's avatar Frederik Jerløv-Troelsen
Browse files

lesson1 projects

parent 9cfa53db
.classpath
.project
*.nxd
*.nxj
bin
import lejos.nxt.Button;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
Button.waitForAnyPress();
}
}
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;
final int sampleInterval = 1000; // 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);
}
}
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 = 10; // ms
DataLogger dl = new DataLogger("Sample_Reference.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((int) Runtime.getRuntime().freeMemory());
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);
}
}
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.SensorPort;
/**
* Test of raw values from a light sensor connected to SensorPort S3
*
* @author Ole Caprani
* @version 17.01.15
*/
public class SensorPortTest
{
public static void main(String[] args)
throws Exception
{
SensorPort port = SensorPort.S3;
// Initialize the sensor port with the TYPE and MODE
// of the sensor connected to the port otherwise
// readRawValue will return -1.
port.setTypeAndMode(SensorPort.TYPE_LIGHT_ACTIVE, SensorPort.MODE_RAW);
LCD.drawString("SensorPort Test:", 0, 0);
while(!Button.ESCAPE.isDown())
{
LCD.drawString("Raw: " + port.readRawValue(), 0, 2);
}
LCD.clear();
LCD.drawString("Program stopped", 0, 0);
Thread.sleep(2000);
}
}
\ No newline at end of file
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