NXC: BT NXT to NXT - ReceiveRemoteString slave synch?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: BT NXT to NXT - ReceiveRemoteString slave synch?

Post by HaWe »

hi,
using on the BT master

Code: Select all

ReceiveRemoteString(INBOX, bDELETE, MSG_STRING);
what are the corresponding cmds on the slave to clear the mailbox and the mailbox queue so that he may put his current (random) string into his mailbox for reading just in that moment of request (to avoid mailbox block or mailbox queue overflow or having old string values)?

Code: Select all

// master
task ReceiveSlaveData()    // master 
{
   string _msg;

   while(true)   {
       until(ReceiveRemoteString(2, true, _msg) == NO_ERR); // for COMM 1
       // do something with the  _msg of slave 1);
       Wait(10);

       until(ReceiveRemoteString(5, true, _msg) == NO_ERR); // for COMM 2
       // do something with the  _msg of slave 2);
       Wait(10);

       until(ReceiveRemoteString(8, true, _msg) == NO_ERR); // for COMM 3
       // do something with the  _msg of slave 3);
       Wait(10);
      }
     Wait(10);
   }
}
each slave:

Code: Select all

// slave
task SlavePuttingDataIntoMailbox() { 
  string out;
  char OUTBOX; //slave 1:OUTBOX=2 , slave 2:OUTBOX=5, slave 3:OUTBOX=8

  while (true) { 
    out= ... // random/changing anything over/during time

    // what to do...if now or before requested by master... 
    // to clear all old and/or unread messages in mailbox or queue 
    // before putting in just the newest / latest string value?
    
    SendResponseString(OUTBOX, out); 
    Wait(20);
  }
}
Last edited by HaWe on 04 Mar 2012, 16:18, edited 3 times in total.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: ReceiveRemoteString - slave synchronization?

Post by HaWe »

don't there really no good examples about real good and safe BT connections and fast and reliable value/string polling and sending from/to 3 slaves exist?
- synchronized, without dropping messages, without clogging the mailboxes, without data loss?
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: ReceiveRemoteString - slave synchronization?

Post by afanofosc »

I am working on an example in my spare time. I hope to have it ready by the end of the week. Patience, Iago, patience.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: ReceiveRemoteString - slave synchronization?

Post by HaWe »

I'm patient as usual :)
but maybe I already can try it by my own: can the slave delete all old messages out of his messge box and those (4-5) chained in the message queue?
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: ReceiveRemoteString - slave synchronization?

Post by afanofosc »

From looking at the firmware source code it would appear that a slave can empty out its own response mailbox using ReceiveMessage or SysMessageRead or ReceiveRemoteString. It would need to call this function repeatedly until it returns the STAT_MSG_EMPTY_MAILBOX status. It would also need to use the mailbox number that it uses with SendResponseString + 10 in order to properly read from the outbound response mailbox rather than the inbound mailbox. Something like this:

Code: Select all

inline void ClearResponseMailbox(byte mailbox)
{
  MessageReadType args;
  args.QueueID = mailbox + 10;
  args.Remove = true;
  args.Result = NO_ERR;
  while (args.Result != STAT_MSG_EMPTY_MAILBOX)
    SysMessageRead(args);
}
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: ReceiveRemoteString - slave synchronization?

Post by HaWe »

no, it didn't help, sending and receiving is still always shaky and it blocks.
Still no way for sending and receiving BT messages safely yet.
I also tried it this following way using completely single centralized BT tasks - no chance, no BT messaging at all, total blocking;
it neither works with

Code: Select all

do {Wait(1);} while (BluetoothStatus(ID) == STAT_COMM_PENDING);
nor using

Code: Select all

Wait(10);
in the master task.

Code: Select all

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
task BTMasterTask()    // NOS = Number of Slaves
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
   string slvmsg;
   string pollmsg="¦¦";
   int ack, ch;
   char ID;

   pollmsg[0]=0xff; pollmsg[1]=0xff;
   while(true)   {
     if (_NOS_>=1) {
       ID=1;
       SendRemoteString(ID,OUTBOX_1,pollmsg);
       do {Wait(1);} while (BluetoothStatus(ID) == STAT_COMM_PENDING);
       //Wait(10);
       ReceiveRemoteString(INBOX_11, true, slvmsg);
       ParseBTResponseMsg(ID-1, slvmsg) ;
       Wait(10);
     }

     if (_NOS_>=2) {
       ID=2;
       SendRemoteString(ID,OUTBOX_2,pollmsg);
       do {Wait(1);} while (BluetoothStatus(ID) == STAT_COMM_PENDING);
       //Wait(10);
       ReceiveRemoteString(INBOX_21, true, slvmsg);
       ParseBTResponseMsg(ID-1, slvmsg) ;
       Wait(10);
     }

     if (_NOS_>=3) {
       ID=3;
       SendRemoteString(ID,OUTBOX_3,pollmsg);
       do {Wait(1);} while (BluetoothStatus(ID) == STAT_COMM_PENDING);
       //Wait(10);
       ReceiveRemoteString(INBOX_31, true, slvmsg);
       ParseBTResponseMsg(ID-1, slvmsg) ;
       Wait(10);
     }
      
      ack=0;
      if (_OUT_ != "") {
        ch=checksum(_OUT_);
        while(ack!=ch)  {
            SendRemoteString(_ID_, _OUTBOX_, _OUT_);
            do {Wait(1);} while (BluetoothStatus(_ID_) == STAT_COMM_PENDING);
            //Wait(10);
            ReceiveRemoteNumber(_INBOX_, true, ack);
            Wait(10);
        }
        _OUT_="";
      }
      
      Wait(5);
   }
}

Code: Select all

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
task BTSlaveTask() {   // slave
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  string in;
  int ack, len;

  string sp, sn, svv;
  string fmtstr, buf, out;
  int i, dec;

  while(true)   {
  
    in= "";

    if (ReceiveRemoteString(INBOX, true, in) != STAT_MSG_EMPTY_MAILBOX)   {

       if((in[0]!=0xff)&&(in[1]!=0xff))  {    // ack for ExecCmd
         ack=checksum(in);
         SendResponseNumber(OUTBOX_2, ack);   // error check
         Wait(1);
         __cmd=in;
         ExecCmd();
       }
       else
       {
          out="";                // return value string
          for(i=0; i<11; i++) {  // Sensorports: 0...3  plus virtual ports
            GetSensorValues(i);
            Wait(1);

            dec=SensorValueDecimals[i];
            sn=NumToStr(dec);                          // value decimals
            fmtstr=StrCat("%",NumToStr(dec),"d");
            svv=FormatNum(fmtstr,SensorCurrArr);

            out = StrCat(out, sn, svv);

          } //  for port i=...

          if (StrLen(out)>58) strncpy(out,out,58);     // 58 = max msg length
          SendResponseString(OUTBOX_1, out);
          Wait(1);
#ifdef debug
          //TextOut(0, 0, out);
#endif
       }
    }   // != STAT_MSG_EMPTY_MAILBOX
    
    ClearResponseMailbox(OUTBOX_1);
    Wait(10);
  }  // while(true)
}

afanofosc wrote:I'm thinking about adding a bit to disable the bluetooth poll operation entirely. I.e., bits 0-4 for the mailbox number, bit 5 for disable bt, and bits 6 and 7 for the connection number. Of course, it has always been possible to call RemoteMessageRead to directly control which connection to read from, followed afterward by an attempt to read from a mailbox but you could wind up triggering a potentially unwanted bluetooth request the way things work in the firmware. I think it might be useful to be able to try to read from a mailbox and be certain that it's not going to send out a bluetooth message unless you specifically ask it to. John Hansen
doc-helmut wrote:don't there really no good examples about real good and safe BT connections and fast and reliable value/string polling and sending from/to 3 slaves exist? - synchronized, without dropping messages, without clogging the mailboxes, without data loss?
afanofosc wrote:I am working on an example in my spare time. I hope to have it ready by the end of the week. Patience, Iago, patience. John Hansen
doc-helmut wrote:Strangely, when sending 4 arrays[≈ 40] shortly one after the other to any slave, each slave seems to receive every "block-of-four" 3-4 times multiple, completely, redundantly, one block after the other, although the 4 arrays have been sent actually only once and then never, and this seems to muddle up the BT communication.
A fail-safe and simple BT comm protocol still urgently needed and very much appreciated!
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: ReceiveRemoteString - slave synchronization?

Post by HaWe »

I tried everything and anything, but either sended cmds are dropped, or they are sent and received several times redundantly, or response strings in slave mailboxes are dropped.
So either the master can't receive strings from slaves or the slaves don't read what the master commanded - or slaves do everything not just once as they should but 3 or 4 times in succession.

what a dung.
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests