Please take a look at my serial handling in my app
in
Integration and Hardware
•
10 months ago
hello.
Im most bad in serial communication. Sometimes I can start Processing app without error and sometimes following error shows up :
error, disabling serialEvent() for //./COM4
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at processing.serial.Serial.serialEvent(Unknown Source)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
at trial2.serialEvent(trial2.java:81)
... 8 more
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at processing.serial.Serial.serialEvent(Unknown Source)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
at trial2.serialEvent(trial2.java:81)
... 8 more
Appreciate any comments regarding serial. Thank You in advance.
regards
---------
Snippet of Arduino output app, x,y,z are double variabletypes:
- //Output the caculations
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.print(",");
Serial.println(z);
Serial.flush();
Snippet of Processing app included in serial communication:
Setup void func:
- println(Serial.list());
frameRate(50);
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[1], 115200);
myPort.bufferUntil(lf);
Serial event func:
- inString = p.readString();
lengde=inString.length();
inString=inString.substring(0, lengde-1);
float[] vals = float(splitTokens(inString,","));
xin=vals[0];
yin=vals[1];
zin=vals[2];
// plot accel combined with gyroscope combined/not combinded with complementary filtering
plott();
teller=teller+1;
if (teller==(int)(displayWidth)-64)
{
teller=192;
fill(202);
rect(193.0,0.0,(float)displayWidth-64.0,(float)line2-1.0);
noFill();
}
}
Full listing of Arduino app:
- #include <SoftwareSerial.h>
- //////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Simple code for the ADXL335, prints calculated orientation via serial
//////////////////////////////////////////////////////////////////
String accel;
//Analog read pins
const int xPin = 0;
const int yPin = 1;
const int zPin = 2;
//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;
int maxVal = 402; - //to hold the caculated values
double x;
double y;
double z; - void setup(){
Serial.begin(115200);
} - 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); - //Output the caculations
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.print(",");
Serial.println(z);
Serial.flush(); - // delay(10);//just here to slow down the serial output - Easier to read
}
Full listing of the Processing app:
- import processing.serial.*;
Serial myPort; // The serial port
PFont myFont; // The display font
String inString; // Input string from serial port
int lf = 10; // ASCII linefeed
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,xin,yin,zin;
int lengde=0,teller=0;
int plotstart=0;
boolean bflag1=false,bflag2=false,bflag3=false,bflag4=false,bflag5=false,bflag6=false,bflag7=false; - 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);
background(202);
drawlines();
makebutton(192,line2+24,66,33,1,"ACCEL-X",8,255,255,255,0,0,180);
makebutton(288,line2+24,66,33,1,"ACCEL-Y",8,255,255,255,100,50,0);
makebutton(384,line2+24,66,33,1,"ACCEL-Z",8,255,255,255,250,0,250);
makebutton(512,line2+24,66,33,1,"COMPLIM",8,255,255,255,0,180,0);
makebutton(608,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(288,line4-56,66,33,1,"GYRO-Y",8,255,255,255,240,140,12);
makebutton(384,line4-56,66,33,1,"GYRO-Z",8,255,255,255,240,140,12);
// List all the available serial ports:
println(Serial.list());
frameRate(50);
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[1], 115200);
myPort.bufferUntil(lf);
}
void draw()
{
// draw LED indicators
indi();
}
void serialEvent(Serial p)
{
inString = p.readString();
lengde=inString.length();
inString=inString.substring(0, lengde-1);
float[] vals = float(splitTokens(inString,","));
xin=vals[0];
yin=vals[1];
zin=vals[2];
// plot accel combined with gyroscope combined/not combinded with complementary filtering
plott();
teller=teller+1;
if (teller==(int)(displayWidth)-64)
{
teller=192;
fill(202);
rect(193.0,0.0,(float)displayWidth-64.0,(float)line2-1.0);
noFill();
}
}
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("360", 150,10 );
text("270", 150,line270);
text("180",150,line1);
text("90", 150,line90);
text("0", 150, line2);
text("ROLL",192,line2+99);
text("NICK",288,line2+99);
text("ROLL",192,line4-99);
text("NICK",288,line4-99);
text("YAW",384,line4-99);
// text("180",150,line3);
// text("0", 150, line4);
text("360", 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 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);
noFill();
fill(trcolor,tgcolor,tbcolor);
// textSize(tekstsize);
text(tekst, bposx+((bwidth-textWidth(tekst))/2), bposy+(bheight/2)+tekstsize/2);
noFill();
}
void mouseClicked()
{ // Button ACCEL X
if (mouseX > 192 && mouseX < 258 && mouseY > (line2+24) && mouseY < (line2+57))
{
bflag1=!bflag1;
}
// Button ACCEL Y
if (mouseX > 288 && mouseX < 354 && mouseY > (line2+24) && mouseY < (line2+57))
{
bflag2=!bflag2;
}
// Button ACCEL Z
if (mouseX > 384 && mouseX < 450 && mouseY > (line2+24) && mouseY < (line2+57))
{
bflag3=!bflag3;
}- // Button COMPLIMENTARY FILTER
if (mouseX > 512 && mouseX < 578 && mouseY > (line2+24) && mouseY < (line2+57))
{
bflag4=!bflag4;
}
}- // draw LED indicators
void indi()
{
if (bflag1==true)
{
fill(0,255,0);
ellipse(225,line2+66,10, 10);
noFill();
}
if(bflag1==false)
{
fill(255,0,0);
ellipse(225,line2+66,10, 10);
noFill();
}
if (bflag2==true)
{
fill(0,255,0);
ellipse(321,line2+66,10, 10);
noFill();
}
if(bflag2==false)
{
fill(255,0,0);
ellipse(321,line2+66,10, 10);
noFill();
}
if (bflag3==true)
{
fill(0,255,0);
ellipse(417,line2+66,10, 10);
noFill();
}
if(bflag3==false)
{
fill(255,0,0);
ellipse(417,line2+66,10, 10);
noFill();
}
if (bflag4==true)
{
fill(0,255,0);
ellipse(545,line2+66,10, 10);
noFill();
}
if(bflag4==false)
{
fill(255,0,0);
ellipse(545,line2+66,10, 10);
noFill();
}
} - void plott()
{
// plot accell x,y,z
if(bflag1==true)
{
stroke(0.0,0.0,180.0);
point(plotstart+teller,(float)line2-xin);
noStroke();
if (bflag4==true)
{
// run complimentary filter
// plot point in green color
}
}
if(bflag2==true)
{
stroke(10.0,50.0,0.0);
point(plotstart+teller,(float)line2-yin);
noStroke(); - if (bflag4==true)
{
// run complimentary filter
// plot point in green color
} - }
if(bflag3==true)
{
stroke(250.0,0.0,250.0);
point(plotstart+teller,(float)line2-zin);
noStroke();
if (bflag4==true)
{
// run complimentary filter
// plot point in green color
} - }
- }
/*- // complimentary filter
- float filterAngle;
float dt=0.02; - float comp_filter(float newAngle, float newRate) {
- float filterTerm0;
float filterTerm1;
float filterTerm2;
float timeConstant; - timeConstant=0.5; // default 1.0
- filterTerm0 = (newAngle - filterAngle) * timeConstant * timeConstant;
filterTerm2 += filterTerm0 * dt;
filterTerm1 = filterTerm2 + ((newAngle - filterAngle) * 2 * timeConstant) + newRate;
filterAngle = (filterTerm1 * dt) + filterAngle; - return previousAngle; // This is actually the current angle, but is stored for the next iteration
}
*/
1