Pages: [1] 2   Go Down
Author Topic: Tagging multiple sensors for Serial.print  (Read 1441 times)
0 Members and 1 Guest are viewing this topic.
Manchester, UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 293
Rodrigo Constanzo is a performer and composer living in Manchester, England. He is an avid improviser and performs regularly using home made electro acoustic, and modified electronic instruments. He is currently working towards a PhD in Composition at th
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So I'm using two Arduinos sending sensor data (3axis accel, 3axis gyro) over two XBees to another XBee + USB shield which is then piping the data into Max/MSP.

At first I was using something similar to the built-in 'VirtualColorMixer' patch, by sending the data, then a ",", then more data.
I added to that tags for each arduino (1 or 2) and then for each sensor type (Ax, Ay, Az),

So I had something like this:

Code:
Serial.print("1 ");
Serial.print("ax ");
Serial.print(analogRead(ax));
Serial.println();

I then shortened that to this:

Code:
Serial.print("1 ax ");
Serial.print(analogRead(ax));
Serial.println();

But then I realized that I have two Arduinos piping data in at the same time, and it's conceivable that I could 'cross the streams' and break all the tagging. (if one device sends its data value before a line break from the next device it would break the parsing side of things).

Then after a suggestion from Grumpy Mike I used a hex byte to label the device (rather than the 4 bytes I'm using there). So 0x11 would be device one, sensor1, 0x24 would be deice two, sensor4 etc...

So I changed the code to look like this:

Code:
Serial.print(char(0x11) + AccX);
Serial.println();

I also moved all of the read functions to the start of the void loop to avoid printing in the middle of reading and delaying when the analog data is sampled from (using some software filtering that compares previous positions, so I would imagine this would improve accuracy).

The problem I'm having is that I don't seem to be getting the hex value in serial monitor (or Max/MSP). In Max after grouping and parsing things a bit I'm getting the 6 sensor values in a row, but nothing in front of each... In serial monitor I'm getting something similar.

Is there something wrong with my serial.print code there? Is this the most thorough way to avoid 'crossing the streams' from two devices sending serial data? (my println is on a separate line, and I guess that's my footer value at the moment, should I remove that and presume that everything before a hex character is the full data?)

Here is all of the code:
Code:
#define aX A0
#define aY A1
#define aZ A2
#define gX A6
#define gY A7
#define gZ A3

int AccX;
int AccY;
int AccZ;
int GyroX;
int GyroY;
int GyroZ;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  AccX = analogRead(aX);
  AccY = analogRead(aY);
  AccZ = analogRead(aZ);
  GyroX = analogRead(gX);
  GyroY = analogRead(gY);
  GyroZ = analogRead(gZ);
 
  Serial.print(char(0x11) + AccX);
  Serial.println();
  Serial.print(char(0x12) + AccY);
  Serial.println();
  Serial.print(char(0x13) + AccZ);
  Serial.println();
  Serial.print(char(0x14) + GyroX);
  Serial.println();
  Serial.print(char(0x15) + GyroY);
  Serial.println();
  Serial.print(char(0x16) + GyroZ);
  Serial.println();
}
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

0x11, in ASCII, is Device Control 1. Serial Monitor doesn't know how to print "Device Control 1", so it doesn't. If you want to send the characters "0x11" then just println("0x11 " + aAccX);

Note that 0x11 is one byte, while "0x11" is 4 bytes ("11" is 2 bytes).

Also, if you have 2 devices that are both piping in data (I don't know how that works...), it's entirely possible that they'll both send data at the same time, so you could get "00xx1112  9587" with all of the data mashed together.
Logged

Manchester, UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 293
Rodrigo Constanzo is a performer and composer living in Manchester, England. He is an avid improviser and performs regularly using home made electro acoustic, and modified electronic instruments. He is currently working towards a PhD in Composition at th
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmm. So how do I send single bytes? (Can I just use higher values like 0x44 as device1, sensor4, and 0x54 as device2, sensor4).

As if I'm sending "0x11" I might as well send my original "1 ax " as I can parse it easier.

As far the last part, I'm not sure how it works either (yet). I have the XBees and everything else but I forgot to buy a voltage booster so I've not test driven the full setup yet. I was assuming (rather hopefully) that the XBee protocol handled how that worked and one XBee receiving data from multiple XBees would just send the data clumped together how it came in. I could be wrong there....
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26492
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Serial.print(char(0x11) + AccX);
  Serial.println();
  Serial.print(char(0x12) + AccY);
I don't see how this helps.

Did you mean something like:
Code:
Serial.print ((0x11 << 8) + AccX);
?

Edit: Thinking about it, even that may not help if you ever drop serial characters.
« Last Edit: October 04, 2011, 03:15:12 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 634
Posts: 50245
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Serial.print(char(0x11) + AccX);
This is not concatenating output the way that you think it will. It is ADDING the two values, which is most certainly not what you want. There are no shortcuts for sending to values to the serial port. You must either stuff the data in an array or use multiple Serial output statements.

Sending AccX using print() is not the best way, either. For speed, you want to send data in binary. Serial.write() would be useful, but note that it is for byte-sized data. To send an int requires sending two bytes.

Whatever you do, you STILL run the risk of two Arduinos/XBees sending data at the same time, unless you implement some form of handshaking (Hey, 1, what's new? (#1 replies). Hey, 2, what's up? (#2 replies)).
Logged

Manchester, UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 293
Rodrigo Constanzo is a performer and composer living in Manchester, England. He is an avid improviser and performs regularly using home made electro acoustic, and modified electronic instruments. He is currently working towards a PhD in Composition at th
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So if I send it with serial.write it would only be three bytes total for the whole message?(one hex byte for the header, and two bytes for the data)

Would I need to do something different to send it as binary?(other than using serial.write instead)
And how do I concat the header and data into one message?

Is it possible to implement a handshake if the receiving side doesn't have an arduino? I just have an XBee and a USB shield
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 634
Posts: 50245
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So if I send it with serial.write it would only be three bytes total for the whole message?(one hex byte for the header, and two bytes for the data)
Yes, assuming one (byte/char) header and one int are the message.

Quote
Would I need to do something different to send it as binary?(other than using serial.write instead)
Code:
Serial.write(0x11, BYTE);
Serial.write(highByte(AccX));
Serial.write(lowByte(AccX));

Quote
And how do I concat the header and data into one message?
You seem to have the impression that a "message" is an atomic unit that won't get intermingled. That is not the case.

Quote
Is it possible to implement a handshake if the receiving side doesn't have an arduino? I just have an XBee and a USB shield
What is the XBee/USB Shield doing? I thought that the device was connected to the PC, which was getting the serial data.
Logged

Manchester, UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 293
Rodrigo Constanzo is a performer and composer living in Manchester, England. He is an avid improviser and performs regularly using home made electro acoustic, and modified electronic instruments. He is currently working towards a PhD in Composition at th
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Serial.write(0x11, BYTE);
Serial.write(highByte(AccX));
Serial.write(lowByte(AccX));

Without reading your middle comment I would worry that this would break apart the message again, but I guess that's a non issue here.
So my code would be that serial.write thing for each sensor value. Would I still use a println as my 'footer'?

Quote
And how do I concat the header and data into one message?

So it doesn't matter if I print them at the same time or separately, they travel over serial as pieces regardless?

The USB shield/XBee is connected to the computer indeed. So would that mean i would be doing the handshaking thing from the computer? Unless I misunderstood what you meant.

Quote
Is it possible to implement a handshake if the receiving side doesn't have an arduino? I just have an XBee and a USB shield
What is the XBee/USB Shield doing? I thought that the device was connected to the PC, which was getting the serial data.
[/quote]
Logged

Manchester, UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 293
Rodrigo Constanzo is a performer and composer living in Manchester, England. He is an avid improviser and performs regularly using home made electro acoustic, and modified electronic instruments. He is currently working towards a PhD in Composition at th
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ignore that bottom part of the message. It seems like I can't scroll the edit box when quoting while on an iPad.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 634
Posts: 50245
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Without reading your middle comment I would worry that this would break apart the message again, but I guess that's a non issue here.
So my code would be that serial.write thing for each sensor value. Would I still use a println as my 'footer'?
Yes. The serial data is a collection of bytes, sent in the order specified. Note that the receiving end must expect binary data, now, not ascii data.

Quote
So it doesn't matter if I print them at the same time or separately, they travel over serial as pieces regardless?
True, where the pieces are bytes.

Quote
The USB shield/XBee is connected to the computer indeed. So would that mean i would be doing the handshaking thing from the computer?
Exactly.
Logged

Manchester, UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 293
Rodrigo Constanzo is a performer and composer living in Manchester, England. He is an avid improviser and performs regularly using home made electro acoustic, and modified electronic instruments. He is currently working towards a PhD in Composition at th
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok that makes sense.

Just to confirm a couple of things, a single int, as sent by two bytes can go up to 1023? (or 255?)
Is serial.println() also only one byte? Or should I use a hex character as a footer?

I finally got my power booster so I can test/use the XBees now. First run wasn't promising... data stopped flowing after 2seconds....
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26492
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, an int is -32768 to+32767.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 634
Posts: 50245
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Just to confirm a couple of things, a single int, as sent by two bytes can go up to 1023?
An int can go up to 65536, if it is an unsigned int, or 32767 if it is a signed int.

Quote
Is serial.println() also only one byte?
No. Two bytes - carriage return AND line feed.

Quote
Or should I use a hex character as a footer?
Up to you.
Logged

Manchester, UK
Offline Offline
Sr. Member
****
Karma: 0
Posts: 293
Rodrigo Constanzo is a performer and composer living in Manchester, England. He is an avid improviser and performs regularly using home made electro acoustic, and modified electronic instruments. He is currently working towards a PhD in Composition at th
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yup that's pretty high.

So for maximum efficiency (if I'm going that route) I would do:

Code:
Serial.write(0x11, BYTE);
Serial.write(highByte(AccX));
Serial.write(lowByte(AccX));
Serial.write(0x99);

So 4 bytes total for header, data, and footer.

So I can't use 0x11 (as Wizened mentioned in the first reply). Are there other hex values you can't send? (Or if several, is there a list of these somewhere?)
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 634
Posts: 50245
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So I can't use 0x11 (as Wizened mentioned in the first reply).
Yes, you can. You just can't expect to print the value. Any value can be tested, though.
Logged

Pages: [1] 2   Go Up
Jump to: