OOP design with NXT

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

OOP design with NXT

Postby aegidius » Tue Jun 10, 2008 11:22 am

For programming courses focused on OOP I intend to develop a Java library with a clean OOP design to be used with the NXT brick. In direct mode there will be support for several bricks at the same time using Bluetooth communication. Part of the design is shown in the following class diagram:

http://www.aplu.ch/dev/nxtrobot.jpg

Is there a general interest and what are your suggestions/proposals?

Aegidius

PS: Here a sample program with one NXT brick

Code: Select all
// RobotEx1.java
// One robot with two motors

import ch.aplu.nxt.*;

public class RobotEx1
{
  RobotEx1()
  {
    // Create robot
    NxtRobot roby = new NxtRobot("NXT");
   
    // Create two motors
    Motor motA = new Motor(MotorPort.A);
    Motor motB = new Motor(MotorPort.B);
   
    // Assemble motors into robot
    roby.addPart(motA);
    roby.addPart(motB);

    // Connect PC and robot
    roby.connect();
   
    // Use motors
    motA.forward();
    motB.forward();
   
    // Disconnect
    roby.disconnect();
  }

  public static void main(String[] args)
  {
    new RobotEx1();
  }
}


Here a sample program with two bricks:

Code: Select all
// RobotEx2.java
// Two robots with each two motors

import ch.aplu.nxt.*;

public class RobotEx2
{
  RobotEx2()
  {
    // Create robots
    NxtRobot john = new NxtRobot("NXT");
    NxtRobot jaye = new NxtRobot("NXT5");
   
    // Create four motors
    Motor motA = new Motor(MotorPort.A);
    Motor motB = new Motor(MotorPort.B);
    Motor motC = new Motor(MotorPort.A);
    Motor motD = new Motor(MotorPort.B);
   
    // Assemble motors into robots
    john.addPart(motA);
    john.addPart(motB);
    jaye.addPart(motC);
    jaye.addPart(motD);

    // Connect PC and robots
    john.connect();
    jaye.connect();
   
    // Use motors
    motA.forward();
    motB.forward();
    motC.setSpeed(50);
    motD.setSpeed(50);
    motC.forward();
    motD.backward();
   
    // Disconnect
    john.disconnect();
    jaye.disconnect();
  }
 
  public static void main(String[] args)
  {
    new RobotEx2();
  }
}
 
Last edited by aegidius on Wed Jul 02, 2008 11:04 pm, edited 2 times in total.
aegidius
Novice
 
Posts: 26
Joined: Thu Mar 20, 2008 9:15 pm
Location: Switzerland

Postby JavaMan » Wed Jun 11, 2008 11:28 am

Interesting. I was going to do something like this with my RCX bricks this summer.

Where will the sample programs run on: the computer or the NXT?

Jman
look up and see the chemtrails
www.carnicom.com
User avatar
JavaMan
Novice
 
Posts: 27
Joined: Tue Apr 01, 2008 1:47 pm
Location: United States

Postby aegidius » Fri Jun 20, 2008 11:44 am

The programs compiles and runs on the PC/Mac and uses the Bluetooth connection to the NXT with the NXT direct commands.

They run with the original Mindstorms or the Lejos firmware. Obviously it is not a typical NXJ project but uses code inspirations from the Lejos source distribution. Like Lejos all library sources are freely distributed.

To port the same strict OOP concepts to standalone programs would require a total reengeneering of the Lejos package. This is too much of work.
aegidius
Novice
 
Posts: 26
Joined: Thu Mar 20, 2008 9:15 pm
Location: Switzerland

Postby aegidius » Wed Jul 02, 2008 11:00 pm

I also implement the usual Java event model. A typical demo application with a touch sensor looks like this
Code: Select all
// RobotEx8.java
// Touch Sensor (simple demo)

import ch.aplu.nxt.*;
import ch.aplu.util.*;

public class RobotEx8 implements TouchListener
{
  private NxtRobot robot;
 
