I'm no expert myself, so I would love to hear comments from other people too.
The program may seem to have lots of unnecessary code in it that because I plan to use them on my project later on.
The NXT setup for this program is very simple. You will only need to have the Brick and Two Motors, no sensor is needed.
- Code: Select all
/*
* September 21, 2009
* Author Tawat Atigarbodee
*
* This program creates a Control Window for controlling NXT brick running NXTtr.java via USB.
*
* To compile this program.
* - Install Lejos 0.8.5
* - Include Lejos_nxj library to the project path
* - Compile the program with javac (I use Eclipse)
*
* To use this program
* - At NXT brick, run NXTtr.java
* - Run NXTremoteControl_TA
* - **Click “Connect” button first**
* - Control the robot by using buttons or keyboard
* a, w, s, d for direction
* i for speed up and k for slow down
*
* Note: This program is a partial of my project file.
* I use “USBSend” and “USBReceive” created by Lawrie Griffiths
* as a pattern for creating USB communication between PC and NXT.
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import lejos.pc.comm.NXTConnector;
public class NXTremoteControl_TA extends JFrame
{
public static JButton quit, connect;
public static JButton forward,reverse, leftTurn, rightTurn, stop, speedUp, slowDown;
public static JLabel L1,L2,L3,L4,L5,L6,L7,L8,L9,L10;
public static ButtonHandler bh = new ButtonHandler();
public static DataOutputStream outData;
public static NXTConnector link;
public NXTremoteControl_TA()
{
setTitle ("Control");
setBounds(650,350,500,500);
setLayout(new GridLayout(4,5));
L1 = new JLabel("");
add(L1);
forward = new JButton("Forward");
forward.addActionListener(bh);
forward.addMouseListener(bh);
forward.addKeyListener(bh);
add(forward);
L2 = new JLabel("");
add(L2);
L3 = new JLabel("");
add(L3);
speedUp = new JButton("Accelerate");
speedUp.addActionListener(bh);
speedUp.addMouseListener(bh);
speedUp.addKeyListener(bh);
add(speedUp);
leftTurn = new JButton("Left");
leftTurn.addActionListener(bh);
leftTurn.addMouseListener(bh);
leftTurn.addKeyListener(bh);
add(leftTurn);
stop = new JButton("Stop");
stop.addActionListener(bh);
stop.addMouseListener(bh);
stop.addKeyListener(bh);
add(stop);
rightTurn = new JButton("Right");
rightTurn.addActionListener(bh);
rightTurn.addMouseListener(bh);
rightTurn.addKeyListener(bh);
add(rightTurn);
L4 = new JLabel("");
add(L4);
slowDown = new JButton("Decelerate");
slowDown.addActionListener(bh);
slowDown.addMouseListener(bh);
slowDown.addKeyListener(bh);
add(slowDown);
L5 = new JLabel("");
add(L5);
reverse = new JButton("Reverse");
reverse.addActionListener(bh);
reverse.addMouseListener(bh);
reverse.addKeyListener(bh);
add(reverse);
L6 = new JLabel("");
add(L6);
L7 = new JLabel("");
add(L7);
L8 = new JLabel("");
add(L8);
connect = new JButton(" Connect ");
connect.addActionListener(bh);
connect.addKeyListener(bh);
add(connect);
L9 = new JLabel("");
add(L9);
L10 = new JLabel("");
add(L10);
quit = new JButton("Quit");
quit.addActionListener(bh);
add(quit);
}
public static void main(String[] args)
{
NXTremoteControl_TA NXTrc = new NXTremoteControl_TA();
NXTrc.setVisible(true);
NXTrc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//End main
private static class ButtonHandler implements ActionListener, MouseListener, KeyListener
{
//***********************************************************************
//Buttons action
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == quit) {System.exit(0);}
if(ae.getSource() == connect) {connect();}
try{
if(ae.getSource() == speedUp) {outData.writeInt(6);}
if(ae.getSource() == slowDown) {outData.writeInt(7);}
outData.flush(); //This forces any buffered output bytes to be written out to the stream.
}
catch (IOException ioe) {
System.out.println("\nIO Exception writeInt");
}
}//End ActionEvent(for buttons)
//***********************************************************************
//Mouse actions
public void mouseClicked(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
public void mousePressed(MouseEvent moe)
{
try {
if(moe.getSource() == forward)outData.writeInt(1);
if(moe.getSource() == reverse)outData.writeInt(2);
if(moe.getSource() == leftTurn)outData.writeInt(3);
if(moe.getSource() == rightTurn)outData.writeInt(4);
if(moe.getSource() == speedUp)outData.writeInt(6);
if(moe.getSource() == slowDown)outData.writeInt(7);
outData.flush();
}
catch (IOException ioe) {
System.out.println("\nIO Exception writeInt");
}
}//End mousePressed
public void mouseReleased(MouseEvent moe)
{
try {
if(moe.getSource() == forward ||
moe.getSource() == reverse ||
moe.getSource() == leftTurn ||
moe.getSource() == rightTurn)
{outData.writeInt(5);}
if(moe.getSource() == slowDown)outData.writeInt(60);
if(moe.getSource() == speedUp)outData.writeInt(70);
outData.flush();
}
catch (IOException ioe) {
System.out.println("\nIO Exception writeInt");
}
}//End mouseReleased
//***********************************************************************
//Keyboard action
public void keyPressed(KeyEvent ke) {}//End keyPressed
public void keyTyped(KeyEvent ke)
{
try {
if(ke.getKeyChar() == 'w')outData.writeInt(1);
if(ke.getKeyChar() == 's')outData.writeInt(2);
if(ke.getKeyChar() == 'a')outData.writeInt(3);
if(ke.getKeyChar() == 'd')outData.writeInt(4);
if(ke.getKeyChar() == 'i')outData.writeInt(6);
if(ke.getKeyChar() == 'k')outData.writeInt(7);
outData.flush();
}
catch (IOException ioe) {
System.out.println("\nIO Exception writeInt");
}
}//End keyTyped
public void keyReleased(KeyEvent ke)
{
try {
if(ke.getKeyChar() == 'w'){outData.writeInt(10);}
if(ke.getKeyChar() == 's'){outData.writeInt(20);}
if(ke.getKeyChar() == 'a'){outData.writeInt(30);}
if(ke.getKeyChar() == 'd'){outData.writeInt(40);}
if(ke.getKeyChar() == 'i'){outData.writeInt(60);}
if(ke.getKeyChar() == 'k'){outData.writeInt(70);}
if(ke.getKeyChar() == 'q'){System.exit(0);}
outData.flush();
}
catch (IOException ioe) {
System.out.println("\nIO Exception writeInt");
}
}//End keyReleased
}//End ButtonHandler
public static void connect()
{
link = new NXTConnector();
if (!link.connectTo("usb://"))
{
System.out.println("\nNo NXT find using USB");
}
outData = link.getDataOut();
System.out.println("\nNXT is Connected");
}//End connect
public static void disconnect()
{
try{
outData.close();
link.close();
}
catch (IOException ioe) {
System.out.println("\nIO Exception writing bytes");
}
System.out.println("\nClosed data streams");
}//End disconnect
}//End ControlWindow class
Download this one onto NXT brick
- Code: Select all
/*
* September 21, 2009
* Author by Tawat Atigarbodee
*
* Install this program on to NXT brick and use it with NXTremoteControl_TA.java
*
* To use this program.
* - Install Lejos 0.8.5
* - Include Lejos_nxj library to the project path
* - Upload the program using lejosdl.bat (I use Eclipse)
* - To exit the program, restart NXT brick (remove battery)
*
* NXT setup
* - Port A for right wheel
* - Port C for left wheel
* - No sensor is needed
*
* Note: This program is a partial of my project file.
* I use “USBSend” and “USBReceive” created by Lawrie Griffiths
* as a pattern for creating USB communication between PC and NXT.
*/
import java.io.*;
import lejos.nxt.*;
import lejos.nxt.comm.*;
public class NXTtr
{
public static DataOutputStream dataOut;
public static DataInputStream dataIn;
public static USBConnection USBLink;
public static BTConnection BTLink;
public static BTConnection btLink;
public static int speed =50, turnSpeed = 50,speedBuffer, speedControl;
public static int commandValue,transmitReceived;
public static boolean[] control = new boolean[6];
public static boolean[] command = new boolean[6];
public static void main(String [] args)
{
connect();
while(true)
{
control = checkCommand();
speedControl = getSpeed(control);
move(control, speedControl);
}
}//End main
public static boolean[] checkCommand()//check input data
{
try {
transmitReceived = dataIn.readInt();
if(transmitReceived == 1) {command[0] = true;}//forward
if(transmitReceived == 10){command[0] = false;}
if(transmitReceived == 2) {command[1] = true;}//backward
if(transmitReceived == 20){command[1] = false;}
if(transmitReceived == 3) {command[2] = true;}//leftTurn
if(transmitReceived == 30){command[2] = false;}
if(transmitReceived == 4) {command[3] = true;}//rightTurn
if(transmitReceived == 40){command[3] = false;}
if(transmitReceived == 5) {command[0] = false;//stop
command[1] = false;
command[2] = false;
command[3] = false;}
if(transmitReceived == 6) {command[4] = true;}//speed up
if(transmitReceived == 60){command[4] = false;}
if(transmitReceived == 7) {command[5] = true;}//slow down
if(transmitReceived == 70){command[5] = false;}
else{};
}
catch (IOException ioe) {
System.out.println("IO Exception readInt");
}
return command;
}//End checkCommand
public static void move(boolean[]D, int S)
{
int movingSpeed;
boolean[] direction = new boolean[4];
direction[0] = D[0];
direction[1] = D[1];
direction[2] = D[2];
direction[3] = D[3];
movingSpeed = S;
Motor.A.setSpeed(movingSpeed);
Motor.C.setSpeed(movingSpeed);
if(direction[0] == true)
{
Motor.A.forward();
Motor.C.forward();
}
if(direction[1] == true)
{
Motor.A.backward();
Motor.C.backward();
}
if(direction[2] == true)
{
Motor.A.setSpeed(turnSpeed);
Motor.C.setSpeed(turnSpeed);
Motor.A.forward();
Motor.C.backward();
}
if(direction[3] == true)
{
Motor.A.setSpeed(turnSpeed);
Motor.C.setSpeed(turnSpeed);
Motor.A.backward();
Motor.C.forward();
}
if(direction[0] == true && direction[2] == true)
{
speedBuffer = (int) (movingSpeed *1.5);
Motor.A.setSpeed(speedBuffer);
Motor.C.forward();
Motor.A.forward();
}
if(direction[0] == true && direction[3] == true)
{
speedBuffer = (int) (movingSpeed *1.5);
Motor.C.setSpeed(speedBuffer);
Motor.C.forward();
Motor.A.forward();
}
if(direction[1] == true && direction[2] == true)
{
speedBuffer = (int) (movingSpeed *1.5);
Motor.A.setSpeed(speedBuffer);
Motor.C.backward();
Motor.A.backward();
}
if(direction[1] == true && direction[3] == true)
{
speedBuffer = (int) (movingSpeed *1.5);
Motor.C.setSpeed(speedBuffer);
Motor.C.backward();
Motor.A.backward();
}
if(direction[0] == false && direction[1] == false &&
direction[2] == false && direction[3] == false)
{
Motor.A.stop();
Motor.C.stop();
}
}//End move
public static void connect()
{
System.out.println("Listening");
//BTLink = Bluetooth.waitForConnection();
//dataOut = BTLink.openDataOutputStream();
//dataIn = BTLink.openDataInputStream();
USBLink = USB.waitForConnection();
dataOut = USBLink.openDataOutputStream();
dataIn = USBLink.openDataInputStream();
}//End connect
public static int getSpeed(boolean[] D)
{
boolean accelerate, decelerate;
accelerate = D[4];
decelerate = D[5];
if(accelerate == true)
{
speed += 50;
command[4] = false;
}
if(decelerate == true)
{
speed -= 50;
command[5] = false;
}
return speed;
}//End getSpeed
}//NXTtr Class
