serial string communication
in
Integration and Hardware
•
10 months ago
hello.
Im trying to send strings between an accelerometer connected to arduino, and processing. Some times it works (i confirm receipt by printing them out in processing) and sometimes dont work. Anyone could have a sneakpeak into code regarding
communication to see what might cause the unstabillity?
The Arduino sketch is furthest below.
Thanks in advance. Regards
- import processing.serial.*;
Serial port;
int environx,environy,environz;
int rotx=90,roty=0,rotz=0; //holds rotation parameters
int linezero; // holds (1/6)- 36 of displayheight
int line1,line2,line3,line4,line5,line6;
float line90,line270;
boolean flag1,flag2,flag3,flag4,flag5,flag6,flag7,flag8,flag9,flag10=false;
// flag 1-3 = accel x,y,z flag4 = Kalman filter flag 5 = PID flag 7-9 = gyro x,y,z
float xposa,yposa,zposa=0; // start of accelerator graph
int xposg,yposg,zposg=0; // start of gyroscope graph
float xin,yin,zin =0.0; //input from serial port
int lf=10; // linefeed in ascii
String input; - void setup()
{
environx=displayWidth;
environy=displayHeight-36;
linezero=environy/6;
line1=linezero;line2=linezero*2;line3=linezero*3;line4=linezero*4;line5=linezero*5;line6=linezero*6;line90=linezero*1.5;line270=linezero*0.5;
size(environx-32, environy,P2D);
port = new Serial(this, Serial.list()[1], 9600);
port.clear();
input = port.readStringUntil(lf);
input = null;
// don't generate a serialEvent() unless you get a newline character:
port.bufferUntil('\n');
// port.write(65);
- // smooth();
// frameRate(25);
drawlines();
// pushMatrix();
// rotatebox(rotx,roty,rotz);
// popMatrix();
makebutton(192 ,line2+24,66,33,1,"ACCEL-X",8,255,255,255,0,0,180);
makebutton(192+64+32 ,line2+24,66,33,1,"ACCEL-Y",8,255,255,255,0,0,180);
makebutton(192+64+32+64+32 ,line2+24,66,33,1,"ACCEL-Z",8,255,255,255,0,0,180);
makebutton(192+64+32+64+32+64+32+32 ,line2+24,66,33,1,"KALMAN",8,255,255,255,0,180,0);
makebutton(192+64+32+64+32+64+32+32+64+32 ,line2+24,66,33,1,"P-I-D",8,255,255,255,180,0,0);
makebutton(192 ,line4-56,66,33,1,"GYRO-X",8,255,255,255,240,140,12);
makebutton(192+64+32 ,line4-56,66,33,1,"GYRO-Y",8,255,255,255,240,140,12);
makebutton(192+64+32+64+32 ,line4-56,66,33,1,"GYRO-Z",8,255,255,255,240,140,12);
// makebutton(192+64+32+64+32+64+32+32 ,line2+56,66,33,1,"KALMAN",8,255,255,255,0,180,0);
// makebutton(192+64+32+64+32+64+32+32+64+32 ,line2+56,66,33,1,"P-I-D",8,255,255,255,180,0,0); - }
- void draw() {
// background(200);
// pushMatrix();
// rotatebox(rotx,roty,rotz);
// popMatrix();
// rotx++;
- }
- void drawlines()
{
stroke(127);
line(192,line1,180,line1);
line(180,line2,environx-64,line2);
line(180,line90,192,line90);
line(180,line270,192,line270);
line(192,line4,environx-64,line4);
line(192,line5,environx-64,line5);
line(192,line6,environx-64,line6);
fill(0,0,0);
text("359", 150,10 );
text("270", 150,line270);
text("180",150,line1);
text("90", 150,line90);
text("0", 150, line2);
//
// text("180",150,line3);
// text("0", 150, line4);
text("359", 150,line4);
text("180",150,line5);
text("0", 150, line6);
line(192,0,192,line2);
// draw vertical lines
// line(192,environy/3,192,environy/3-180); - }
- void rotatebox(int a,int b,int c)
{
translate(environx/2,environy/2, 0);
rotateX(radians(a));
rotateY(radians(b));
rotateX(radians(c));
noFill();
box(environx/6, environy/6, 30);
} - void makebutton(int bposx,int bposy,int bwidth,int bheight,int bscale,String tekst,int tekstsize, int trcolor,int tgcolor,int tbcolor,int brcolor,int bgcolor,int bbcolor)
{
fill(brcolor,bgcolor,bbcolor);
rect(bposx,bposy,bwidth*bscale,bheight*bscale);
fill(trcolor,tgcolor,tbcolor);
// textSize(tekstsize);
text(tekst, bposx+((bwidth-textWidth(tekst))/2), bposy+(bheight/2)+tekstsize/2);
}
void serialEvent (Serial port) {
// get the ASCII string:
// input = port.readStringUntil('*');
// String input = port.readString();
while (port.available() > 0) {
input = port.readStringUntil(lf);
if (input != null) {
// Print message received
println( "Receiving:" + input);
// trim off any whitespace
// input = trim(input);
int[] vals = int(splitTokens(input,", "));
xin=vals[0];
yin=vals[1];
zin=vals[2];
println( "xin= "+xin);
// println( "y= "+yin);
// println( "z= "+zin);
// println(line2);
// println(xposa);
point(191.0+xposa,(float)line2-xin);
// println((float)line2+(float)xin);
// start at beginning of graph and clear graph, if end of graph
if (xposa >= displayWidth-64.0) {
xposa = 191;
// rect(193,line2-1,environx-192-64,line2-1);
}
// increment the horizontal position:
xposa++;
yposa++;
zposa++;
} // end if input !=null
} //end while
} // end event
----
arduino sketch:
- //////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Simple code for the ADXL335, prints calculated orientation via serial
////////////////////////////////////////////////////////////////// - //Analog read pins
const int xPin = 0;
const int yPin = 1;
const int zPin = 2;
String accel;
String string;
//The minimum and maximum values that came from
//the accelerometer while standing still
//You very well may need to change these
int minVal = 265;
int maxVal = 402; - //to hold the caculated values
double x;
double y;
double z; - void setup(){
Serial.begin(9600);
} - void loop(){
- //read the analog values from the accelerometer
int xRead = analogRead(xPin);
int yRead = analogRead(yPin);
int zRead = analogRead(zPin); - //convert read values to degrees -90 to 90 - Needed for atan2
int xAng = map(xRead, minVal, maxVal, -90, 90);
int yAng = map(yRead, minVal, maxVal, -90, 90);
int zAng = map(zRead, minVal, maxVal, -90, 90); - //Caculate 360deg values like so: atan2(-yAng, -zAng)
//atan2 outputs the value of -π to π (radians)
//We are then converting the radians to degrees
x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);
x=constrain(x,0,360);
y=constrain(y,0,360);
z=constrain(z,0,360); - accel=doubleToString(x,0)+","+doubleToString(y,0)+","+doubleToString(z,0);
//Output the caculations
// Serial.print("x: "); - Serial.println(accel);
// Serial.println((int)x);
// Serial.print(" | y: ");
// Serial.print(y);
// Serial.print(" | z: ");
// Serial.println(z); - delay(150);//just here to slow down the serial output - Easier to read
} - //Rounds down (via intermediary integer conversion truncation)
String doubleToString(double input,int decimalPlaces){
if(decimalPlaces!=0){
string = String((int)(input*pow(10,decimalPlaces)));
if(abs(input)<1){
if(input>0)
string = "0"+string;
else if(input<0)
string = string.substring(0,1)+"0"+string.substring(1);
}
return string.substring(0,string.length()-decimalPlaces)+"."+string.substring(string.length()-decimalPlaces);
}
else {
return String((int)input);
}
}
1