Using realtime pitch and roll data from a Wii remote

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

Using realtime pitch and roll data from a Wii remote

Postby enzomango » Wed Oct 22, 2008 3:27 am

The purpose of this project was to test whether a NXT robot running LeJOS could use realtime data from a Wii Remote to monitor its pitch and roll and avoid tipping over or climbing obstacles. The Wii remote was strapped to the side of the robot and connected to a nearby notebook PC using bluetooth and wiiuseJ. There were two software components: an arbitrator/behavior application running on the NXT and a Wii proxy running on the notebook. A “Tilt” behavior class was developed and its WantControl() method sends a request to the Wii proxy via bluetooth. The Wii proxy continuously monitors the Wii remote and responds by sending the most recent pitch and roll data back to the NXT. The experiment worked well but if you don't have a Wii remote you're probably better off buying the Lego NXT accelerometer.
Here's the video:
http://www.youtube.com/watch?v=Y1_4_FDGIPs
enzomango
Novice
 
Posts: 30
Joined: Tue Feb 26, 2008 5:20 pm

Re: Using realtime pitch and roll data from a Wii remote

Postby Shawn » Mon Oct 27, 2008 1:17 am

enzomango wrote:The purpose of this project was to test whether a NXT robot running LeJOS could use realtime data from a Wii Remote to monitor its pitch and roll and avoid tipping over or climbing obstacles.


Sweet!!!!

enzomango, could you, would you make your code available? Please! Pretty please. Any hints?!?

Shawn
User avatar
Shawn
Advanced Member
 
Posts: 723
Joined: Wed Sep 12, 2007 4:59 am
Location: Tokyo

Postby enzomango » Tue Oct 28, 2008 12:47 am

I created two small classes that do all the work. The first is the WiiNXT_Proxy class that runs on a PC or notebook equipped with Bluetooth and wiiusej.

WiiNXT_Proxy.java:

Code: Select all
import lejos.pc.comm.*;
import java.io.*;
import wiiusej.*;
import wiiusej.wiiusejevents.physicalevents.*;
import wiiusej.wiiusejevents.utils.*;
import wiiusej.wiiusejevents.wiiuseapievents.*;

public class WiiNXT_Proxy implements WiimoteListener {
   private static int intPitch = 0;
   private static int intRoll = 0;
   
   private void ConnectWii() {
       Wiimote[] wiimotes = WiiUseApiManager.getWiimotes(1, true, WiiUseApiManager.WIIUSE_STACK_MS);
       Wiimote wiimoteTilt = wiimotes[0];
       wiimoteTilt.activateMotionSensing();
       wiimoteTilt.setTimeout((short)100, (short)100);
       wiimoteTilt.addWiiMoteEventListeners(this);
       System.out.println("Connected to Wii");
   }

   private void ConnectNXT(String[] args) {
      if (args.length != 2) {
         System.out.println("Usage: WiiNXT_Proxy name address");
         System.exit(1);
      }
      
      NXTComm nxtComm = null;
      try {
         nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
      } catch (NXTCommException e) {
         System.out.println("Failed to load Bluetooth driver");
         System.exit(1);
      }
      
      NXTInfo[] nxtInfo = new NXTInfo[1];
      nxtInfo[0] = new NXTInfo(args[0],args[1]);
      System.out.println("Connecting to " + nxtInfo[0].btResourceString);
      boolean opened = false;
      
      try {
         opened = nxtComm.open(nxtInfo[0]);
      } catch (NXTCommException e) {
         System.out.println("Failed to open " + nxtInfo[0].name);
         System.exit(1);
      }
      
      if (!opened) {
         System.out.println("Failed to open " + nxtInfo[0].name);
         System.exit(1);
      }
      
      System.out.println("Connected to " + nxtInfo[0].btResourceString);
      
      InputStream is = nxtComm.getInputStream();
      OutputStream os = nxtComm.getOutputStream();
      
      DataOutputStream dos = new DataOutputStream(os);
      DataInputStream dis = new DataInputStream(is);
      
      while (true) {
         try {
            if ( dis.readInt() == 10 ) {
               dos.writeInt(intPitch);
               dos.flush();
               dos.writeInt(intRoll);               
               dos.flush();
               //System.out.println("Sent Data: Pitch= " + intPitch + ", Roll=" + intRoll);
            }
         }catch(Exception e) {
            e.printStackTrace();
            System.exit(1);
         }
      }
   }
   
   public void onMotionSensingEvent(MotionSensingEvent arg0) {
      intPitch = (int)(arg0.getOrientation().getPitch());
      intRoll = (int)(arg0.getOrientation().getRoll());
    }
   
   public void onButtonsEvent(WiimoteButtonsEvent arg0) {}
   public void onDisconnectionEvent(DisconnectionEvent arg0) {}
   public void onIrEvent(IREvent arg0) {}
   public void onExpansionEvent(ExpansionEvent arg0) {}
   public void onStatusEvent(StatusEvent arg0) {}
   public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) {}
   public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) {}

   public static void main(String[] args) {
      WiiNXT_Proxy p = new WiiNXT_Proxy();
      p.ConnectWii();
      p.ConnectNXT(args);
   }
}

The second class is called Wii.java and runs on the NXT and is used by a tilt behavior class.

Wii.java:
Code: Select all
import lejos.nxt.comm.*;
import java.io.*;

public final class Wii {
   private static Wii _instance = null;
   private static final int GETPITCHROLL = 10;
   private static BTConnection btc = null;
   private static DataInputStream dis = null;
   private static DataOutputStream dos = null;

   private Wii() {
      try {
         LCDBuffer.getHandle().DisplayMessage("Wii: Waiting...");      
          btc = Bluetooth.waitForConnection();
          dis = btc.openDataInputStream();
         dos = btc.openDataOutputStream();
         LCDBuffer.getHandle().DisplayMessage("Wii: Connected");   
      }catch(Exception e) {
         LCDBuffer.getHandle().DisplayMessage("Wii: ConErr");            
      }
    }
   
   public synchronized static Wii getInstance() {
      if (_instance == null ) {
         _instance = new Wii();
      }
      return _instance;
   }

   public synchronized int[] getPitchRoll() {
      int[] Readings = {0,0};
      try {
         dos.writeInt(GETPITCHROLL);
         dos.flush();
         Readings[0] = dis.readInt();
         Readings[1] = dis.readInt();
      }catch(Exception e) {
         LCDBuffer.getHandle().DisplayMessage("Wii: ComErr");   
      }
      return Readings;
   }
   
   public void shutdown() throws Exception {
      dis.close();
      dos.close();
      Thread.sleep(100);
      btc.close();
   }
}


That's really all there is to it. All the hard work was done by the folks that created LeJOS and wiiusej.
enzomango
Novice
 
Posts: 30
Joined: Tue Feb 26, 2008 5:20 pm


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 3 guests

more stuff