Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Group 5
Group 5 - Lesson 1
Commits
1a9f89f6
Commit
1a9f89f6
authored
Mar 02, 2015
by
Mikkel
Browse files
All programs from Lesson 1
parent
aa9282aa
Changes
30
Hide whitespace changes
Inline
Side-by-side
Lesson 1 - Exercise 1 - LineFollower/.classpath
0 → 100644
View file @
1a9f89f6
<?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>
Lesson 1 - Exercise 1 - LineFollower/.project
0 → 100644
View file @
1a9f89f6
<?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>
Lesson 1 - Exercise 1 - LineFollower/LineFollower.nxd
0 → 100644
View file @
1a9f89f6
File added
Lesson 1 - Exercise 1 - LineFollower/LineFollower.nxj
0 → 100644
View file @
1a9f89f6
File added
Lesson 1 - Exercise 1 - LineFollower/bin/LineFollower.class
0 → 100644
View file @
1a9f89f6
File added
Lesson 1 - Exercise 1 - LineFollower/src/LineFollower.java
0 → 100644
View file @
1a9f89f6
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
);
}
}
Lesson 1 - Exercise 4 - SLineFollower/.classpath
0 → 100644
View file @
1a9f89f6
<?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>
Lesson 1 - Exercise 4 - SLineFollower/.project
0 → 100644
View file @
1a9f89f6
<?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>
Lesson 1 - Exercise 4 - SLineFollower/SLineFollower.nxd
0 → 100644
View file @
1a9f89f6
File added
Lesson 1 - Exercise 4 - SLineFollower/SLineFollower.nxj
0 → 100644
View file @
1a9f89f6
File added
Lesson 1 - Exercise 4 - SLineFollower/bin/DataLogger.class
0 → 100644
View file @
1a9f89f6
File added
Lesson 1 - Exercise 4 - SLineFollower/bin/SLineFollower.class
0 → 100644
View file @
1a9f89f6
File added
Lesson 1 - Exercise 4 - SLineFollower/src/DataLogger.java
0 → 100644
View file @
1a9f89f6
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
);
}
}
}
Lesson 1 - Exercise 4 - SLineFollower/src/SLineFollower.java
0 → 100644
View file @
1a9f89f6
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
);
}
}
Lesson 1 - Exercise 5 - SensorPortTest/.classpath
0 → 100644
View file @
1a9f89f6
<?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>
Lesson 1 - Exercise 5 - SensorPortTest/.project
0 → 100644
View file @
1a9f89f6
<?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>
Lesson 1 - Exercise 5 - SensorPortTest/SLineFollower.nxd
0 → 100644
View file @
1a9f89f6
File added
Lesson 1 - Exercise 5 - SensorPortTest/SLineFollower.nxj
0 → 100644
View file @
1a9f89f6
File added
Lesson 1 - Exercise 5 - SensorPortTest/SensorPortTest.nxd
0 → 100644
View file @
1a9f89f6
File added
Lesson 1 - Exercise 5 - SensorPortTest/SensorPortTest.nxj
0 → 100644
View file @
1a9f89f6
File added
Prev
1
2
Next
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment