Tuesday, April 20, 2010

MindSqualls rocks! NXTasy.org does, too!

Now that I decided on using MindSqualls, let's get coding. First stumble block was the MessageRead() method takes as a second argument the local mailbox. What is the local mailbox? I decided to ask in the NXTasy forum and John Hansen promptly replied saying that the local mailbox should be ignored because there isn't one. He also mentioned something else that at the time I didn't understand:
The call to SendResponseString will simply write the value of out (S1, S2, etc...) to the outbound or response mailbox numbered 11. Response mailboxes have numbers equal to the specified mailbox number plus 10. You actually have to pass the response mailbox number (i.e., 10..19) rather than the "regular" mailbox number (0..9), unless Mindsquals is doing something weird and adding 10 to the number you pass in (which it should not do).
On the NXT side I stuck with the code I mentioned in my previous post (page 43 of Daniele Benedettelli's tutorial at bricxcc site):

//SLAVE
#define BT_CONN 1
#define INBOX 5
#define OUTBOX 1

task main(){
  string in, out, iStr;
  int i = 0;
  while(true){
    iStr = NumToStr(i);
    out = StrCat("S",iStr);
    ReceiveRemoteString(INBOX, true, in);
    SendResponseString(OUTBOX,out);
    TextOut(10,LCD_LINE3,in);
    TextOut(10,LCD_LINE5,out);
    Wait(100);
    i++;
  }
}

On the PC side I wrote something really simple (please note that I am using this code only as a rough example, it is by no means 100% functional, large sections were left out):
using NKH.MindSqualls;

namespace BTMasterMessage
{
    public partial class Form1 : Form
    {
        ............................
        private void GetMessage()
        {
            byte comPort = byte.Parse(this.txtComPort.Text);
            NxtCommunicationProtocol comm = new NxtBluetoothConnection(comPort);
            comm.Connect();
            string msg = comm.MessageRead(NxtMailbox2.Box1, NxtMailbox.Box0, true);
            this.Text = msg;
        }
    }
}

Well, nothing worked, there was no message displayed PC side. Then I read more closely what John mentioned in his answer and I decided to change the first argument of MessageRead from Box1 to Box11:
string msg = comm.MessageRead(NxtMailbox2.Box11, NxtMailbox.Box0, true);

It was a magical moment when I ran the program and I got an "S173" or something like it on my screen. It was so awesome!

The conclusion: MindSqualls rocks and the helpful and friendly people in the NXTasy forum do as well! Big thanks to both teams!

Monday, April 19, 2010

Starting with Bluetooth

Now that I am able to connect to my NXT via Bluetooth is time to try to communicate with it. I don't have 2 NXT's so what I focused on was communicating between a PC and the NXT brick. Since I know Java, I looked for a Java library that was able to accomplish this. iCommand is a sub-project of leJOS and it was the first thing I tried. I downloaded iCommand 0.7 and tried to make the sample programs work. To my surprise, the bluecove jar mentioned in the docs was missing but I was able to find it in an older version: 0.6. I asked on the leJOS forum why is this and was told that iCommand is not supported anymore.

Anyway, after I installed the missing jar and I set the classpath, I was able to run most of the sample programs that came with it with great results. It was a real breakthrough and I was very happy! However, none of the samples were showing how to send a message to the NXT and receive some message back. After quite a lot of digging, I found out there are 2 ways of communicating with the NXT: one is Direct Commands (basically, your code on the PC sends a command to the NXT that is interpreted directly by the firmware), the other is sending/receiving messages to/from specific mailboxes (sometimes named queues in some libraries I found). To test the communication, I got a very simple NXC program from Daniele Benedettelli's tutorial at bricxcc site (page 43) that is supposed to receive a message from another NXT and send a response message back.

Because I was able to use iCommand successfully, I figured next step was to use it to send a message to the NXT. Unfortunately, it didn't work. Later I found out in the leJOS forum what I already mentioned: that iCommand is not supported anymore. Indeed, looking in the source code for NXTCommand class that is supposed to handle the communication, messageRead and messageWrite are marked UNTESTED. It looks to me that they should work but they didn't and without being able to get some help, I moved on.

Next try: use the newer code in the leJOS project. I installed it and tried some sample code that I found, again without any success. A response to my question in the leJOS forum revealed that leJOS uses streams to talk to the NXT and not mailboxes. The solution is to install the leJOS firmware on the NXT and use streams. This didn't sound too bad but I didn't want to get too deep in proprietary firmware and communication stuff so I abandoned the idea.

Looking around I found several libraries that seemed able to use mailboxes, a comprehensive list is here on Team Hassenplug's site. I decided that if Java didn't work, maybe I should try C# (which I don't know but it is close to Java as far as I know) so I decided to try MindSqualls (another alternative seems to be NXT# but the website was down every time I tried).

So, it is MindSqualls' turn. But about this, in a later post.

A great book and a solution to my Bluetooth problem

A few weeks ago I've seen in a local bookstore Daniele Benedettelli's Creating Cool MINDSTORMS NXT Robots book and I loved it from the moment I opened it. I purchased a copy and started reading it and immediately I was very happy with it: the way theory aspects are presented and explained, the building instructions, the code, everything is really accessible.

One thing I am really grateful for are the steps for connecting the NXT brick to the PC via Bluetooth. I tried to do this before following the steps in the Mindstorms docs (initiating the connection from the brick) and even if the NXT connected fine with the PC, the Mindstorms software could not see it. I gave up thinking the BT driver on my computer is somehow unable to connect properly. After following the steps in the Appendix in this book (and initiated the connection from the BT device manager on the PC) I was able to connect not only the Bricxx IDE but also the Mindstorms software. This is a huge breakthrough - no more wires that need to be connected to load the program and disconnected later to run it. Just this simple aspect makes purchasing this book great, at least for me!

Those of you that don't have this book but have problems connecting via Bluetooth can find help in John Hansen's article on nxtasy.org

How it all started

I am a beginner with electronics, microcontrollers, robots, LEGOs... you name it. But I want to learn! So a couple years back when I decided to get on with it, I looked around and found David Cook's Robot Building for Beginners (the older edition) and I was hooked. Bought it, read it, built the robot. It was my very first project that I actually finished - with others before I got to a certain point and then I figured, the rest of it is easy, just put it in a box, add wheels and it will work - wrong! as I discovered there are a lot of challenges to have a fully functional project done and working 100%.

The experience and the amount of learning was great! I figured the next step was to build a robot using a microcontroller. I started playing with ATmega's and TI's MSP430 and again I learned a lot but never finished a robot.

That's when I got a LEGO Mindstorms NXT for Christmas. Talk about really awesome! I am only starting to scratch the surface regarding programing the NXT (learning NXC right now), communicating with it (trying to learn Bluetooth communication) but it looks great.Hopefully I'll be able to do some cool projects that I will share here.

Welcome and thanks for reading!