Bluetooth "extras"

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

Re: Bluetooth "extras"

Post by HaWe »

hi,
thank you, I'll try it out asap, maybe already this weekend.

how would you propose to write the slave's routines?
In the moment there are endless loops in the send-string sub for the sensor+variable values.
Should it stay as it is or would you install a hand-shake like

master sends 0xff to the slaves as a request (one after the other)
slaves: wait until 0xff is in their specified INBOX - if 0xff found then put string with the sensor+variable values into OUTBOX
master receives remote string from all slaves (one after the other)

how would you write the slave routine "if 0xff found in INBOX"
how could one be sure that the INBOX would be cleared after that and all pending messages in the queue as well so that there won't be any redundand reading/sending?
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: Bluetooth "extras"

Post by mcsummation »

What I have running in the slave of my remote control car is:
- Master does a JHSSendRemoteNumber to MailBox10 and the specific connection for that slave with a specified value (to indicate "send values").
- Master then starts looking for a response in MailBox10 by using "JHSReceiveRemoteStruct, no wait".
- Slave has one thread hanging on a JHSReceiveRemoteNumber on MailBox10.
- Slave looks at value received and, based on the value, puts the requested information into a struct.
- Slave then does a JHSSendResponseStruct on MailBox10.
- Master gets the response in MailBox10, in the struct expected.

If you have your data in a string, you could use the ...String calls instead of the ...Struct calls.

This brings all the data from the slave back to the master in one set of messages.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Bluetooth "extras"

Post by HaWe »

yes that is just like I thought a "handshake" would work...

how exactly does your slave check if there is a request currently pending?
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: Bluetooth "extras"

Post by mcsummation »

Here is the master code:

Code: Select all

task tGetData()
{
    struct Car {
        long lSteer;
        long lPower;
        long lBattery;
    };
    Car CarData;
    
    until (bBT_Stop)
    {
        JHSSendRemoteBool( CONNECTION_NUMBER, MAILBOX9, true);  // Poke the Car that I want data.
        JHSReceiveRemoteStruct( CONNECTION_NUMBER, MAILBOX9, CarData);
        lLeftRight = -CarData.lSteer;
        lUpDown = CarData.lPower;
        lBatteryStateNow = CarData.lBattery;
        DashBoard(false);
        Wait(200);
    }
}
Here is the slave code:

Code: Select all

task tSendData()
{
    bool bResult;
    struct Car {
        long lSteer;
        long lPower;
        long lBattery;
    };
    Car CarData;
    
    until (bBT_Stop)
    {
        JHSReceiveRemoteBool( CONNECTION_NUMBER, MAILBOX9, true, bResult);
        // A Poll request came down.
        CarData.lSteer = (MotorRotationCount(OUT_A) *100)/lSteeringNormalization;
        CarData.lPower = MotorPower(OUT_B);
        CarData.lBattery = BatteryLevel();
        JHSSendResponseStruct( CONNECTION_NUMBER, MAILBOX9, CarData);
    }
}
In this case, I only have one type of data to send back, so I am using a Boolean to signal the slave that data is needed.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Bluetooth "extras"

Post by HaWe »

thanks again,

so your
JHSReceiveRemoteBool( CONNECTION_NUMBER, MAILBOX9, true, bResult);
keeps this loop waiting (idle) until a msg will come in...

that looks pretty easy to copy for my purposes, I'll gladly try it, asap!
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Bluetooth "extras"

Post by HaWe »

hi JHS,
just another couple of questions:


1st, in your slave's code

Code: Select all

until (bBT_Stop)
    {
        JHSReceiveRemoteBool( CONNECTION_NUMBER, MAILBOX9, true, bResult);
        // A Poll request came down.
        CarData.lSteer = (MotorRotationCount(OUT_A) *100)/lSteeringNormalization;
        CarData.lPower = MotorPower(OUT_B);
        CarData.lBattery = BatteryLevel();
        JHSSendResponseStruct( CONNECTION_NUMBER, MAILBOX9, CarData);
    }
what is CONNECTION_NUMBER for the slave ?
AFAIK any slave always uses "0" as his CONNECTION_NUMBER - is this true also for you code or is this the corresponding CONNECTION_NUMBER of the master to this slave (1, 2, or 3 )?

2nd,
what is
bResult
for and where has it been declared?


3rd,
while the master is running the BT-setup procedure

Code: Select all

bool Init_BT(char max_conn)
{
    int iResult;
    bool bSlave1Ready;
    char conn;
    _NOS_ = max_conn;
    // In case it is already connected to something.
    for ( conn=1; conn<=_NOS_; ++conn) {
      DisconnectBT(conn);
    }
    PlayTone(TONE_C4, 200);   Wait(1000);
    for ( conn=1; conn<=_NOS_; ++conn) {
      until (ConnectBT(conn, SLAVE[conn])) ;
      PlayTone(TONE[conn], 200);  Wait(200);
    }
}

what is the slave's routine for BT-Setup to wait until the connection has been established?

I currently use

Code: Select all

void BT_SlaveCheck()
{
  do  {
    printf("%s", "search: BTmaster");
    Wait(50);
  } while (BluetoothStatus(0)!=NO_ERR)
  printf("%s", "BTmaster connected");
}
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: Bluetooth "extras"

Post by mcsummation »

1) I put "connection number" in the slave routines to be consistent. In the slave, it is "0".

2) bResult was declared earlier in that task.

3) When my slave has completed whatever setup/initialization it needs, it sends a "true" back to the master in MailBox10 to indicate it is ready for commands from the master.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Bluetooth "extras"

Post by HaWe »

2) what is bResult as default? 1 or 0? And what is it for? I don't see any variable assignment...?

3) but how does your slave know if BT has been established correctly? How does slave's BT-setup actually look like?
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: Bluetooth "extras"

Post by mcsummation »

2) bResult is the output from the JHSReceiveRemoteBool( CONNECTION_NUMBER, MAILBOX9, true, bResult); statement.

3) Because the slave is started from the master, the BT connection must have been established properly; therefore, it doesn't need to do anything to verify it. Remember, the master does all the work. The fact the slave code is running indicates the BT connection is alive and working.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Bluetooth "extras"

Post by HaWe »

i have to start the master manually and all slaves manually, too.
it is not intended and not possible to start a slave by the master:
one or more of the 3 slave's program names change from time to time, depending on updated versions,
so the master is not able to know all current slaves' program names.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests