Sunday, July 4, 2010

Report 3 | #4th of July 2010

The code that controls our test chassis from Java GUI that we have designed at the summer project team of Robotica Oslonesis. Special thanks to [b]Ehab[/b] who did most of the job in designing the GUI/debugging and making the moves as smooth as possible :-) We believe our code still is not perfect and we are going to keep working on it ...

The code is consist of 3 classes(Main, Frame & arudinoCom)



Download the files ...

Main.java
[code]
public class Main {

static arduinoCom myArduino;

public static void main(String[] args){

Frame main = new Frame();

}//end of frame
}
[/code]

[b]Frame.java[/b]
[code]
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;


class Frame extends JFrame implements MouseListener, KeyListener {

JButton ButtonF, ButtonB, ButtonR, ButtonL, ButtonS;
JLabel x, Aspeed, Dspeed;
int speed;
boolean exit;
JPanel baby;

arduinoCom myArduino;

public Frame()
{
speed = 0;
setTitle("Vehicle Control Window");
setSize(500,600);
setBackground(Color.BLACK);
exit=false;

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
exit = true;
System.exit(0);
}
} );
start();
}



public void ButtonFmousePressed(MouseEvent e){
myArduino.sendData('F');

/*int x = e.getButton();
int y = e.getButton() ;

System.out.println(x);
System.out.println(y);
if (x>199 && x<301 && y>349 && y<451) myArduino.sendData('F'); else if (x>200 && x<302 && y>450 && y<552) myArduino.sendData('B'); else if (x>99 && x<201 && y>450 && y<552) myArduino.sendData('L'); else if(x>300 && x<403 && y>450 && y<552) myArduino.sendData('R'); */ } public void ButtonBmousePressed(MouseEvent e){ myArduino.sendData('B'); } public void ButtonRmousePressed(MouseEvent e){ myArduino.sendData('R'); } public void ButtonLmousePressed(MouseEvent e){ myArduino.sendData('L'); } public void ButtonFmouseReleased(MouseEvent e) { myArduino.sendData('r'); /* int x = e.getXOnScreen(); int y = e.getYOnScreen(); if (x>199 && x<301 && y>349 && y<451) myArduino.sendData('r'); else if (x>200 && x<302 && y>450 && y<552) myArduino.sendData('r'); else if (x>99 && x<201 && y>450 && y<552) myArduino.sendData('L'); else if(x>300 && x<403 && y>450 && y<552)
myArduino.sendData('R');

*/
}

public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}



public void keyPressed(KeyEvent e){

System.out.println(e.getKeyChar() + " djgdub");

}
public void keyReleased(KeyEvent e){



}
public void keyTyped(KeyEvent e){
}


public void ButtonBmouseReleased(MouseEvent e) {

myArduino.sendData('r');
}




public void start(){

myArduino = new arduinoCom();
myArduino.start("COM7", 9600);

baby = new JPanel(new GridLayout());
baby.setBackground(Color.BLACK);
baby.setFocusable(true);


x = new JLabel("Controls");
x.setVerticalAlignment(SwingConstants.TOP);
//x.setHorizontalAlignment(SwingConstants.RIGHT);
x.setLocation(140, 10);
x.setFont(new Font("GENEVA", Font.BOLD, 20));
x.setForeground(Color.WHITE);

Dspeed = new JLabel("Desired Speed");
Dspeed.setFont(new Font("SansSerif", Font.BOLD, 16));
Dspeed.setVerticalAlignment(SwingConstants.CENTER);
Dspeed.setHorizontalAlignment(SwingConstants.LEFT);
Dspeed.setLocation(10, 200);
Dspeed.setForeground(Color.WHITE);



Aspeed = new JLabel("Actual Speed");
Aspeed.setFont(new Font("SansSerif", Font.BOLD, 16));
Aspeed.setVerticalAlignment(SwingConstants.CENTER);
Aspeed.setHorizontalAlignment(SwingConstants.RIGHT);
Aspeed.setLocation(300, 200);
Aspeed.setForeground(Color.WHITE);


ButtonF = new JButton("Forward");
ButtonF.setFont(new Font("SansSerif", Font.BOLD, 14));
ButtonF.setLocation(200,350);
ButtonF.setSize(100,100);
ButtonF.setFocusable(false);

ButtonB = new JButton("Reverse");
ButtonB.setFont(new Font("SansSerif", Font.BOLD, 14));
ButtonB.setLocation(201,451);
ButtonB.setSize(100,100);
ButtonB.setFocusable(false);

ButtonR = new JButton("Right");
ButtonR.setFont(new Font("SansSerif", Font.BOLD, 14));
ButtonR.setLocation(302,451);
ButtonR.setSize(100,100);
ButtonR.setFocusable(false);

ButtonL = new JButton("Left");
ButtonL.setFont(new Font("SansSerif", Font.BOLD, 14));
ButtonL.setLocation(100,451);
ButtonL.setSize(100,100);
ButtonL.setFocusable(false);



ButtonF.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
ButtonFmousePressed(e);
}
public void mouseReleased(MouseEvent e){
ButtonFmouseReleased(e);
}
});





ButtonB.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
ButtonBmousePressed(e);
}
public void mouseReleased(MouseEvent e){
ButtonBmouseReleased(e);
}
});



ButtonL.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
ButtonLmousePressed(e);
}
});



ButtonR.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
ButtonRmousePressed(e);
}
});

