Commit 6a807796 authored by Mikkel's avatar Mikkel
Browse files

All programs for lesson 2

parents
<?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 2 - Exercise 1</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 sonar sensor test program.
*
* The sensor should be connected to port 1. In the known bugs and limitations
* of leJOS NXJ version alfa_03 it is mentioned that a gap of at least 300 msec
* is needed between calls of getDistance. This is the reason for the delay of
* 300 msec between sonar readings in the loop.
*
* @author Ole Caprani
* @version 30.08.07
*/
public class SonicSensorTest {
public static void main(String[] args) throws Exception {
UltrasonicSensor us = new UltrasonicSensor(SensorPort.S1);
LCD.drawString("Ultrasonic ", 0, 0);
LCD.drawString("Distance(cm) ", 0, 2);
while (!Button.ESCAPE.isDown()) {
LCD.drawInt(us.getDistance(), 3, 13, 2);
Thread.sleep(1);
}
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 2 - Exercise 4</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 locomotion module with methods to drive a car with two independent motors.
* The left motor should be connected to port C and the right motor to port B.
*
* @author Ole Caprani
* @version 17.4.08
*/
public class Car {
// Commands for the motors
private final static int forward = 1, backward = 2, stop = 3;
private static MotorPort leftMotor = MotorPort.C;
private static MotorPort rightMotor = MotorPort.B;
private Car() {
}
public static void stop() {
leftMotor.controlMotor(0, stop);
rightMotor.controlMotor(0, stop);
}
public static void forward(int leftPower, int rightPower) {
leftMotor.controlMotor(leftPower, forward);
rightMotor.controlMotor(rightPower, forward);
}
public static void backward(int leftPower, int rightPower) {
leftMotor.controlMotor(leftPower, backward);
rightMotor.controlMotor(rightPower, backward);
}
}
import lejos.nxt.*;
import lejos.util.Delay;
/**
* A LEGO 9797 car with a sonar sensor. The sonar is used to maintain the car at
* a constant distance to objects in front of the car.
*
* The sonar sensor should be connected to port 1. The left motor should be
* connected to port C and the right motor to port B.
*
* @author Ole Caprani
* @version 24.08.08
*/
public class Tracker extends Thread {
private boolean running;
private int sampleInterval = 100; // ms default value
private UltrasonicSensor us = new UltrasonicSensor(SensorPort.S1);
private final int noObject = 255;
private int distance, desiredDistance = 35, // cm, default value
power, minPower = 50, maxPower = 100; // default values
private float error, Pgain = 2.0f; // default value
public Tracker() {
}
public void go() {
running = true;
this.start();
}
public void stop() {
running = false;
}
public void run() {
while (running) {
distance = us.getDistance();
if (distance != noObject) {
error = distance - desiredDistance;
power = (int) (Pgain * error);
if (error > 0) {
power = Math.min(minPower + power, maxPower);
Car.forward(power, power);
} else {
power = Math.min(minPower + Math.abs(power), maxPower);
Car.backward(power, power);
}
Delay.msDelay(sampleInterval);
}
}
}
public int getDistance() {
return distance;
}
public int getPower() {
return power;
}
public int getMinPower() {
return minPower;
}
public float getPgain() {
return Pgain;
}
public void setMinPower(int minPower) {
this.minPower = minPower;
}
public void setPgain(float Pgain) {
this.Pgain = Pgain;
}
}
import lejos.nxt.*;
import lejos.util.Delay;
/**
* A simple main program to test the Tracker class.
*
* @author Ole Caprani
* @version 02.02.15
*/
public class TrackerTester {
public static void main(String[] aArg) {
int distance, power;
Tracker tracker = new Tracker();
LCD.drawString("Tracker", 0, 0);
LCD.drawString("Distance: ", 0, 3);
LCD.drawString("Power: ", 0, 4);
tracker.go();
while (!Button.ESCAPE.isDown()) {
distance = tracker.getDistance();
power = tracker.getPower();
LCD.drawInt(distance, 4, 10, 3);
LCD.drawInt(power, 4, 10, 4);
Delay.msDelay(100);
}
tracker.stop();
LCD.clear();
LCD.drawString("Program stopped", 0, 0);
Delay.msDelay(2000);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.lejos.nxt.ldt.LEJOS_LIBRARY_CONTAINER/pc"/>
<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 2 - Exercise 5 (PC)</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
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