Go Down

Topic: Serial Communication between XBee using Arduino (Read 2317 times) previous topic - next topic

Robin2

Can you suggest me some idea of how to send autonomously as I didn't get properly as how to select the Router on its own to which the message to be sent?
If this was my problem and I was not familiar with XBees I would first figure out how to send data that comes from the PC - as you have already been considering.

When you can do that then communication between the XBees should be straightforward.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

Actually, data is not to be given by the PC, it is the sensor data but since right now the sensor is not attached, we are self-defining the range.

I explain you with an example of what I'm looking for, suppose a sensor has 5cm of range. Now, one Router has located at a point and it will calculate the distance between the other XBee(s) if present in 5 cm of range. If found, it will send the message which is its own location. So, how should we get to know that which XBee is present among the other 5 XBee(s)?

Please tell me if you got my question, I know it's a little bit tricky to understand in one go.

Robin2

Actually, data is not to be given by the PC, it is the sensor data
Never mind that for now ....

We are at Reply #62 almost all of which time has been spent trying to figure out how to get data from a PC and send it to another XBee. And it seemed to me you were nearly at the successful end of that exploration in Reply #57.

Finish that to the point where you know how to do it and then you will almost certainly know how to use the Xbee for other purposes.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

Quote
We are at Reply #62 almost all of which time has been spent trying to figure out how to get data from a PC and send it to another XBee. And it seemed to me you were nearly at the successful end of that exploration in Reply #57.
Actually, after asking my professor, she said that we don't have to do the process explained in Reply #57.

The thing we need to do is to make them work autonomously, rather than providing input manually.

Robin2

#64
Aug 30, 2018, 10:42 pm Last Edit: Aug 30, 2018, 10:43 pm by Robin2
Actually, after asking my professor, she said that we don't have to do the process explained in Reply #57.
I know that. You said so in Reply #59.

Everything I have written since Reply #59 has taken that into account. This is getting very tiresome - especially as I think the essence of the solution was in Reply #13.

Why don't you just ask your Professor to help you? That's what s/he is paid for.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

Hi Robin,

Sorry for the delay in replying, but I figured out a way to make them work autonomously and it was working as well. Now, I want to make the data in real time rather than provided with a certain value.

As I explained my project earlier, I'll explain again for your reference but I'll make it short so that you can understand the exactness of my need. I've done the part of transmission and reception from an Arduino to another Arduino using XBee(s). Now I have to take data in real time using a Color Sensor provided by LEGO in EV3 Mindstorm Robots. For getting data from the sensor I'm using a Pixel Map made from Red and Blue colors making green (third primary color) completely zero.

Now, as the sensor scans the pixels in a range of 0 to 255 for each of the two colors, we need to convert them into a coordinate system (x-axis, y-axis). For converting I find a way by using MATLAB function "pix2map" by which pixels can be converted into a coordinate system, but since I'm not having experience of making them work together, I thought of seeking your help.

I hope you get my problem, if not please feel free to ask any single detail left by me just in case.

For your help, I'm attaching a link of how to make LEGO Robots communicate with Arduino. I know it won't make much difference for my problem but it will give you an idea of the product I'm using. Link: Connecting EV3 with Arduino

Robin2

Sorry for the delay in replying, but I figured out a way to make them work autonomously and it was working as well.
I reckon that by now I should not have to waste time asking you to post the program.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

Actually, I asked the Professor directly that what exactly she means by autonomously, to which she told me the exact requirement of the project. And after realizing the basicness of the requirement, I didn't want to waste your time by asking a silly question. Moreover, I can provide the working code I've done for communication between six XBee(s).

Here's my code for one Router communicating with other five Routers:

Code: [Select]

#include <XBee.h>
#include <time.h>
#include <stdlib.h>
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
XBeeAddress64 ROUTER1 = XBeeAddress64(0x0013A200,0x4147FE2A);
XBeeAddress64 ROUTER2 = XBeeAddress64(0x0013A200,0x4147FE2B);
XBeeAddress64 ROUTER3 = XBeeAddress64(0x0013A200,0x4147FE2E);
XBeeAddress64 ROUTER4 = XBeeAddress64(0x0013A200,0x414E65AA);
XBeeAddress64 ROUTER5 = XBeeAddress64(0x0013A200,0x414E65AE);
XBeeAddress64 ROUTER6 = XBeeAddress64(0x0013A200,0x414E65B1);
uint8_t count = 0;
uint8_t data[4];
uint8_t receivedData[4];
byte* dataPointer;
uint8_t dist = 0;

