Commit 39dcd7bf authored by Frederik Jerløv-Troelsen's avatar Frederik Jerløv-Troelsen
Browse files

lesson2

parent 79fd83be
.classpath
.project
metadata
*.nxd
*.nxj
bin
.settings
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.nxt.comm.*;
import lejos.util.Delay;
import java.io.*;
/**
* Receive data from a PC, a phone, or another Bluetooth device.
*
* Waits for a Bluetooth connection, receives an integers and a float that are
* interpreted as minPower and gain in a Tracker.
*
* Based on Lawrie Griffiths BTSend
*
* @author Ole Caprani
* @version 27-1-15
*/
public class PCconnection extends Thread
{
private String connected = "BT Connected";
private String waiting = "BT Waiting...";
private String closing = "BT Closing...";
private BTConnection btc;
private DataInputStream dis;
private boolean running;
private Tracker tracker;
private int minPower;
private float Pgain;
public PCconnection(Tracker tracker)
{
LCD.drawString(waiting,0,0);
btc = Bluetooth.waitForConnection();
LCD.clear();
LCD.drawString(connected,0,0);
dis = btc.openDataInputStream();
this.tracker = tracker;
}
private void close()
{
LCD.clear();
LCD.drawString(closing,0,0);
try
{
dis.close();
Thread.sleep(100); // wait for data to drain
btc.close();
}
catch (Exception e)
{}
}
public void go()
{
running = true;
this.start();
}
public void stop()
{
close();
running = false;
}
public void run()
{
while ( running )
{
try
{
minPower = dis.readInt();
Pgain = dis.readFloat();
tracker.setMinPower(minPower);
tracker.setPgain(Pgain);
Delay.msDelay(100);
}
catch (Exception e)
{}
}
}
public int getMinPower()
{
return minPower;
}
public float getPgain()
{
return Pgain;
}
}
import lejos.nxt.*;
import lejos.nxt.comm.*;
import lejos.util.Delay;
import java.io.*;
/**
* Setup a Bluetooth connection to a PC that sends parameters to control
* a Tracker
*
* @author Ole Caprani
* @version 27-1-15
*/
public class RCparamTracker
{
public static void main(String [] args)
{
Tracker tracker = new Tracker();
PCconnection pc = new PCconnection(tracker);
pc.go();
tracker.go();
LCD.drawString("RCtracker", 0, 1);
while (! Button.ESCAPE.isDown())
{
LCD.drawString("minPower: " + tracker.getMinPower() + " ",0,3);
LCD.drawString("Pgain: " + tracker.getPgain() + " ",0,4);
LCD.drawString("distance: " + tracker.getDistance() + " ",0,5);
LCD.drawString("power: " + tracker.getPower() + " ",0,6);
Delay.msDelay(100);
}
tracker.stop();
pc.stop();
LCD.clear();
LCD.drawString("Program stopped", 0, 0);
Delay.msDelay(2000);
}
}
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(300);
}
LCD.clear();
LCD.drawString("Program stopped", 0, 0);
Thread.sleep(2000);
}
}
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);
}
}
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import lejos.pc.comm.*;
import java.io.*;
/**
* A GUI that makes it possible to establish a Bluetooth connection
* to an NXT running the RCparamTracker program. Two parameters in
* the tracker can be set in the GUI and send to the tracker.
*
* @author Ole Caprani 27.1.15
*
*
*/
public class TrackerController extends JFrame implements ActionListener
{
private TextField nameField = new TextField(12);
private TextField addressField = new TextField(20);
private String name = "NXT8";
private String address = "00165310C79D";
private TextField minPowerField = new TextField(10);
private TextField gainField = new TextField(10);
private NXTComm nxtComm;
private NXTInfo nxtInfo;
private OutputStream os;
private DataOutputStream dos;
private JButton connectButton = new JButton("Connect");
private JButton goButton = new JButton("Go");
/**
* Constructor builds GUI
*/
public TrackerController()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Control Tracker");
setSize(500,300);
// Labels and text fields for NXT
JPanel p1 = new JPanel();
p1.add(new JLabel("Name:"));
p1.add(nameField);
nameField.setText(name);
p1.add(new JLabel("Address:"));
p1.add(addressField);
addressField.setText(address);
try
{
nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
}
catch (NXTCommException nce) {
}
nxtInfo = new NXTInfo();
// Connect button
JPanel p2 = new JPanel();
p2.add(connectButton);
connectButton.addActionListener(this);
// Labels and text fields for parameters
JPanel p3 = new JPanel();
p3.add(new JLabel("minPower:"));
p3.add(minPowerField);
minPowerField.setText("50");
p3.add(new JLabel("Gain:"));
p3.add(gainField);
gainField.setText("1.0");
// Go button
JPanel p4 = new JPanel();
p4.add(goButton);
goButton.addActionListener(this);
// North area of the frame
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5,1));
panel.add(p1);
panel.add(p2);
panel.add(p3);
panel.add(p4);
add(panel,BorderLayout.NORTH);
}
/**
* Required by action listener;
* only action is generated by the Connect and Go button
*/
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== connectButton)
{
String name = nameField.getText();
String address = addressField.getText();
nxtInfo.name = name;
nxtInfo.deviceAddress = address;
try
{
nxtComm.open(nxtInfo);
os = nxtComm.getOutputStream();
dos = new DataOutputStream(os);
}
catch (Exception ex)
{
}
}
if(e.getSource()== goButton)
{
try
{
String minPowerString = minPowerField.getText();
int minPower = new Integer(minPowerString).intValue();
dos.writeInt(minPower);
dos.flush();
String gainString = gainField.getText();
float gain = new Float(gainString).floatValue();
dos.writeFloat(gain);
dos.flush();
}
catch (Exception ex)
{
}
}
}
/**
* Initialize the display Frame
*/
public static void main(String[] args)
{
TrackerController frame = new TrackerController();
frame.setVisible(true);
}
}
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