Controlling NXT Car (steer driving) with another NXT.

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

Controlling NXT Car (steer driving) with another NXT.

Postby Joshamuffin » Tue Jun 02, 2009 10:21 pm

My second NXT has two motors used as rotation sensors. It sends two integers at a time out of bluetooth towards my RC NXT Car. I'm trying to figure out how to get the car to drive in accordance to my commands. Here is the I wrote code and then started playing with it. First I tried to shove everything into main. I tried to multithread it. (My first attempt at multithreading.) I'm not sure if this is the best route. It still doesn't work.

In both codes, Motor B is steering, Motor C powers the drive wheels.


Here is the car's code:
Code: Select all
import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;

public class DriveSteer //program on the car
{
   public static void main(String [] args)
   throws Exception
   {
      BTConnection mastercomm = Bluetooth.waitForConnection(); //wait for connection
      DataInputStream btin = mastercomm.openDataInputStream(); //bluetooth input
      SteerThread strthrd = new SteerThread(btin); //steering thread
      DriveThread drvthrd = new DriveThread(btin); //driving thread
      strthrd.start(); //start receiving the steering
      drvthrd.start(); //start reveiving the driving
      btin.close(); //close connectionn
      mastercomm.close(); //close
   }
}

class SteerThread //steering
extends Thread
implements Runnable
{
   protected DataInputStream btin;
   SteerThread(DataInputStream btinArg){btin = btinArg;} //copy bluetooth input
   public void run()
   {
      int steer = 0;
      while(!Button.ENTER.isPressed()) //while the orange button isn't pressed
      {
         try
         {
            steer = btin.readInt(); //try to receive
         }
         catch(IOException e)
         {
            //stay still
         }
         Motor.B.rotateTo(steer); //act upon reception
      }
      try
      {
         btin.close(); //try to close
      }
      catch(IOException e)
      {
         //nothing
      }
   }
}


class DriveThread //driving
extends Thread
implements Runnable
{
   protected DataInputStream btin;
   DriveThread(DataInputStream btinArg) {btin = btinArg;} //copy input stream
   public void run()
   {
      int drive = 0;
      while(!Button.ENTER.isPressed()) //while orange button has not been pressed
      {
         try
         {
            drive = btin.readInt(); //try to receive
         }
         catch(IOException e)
         {
            drive = 0; //stay still
         }
         MotorPort.C.controlMotor(drive,1); //act upon reception
      }
      try
      {
         btin.close(); //try to close
      }
      catch(IOException e)
      {
         //nothing
      }
   }
}







Here is the code for the controller:
Code: Select all
import java.io.IOException;

import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;
import javax.bluetooth.*;

public class ControlSteer
{
   public static void main(String[] args)
   throws Exception
   {
      Motor.B.flt();
      Motor.C.flt();
      String slavename;
      slavename = "Joshamfn"; //change based on driving NXT Brick
      RemoteDevice slave = Bluetooth.getKnownDevice(slavename); //find slave
      if(slave != null) //if device exists
      {
         BTConnection slavecomm = Bluetooth.connect(slave); //connect to slave
         if(slavecomm != null) //if connection successful
         {
            DataOutputStream btout = slavecomm.openDataOutputStream(); //initialize command stream
            while(!Button.ENTER.isPressed()) //command continuously
            {
               btout.writeInt(-(Motor.B.getTachoCount()/4));
               btout.writeInt(Motor.C.getTachoCount());
               btout.flush(); //send commands
            }
            btout.close(); //close
            slavecomm.close(); //close
         }
      }
   }
}
RAWR
Joshamuffin
New User
 
Posts: 2
Joined: Tue Jun 02, 2009 10:11 pm
Location: Missouri

Postby pEhrlich » Tue Jun 02, 2009 10:28 pm

Hmm. Whats happening - do you have any diagnostics for us? Thread issues? BT issues? motor issues?

A couple things catch the eye:

There are two methods of threading: extending thread and implementing runnable. From what I've read, runnable is the preferred solution, so make your classes only do that.

Then you need to start it a little differently. Here's a nice working example for a runnable class WriteAccel I made recently:
Code: Select all
Runnable a = new WriteAccel();
      Thread b = new Thread(a);
      b.start();


Let me know if this helps..
User avatar
pEhrlich
Advanced Member
 
Posts: 158
Joined: Fri Jan 04, 2008 1:38 am

Postby Joshamuffin » Tue Jun 02, 2009 10:52 pm

Thanks for the thread input. I'll read some more on it.


In the meantime, I shoved it all back into main. I finally realized what the immediate return argument meant. I got it to work somewhat but it is extremely unresponsive. It does do at least a hint of what I wanted it to do.

Here is the new DriveSteer.java:
Code: Select all
import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;

public class DriveSteer //program on the car
{
   public static void main(String [] args)
   throws Exception
   {
      BTConnection mastercomm = Bluetooth.waitForConnection(); //wait for connection
      DataInputStream btin = mastercomm.openDataInputStream(); //bluetooth input
      int steer = 0, drive = 0;
      while(!Button.ENTER.isPressed()) //while orange button has not been pressed
      {
         try
         {
            steer = btin.readInt(); //try to receive
         }
         catch(IOException e)
         {
            //stay still
         }
         try
         {
            drive = btin.readInt(); //try to receive
         }
         catch(IOException e)
         {
            drive = 0; //stay still
         }
         MotorPort.C.controlMotor(drive,1); //act upon reception
         Motor.B.rotateTo(steer,true); //act upon reception
      }
      btin.close(); //close connectionn
      mastercomm.close(); //close
   }
}
RAWR
Joshamuffin
New User
 
Posts: 2
Joined: Tue Jun 02, 2009 10:11 pm
Location: Missouri


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 1 guest

cron
more stuff