Commit 1ad4c70f authored by Enniwhere's avatar Enniwhere
Browse files

Initial commit

parents
Pipeline #3141 skipped
.idea/
build/
NXT_Programming_Lesson_7.iml
# Tells eclipse editor that we have a custom config
custom = true
nxj.home=${env.NXJ_HOME}
source.dir = src
source.pc_dir = pc_src
source.include =
source.exclude =
source.encoding = utf-8
source.warn = all,-fallthrough,-deprecation
source.version = 1.5
target.version = 1.5
build.dir=build
classes.dir=${build.dir}/classes
docs.dir=${build.dir}/docs
main.class=Vehicle1Dancing
output.basename=Vehicle1Dancing
main.pc_class=
output.pc_basename=
output.nxj=${build.dir}/${output.basename}.nxj
output.nxd=${build.dir}/${output.basename}.nxd
dist.zip = ${build.dir}/dist.zip
dist.include =
dist.exclude = bin/**, target/**, ${build.dir}/**
<project name="lego15_NXJ2" default="uploadandrun">
<description>
HelloWorld build file
</description>
<!-- set properties for this build -->
<property environment="env" />
<property file="build.properties" />
<!-- check whether nxj.home is correct -->
<available file="${nxj.home}/lib/pc" property="lejos.pc.exists" />
<available file="${nxj.home}/lib/nxt" property="lejos.nxt.exists" />
<fail unless="lejos.pc.exists" message="nxj.home variable incorrect, ${nxj.home}/lib/pc does not exist" />
<fail unless="lejos.nxt.exists" message="nxj.home variable incorrect, ${nxj.home}/lib/nxt does not exist" />
<!-- define the search paths -->
<path id="lejos.pc">
<fileset dir="${nxj.home}/lib/pc">
<include name="**/*.jar" />
</fileset>
</path>
<path id="lejos.nxt">
<fileset dir="${nxj.home}/lib/nxt">
<include name="**/*.jar" />
</fileset>
</path>
<condition property="d32Flag" value="-d32" else="-Dblank.dummy.arg=">
<and>
<os family="mac" />
</and>
</condition>
<!-- deletes generated files -->
<target name="clean" description="clean up all generated files">
<delete dir="${build.dir}" />
</target>
<target name="compile" depends="clean" description="compile the source ">
<!-- Compile the java code from ${source.dir} to ${classes.dir} -->
<mkdir dir="${classes.dir}" />
<javac
srcdir="${source.dir}"
includes="${source.include}"
excludes="${source.exclude}"
encoding="${source.encoding}"
source="${source.version}"
target="${target.version}"
destdir="${classes.dir}"
debug="true"
includeAntRuntime="false"
includeJavaRuntime="false"
verbose="false"
>
<bootclasspath refid="lejos.nxt" />
<!-- enable warnings -->
<compilerarg value="-Xlint:${source.warn}" />
<!--
Without this, there are some JDK jars in the classpath.
Set verbose=true to check that the classpath is really clean.
-->
<compilerarg value="-extdirs" />
<compilerarg value="" />
</javac>
</target>
<target name="link" depends="compile" description="link the binary ">
<!-- Link the binary and create a signature file -->
<java classname="lejos.pc.tools.NXJLink" failonerror="true">
<classpath refid="lejos.pc" />
<arg value="--bootclasspath" />
<arg pathref="lejos.nxt" />
<arg value="--classpath" />
<arg path="${classes.dir}" />
<arg value="--writeorder" />
<arg value="LE" />
<arg value="-o" />
<arg file="${output.nxj}" />
<arg value="-od" />
<arg file="${output.nxd}" />
<arg value="-v" />
<arg value="${main.class}" />
</java>
</target>
<target name="debuglink" depends="compile" description="link the binary">
<!-- Link the binary and create a signature file -->
<java classname="lejos.pc.tools.NXJLink" failonerror="true">
<classpath refid="lejos.pc" />
<arg value="--bootclasspath" />
<arg pathref="lejos.nxt" />
<arg value="--classpath" />
<arg path="${classes.dir}" />
<arg value="--writeorder" />
<arg value="LE" />
<arg value="-o" />
<arg file="${output.nxj}" />
<arg value="-od" />
<arg file="${output.nxd}" />
<arg value="-gr" />
<arg value="-v" />
<arg value="${main.class}" />
</java>
</target>
<target name="upload" depends="link" description="upload the binary">
<java classname="lejos.pc.tools.NXJUpload" fork="true" failonerror="true">
<sysproperty key="nxj.home" file="${nxj.home}"/>
<jvmarg value="${d32Flag}" />
<classpath refid="lejos.pc" />
<arg value="${output.nxj}" />
</java>
</target>
<target name="uploadandrun" depends="link" description="upload and run the binary">
<java classname="lejos.pc.tools.NXJUpload" fork="true" failonerror="true">
<sysproperty key="nxj.home" file="${nxj.home}"/>
<jvmarg value="${d32Flag}" />
<classpath refid="lejos.pc" />
<arg value="-r" />
<arg file="${output.nxj}" />
</java>
</target>
<target name="debuguploadandrun" depends="debuglink" description="upload and run the binary">
<java classname="lejos.pc.tools.NXJUpload" fork="true" failonerror="true">
<sysproperty key="nxj.home" file="${nxj.home}"/>
<jvmarg value="${d32Flag}" />
<classpath refid="lejos.pc" />
<arg value="-r" />
<arg file="${output.nxj}" />
</java>
</target>
<target name="console" depends="debuguploadandrun" description="run a console">
<sleep seconds="4" />
<java classname="lejos.pc.tools.NXJConsole" fork="true" failonerror="true">
<sysproperty key="nxj.home" file="${nxj.home}"/>
<jvmarg value="${d32Flag}" />
<classpath refid="lejos.pc" />
<arg value="-di" />
<arg file="${output.nxj}" />
</java>
</target>
<target name="consoleviewer" depends="debuguploadandrun" description="run the GUI Console Viewer">
<sleep seconds="4" />
<java classname="lejos.pc.tools.NXJConsoleViewer" fork="true" failonerror="true">
<sysproperty key="nxj.home" file="${nxj.home}"/>
<jvmarg value="${d32Flag}" />
<classpath refid="lejos.pc" />
<arg value="-di" />
<arg file="${output.nxd}" />
</java>
</target>
<target name="debugtool" description="run the debug tool">
<input
message="Exception class:"
addproperty="class"
/>
<input
message="Method number:"
addproperty="method"
/>
<input
message="PC value:"
addproperty="pc"
/>
<java classname="lejos.pc.tools.NXJDebugTool" fork="true" failonerror="true">
<sysproperty key="nxj.home" file="${nxj.home}"/>
<classpath refid="lejos.pc" />
<arg value="-di" />
<arg file="${output.nxd}" />
<arg value="-c" />
<arg value="-m" />
<arg value="${class}" />
<arg value="${method}" />
<arg value="${pc}" />
</java>
</target>
<!-- generate javadocs -->
<target name="docs" description="generate javadocs">
<delete dir="${docs.dir}" />
<mkdir dir="${docs.dir}" />
<javadoc
destdir="${docs.dir}"
source="${source.version}"
encoding="${source.encoding}"
charset="utf-8"
locale="en_US"
author="true"
access="protected"
windowtitle="Project documentation"
verbose="false"
>
<bootclasspath refid="lejos.nxt" />
<!--
Without this, there are some JDK jars in the classpath.
Set verbose=true to check that the classpath is really clean.
-->
<arg value="-extdirs" />
<arg value="" />
<packageset dir="${source.dir}"
includes="${source.include}"
excludes="${source.exclude}"
/>
</javadoc>
</target>
<!-- zip the project -->
<target name="dist.zip" description="generate zip of sources">
<zip
destfile="${dist.zip}"
basedir="${basedir}"
includes="${dist.include}"
excludes="${dist.exclude}"
/>
</target>
<!-- used only for modifying the Netbeans NXJPlugin -->
<target name="Zip for Netbeans" description="Zip the application to the sample project">
<property name="plugin" location="../NXJPlugin/src/nxjplugin/" />
<zip basedir="." destfile="${plugin}/NXJSampleProject.zip">
<exclude name="build/**" />
<exclude name="bin/**" />
<exclude name="dist/**" />
<exclude name="nbproject/private/**" />
<exclude name=".project" />
<exclude name=".classpath" />
</zip>
</target>
<target name="compile_pc" depends="clean" description="compile the source ">
<!-- Compile the java code from ${source.dir} to ${classes.dir} -->
<mkdir dir="${classes.dir}" />
<javac
srcdir="${source.pc_dir}"
includes="${source.include}"
excludes="${source.exclude}"
encoding="${source.encoding}"
source="${source.version}"
target="${target.version}"
destdir="${classes.dir}"
debug="true"
includeAntRuntime="false"
includeJavaRuntime="false"
verbose="false"
>
<classpath refid="lejos.pc"/>
<classpath refid="lejos.nxt"/>
<!-- enable warnings -->
<compilerarg value="-Xlint:${source.warn}" />
</javac>
</target>
<target name="run_pc" description="run the debug tool" depends="compile_pc">
<java classname="${main.pc_class}" fork="true" failonerror="true">
<sysproperty key="nxj.home" file="${nxj.home}"/>
<jvmarg value="${d32Flag}" />
<classpath refid="lejos.pc" />
<classpath location="${classes.dir}" />
</java>
</target>
</project>
/*
* Arbiter pass the highest priority car command
* from the behaviors to the car driver.
*/
public class Arbiter extends Thread
{
private SharedCar [] car;
private CarDriver cd;
private int winner;
public Arbiter(SharedCar [] car, CarDriver cd)
{
this.car = car;
this.cd = cd;
}
public void run()
{
while ( true )
{
for (int i=0; i < car.length; i++)
{
CarCommand carCommand = car[i].getCommand();
if ( carCommand != null)
{
cd.perform(carCommand);
winner = i;
break;
}
}
}
}
public int winner()
{
return winner;
}
}
import lejos.nxt.*;
import lejos.util.Delay;
/*
* Avoid behavior
*/
class Avoid extends Thread
{
private SharedCar car = new SharedCar();
private int power = 70, ms = 500;
UltrasonicSensor sonar = new UltrasonicSensor(SensorPort.S1);
int frontDistance, leftDistance, rightDistance;
int stopThreshold = 30;
public Avoid(SharedCar car)
{
this.car = car;
}
public void run()
{
while (true)
{
// Monitor the distance in front of the car and stop
// when an object gets to close
frontDistance = sonar.getDistance();
while ( frontDistance >= stopThreshold )
{
car.noCommand();
frontDistance = sonar.getDistance();
}
car.stop();
Delay.msDelay(ms);
// Get the distance to the left
car.forward(0, power);
Delay.msDelay(ms);
leftDistance = sonar.getDistance();
// Get the distance to the right
car.backward(0, power);
Delay.msDelay(ms);
car.forward(power, 0);
Delay.msDelay(ms);
rightDistance = sonar.getDistance();
// Turn in the direction with most space in front of the car
if ( leftDistance > rightDistance ){
car.backward(power, 0);
Delay.msDelay(ms);
car.forward(0, power);
Delay.msDelay(ms);
}
}
}
}
import lejos.nxt.*;
import lejos.util.Delay;
/*
* A simple behavior control program Avoid,
* Figure 9.3 in Jones, Flynn, and Seiger:
* "Mobile Robots, Inspiration to Implementation",
* Second Edition, 1999.
*/
public class AvoidFigure9_3 {
public static void main(String [] args) throws Exception {
PrivateCar car = new PrivateCar();
int power = 70, ms = 500;
UltrasonicSensor sonar = new UltrasonicSensor(SensorPort.S1);
int frontDistance, leftDistance, rightDistance;
int stopThreshold = 30;
// ESCAPE button listener makes it possible to exit the program
// at any time.
Button.ESCAPE.addButtonListener(new ButtonListener() {
public void buttonPressed(Button b) {
System.exit(1);
}
public void buttonReleased(Button b) {
}
});
LCD.drawString("Avoid 9.3", 0, 0);
Button.waitForAnyPress();
while ( true )
{
// Go forward
car.forward(power, power);
// Monitor the distance in front of the car and stop
// when an object gets to close
frontDistance = sonar.getDistance();
while ( frontDistance > stopThreshold )
{
frontDistance = sonar.getDistance();
}
car.stop();
// Get the distance to the left
car.forward(0, power);
Delay.msDelay(ms);
leftDistance = sonar.getDistance();
// Get the distance to the right
car.backward(0, power);
Delay.msDelay(ms);
car.forward(power, 0);
Delay.msDelay(ms);
rightDistance = sonar.getDistance();
// Turn in the direction with most space in front of the car
if ( leftDistance > rightDistance ){
car.backward(power, 0);
Delay.msDelay(ms);
car.forward(0, power);
Delay.msDelay(ms);
}
}
}
}
/**
* A locomotion interface with methods to drive
* a differential car with two independent motors.
*
* @author Ole Caprani
* @version 21.3.14
*/
public interface Car
{
public void stop();
public void forward(int leftPower, int rightPower);
public void backward(int leftPower, int rightPower);
}
public class CarCommand
{
public enum Command {
FORWARD, BACKWARD, STOP
}
public volatile Command command;
public volatile int leftPower, rightPower;
}
import lejos.nxt.*;
/*
* A car driver module with a method to drive
* a differential car with two independent motors. The left motor
* should be connected to port B and the right motor
* to port C.
*
* The car driver method turns car commands into actual commands
* for the two physical motors.
*
* @author Ole Caprani
* @version 21.3.14
*/
public class CarDriver
{
// Commands for the motors
final int forward = 1,
backward = 2,
stop = 3;
private MotorPort leftMotor = MotorPort.C;
private MotorPort rightMotor= MotorPort.B;
public CarDriver()
{
}
private int ccToMc(CarCommand.Command carCommand)
{
switch ( carCommand )
{
case FORWARD: return forward;
case BACKWARD: return backward;
case STOP: return stop;
}
return -1;
}
public void perform(CarCommand command)
{
leftMotor.controlMotor(command.leftPower, ccToMc(command.command));
rightMotor.controlMotor(command.rightPower,ccToMc(command.command));
}
}
/*
* Cruise behavior, p. 303 in
* Jones, Flynn, and Seiger:
* "Mobile Robots, Inspiration to Implementation",
* Second Edition, 1999.
*/
class Cruise extends Thread
{
private SharedCar car;
private int power = 70;
public Cruise(SharedCar car)
{
this.car = car;
}
public void run()
{
while (true)
{
/* Drive forward */
car.forward(power, power);
}
}
}
import lejos.nxt.*;
import lejos.util.Delay;
/*
* Follow behavior , inspired by p. 304 in
* Jones, Flynn, and Seiger:
* "Mobile Robots, Inspiration to Implementation",
* Second Edition, 1999.
*/
class Follow extends Thread
{
private SharedCar car = new SharedCar();
private int power = 70, ms = 500;
LightSensor light = new LightSensor(SensorPort.S4);
int frontLight, leftLight, rightLight, delta;
int lightThreshold;
public Follow(SharedCar car)
{
this.car = car;
lightThreshold = light.getLightValue();
}
public void run()
{
while (true)
{
// Monitor the light in front of the car and start to follow
// the light if light level is above the threshold
frontLight = light.getLightValue();
while ( frontLight <= lightThreshold )
{
car.noCommand();
frontLight = light.getLightValue();