Monday, April 28, 2008

MAX vs Something Else

I now need to figure out what program to use. Right now I am mostly using MAX/MSP 5. It works but it currently is a little slower than I want. I will however change the baud rate on the XBee and speed up one of the metro objects. This should decrease the latency of the program. Also it is only working with one Xbee and not seven and no data going back to an Arduino. I am mainly using MAX because it can interface with LUA and now the Ogre engine. The LUA is pretty fast but I know nothing of the Ogre engine. As I stated before if worse comes to worse I'll have to take it into C or Python which might add another semester to this project.

Processsing Code for Accelerometer

Many threads made I made code for the Arduino to take in accelerometer data. Here is the processing code to calculate the orientation.

/*
Sections of this code is taken from Tom Igoe's
Making Things Talk book. Specifically from
pages 288 to 292.
*/
import processing.serial.*;
import processing.opengl.*;

Serial myPort;
boolean madeContact = false;
boolean runOnce = false;
float check = 0;
int vals[] = new int[3];
int valsInit[] = new int[3];
float mapVals[] = new float[3];
float staticVals[] = new float[3];
float dynamicVals[] = new float[3];
float staticXYZ[] = new float[3];
float dynamicXYZ[] = new float[3];
float XYZ[] = new float[3];

int staticCum = 1;
int dynamicCum = 1;
String myString;

PFont font;

void setup(){
size(800,800,OPENGL);
background(0);
font = loadFont("Osaka-24.vlw");
textFont(font, 24);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 19200);
myPort.bufferUntil('\r');
}

void serialEvent(Serial myPort){
madeContact = true;
myString = myPort.readStringUntil('\n');
parseString(myString);
}

void parseString(String Str){
if (Str != null){
Str = trim(Str);
int sensors[] = int(split(Str, ','));
if (sensors.length >= 3){
for (int i = 0; i < 3; i++){
vals[i] = sensors[i];
mapVals[i] = map(vals[i], 0 ,1024, -1, 1);
}
if (runOnce == false){
valsInit = sensors;
runOnce = true;
}
}
myPort.write('\r');
}
}

void arrow(){
stroke(204, 102, 0);
pushMatrix();
translate(width/2,height/2);
beginShape(LINES);
vertex(0,0,0);
vertex(XYZ[0],XYZ[1],XYZ[2]);
endShape();
popMatrix();

}

void draw(){
background(0);
if (madeContact == false){ //if there is no serial event send a character
myPort.write('\r');
}
float sqrtVal = sqrt(sq(mapVals[0])+sq(mapVals[1])+sq(mapVals[2]));
if(sqrtVal >= 0.58 && sqrtVal <= 0.64){
//the accelerometer is static
for(int i=0; i < 3; i++){
staticVals[i] = mapVals[i] + staticVals[i];
staticXYZ[i] = staticVals[i]/staticCum;//lost this array somewhere
}
dynamicCum = 1;
for(int i=0; i < 3; i++){
dynamicVals[i] = 0;
}
staticCum++; //accumulator was in the wrong place
}
else{
//this is dynamic!!!
for(int i=0; i < 3; i++){
dynamicVals[i] = (mapVals[i] + dynamicVals[i]);
dynamicXYZ[i] = dynamicVals[i]/dynamicCum;
}
staticCum = 1;
for(int i=0; i < 3; i++){
staticVals[i] = 0;
}
dynamicCum++;
}
for (int i=0; i < 3; i++){
XYZ[i] = width*(staticVals[i]-dynamicVals[i]);
}
int yHold = 50;

for (int i = 0; i < 3; i++){
text(staticXYZ[i], 250, yHold+50);
text(dynamicXYZ[i], 150, yHold+50);
yHold = yHold + 50;
text(sqrtVal, 300, 300);
}
arrow();
}

Friday, April 25, 2008

Trouble Shooting XBee Issues Part 2

Last problem solved. I was playing around with the connections when I noticed if the last input was connected to power the readings were correct. Lo and behold there is a voltage reference pin on the XBee (VREF it's pin 14). Once that was connected I got a working Xbee accelerometer! Now to connect it to my MAX orientation program. In a technical note MAX 5 is out and seems to be interesting.

Trouble Shooting XBee Issues

I have gotten some progress on the XBees transmitting data through sensors. The main issue was that XBees are programmed in hexadecimal. When I programmed the address to 1234, I should have programmed it to 4D2. Once that was fixed I still had a problem because the address was off. I looked up how the XBee sends data and it sends it in 10 bits. I am so used to 8 bits from the Arduino that I had the math translating the most significant bit as it were from an 8 bit piece of data. Once the math was fixed I had the correct address displayed as 1234. The only problem currently is that data coming in from the sensor only reads 1024 even when attached to ground. Hopefully I can solve this issue this weekend.

Wednesday, April 23, 2008

Trying to use the XBee with an accelerometer

So importantly do not have the Xbee getting the data have the IU command enabled. It will not work. My current set up is an Xbee connected to an accelerometer. The Xbee with the accelerometer transmits to an Xbee connected to my computer. This is sort of working. The data is send and MAX picks it up. However I have not been receiving data change in the accelerometer as well as I am not getting the correct senders ID. At least I am receiving something.

Max serial device is also crashing after a few minutes... go figure. This might have to be run entirely through the Arduino in the end. I'll see if processing will work with the current set up.

Xbee and its UART

I have been having issues sending data of the xBee through its UART. I have finally gotten some data from one Xbee to another.

Tuesday, April 08, 2008

Why XBee?

I have completed a beta version of the processing code that does what the MAX/MSP program does. I just need to run it and see how well it works. If it works well I will post the code.

More of the question is why use the ZigBee protocol. The greatest advantage is that I do not have to connect and pair every device to the central hub. If I used Bluetooth the coding might be easier but I will have to pair seven devices and have seven sessions running simultaneously. This might add to some serious latency within the installation. With the Xbee I can target specific modems within the network as well as broadcast the information. To give the networking example of this, within a specific IP 124.124.1 (just made it up) I can target a specific node 124.124.1.124 or broadcast to that network only 124.124.1.255. This allows me to have data all be sent to one location. That location can understand which modem it can from and then send broadcast data or specifically target one modem. I have gotten them to broadcast data but and send to one receiver but I have yet to figure out how to have the central Xbee switch the targeted modem or how to have the other modems pick the correct information (instructions for them) out of a broadcast. I am trying to do away of the Arduino to save energy, save room, and remove latency. I might still be forced to use one because I only have six inputs and outputs. Three are being used by the accelerometer. One might be used by a Qprox sensor, if that is the case I am only left with two PWMs. If this is the case I would have to light the bottle with one LED. The LED would probably have to be a Luxeon, which are amazing but pull about an amp for power and can kill batteries quickly. The Arduino gives me more outputs (6 PWMs). Alternatively I can have the Arduino only receive the data and control the LEDs, sound, and vibration (and possible smoke). This should not cause noticeable latency but does not save on space and power.

I also ordered an I2C accelerometer. I am going to play with them since I2C products are much cheaper and in theory very fast. I also purchased a LiPo battery. This is a lithium polymer battery which is better than Lithium Ion (though it might be more toxic). The battery will be good for prototyping but does not have enough mA hours to run the LEDs for long (1 hour for one luxeon). I will order a VDPI1 from mouser to experiment with pull sound files off on to an Arduino. If this works I with have to use the Arduino but will try to have the XBee do the sensory work while the Arduino controls the bottle.

Tom Igoe has some nice code on page 273 on his "Making Things Talk Book". This explains how the Xbee sends data and how you can parse it. Included in the packet if data that the Xbee sends is the signal strength. I can use this to solve the issue of how close the user is to the alchemy table.