void setup()
{
  Serial.begin(9600);
  xbee.setSerial(Serial);
  Serial.println("Stating Up!");
  srand((unsigned)time(NULL));
  data[0] = '6';
  data[1] = '9';
  data[2] = ',';
  data[3] = '8';
}

void loop()
{
  delay(1000);
  transmitXBee();
  delay(1000);
  receiveXBee();
  delay(1000);
}

void transmitXBee()
{
  dist = sqrt((receivedData[1]-data[1])^2 + (receivedData[3]-data[3])^2);
  ZBTxRequest zbTx1 = ZBTxRequest(ROUTER1, data, sizeof(data));
  ZBTxRequest zbTx2 = ZBTxRequest(ROUTER2, data, sizeof(data));
  ZBTxRequest zbTx3 = ZBTxRequest(ROUTER3, data, sizeof(data));
  ZBTxRequest zbTx4 = ZBTxRequest(ROUTER4, data, sizeof(data));
  ZBTxRequest zbTx5 = ZBTxRequest(ROUTER5, data, sizeof(data));
  ZBTxRequest zbTx6 = ZBTxRequest(ROUTER6, data, sizeof(data));
  delay(500);
  Serial.print("Distance: ");
  Serial.println(dist);
  delay(500);
  Serial.println("Start Transmitting");
  Serial.print("Data Sent:\t");
  Serial.print(data[0]);
  Serial.print(data[1]);
  Serial.print(data[2]);
  Serial.println(data[3]);
  delay(500);
  data[1]++;
  data[3]++;
 
  if ((dist > 0) && (dist <= 1))
  {
    Serial.println("To Router 1");
    xbee.send(zbTx1);
    Serial.println();
    delay(500);   
  }
  else if((dist>1) && (dist<=2))
  {
    Serial.println("To Router 5");
    xbee.send(zbTx5);
    Serial.println();
    delay(500);
  }
  else if((dist>2) && (dist<=3))
  {
    Serial.println("To Router 3");
    xbee.send(zbTx3);
    Serial.println();
    delay(500);
  }
  else if((dist>3) && (dist<=4))
  {
    Serial.println("To Router 4");
    xbee.send(zbTx4);
    Serial.println();
    delay(500);
  }
  else if((dist>4) && (dist<=5))
  {
    Serial.println("To Router 2");
    xbee.send(zbTx2);
    Serial.println();
    delay(500);
  }
  else
    Serial.println("No XBee Found");
}