/*

ButtonF.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
System.out.println(e.getSource());
//myArduino.sendData('F');
}
});
ButtonL.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
ButtonL.setForeground(Color.RED);
myArduino.sendData('L');
}
});
ButtonB.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
ButtonB.setForeground(Color.RED);
myArduino.sendData('B');
}
});
ButtonR.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
ButtonR.setForeground(Color.RED);
myArduino.sendData('R');
}
});
*/
addMouseListener(this);

baby.addKeyListener(new KeyListener() {

@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

@Override
public void keyReleased(KeyEvent e) {
myArduino.sendData('r');

}

@Override
public void keyPressed(KeyEvent e) {
int x = e.getKeyCode();
if (x==37)
myArduino.sendData('L');
else if(x==38)
myArduino.sendData('F');
else if(x==39)
myArduino.sendData('R');
else if(x==40)
myArduino.sendData('B');

}
});



add(ButtonF);
add(ButtonB);
add(ButtonR);
add(ButtonL);
getContentPane().add(baby);
baby.add(x);
baby.add(Aspeed);
baby.add(Dspeed);


//this.repaint();
this.setVisible(true);

while(exit != true){
//update current speed
this.repaint();
}
}

}
[/code]

[b]arduinoCom.java[/b]
[code]//This class:
// - Starts up the communication with the Arduino.
// - Reads the data coming in from the Arduino and
// converts that data in to a useful form.
// - Closes communication with the Arduino.

//Code builds upon this great example:
//http://www.csc.kth.se/utbildning/kth/kurser/DH2400/interak06/SerialWork.java
//The addition being the conversion from incoming characters to numbers.

//Load Libraries
import java.io.*;
import java.util.TooManyListenersException;

//Load RXTX Library
import gnu.io.*;

class arduinoCom implements SerialPortEventListener
{

//Used to in the process of converting the read in characters-
//-first in to a string and then into a number.
String rawStr="";

//Declare serial port variable
SerialPort mySerialPort;

//Declare input steam
InputStream in;
OutputStream out;

boolean stop=false;


//This open's the communcations port with the arduino
public void start(String portName,int baudRate)
{

stop=false;
try
{
//Finds and opens the port
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM7");
mySerialPort = (SerialPort)portId.open("my_java_serial" + "COM7", 2000);
System.out.println("Serial port found and opened");

//configure the port
try
{
mySerialPort.setSerialPortParams(baudRate,
mySerialPort.DATABITS_8,
mySerialPort.STOPBITS_1,
mySerialPort.PARITY_NONE);
System.out.println("Serial port params set: "+baudRate);
}
catch (UnsupportedCommOperationException e)
{
System.out.println("Probably an unsupported Speed");
}

//establish stream for reading from the port
try
{
in = mySerialPort.getInputStream();
out = mySerialPort.getOutputStream();
}
catch (IOException e)
{
System.out.println("couldn't get streams");
}

// we could read from "in" in a separate thread, but the API gives us events
try
{
mySerialPort.addEventListener(this);
mySerialPort.notifyOnDataAvailable(true);
System.out.println("Event listener added");
}
catch (TooManyListenersException e)
{
System.out.println("couldn't add listener");
}
}
catch (Exception e)
{
System.out.println("Port in Use: "+e);
}
}

//Used to close the serial port
public void closeSerialPort()
{
try
{
in.close();
stop=true;
mySerialPort.close();
System.out.println("Serial port closed");

}
catch (Exception e)
{
System.out.println(e);
}
}

//Method sends a character
public void sendData(char ch)
{
try
{
System.out.println("We are in sendData");
out.write('V');
out.write('4');
out.write('0');
out.write('#');
out.write(ch);
}
catch (Exception e)
{
System.out.println(e);
}
}



/*
This method reads the characters sent by the arduino
The Arduino output looks like this:

128.43A153.25B0.84C242.62D0.63E128.43A153.25B0.84C242.62D0.63E128.43A153.25B0.84C242.62D0.63E

The method below divides this continuous stream of characters send by the Arduino into values
The letter after the numbers plus decimal point identifies the numbers and decimal point that
preceded it.

So:

128.43A: value = 128.43 with identifier A (A in this case is real power)

Lets go through this in further detail:

Program recieves characters one after the other like this:
ch = 1 - isDigit(ch) = true and so it is added to the string rawStr
ch = 2 - isDigit(ch) = true and so it is added to the string rawStr
ch = 8 - isDigit(ch) = true and so it is added to the string rawStr
ch = . - isDigit(ch) = false but we check if ch='.' and if so it is added to the string rawStr
ch = 4 - isDigit(ch) = true and so it is added to the string rawStr
ch = 3 - isDigit(ch) = true and so it is added to the string rawStr
ch = A - isDigit(ch) = false but - isLetter(ch) = true and so we then convert the rawStr that is now "128.43" to a double with double value = Double.parseDouble(rawStr);
Now we have a value with an identifier and we then print the value to terminal but we could just as well
if (ch=='A') realPower = value;

The last thing we do is reset rawStr="" and then the above process is repeated.

*/
public void serialEvent(SerialPortEvent event)
{

//Reads in data while data is available
while (event.getEventType()== SerialPortEvent.DATA_AVAILABLE)
{
try
{
//-------------------------------------------------------------------

//Read in the available character
//System.out.println("Inside SerialPortEvetn");
char ch = (char)in.read();
if (ch == 'x')
System.out.print(ch);
break;
}
catch (IOException e)
{
}
}//end of while


}
}[/code]

No comments:

Post a Comment