  public RobotEx8()
  {
    // Create a touch sensor at port S1
    TouchSensor ts = new TouchSensor(SensorPort.S1);

    // Register it to enable the callbacks
    ts.addTouchListener(this);
 
    // Create a robot with given Bluetooth name
    robot = new NxtRobot("NXT");
 
    // Assemble the touch sensor into the robot
    robot.addPart(ts);
 
    // Connect the PC to the robot
    if (!robot.connect())
      return;
   
    // Show a modal dialog until the quit button is hit
    while(!QuitPane.quit());
   
    // Cleanup
    robot.exit();
  }
 
  // Callback method called whenever the touch sensor is pressed
  public void pressed()
  {
    robot.playTone(750, 400);
  }

  // Callback method called whenever the touch sensor is released
  public void released()
  {
    robot.playTone(600, 400);
  }

  // Main
  public static void main(String[] args)
  {
    new RobotEx8();
  }
}

A hidden thread polls the touch sensor every 100 ms. This example already works fine.
aegidius
Novice
 
Posts: 26
Joined: Thu Mar 20, 2008 9:15 pm
Location: Switzerland

Postby roger » Thu Jul 03, 2008 3:49 pm

I am interested in the design of your library. Does it use lejos NXJ? Or is it completely independent? Is it to be used only on a PC to control the robot in direct mode? or to be run on the NXT also?
I notice that your Gear class is quite similar to the Pilot class of NXJ. Why did you decide to not use the Pilot ?
Roger
roger
Moderator
 
Posts: 368
Joined: Fri Jun 01, 2007 4:31 am
Location: Berkeley, CA

Method addTouchListener

Postby esmetaman » Wed Jul 09, 2008 7:44 am

Hi,

I saw your example and I liked it, but currently doesnt have the method: addTouchListener. How to use that method?

Thanks
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 299
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby aegidius » Wed Jul 09, 2008 4:43 pm

Hello Roger,
Thanks for your questions.

roger wrote:Does it use lejos NXJ? Or is it completely independent? Is it to be used only on a PC to control the robot in direct mode? or to be run on the NXT also?


The library is only used to control the NXT in direct mode. It is completely independent of the leJOS NXJ or iCommand, but uses some low level code from the leJOS iCommand distribution (did not reinvent the wheel).
The main advantage is it's clean OOP design. We need this for our Java courses, because Java + Robotics is a very motivating.

roger wrote:I notice that your Gear class is quite similar to the Pilot class of NXJ. Why did you decide to not use the Pilot ?


That's true, but Pilot is not available in leJOS iCommand. It's quite natural to create a class that supports the two wheels together (there was once a class called SynchMotors)

My library supports more than one brick at the same time, and this is a must for our OOP concept. All sensor classes use the Java event model, because event handling is another highlight of our teaching. The user of the library does not "poll" the sensors, but registers a callback method to get an event trigger.
aegidius
Novice
 
Posts: 26
Joined: Thu Mar 20, 2008 9:15 pm
Location: Switzerland

Re: Method addTouchListener

Postby aegidius » Wed Jul 09, 2008 4:47 pm

Hi,

esmetaman wrote:I saw your example and I liked it, but currently doesnt have the method: addTouchListener. How to use that method?


This method is implemented in my library. It's almost terminated and an alpha version will be submitted to the public in August 08. I will post a message containing the download link.
aegidius
Novice
 
Posts: 26
Joined: Thu Mar 20, 2008 9:15 pm
Location: Switzerland

Postby esmetaman » Wed Jul 09, 2008 5:10 pm

Ok, I will wait :D
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 299
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

NxtJLib (NXT Java library) available

Postby aegidius » Sun Aug 03, 2008 5:01 am

The first official release of my NXT Java Library is available at http://www.aplu.ch/nxt. It contains NxtJLib.jar for the J2SE platform and NxtJLibMP.jar for the J2ME platform to develop MIDlets for Java-enabled mobile phones.

Both are beta versions. Therefore the source code is not yet included.

Try it and give me a feedback in this forum or use the e-mail address given at http://www.aplu.ch/home/kontakt.jsp
aegidius
Novice
 
Posts: 26
Joined: Thu Mar 20, 2008 9:15 pm
Location: Switzerland


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 3 guests

cron
more stuff