Commit dc94edb6 authored by Stefan Michael Pedersen's avatar Stefan Michael Pedersen
Browse files

Added Lab 2 Folder.

parent 3c15ca3d
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.Button;
/**
* Example leJOS Project with an ant build file
*/
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 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(10);
}
// 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.*;
/**
* 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;
DataLogger dl = new DataLogger("Sample.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(20);
}
// 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);
}
}
This diff is collapsed.
1 54
7 54 45,410299 avg
13 54 57 max
19 54 37 min
25 54
31 54
37 55
43 55
49 55
55 55
61 56
67 55
73 55
79 55
85 55
91 56
97 56
103 56
109 56
115 56
121 56
127 56
133 57
139 57
145 57
151 57
157 57
163 57
169 57
175 57
181 57
187 57
193 57
199 57
205 57
219 56
225 57
231 57
237 57
243 57
249 57
255 56
261 56
267 56
273 56
279 56
285 55
291 54
297 51
303 44
309 39
318 37
324 37
330 37
336 37
342 37
348 37
354 37
360 37
366 37
372 38
378 38
384 38
390 38
396 39
402 39
408 39
422 39
428 39
434 39
440 39
446 39
452 39
458 39
464 39
470 39
476 39
482 38
488 38
494 38
500 38
506 38
512 38
518 38
524 39
530 39
536 39
542 39
548 39
554 39
560 39
566 39
572 39
578 39
584 39
590 39
596 39
602 39
608 39
622 38
628 38
634 38
640 38
646 38
652 38
658 38
664 38
670 38
676 38
682 38
688 38
694 38
700 38
706 37
712 37
718 37
724 37
730 38
736 37
742 37
748 37
754 37
760 38
766 38
772 38
778 38
784 38
790 39
796 39
802 41
808 42
822 48
828 50
834 52
840 53
846 54
852 55
858 55
864 56
870 56
876 56
882 56
888 56
894 56
900 56
906 56
912 56
918 56
924 55
930 54
936 53
942 52
948 50
954 47
960 44
966 41
972 39
978 38
984 38
990 37
996 37
1002 37
1008 37
1022 37
1028 37
1034 37
1040 38
1046 37
1052 38
1058 38
1064 38
1070 37
1076 37
1082 37
1088 37
1094 37
1100 37
1106 37
1112 37
1118 37
1124 37
1130 37
1136 37
1142 37
1148 37
1154 38
1160 38
1166 39
1172 40
1178 42
1184 44
1198 50
1204 52
1210 53
1216 54
1222 54
1228 55
1234 55
1240 56
1246 56
1252 56
1258 56
1264 56
1270 56
1276 56
1282 56
1288 56
1294 56
1300 55
1306 55
1312 54
1318 53
1324 52
1330 51
1336 49
1342 46
1348 43
1354 40
1360 38
1366 37
1380 37
1386 37
1392 37
1398 37
1404 37
1410 37
1416 37
1422 37
1428 37
1434 37
1440 37
1446 37
1452 37
1458 37
1464 37
1470 37
1477 37
1483 37
1489 37
1495 37
1501 38
1507 38
1513 38
1519 38
1525 38
1531 39
1537 40
1543 43
1557 49
1563 52
1569 53
1575 54
1581 55
1587 56
1593 56
1599 56
1605 56
1611 56
1617 56
1623 56
1629 56
1635 56
1641 56
1647 56
1653 56
1659 55
1665 55
1671 54
1677 53
1683 52
1689 49
1695 46
1701 43
1707 40
1713 39
1719 38
1734 37
1740 37
1746 37
1752 37
1758 37
1764 37
1770 37
1776 37
1782 37
1788 37
1794 37
1800 37
1806 37
1812 37
1818 37
1824 37
1830 37
1836 37
1842 37
1848 37
1854 38
1860 38
1866 39
1872 40
1878 43
1884 46
1890 49
1896 51
1902 52
1916 55