2009-06-30

Useful sensors

This weekend I read the book Develop LeJOS Programs Step by Step by Juan Antonio Breña Moral. It was really good with alot of nice examples of how to do LeJOS stuff. It also included some information not directly connected to LeJOs, like Lego Mindstorms history for example, but this was really entertaining to read.



Anyhow the book included some useful links and one of the brought me to the company Mindsensors. I found a really nice sensor that could improve this project alot. The High Precision Long Range Infrared distance sensor for NXT (DIST-Nx-Long-v2) would allow us to calculate distances between 20 to 150 cm in millimeters!! Thats 10 times better than with the Ultrasonicsensor that only gives us the distance in cm.



I also read that the CompassNavigator in leJOS may take advantage of the compass sensor from HiTechnic. This would be really nice to have because (as i've understood it) the turns won't depend on the tachometer in the motor, instead it will look at the angle the compass returns i.e. it will rotate until it has rotated as much as needed.


2009-06-21

Going the Right Way

Now we've finally begun to build something that with a bit of imagination remotely resembles our ultimate goal. UltrasonicScanner is a stationary robot which is much like the UltrasonicTest, but now we've integrated communication between the computer and the NXT through Bluetooth and we can let the computer do all the calculation and show the results graphically.

UltrasonicScaner
http://code.google.com/p/penemunxt/source/browse/#svn/trunk/UltrasonicScannerClient/src
http://code.google.com/p/penemunxt/source/browse/#svn/trunk/UltrasonicScannerServer/src



The code is more or less CommunicationTest combined with UltrasonicTest. We've established streaming of both the ultrasonic sensor getDistance() and motor getTachoCount() (which returns the angle from it's original position) and then perform all the necessary calculations serverside. We also have a third "channel" streaming data to allow us to give commands in both directions. This means that both the NXT and the JAVA application have the ability to close both programs.

private Point getScreenPos(int Distance, int Angle) {
Angle += 90;
int x, y;
int distx, disty;

distx = (int) (((Math.min(ULTRASONIC_SENSOR_MAX_DISTANCE, Distance) / (double) ULTRASONIC_SENSOR_MAX_DISTANCE) * (getWidth() / 2)));
disty = (int) ((Math.min(ULTRASONIC_SENSOR_MAX_DISTANCE, Distance) / (double) ULTRASONIC_SENSOR_MAX_DISTANCE) * getHeight());
x = (int) ((distx * Math.cos((Angle) * Math.PI / 180)) + (getWidth() / 2));
x = getWidth() + (x * -1);
y = (int) (-1 * (disty * Math.sin(Angle * Math.PI / 180)) + getHeight());

return new Point(x, y);
}


This is the algorithm we use to calculate the coordinates on the screen based on the data from the NXT, and it's all straightforward mathematics.






As you can see on these pictures the "map" the robot managed to create isn't totally accurate. Apparently the sensor can't give accurate data when facing at an angle to a flat surface, with the result that flat surfaces seems to be curved around the robot. Once we have the robot mobile a lot of this problem should be solved.

If check out our videos you can also see a version before we implemented Bluetooth for it.

This is the final result:

It's working!


It's astonishing how much you can manage to do in just one day of coding. Or astonishing how long time coding takes, depending on how you look upon it. In one day we've started to familiarize ourselves with the NXT and we've created all kinds of smaller test programs, all of which you can find on our Google Code page.


HelloWorld
http://code.google.com/p/penemunxt/source/browse/#svn/trunk/HelloWorld/src

The first basic program to show that the NXT and leJOS works properly. The same program that every programmer has done at least once during his early career. What it does is to simply write a String on our NXT's screen. The leJOS code for writing on the screen of the NXT is:
LCD.drawString("String",x,y);

UltrasonicTest
http://code.google.com/p/penemunxt/source/browse/#svn/trunk/UltrasonicScannerClient/src
http://code.google.com/p/penemunxt/source/browse/#svn/trunk/UltrasonicScannerServer/src

In this project with its very intuitive name we started playing with the Ultrasonic Sensor. This works like a sonar; it sends out ultrasonic sound and measures the time to the echo. To use the sensor you simply declare the sensor, which then has a number of predetermined methods. Note that you have to specify the port he sensor is located in as well.
UltrasonicSensor sensor = new UltrasonicSensor(SensorPort.S1);
int distance = sensor.getDistance();
LCD.drawInt (distance , 1, 1);



These lines will cause the sensor to measure the distance ahead of it and write out the distance in centimeters on the display.