void receiveXBee()
{
  xbee.readPacket(500);
  Serial.println("Reading for Packet");
  if(xbee.getResponse().isAvailable())
  {
    Serial.print("Packet Available: ");
    Serial.println(count);
    delay(500);
    if(xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
    {
      xbee.getResponse().getZBRxResponse(rx);
      delay(500);
      dataPointer = reinterpret_cast<byte *>(receivedData);
      for(int n =0; n < rx.getDataLength(); n++)
      {
        *dataPointer = rx.getData(n);
        dataPointer += 1;
      }
      Serial.print("Packet Received: ");
      for (byte n = 0; n < 4; n++)
      {
        Serial.print(receivedData[n]);
        delay(500);
      }
      Serial.println();
      Serial.print("count: ");
      count++;
      Serial.println(count);
      delay(500);
    }
  }
  else if(xbee.getResponse().isError())
  {
    Serial.print("Error Reading Packet. Error Code: ");
    Serial.println(xbee.getResponse().getErrorCode());
    delay(500);
  }
  else
    Serial.println("No data received");
}

Sorry for stretching the topic so long. Could you help me with the part I asked regarding the MATLAB and Arduino thing to convert pixels into coordinates?

Robin2

#68
Sep 19, 2018, 07:57 pm Last Edit: Sep 19, 2018, 07:59 pm by Robin2
Actually, I asked the Professor directly that what exactly she means by autonomously, to which she told me the exact requirement of the project.
It would be a great help if you would write down what your Professor said and get her agreement that your written description is correct and then post the description for us to read.

Is the code you posted in Reply #67 the program that you were referring to in Reply #65 when you said "I figured out a way to make them work autonomously and it was working as well". That is not clear from your wording in Reply #67.

...R

PS ... I presume you have made your Professor aware of this Thread?
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

Quote
It would be a great help if you would write down what your Professor said and get her agreement that your written description is correct and then post the description for us to read.
She said that right now since you don't have any real time value, give the coordinates of the robots in starting only and in every iteration update that distance by incrementing the coordinates so that you distance, for now, remains same and you can decide that to which Router you need to give the priority.

So, as you see in the code, I've provided the coordinates of x and y as (9,8) for Router 6 and it is (4,8) for Router 1. So, in the next iteration, it will be (10,9) for Router 6. Like this, the distance will remain constant for now and it will be changed when the data will be collected in real time on which I'm seeking help.

Quote
Is the code you posted in Reply #67 the program that you were referring to in Reply #65 when you said "I figured out a way to make them work autonomously and it was working as well". That is not clear from your wording in Reply #67.
Yes, the code I provided in Reply# 67 is the final and updated code of the program I was referring in Reply# 65 in which they are sending and receiving data autonomously.

Robin2

This line
Code: [Select]
  dist = sqrt((receivedData[1]-data[1])^2 + (receivedData[3]-data[3])^2);
seems to be what changes the data to be sent.

Put that into its own function - perhaps called updateDataToSend() and change the code in loop() to this
Code: [Select]
void loop()
{
  delay(1000);
  updateDataToSend();
  transmitXBee();
  delay(1000);
  receiveXBee();
  delay(1000);
}


Then, when you want to change the data to be sent just change the code in the function updateDataToSend()

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

Quote
Then, when you want to change the data to be sent just change the code in the function updateDataToSend()
Yeah, it is a better way to write. Thank you for it.

Can you help me how to convert the pixel value which is being taken from Serial.read() function to convert into coordinates so that the data which is updated can be done in real time. The details of it are mentioned in Reply# 65 for your reference. Because I don't know how to combine the code of MATLAB and Arduino together.

Robin2

#72
Sep 19, 2018, 11:32 pm Last Edit: Sep 19, 2018, 11:35 pm by Robin2
Can you help me how to convert the pixel value which is being taken from Serial.read() function to convert into coordinates so that the data which is updated can be done in real time.
FIRST -- you have not said that your Professor knows about this Thread and approves of it. I don't think I should help further until you confirm that.

Assuming you do confirm that, then


Please provide a typical example of the pixel data that is received and the corresponding output that you want to create.

Be aware that I know nothing about Matlab.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Priyanshu27

Quote
FIRST -- you have not said that your Professor knows about this Thread and approves of it. I don't think I should help further until you confirm that.
Yes, she knows that I'm seeking help from the Forum, and she approves the description I gave is said by her.

Quote
Please provide a typical example of the pixel data that is received and the corresponding output that you want to create.
I too, don't have any idea of the example I should suggest to you, but after reading a little about the color sensor, I can tell you some estimates of the project requirement. But how to implement them is the problem here.

Two colors are used as the coordinate system, where blue color represents the x-axis and red color represents the y-axis making it a grid of 255*255, thus resulting in the total number of unique grids are 65025. Each unique grid has a resolution of 1.2cm2 resulting in the total area of the map as 7.8m2 in size.

So, how to get the coordinates of a point where a robot is standing currently so that once the coordinates are known we can calculate the distance if we consider a reference point on the table, and set priorities to the Routers it should encounter first and use the collision avoidance algorithm.

Robin2

I too, don't have any idea of the example I should suggest to you, but after reading a little about the color sensor, I can tell you some estimates of the project requirement. But how to implement them is the problem here.

Two colors are used as the coordinate system, where blue color represents the x-axis and red color represents the y-axis making it a grid of 255*255, thus resulting in the total number of unique grids are 65025. Each unique grid has a resolution of 1.2cm2 resulting in the total area of the map as 7.8m2 in size.

So, how to get the coordinates of a point where a robot is standing currently so that once the coordinates are known we can calculate the distance if we consider a reference point on the table, and set priorities to the Routers it should encounter first and use the collision avoidance algorithm.
You are making this far too complicated at this stage. Computer programs need to be developed in very small steps.

Do you have a simple program that gets the pixel data from the sensor and prints it on the Serial Monitor?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up