Sorry to raise this thread from the dead but an update and a new version of this problem.
To kind of summarize the thread up to this point I'm using a 9DOF sensor setup with a built-in Arduino. I'm sending that over XBee (series1) to another XBee connected to an XBee USB explorer. I was doing it at 115200 but it would stop working after a couple of seconds. I did two things, one was incorporate a serial call-reponse setup, and bring the baud rate down to 57600.
I didn't touch this project for many months (since about the last post) but recently dusted it off, wired it up, housed it, and began tweaking things. I got it working consistently, streaming all (9) sensors over XBee/serial at 57600 using a call-reponse setup with Max/MSP.
The thing is, every now and again (it honestly seems random) it drops out. It will run fine for over an hour then stop working, or sometimes it just keeps dying after a few minutes a pop.
I'm wondering if this call-response type setup is more prone to death or buffer overflow (ie if my computer doesn't send a 'hello' within X amount of time, that it overruns the receiving serial buffer).
It does seem to happen more often (though that could be psychosomatic) when the computer is bogged down. I'm using this with a Max/MSP patch that is doing quite intensive audio processing in real-time. I'm polling the serialport every 10ms, but that 10ms isn't always 10ms as the scheduler for that kind of thing isn't perfect in Max.
Another problem is that the Arduino code I'm running only sends out a "hello" byte on the startup loop, so if it dies, I literally have to restart the unit, which is a bummer.
Here is the code I'm currently running:
// i2c Arduino Library
#include <Wire.h>
// 0011110b, i2c 7bit address of HMC5883
#define address 0x1E
// Define sensor variables
int AccX;
int AccY;
int AccZ;
int GyroX;
int GyroY;
int GyroZ;
int SW1 = 9;
int SW2 = 10;
#define aX A0
#define aY A1
#define aZ A2
#define gX A6
#define gY A7
#define gZ A3
// Incoming serial byte
int inByte = 0;
void setup() {
// Start serial port at 57600 bps:
Serial.begin(57600);
// Set incoming switch mode
pinMode(SW1,INPUT);
pinMode(SW2,INPUT);
// Start i2c communication
Wire.begin();
// Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(address); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();
// Send a byte to establish contact until receiver responds
establishContact();
}
void loop() {
// Define variables for magnetometer
int MagX,MagY,MagZ;
// Tell the HMC5883 where to begin reading data
Wire.beginTransmission(address);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();
// If we get a valid byte, read analog ins:
if (Serial.available() > 0) {
inByte = Serial.read();
AccX = analogRead(aX);
AccY = analogRead(aY);
AccZ = analogRead(aZ);
GyroX = analogRead(gX);
GyroY = analogRead(gY);
GyroZ = analogRead(gZ);
// Read data from each axis of magnetometer, 2 registers per axis
Wire.requestFrom(address, 6);
if(6<=Wire.available()){
MagX = Wire.read()<<8; // X msb
MagX |= Wire.read(); // X lsb
MagZ = Wire.read()<<8; // Z msb
MagZ |= Wire.read(); // Z lsb
MagY = Wire.read()<<8; // Y msb
MagY |= Wire.read(); // Y lsb
}
// Send sensor data out
// Accelerometer X
Serial.write(highByte(AccX));
Serial.write(lowByte(AccX));
// Accelerometer Y
Serial.write(highByte(AccY));
Serial.write(lowByte(AccY));
// Accelerometer Z
Serial.write(highByte(AccZ));
Serial.write(lowByte(AccZ));
// Gyroscope X
Serial.write(highByte(GyroX));
Serial.write(lowByte(GyroX));
// Gyroscope Y
Serial.write(highByte(GyroY));
Serial.write(lowByte(GyroY));
// Gyroscope Z
Serial.write(highByte(GyroZ));
Serial.write(lowByte(GyroZ));
// Magnetometer X
Serial.write(highByte(MagX));
Serial.write(lowByte(MagX));
// Magnetometer Y
Serial.write(highByte(MagY));
Serial.write(lowByte(MagY));
// Magnetometer Z
Serial.write(highByte(MagZ));
Serial.write(lowByte(MagZ));
// Switch 1
Serial.write(digitalRead(SW1));
// Switch 2
Serial.write(digitalRead(SW2));
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.write('A'); // send a capital A
delay(300);
}
}