NXT-NXT bluetooth

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
roscohead
Posts: 18
Joined: 29 Sep 2010, 03:09

NXT-NXT bluetooth

Post by roscohead »

OK, I just decided to start a project that requires my NXTs to communicate. It's a fairly simple deal, with one NXT being the main controller (with motors sensors etc) and the other being a remote controller (sends commands, receives status updates). I found Danny's bluetooth lib http://robotics.benedettelli.com/BT_NXC.htm that I'm currently using unchanged, and have included my code below.

This code does not work. Usually the remote NXT will crash (ticking), sometimes after it has sent a message, sometimes before. The master NXT doesn't crash, and will sometimes show 1 or 2 messages received. I really have no idea where to go from here, is there a tutorial somewhere that shows how to implement something like this?

Both are running NXC enh FW 1.31.

message.nxc (shared code)

Code: Select all

#ifndef MESSAGE
#define MESSAGE
#include "BTlib.nxc"

struct MessageCommand {
       int Command;
};

struct MessageStatus {
       int Status;
       float Azimuth;
       float Elevation;
};

int SendCommand(byte connection, byte mailbox, MessageCommand& command1) {
    string sDataOut;
    sDataOut = FlattenVar(command1);
    BTSendMessage(connection,mailbox,sDataOut);
    return 0;
}

int ReceiveCommand(byte connection, byte mailbox, MessageCommand& command1) {
     string sDataIn;
     sDataIn = BTReceiveMessage(connection, mailbox, TRUE);
     if (strlen(sDataIn) > 0) {
        UnflattenVar(sDataIn, command1);
        return 0;
     }
     return -1;
}

int SendStatus(byte connection, byte mailbox, MessageStatus& status1) {
    string sDataOut;
    sDataOut = FlattenVar(status1);
    BTSendMessage(connection,mailbox,sDataOut);
    return 0;
}

int ReceiveStatus(byte connection, byte mailbox, MessageStatus& status1) {
     string sDataIn;
     sDataIn = BTReceiveMessage(connection, mailbox, TRUE);
     if (strlen(sDataIn) > 0) {
        UnflattenVar(sDataIn, status1);
        return 0;
     }
     return -1;
}

#endif

m1.nxc (runs on master controller, which is BT slave)

Code: Select all

#include "NXCDefs.h"
#include "message.nxc"

#define BT_CONN 0 //line where slave is connected
#define MAILBOX 0

string Connected = "N" ;

task HandleRemote() {
     int recv = 0;
     MessageCommand command1;
     MessageStatus status1;
     while (true) {
     while (BTCommCheck(BT_CONN)) {
           Connected = "Y" ;
           if (ReceiveCommand(BT_CONN, MAILBOX, command1) == 0) {
              recv++;
              NumOut(50,LCD_LINE7,recv);
              status1.Status = recv;
              status1.Azimuth = recv * 0.9;
              status1.Elevation = recv * 1.1;
              SendStatus(BT_CONN, MAILBOX, status1);
           }
           Wait(100);
     }
     Connected = "N" ;
     Wait(100);
     }
}

task main() {
     Precedes(HandleRemote);
     TextOut(50,LCD_LINE1,"start" );
}
r1.nxc (runs on remote BT master)

Code: Select all

#include "NXCDefs.h"
#include "message.nxc"

#define BT_CONN 1
#define MAILBOX 0

int Status1 = 1;
float Azimuth = 0;
float Elevation = 0;
string Connected = "N" ;

task PollMaster() {
     int sent = 0;
     MessageCommand command1;
     command1.Command = 0;
     while (true) {
     TextOut(50,LCD_LINE2,"a" );
     while (BTCommCheck(BT_CONN)) {
           Connected = "Y" ;
           SendCommand(BT_CONN, MAILBOX, command1);
           sent++;
           NumOut(40,LCD_LINE7,sent);
           Wait(1500);
     }
     Connected = "N" ;
     Wait(1000);
     }
}

task HandleMaster() {
     int recv = 0;
     MessageStatus status1;
     while (true) {
     while (BTCommCheck(BT_CONN)) {
           Connected = "Y" ;
           ReceiveStatus(BT_CONN, MAILBOX, status1);
           recv++;
           NumOut(50,LCD_LINE7,recv);
           Status1 = status1.Status;
           Azimuth = status1.Azimuth;
           Elevation = status1.Elevation;
           Wait(50);
     }
     Connected = "N" ;
     Wait(1000);
     }
}

task main() {
     Precedes(PollMaster, HandleMaster);
     TextOut(50,LCD_LINE1,"start" );
}
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXT-NXT bluetooth

Post by afanofosc »

I have attached a version of the above code which seems to work fine with two of my NXTs connected via Bluetooth.

I think you may have run into issues with multiple threads calling the same function (BTWait or BTCommCheck) at the same time.
rosco.zip
(1.62 KiB) Downloaded 470 times
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: No registered users and 47 guests