Marito, lo que pasa es q por el puerto serie tenes q enviar el dato codificado en ascii como una cadena, luego en processing lo lees y lo convertis al tipo de dato q quieras (entero, flotante..).
Te paso mi programa en processing:
import processing.serial.*;
Serial myPort; // The serial port
float acc_angle=0;
float gyro_angle=0;
float angle=0;
void setup()
{
size(650,300);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.write("m\r");
}
void draw ()
{
while (myPort.available() > 0)
{
String recibido = myPort.readStringUntil('\n');
if (recibido != null)
{
float[] val = float(split(recibido,","));
drawGraph(val);
}
}
}
void drawGraph(float[] val)
{
float x_acc = (val[0]-513)/97;
float y_acc = (val[1]-500)/105;
float z_acc = -(val[2]-511)/103;
float gyro = -(val[3]-455)/67; //*0.015;
float dt=0.1;
acc_angle = atan(z_acc/y_acc);
gyro_angle += gyro * dt;
angle = (0.95)*(angle + gyro * dt) + (0.05)*(acc_angle);
stroke(0);
background(255);
rectMode(CENTER);
textSize(14);
pushMatrix();
fill(255,100,100);
translate(100, 200);
text("acc angle = " + degrees(acc_angle) + "º" ,-90,-100);
rotate(acc_angle);
rect(0, 0, 100, 30);
popMatrix();
pushMatrix();
fill(100,255,100);
translate(300, 200);
text("gyro angle = " + degrees(gyro_angle) + "º" ,-90,-100);
rotate(gyro_angle);
rect(0, 0, 100, 30);
popMatrix();
pushMatrix();
fill(100,100,255);
translate(500, 200);
text("filtered angle = " + degrees(angle) + "º" ,-90,-100);
rotate(angle);
rect(0, 0, 100, 30);
popMatrix();
/*strokeWeight(10);
line(width/2 , height/2 +100, width/2 + v.x , height/2 + v.y +100);*/
}
En el pic tengo un programa que envia las lecturas del acelerometro y gyro sin procesar en este formato:
printf("%li,%li,%li,%li,%li,\n",val[0],val[1],val[2],val[3],val[4]);
Estoy mandando 5 enteros porq el ADC convierte a valores enteros, pero podes enviar flotantes tranquilamente, fijate en el codigo de processing que convierte todo a flotante en esta linea:
float[] val = float(split(recibido,","));
Aca va otro programa similar, pero que grafica en funcion del tiempo:
import processing.serial.*;
Serial myPort; // The serial port
int i = 0; // counter
void setup()
{
size(800, 600);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600); // Serial.list()[0] modify that number if don't works
background(255,255,255);
//myPort.write(83);
}
void draw ()
{
while (myPort.available() > 0)
{
String a0 = myPort.readStringUntil('\n');
if (a0!=null)
{
float[] val = float(split (a0, ","));
//println (val[0] + "," + val[1] + "," + val[2]);
drawGraph_accel(val[0]*100,val[1]*100,val[2]*100); //100 is a scale factor
}
}
}
void drawGraph_accel (float d_ax, float d_vx, float d_px)
{
stroke(255,0,0,70);
line(i, height/4, i, height/4 - d_ax);
stroke(0,0,255,70);
line(i, height/4*2, i, height/4*2 - d_vx);
stroke(0,255,0,70);
line(i, height/4*3, i, height/4*3 - d_px);
if (i >= width-2)
{
i = 0;
background(255,255,255);
}
else
{
i++;
}
}
Podes modificar el programa a tu gusto para q se adapte a tus necesidades. cualquier duda avisame.
Saludos!