Bluetooth Message Control

Discussion specific to projects ideas and support.
Post Reply
abcde13
Posts: 10
Joined: 29 Sep 2010, 03:22

Bluetooth Message Control

Post by abcde13 »

So, I've been working on a simple project where one robot, using and ultrasonic sensor, completes a simple maze and records its encoder values of its right wheel. Every time it stops and turns, it sends to messages: 1 for how far it moved forward, and one for how much it turned, left or right. I'm having an issue though, with the receiving of a messages on my clone, which will run the maze blind. I use the debug stream to see what values my clone is receiving. However, as soon as one message is sent, it writes it on the debug stream an infinite amount of times. I'm thinking it should write it once, clear the message, and await for the next message. Any help please?

Here's the code:

MASTER:

Code: Select all

#pragma config(Sensor, S1,     sonar,          sensorSONAR)
#pragma config(Motor,  motorA,          right,         tmotorNormal, PIDControl, reversed, encoder)
#pragma config(Motor,  motorB,          left,          tmotorNormal, PIDControl, reversed, encoder)
#pragma config(Motor,  motorC,          look,          tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

/*Maze communication

Goes throug a simple maze (i.e, no dead ends, just one route, start to finish, but with turns) and
sends the encoder amounts to simple clone, which navigates without sensors.*/

void angleTurn(string t)
{
  float arcLength = 2.2 * 2 * PI * 0.25;
  float degreesNeeded = 1.3 * 2 * PI;
  degreesNeeded =arcLength * 360/degreesNeeded;
  nMotorEncoder[right] = 0;
  if(t == "right")
  {
    while(nMotorEncoder[right] < degreesNeeded)
    {
      motor[right] = 10;
      motor[left] = -10;
    }
    sendMessageWithParm(1, 1, nMotorEncoder[right]);
    wait1Msec(10);
    motor[right] = 0;
    motor[left] = 0;
    wait1Msec(50);
  }
  else
  {
    while(nMotorEncoder[right] > -degreesNeeded)
    {
      motor[right] = -10;
      motor[left] = 10;
    }
    sendMessageWithParm(2, 2, nMotorEncoder[right]);
    wait1Msec(10);
    motor[right] = 0;
    motor[left] = 0;
    wait1Msec(50);
  }
}

void checkSides()
{
    int x;
    int y;
    x = SensorValue[sonar];
    if(x < 20)
    {
      motor[right] = 0;
      motor[left] = 0;
      y = nMotorEncoder[right];
      sendMessageWithParm(3, 0,y);
      wait1Msec(10);
      nxtDisplayCenteredTextLine(4, "%4d", y);
      wait1Msec(1000);
      nMotorEncoder[look] = 0;
      while(nMotorEncoder[look] < 90)
      {
        motor[look] = 8;
      }
      motor[look] = 0;
      wait1Msec(50);
      writeDebugStreamLine("checked forward");
      int f = SensorValue[sonar];
      nMotorEncoder[look] = 0;
      while(nMotorEncoder[look] > -180)
      {
        motor[look] = -8;
        nxtDisplayCenteredTextLine(3, "%4d", nMotorEncoder[look]);
      }
      motor[look] = 0;
      wait1Msec(50);
      writeDebugStreamLine("Checked Reverse");
      int b = SensorValue[sonar];
      nMotorEncoder[look] = 0;
      while(nMotorEncoder[look] < 90)
      {
        motor[look] = 8;
      }
      motor[look] = 0;
      wait1Msec(50);
      if(f > b)
      {
        angleTurn("right");
      }
      else
      {
        angleTurn("left");
      }

    writeDebugStreamLine("%4d", SensorValue[sonar]);
    nMotorEncoder[right] = 0;
    wait1Msec(1000);
  }
  else
  {
    motor[right] = 30;
    motor[left] = 30;
  }

}

task main()
{
  btConnect(3, "Wifi 2");
  wait1Msec(2000);
  nMotorEncoder[right] = 0;
  while(true)
  {
    if(nNxtButtonPressed == 3)
    {
      nxtDisplayCenteredTextLine(4, "DONE");
      motor[right] = 0;
      motor[left] = 0;
      sendMessageWithParm(1,3,0);
      wait1Msec(10);
      break;
    }
    checkSides();
  }
}
CLONE:

Code: Select all

#pragma config(Sensor, S1,     sonar,          sensorSONAR)
#pragma config(Motor,  motorA,          right,         tmotorNormal, PIDControl, reversed, encoder)
#pragma config(Motor,  motorB,          left,          tmotorNormal, PIDControl, reversed, encoder)
#pragma config(Motor,  motorC,          look,          tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

/*Maze communication

Goes throug a simple maze (i.e, no dead ends, just one route, start to finish, but with turns) and
sends the encoder amounts to simple clone, which navigates without sensors.*/


task main()
{
  int messages[30];
  int x = 0;
  while(true)
  {
    if(bQueuedMsgAvailable())
    {
      writeDebugStreamLine("%4d", messageParm[1]);
      ClearMessage();
      wait1Msec(50);
    }
    
  }
  writeDebugStreamLine("Done from master");
  for(int i = 0; i < 30; i++)
  {
    if(messageParm[1]==0)
    {
       nMotorEncoder[right] = 0;
       while(nMotorEncoder[right] < messageParm[2])
       {
         motor[right] = 10;
         motor[left] = 10;
       }
      }
     else if(messageParm[1]==1)
     {
       nMotorEncoder[right] = 0;
       while(nMotorEncoder[right] < messageParm[2])
       {
         motor[right] = 10;
         motor[left] = -10;
       }
      }
     else
     {
       nMotorEncoder[right] = 0;
       while(nMotorEncoder[right] > messageParm[2])
       {
        motor[right] = -10;
        motor[left] = 10;
       }
     }
   }
   btDisconnect(3);
 }
The writing to the debug stream occurs in the clone's first if statement. The array is there because I was going to save all the values in an array and execute the items from the array later on, but I'm having separate problems with that, so I'm tackling my issues one at a time.

Thanks!
hassenplug
Posts: 346
Joined: 27 Sep 2010, 03:05
Contact:

Re: Bluetooth Message Control

Post by hassenplug »

Without getting into the code, there are commonly two types of problems that would cause what you're seeing. Either the slave thinks it keeps getting messages, or it actually does keep getting messages, because the master keeps sending them.

Make sure the master is not continuing to send messages, after the first one.

Steve
---> Link to lots of MINDSTORMS stuff under my picture --->
abcde13
Posts: 10
Joined: 29 Sep 2010, 03:22

Re: Bluetooth Message Control

Post by abcde13 »

So, I've looked into my code to check if what you said was happening, and I can't seem to find anything wrong. However, I do know you must be write somewhere, as the debug stream does show messages being received. Whether it is due to the clone actually receiving a bunch of messages or if the clone is just somehow looped on that one message, I still can't figure out. I don't think it's stuck on that message; frankly, I think my master is somehow sending tons of messages. But I'm not sure.

Plus, I noticed that it doesn't even return the right number. When the encoder moves 215, or 356, or 784, the debug stream, reading the message from the clone, still writes 0.

This stumps me.
hassenplug
Posts: 346
Joined: 27 Sep 2010, 03:05
Contact:

Re: Bluetooth Message Control

Post by hassenplug »

Looking over the code, it seems OK. I haven't used bluetooth in Robot C for a couple years, but I have to assume the bQueuedMsgAvailable() and/or ClearMessage() functions are not doing what you think they're doing. I would suggest looking at other options for doing this.

One choice would be to send a "counter" along with the message. If the counter does not change on the clone, it would ignore the message.

Steve
---> Link to lots of MINDSTORMS stuff under my picture --->
abcde13
Posts: 10
Joined: 29 Sep 2010, 03:22

Re: Bluetooth Message Control

Post by abcde13 »

Ah, yes, I should probably check with a counter. I'll do that when I can get back to my robots.

I think I may just use the lines from http://www.robotc.net/support/nxt/Minds ... saging.htm This will be more manual, but will probably ensure better results.

I'll report on my results later.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest