Problem NXT bluetooth code

Post your NXJ projects, project ideas, etc here!

Moderators: roger, 99jonathan, imaqine

Problem NXT bluetooth code

Postby polakx » Sat Apr 28, 2012 6:23 pm

Hi, i have to do a project for my school using lejos. i decided to make a bluetooth controlled car with a third motor being used as an arm. i found on this forum the code for the bluetooth car, but when i add the third motor, i get errors such as IO Exception writeInt or

Exception:28
at: 50:35
at: 48:25
this is what shows up on the nxt. i'm new to programming, please help? this is the code i use on PC:

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 NXTremotrControl_TA extends JFrame
    {
      public static JButton quit, connect;
      public static JButton forward,reverse, leftTurn, rightTurn, stop, speedUp, slowDown;
      public static JButton grab, release;
      public static JLabel L1,L2,L3,L4,L5,L6,L7,L8,L9,L10,L11,L12;
      public static ButtonHandler bh = new ButtonHandler();
      public static DataOutputStream outData;
      public static NXTConnector link;
     
      public NXTremotrControl_TA()
      {
        setTitle ("Control");
        setBounds(650,350,500,500);
        setLayout(new GridLayout(5,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);
       
        L11 = new JLabel("");
        add(L11);
        grab = new JButton("Grab");
        grab.addActionListener(bh);
        grab.addMouseListener(bh);
        grab.addKeyListener(bh);
        add(grab);
       
        L12 = new JLabel("");
        add(L12);
        release = new JButton("Release");
        release.addActionListener(bh);
        release.addMouseListener(bh);
        release.addKeyListener(bh);
        add(release);
       
        quit = new JButton("Quit");
        quit.addActionListener(bh);
        add(quit);

      }
     
      public static void main(String[] args)
      {
         NXTremotrControl_TA NXTrc = new NXTremotrControl_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);
                if(moe.getSource() == grab)outData.writeInt(8);
                if(moe.getSource() == release)outData.writeInt(9);
             
                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);
             if(moe.getSource() == grab)outData.writeInt(80);
             if(moe.getSource() == release)outData.writeInt(90);
             
             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);
              if(ke.getKeyChar() == 'g')outData.writeInt(8);
              if(ke.getKeyChar() == 'h')outData.writeInt(9);
             
             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() == 'g'){outData.writeInt(80);}
              if(ke.getKeyChar() == 'h'){outData.writeInt(90);}
             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("btspp://"))
         {
            System.out.println("\nNo NXT find using USB");
            }
       
         outData = new DataOutputStream(link.getOutputStream());;
         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




this is what i uploaded to the NXT:

Code: Select all
/*
*
* NXT setup
*  -  Port A for right wheel
*  -  Port C for left wheel
*  -  Porb B for gripper
*  -  No sensor is needed
*

*/

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;}
       if(transmitReceived == 8) {command[6] = true;}//grab
       if(transmitReceived == 80){command[6] = false;}
       if(transmitReceived == 9) {command[7] = true;}//release
       if(transmitReceived == 90){command[7] = 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[6];

  direction[0] = D[0];
  direction[1] = D[1];
  direction[2] = D[2];
  direction[3] = D[3];
  direction[6] = D[6];
  direction[7] = D[7];
 
  movingSpeed = S;

  Motor.A.setSpeed(movingSpeed);
  Motor.C.setSpeed(movingSpeed);
  Motor.B.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();
      }
   
    if(direction[6] == true)
    {
     Motor.B.setSpeed(speedBuffer);   
     Motor.B.forward();
      }
   
    if(direction[7] == true)
    {
     Motor.B.setSpeed(speedBuffer);   
     Motor.B.backward();
      }
   
}//End move

public static void connect()
{
System.out.println("Listening");
BTLink = Bluetooth.waitForConnection();
dataOut = BTLink.openDataOutputStream();
dataIn = BTLink.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



thank you for any help.
polakx
New User
 
Posts: 4
Joined: Tue Apr 17, 2012 11:51 pm

Re: Problem NXT bluetooth code

Postby polakx » Thu May 03, 2012 4:27 pm

any ideas where i went wrong? Please helppp :|
polakx
New User
 
Posts: 4
Joined: Tue Apr 17, 2012 11:51 pm

Re: Problem NXT bluetooth code

Postby TechnoX » Thu May 03, 2012 6:15 pm

Try to compile the NXT code with the verbose option. If you use eclipse it's in the preferences for the Lejos plugin, otherwise you can add -v to the command.

Then you can look up what the 28 exception is, and where in your code it throws.


Can you try to isolate the problem in a smaller program? I don't have the time to read all this code.
Does the error exists if you scale down the program?
TechnoX
Novice
 
Posts: 51
Joined: Tue May 03, 2011 5:57 pm
Location: Sweden


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 0 guests

more stuff