We used this to create a program that measures the distance in a 180 degrees radius and then write it out on the LCD screen. Here we also used a motor to rotate the ultrasonic sensor. The motors also have a set of predetermined methods, but you don't have to declare them.

class HeadRotator extends Thread {
@Override
public void run() {
Motor.A.setSpeed(40);
while (!this.isInterrupted()) {
Motor.A.rotateTo(-100, true);
Sound.beep();
while (Motor.A.getTachoCount() >= -90 && !this.isInterrupted()) {
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
Sound.beep();
Motor.A.rotateTo(100, true);
while (Motor.A.getTachoCount() <= 90 && !this.isInterrupted()) { } try { Thread.sleep(200); } catch (InterruptedException e) { } Sound.beep(); } } }


This loop makes the sensor rotate back and forth in a 180 degree arc. In a separate thread we continuously gather data which we then translate into coordinates on the LCD and write it out.

We noted that the sensor isn't very accurate when facing at an angle to a flat surfaces, and when the sensor sweeped over a flat surface the surface would appear curved on the screen. This is something we'll have to take into consideration later on. Also, we noticed the limits of the NXT' computing capacity, as it didn't quite manage to keep up with the data income. Though the most difficult part for it was to manage to write all the data to the LCD fast enough, so the screen flickered.


CommunicationTest
http://code.google.com/p/penemunxt/source/browse/#svn/trunk/CommunicationTestClient/src
http://code.google.com/p/penemunxt/source/browse/#svn/trunk/CommunicationTestServer/src

To solve the problem with the NXT's limited computing capacity we wanted to be able to run most of the computation on a server, in this case a computer. Also we want to be able to show the results on a real screen instead of the very limited LCD of the NXT. Thus, we had to establish a communication link.

The first comm test was just a simple app sending the data from a touch sensor to the computer through USB. When the touchsensor was pressed the computer app should exit. leJOS included some really great examples for rthis so it wasn't that hard to establish the first connection.

We extended the data transfer so that it also transmitted the data from the soundsensor aswell as the ultrasonicsensor in real time. The PC recieved the data and painted it to an applet as a graph. The PC also always sends back a status code telling the NXT if the status is OK or not, if it's not OK everything should shut down.

Then it wasn't that hard to switch to Bluetooth mode instead. The use the same connector but with different protocols:

USB:NXTConnector conn = new NXTConnector();
conn.connectTo("usb://")

Bluetooth:NXTConnector conn = new NXTConnector();
conn.connectTo("btspp://")

Though there are better ways to implement it which we will test later on. Anyhow we missed one important thing in the beginning, we forgot to add the reference to the BT package bluecove.jar. It took us at least one hour to figure out that this was the missing part.

Video of the CommunicationTest:


Summary
So what we've learned so far will be really important. Now we will use our know knowledge in BT communication and ultrasonic data to build a simple bot that rotates, collects US data and sends it to the computer to display it.

Stay tuned!!
/Peter And Josef

Links
http://lejos.sourceforge.net/nxt/nxj/api/index.html leJOS NXT API
http://lejos.sourceforge.net/nxt/pc/api/index.html leJOS PC API
http://lejos.sourceforge.net/nxt/nxj/tutorial/Communications/Communications.htm leJOS Communication Tutorial

2009-06-20

PenemuNXT is up and running!

We have now officially started on our project!

PenemuNXT is a project by Josef Hansson and Peter Forss. PenemuNXT means "explorer" in Indonesian, and that's what it's going to be. We are doing this project as our final main project at our school Berzeliusskolan in Linköping, Sweden. The main reason for the project is to learn more about programming and JAVA, but since we wanted a result we could see and feel for ourselves we decided to build a robot in Lego Mindstorms NXT.

The robot is in its final form going to be able to, using the UltraSonic Sensor from Lego, scan its environment and then wirelessly send it to a server which then processes the data into a map which will hopefully be reasonable close to how the environment actually looks.

The firmware leJOS provides us with the means to program the NXT using JAVA, a language we are already somewhat familiar with and which can be used in other applications as well. The code wil be open source and available through Google Code.

While we go about on our project you'll most likely see all kinds of different smaller robots and programs we create while we familiarize ourselves with the NXT and learn new ways of programming.



Let the project begin!
Josef Hansson
Peter Forss



Links
http://code.google.com/p/penemunxt/ - All our code will be hosted here.
http://lejos.sourceforge.net/ - The firmware which allows us to program the NXT using JAVA