//
you're reading...
How-to's

HTTYR: Use a NXT as a Tilt Controller

2014-04-26 22.15.59

This is part of a series of posts on how to train your robot.

If you have a NXT brick, you might like to use it as a controller for your leJOS EV3 robot.

I wrote a leJOS NXJ program to control one NXT by using another as a Wii-like controller, some time ago. It uses an old Mindsensors acceleration sensor, but it is easy to change the program to use any other acceleration sensor that works with the NXT:

import java.io.*;
import lejos.nxt.*;
import lejos.nxt.addon.*;
import lejos.nxt.comm.*;

public class TiltController {

  /**
   * Wii like controller for the NXT.
   * 
   * Needs two NXTs: one for the controller and one to control.
   */
  public static void main(String[] args) throws Exception {
    AccelMindSensor controller = new AccelMindSensor(SensorPort.S1);
    int x, y, z;
    String name = "EV3";

    LCD.drawString("Connecting ...", 0, 0);
    LCD.refresh();

    BTConnection btc = Bluetooth.connect(name, NXTConnection.PACKET);

    LCD.clear();
    LCD.drawString("Connected", 0, 0);
    LCD.refresh();

    DataInputStream dis = btc.openDataInputStream();
    DataOutputStream dos = btc.openDataOutputStream();

    while (true) {
      x = controller.getXTilt();
      y = controller.getYTilt();
      z = controller.getZTilt();

      LCD.drawInt(x, 3, 0, 1);
      LCD.drawInt(y, 3, 0, 2);
      LCD.drawInt(z, 3, 0, 3);

      dos.writeByte(x);
      dos.writeByte(y);
      dos.writeByte(z);
      dos.flush();
      byte ack = dis.readByte();
      LCD.drawInt(ack, 3, 0, 4);
    }
  }
}

We need a Bluetooth responder program running on the EV3 to receive the tilt values from the NXT and convert them into robot movements. Here it is:

import java.io.DataInputStream;
import java.io.DataOutputStream;

import lejos.hardware.Bluetooth;
import lejos.hardware.lcd.LCD;
import lejos.hardware.motor.Motor;
import lejos.remote.nxt.NXTConnection;
import lejos.robotics.navigation.DifferentialPilot;

public class RemoteSteer {

  public static void main(String[] args) throws Exception {
    DifferentialPilot robot = new DifferentialPilot(2.1f,6f,Motor.A, Motor.C,true);
    String connected = "Connected";
        String waiting = "Waiting";
    LCD.drawString(waiting,0,0);
    LCD.refresh();

    NXTConnection btc = Bluetooth.getNXTCommConnector().waitForConnection(0, NXTConnection.PACKET);
        
    LCD.clear();
    LCD.drawString(connected,0,0);
    LCD.refresh();  

    DataInputStream dis = new DataInputStream(btc.openInputStream());
    DataOutputStream dos = new DataOutputStream(btc.openOutputStream());

    while (true) {
      
      int x = dis.readByte();
      int y = dis.readByte() & 0xFF;
      int z = dis.readByte() & 0xFF;
      
      dos.writeByte((byte) 0xFF); // Ack
      dos.flush();
          
      LCD.drawInt(x,4, 0,1);
      LCD.drawInt(y,4, 0,2);
      LCD.drawInt(z,4, 0,3);
      
      int speed = (180 - y) * 20;
      
      LCD.drawInt(speed, 5, 0, 4);
      LCD.refresh();
      
      int ax = (x < 0 ? -x : x);
      
      if (speed > 0) {
        robot.setTravelSpeed(speed);
        System.out.println("forward");
        if (ax < 5) robot.forward();
        else robot.steer(-x);
      } else {
        speed = - speed;
        robot.setTravelSpeed(speed);
        System.out.println("Backward");
        robot.backward();
      }
    }
  }
}
Advertisements

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

About leJOS News

leJOS News keeps you up-to-date with leJOS. It features latest news, explains cool features, shows advanced techniques and highlights amazing projects. Be sure to subscribe to leJOS News and never miss an article again. Best of all, subscription is free!
Follow leJOS News on WordPress.com
%d bloggers like this: