Hello,
I am writing an inverse kinematik program and I works perfectly fine...exept for the fact that I want to send the information calculated to my spiderrobot (Arduino). As soon I enable the function that is sending the data over serial to the Arduino it starts to run really chunky. I now have extracted the core of evil. the "port.write()" function itself.
even when I put a single number into that function and run the code everithing is messed up.
I allready wrote into the Aduino forum >>HERE<< but I know realized that it is more likely a Processing subject.
I posted the Processing code at the end of the first page there. You don't need an Arduino to try it out.
what is the reason why the "port.write" is slowing everything down so much and is there a way so fix that?
best regards,
Florian
Here is the Processing code and I maked the part that is slowing everything down (pretty much at the end of the code)
SendLeg(leg, Deg); /////////// slows down the programm immensly!!!
PS.
you can do it with that too and see there is no diffrence:
port.write(1);
port.write(1);
port.write(1);
I am writing an inverse kinematik program and I works perfectly fine...exept for the fact that I want to send the information calculated to my spiderrobot (Arduino). As soon I enable the function that is sending the data over serial to the Arduino it starts to run really chunky. I now have extracted the core of evil. the "port.write()" function itself.
even when I put a single number into that function and run the code everithing is messed up.
I allready wrote into the Aduino forum >>HERE<< but I know realized that it is more likely a Processing subject.
I posted the Processing code at the end of the first page there. You don't need an Arduino to try it out.
what is the reason why the "port.write" is slowing everything down so much and is there a way so fix that?
best regards,
Florian
Here is the Processing code and I maked the part that is slowing everything down (pretty much at the end of the code)
SendLeg(leg, Deg); /////////// slows down the programm immensly!!!
- import processing.opengl.*;
import processing.serial.*;
Serial port;
float hx;
float hy;
float hz;
float ex;
float ey;
float ez;
float Bxz;
float bxz;
float Bleg;
float B; //B: length of imaginary line
float q1; //q1: angle between X-axis and imaginary line
float q2; //q2: interior angle between imaginary line and link l1
float Qh; //Qh; angle of of the hip on the ground plain
float Q1rad;
float Q1deg; //Q1: angle between x-axis and "l1"
float Q2rad; //Q2: angle between "l1" and "l2"
float Q2deg;
static long l1 = 130; //l1: length of the upper leg in mm
static long l2 = 180; //l2: length of the lower leg in mm
static long hL = 60; // length between inner hip and outer hip
float Xpos; //Xpos: relative X-position out of Xhip and Xfoot where the leg should move to
float Ypos; //Ypos: relative Y-position out of Yhip and Yfoot where the leg should move to
float Xfoot;
float Yfoot;
float Zfoot;
float Xhip; //Xpos: relative X-position out of Xhip and Xfoot where the leg should move to
float Yhip;
int Stepp = 5; // beide sind für die automatische begegung zuständig
int Koord = 0;
float steppLength = 50;
float steppHight = 50;
void setup(){
size(screen.width, screen.height);
println(Serial.list()); // List COM-ports
port = new Serial(this, Serial.list()[2], 115200);
}
void draw(){
background(255);
Zfoot = mouseX-(width/2);
if (Zfoot <= -150){Zfoot = -150;}
if (Zfoot >= 150){Zfoot = 150;}
Xfoot = 120;
/*____________________________________________________________________________
Koord = Koord + Stepp; // einfache schleife
if (Koord == 20 || Koord == -90) {
Stepp = -Stepp ;
}
Yfoot = Koord ;
/____________________________________________________________________________________________
*/
Yfoot = (height/2)-mouseY; //(90*sqrt((150*150)-(Zfoot*Zfoot))/150);
if (Yfoot <= -100){Yfoot = -100;}
if (Yfoot >= 20){Yfoot = 20;}
KoordinatenSysteme();
DrawLeg(0,0,60,Xfoot,Yfoot,Zfoot);
DrawLeg(1,0,60,Xfoot,Yfoot,Zfoot);
DrawLeg(2,0,60,Xfoot,Yfoot,Zfoot);
DrawLeg(3,0,60,Xfoot,Yfoot,Zfoot);
DrawLeg(4,0,60,Xfoot,Yfoot,Zfoot);
DrawLeg(5,0,60,Xfoot,Yfoot,Zfoot);
DrawLeg(6,0,60,Xfoot,Yfoot,Zfoot);
DrawLeg(7,0,60,Xfoot,Yfoot,Zfoot);
}
//---------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------| Begin XZ koordinatesystem |------------------------------------
void KoordinatenSysteme(){
stroke(0,0,255,30);
strokeWeight(4);
int gapH = height/100;
int gapW = width/100;
int[] Height = {height*1/4,height*2/4,height*3/4,height*4/4};
int[] Width = {width*1/4,width*2/4,width*3/4,width*4/4};
//koordinaten system nullpunkten
PVector Zero11 = new PVector(Width[0]/2,Height[0]);
PVector Zero12 = new PVector(Width[1]+((Width[0]-Width[1])/2),Height[0]);
PVector Zero21 = new PVector(Width[2]+((Width[0]-Width[1])/2),Height[0]);
PVector Zero22 = new PVector(Width[3]+((Width[0]-Width[1])/2),Height[0]);
PVector Zero31 = new PVector(Width[0]/2,Height[1]);
PVector Zero32 = new PVector(Width[1]+((Width[0]-Width[1])/2),Height[1]);
PVector Zero41 = new PVector(Width[2]+((Width[0]-Width[1])/2),Height[1]);
PVector Zero42 = new PVector(Width[3]+((Width[0]-Width[1])/2),Height[1]);
PVector Zero51 = new PVector(Width[0]/2,Height[2]);
PVector Zero52 = new PVector(Width[1]+((Width[0]-Width[1])/2),Height[2]);
PVector Zero61 = new PVector(Width[2]+((Width[0]-Width[1])/2),Height[2]);
PVector Zero62 = new PVector(Width[3]+((Width[0]-Width[1])/2),Height[2]);
PVector Zero71 = new PVector(Width[0]/2,Height[3]);
PVector Zero72 = new PVector(Width[1]+((Width[0]-Width[1])/2),Height[3]);
PVector Zero81 = new PVector(Width[2]+((Width[0]-Width[1])/2),Height[3]);
PVector Zero82 = new PVector(Width[3]+((Width[0]-Width[1])/2),Height[3]);
//Quadranten einteilung
line(0,Height[0],width,Height[0]); //horizontal
line(0,Height[1],width,Height[1]); //horizontal
line(0,Height[2],width,Height[2]); //horizontal
line(Width[0],0,Width[0],height); //vertical
line(Width[1],0,Width[1],height); //vertical
line(Width[2],0,Width[2],height); //vertical
//Koordinatensysteme
stroke(0);
strokeWeight(2);
//Horizontal
line(0+gapW,Height[0],Width[0]-gapW,Height[0]);
line(Width[0]+gapW,Height[0],Width[1]-gapW,Height[0]);
line(Width[1]+gapW,Height[0],Width[2]-gapW,Height[0]);
line(Width[2]+gapW,Height[0],Width[3]-gapW,Height[0]);
line(0+gapW,Height[1],Width[0]-gapW,Height[1]);
line(Width[0]+gapW,Height[1],Width[1]-gapW,Height[1]);
line(Width[1]+gapW,Height[1],Width[2]-gapW,Height[1]);
line(Width[2]+gapW,Height[1],Width[3]-gapW,Height[1]);
line(0+gapW,Height[2],Width[0]-gapW,Height[2]);
line(Width[0]+gapW,Height[2],Width[1]-gapW,Height[2]);
line(Width[1]+gapW,Height[2],Width[2]-gapW,Height[2]);
line(Width[2]+gapW,Height[2],Width[3]-gapW,Height[2]);
line(0+gapW,Height[3],Width[0]-gapW,Height[3]);
line(Width[0]+gapW,Height[3],Width[1]-gapW,Height[3]);
line(Width[1]+gapW,Height[3],Width[2]-gapW,Height[3]);
line(Width[2]+gapW,Height[3],Width[3]-gapW,Height[3]);
//Vertikal
line(Width[0]/2,0+gapH,Zero11.x,Zero11.y);
line(Width[0]/2,Height[0]+gapH,Zero31.x,Zero31.y);
line(Width[0]/2,Height[1]+gapH,Zero51.x,Zero51.y);
line(Width[0]/2,Height[2]+gapH,Zero71.x,Zero71.y);
line(Width[1]+((Width[0]-Width[1])/2),0+gapH,Zero12.x,Zero12.y);
line(Width[1]+((Width[0]-Width[1])/2),Height[0]+gapH,Zero32.x,Zero32.y);
line(Width[1]+((Width[0]-Width[1])/2),Height[1]+gapH,Zero52.x,Zero52.y);
line(Width[1]+((Width[0]-Width[1])/2),Height[2]+gapH,Zero72.x,Zero72.y);
line(Width[2]+((Width[0]-Width[1])/2),0+gapH,Zero21.x,Zero21.y);
line(Width[2]+((Width[0]-Width[1])/2),Height[0]+gapH,Zero41.x,Zero41.y);
line(Width[2]+((Width[0]-Width[1])/2),Height[1]+gapH,Zero61.x,Zero61.y);
line(Width[2]+((Width[0]-Width[1])/2),Height[2]+gapH,Zero81.x,Zero81.y);
line(Width[3]+((Width[0]-Width[1])/2),0+gapH,Zero22.x,Zero22.y);
line(Width[3]+((Width[0]-Width[1])/2),Height[0]+gapH,Zero42.x,Zero42.y);
line(Width[3]+((Width[0]-Width[1])/2),Height[1]+gapH,Zero62.x,Zero62.y);
line(Width[3]+((Width[0]-Width[1])/2),Height[2]+gapH,Zero82.x,Zero82.y);
}
float[] IK(float Xhip, float Yhip, float Xfoot, float Yfoot, float Zfoot){
Xpos = abs(Xhip-Xfoot); //relative distance of Xhip and Xfoot
Ypos = abs(Yhip-Yfoot); //relative distance of Yhip and Yfoot
Bleg = sqrt((Xfoot*Xfoot)+(Zfoot*Zfoot)); //total leg lenght including hL
Qh = HALF_PI-asin(Zfoot/Bleg); // calculation of angle
Bxz = Bleg-hL; //leg length without HL
B = sqrt(Bxz*Bxz + Ypos*Ypos); // lenght between X-,Y-hip and X-,Y-foot
bxz = cos(Q1rad)*l1;
q1 = atan2(Ypos,Xpos);
q2 = acos((l1*l1 - l2*l2 + B*B)/(2*l1*B)); //the law of cosines and converted from radiants into degrees
Q1rad = q2 - q1;
Q1deg = degrees(Q1rad);
Q2rad = (acos((l1*l1 + l2*l2 - B*B)/(2*l1*l2))); //the law of cosines and converted from radiants into degrees
Q2deg = degrees(Q2rad);
hx = sin(Qh)*hL; // XYZ position of the outer hip joint
hy = Yhip;
hz = cos(Qh)*hL;
ex = (cos(Q1rad)*l1) + hx; //XYZ positions of the knee joint /////////////falsch falsch falsch keine ahnung was da falsch ist....
ey = (sin(Q1rad)*l1) + Yhip;
ez = sin(Qh)*(bxz+hL); // falsch
float[] DegAndKoor = {Qh,Q1rad,Q2rad,hx,hy,hz,ex,ey,ez,Xfoot,Yfoot,Zfoot,Xpos,Ypos,Xhip,Yhip}; //stores the Values in array
return DegAndKoor; // puts out the array
//---------------------------------------------------------------------------------------------------------------------------------
}
void DrawLeg(int leg, float Xhip, float Yhip, float Xfoot, float Yfoot, float Zfoot){
//koordinaten system nullpunkten
int[] Height = {height*1/4,height*2/4,height*3/4,height*4/4};
int[] Width = {width*1/4,width*2/4,width*3/4,width*4/4};
PVector Zero11 = new PVector(Width[0]/2,Height[0]);
PVector Zero12 = new PVector(Width[1]+((Width[0]-Width[1])/2),Height[0]);
PVector Zero21 = new PVector(Width[2]+((Width[0]-Width[1])/2),Height[0]);
PVector Zero22 = new PVector(Width[3]+((Width[0]-Width[1])/2),Height[0]);
PVector Zero31 = new PVector(Width[0]/2,Height[1]);
PVector Zero32 = new PVector(Width[1]+((Width[0]-Width[1])/2),Height[1]);
PVector Zero41 = new PVector(Width[2]+((Width[0]-Width[1])/2),Height[1]);
PVector Zero42 = new PVector(Width[3]+((Width[0]-Width[1])/2),Height[1]);
PVector Zero51 = new PVector(Width[0]/2,Height[2]);
PVector Zero52 = new PVector(Width[1]+((Width[0]-Width[1])/2),Height[2]);
PVector Zero61 = new PVector(Width[2]+((Width[0]-Width[1])/2),Height[2]);
PVector Zero62 = new PVector(Width[3]+((Width[0]-Width[1])/2),Height[2]);
PVector Zero71 = new PVector(Width[0]/2,Height[3]);
PVector Zero72 = new PVector(Width[1]+((Width[0]-Width[1])/2),Height[3]);
PVector Zero81 = new PVector(Width[2]+((Width[0]-Width[1])/2),Height[3]);
PVector Zero82 = new PVector(Width[3]+((Width[0]-Width[1])/2),Height[3]);
float[] ZeroX ={Zero11.x,Zero12.x,Zero21.x,Zero22.x,Zero31.x,Zero32.x,Zero41.x,Zero42.x,Zero51.x,Zero52.x,Zero61.x,Zero62.x,Zero71.x,Zero72.x,Zero81.x,Zero82.x,};
float[] ZeroY ={Zero11.y,Zero12.y,Zero21.y,Zero22.y,Zero31.y,Zero32.y,Zero41.y,Zero42.y,Zero51.y,Zero52.y,Zero61.y,Zero62.y,Zero71.y,Zero72.y,Zero81.y,Zero82.y,};
float[] DegAndKoor = IK(Xhip, Yhip, Xfoot, Yfoot, Zfoot); //copies the IK array into the new array
float[] Deg = {DegAndKoor[0],DegAndKoor[1],DegAndKoor[2]}; //copies array into smaller sections for better overview
PVector h = new PVector(DegAndKoor[3],DegAndKoor[4],DegAndKoor[5]);
PVector e = new PVector(DegAndKoor[6],DegAndKoor[7],DegAndKoor[8]);
PVector foot = new PVector(DegAndKoor[9],DegAndKoor[10],DegAndKoor[11]);
PVector pos = new PVector(DegAndKoor[12],DegAndKoor[13]);
PVector hip = new PVector(DegAndKoor[14],DegAndKoor[15]);
SendLeg(leg, Deg); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// slows down the programm immensly!!!!!!!!!!!!!!!
//---------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------| ZX Axis |----------------------------------------
//--------------------------------------------------------| drawing leg |----------------------------------------
strokeWeight(1);
fill(240,0,0,200);
ellipse(ZeroX[leg],ZeroY[leg]-hip.x,8,8); //Hip joint
fill(100,0,100,200);
line(ZeroX[leg],ZeroY[leg],ZeroX[leg]+h.z,ZeroY[leg]-h.x); //from the hip joint to the outer hip
fill(0,100,100,200);
ellipse(ZeroX[leg]+h.z,ZeroY[leg]-h.x,15,15); //outer hip joint
stroke(2);
line(ZeroX[leg]+h.z,ZeroY[leg]-h.x,ZeroX[leg]+foot.z,ZeroY[leg]-foot.x); // from the outer hip joint to the foot
fill(240,0,0,200);
ellipse(ZeroX[leg]+foot.z,ZeroY[leg]-foot.x,8,8);
//--------------------------------------------------------| end drawing leg |------------------------------
//---------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------| drawing Reference dots |------------------------------
fill(100,0,200,100);
ellipse(ZeroX[leg],ZeroY[leg]-foot.x,10,10); // Xfoot-axis
text(" Xfoot: " + foot.x, ZeroX[leg], ZeroY[leg]-foot.x+5);
ellipse(ZeroX[leg]+foot.z,ZeroY[leg],10,10);
text(" Zfoot: " + foot.z, ZeroX[leg]+foot.z, ZeroY[leg]+20);
//--------------------------------------------------------| end drawing Reference dots |-----------------------------
//---------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------| ZY Axis |-----------------------------
strokeWeight(1);
fill(240,0,0,200);
ellipse(ZeroX[leg+8]+foot.z,ZeroY[leg+8]-foot.y,8,8);
//---------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------| drawing Reference dots |------------------------------
fill(100,0,200,100);
ellipse(ZeroX[leg+8],ZeroY[leg+8]-foot.y,10,10); // Xfoot-axis
text(" Yfoot: " + foot.y, ZeroX[leg+8], ZeroY[leg+8]-foot.y+5);
ellipse(ZeroX[leg+8]+foot.z,ZeroY[leg+8],10,10);
text(" Zfoot: " + foot.z, ZeroX[leg+8]+foot.z, ZeroY[leg+8]+20);
//--------------------------------------------------------| end drawing Reference dots |-----------------------------
//---------------------------------------------------------------------------------------------------------------------------------
// naming each sector by leg name
fill(0);
text("Leg : " +(leg+1) ,ZeroX[leg]-(ZeroX[0]*6/7), ZeroY[leg]-(ZeroY[0]*6/7));
text("Leg : " +(leg+1) ,ZeroX[leg+8]-(ZeroX[0]*6/7), ZeroY[leg+8]-(ZeroY[0]*6/7));
text("Qh : " + degrees(Qh) ,ZeroX[leg]-(ZeroX[0]*6/7), ZeroY[leg]-(ZeroY[0]*5/7));
text("Q1 : " + int((degrees(Q1rad)+90)) ,ZeroX[leg]-(ZeroX[0]*6/7), ZeroY[leg]-(ZeroY[0]*4/7));
text("Q2 : " + int(degrees(Q2rad)) ,ZeroX[leg]-(ZeroX[0]*6/7), ZeroY[leg]-(ZeroY[0]*3/7));
}
void SendLeg(int leg, float[] Deg){
// Method to write to the arduino
String[] Order = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x"};
port.write((int (degrees(Deg[0]))) + Order[3*leg]);
port.write((int (degrees(Deg[1]))+90) + Order[(3*leg)+1]);
port.write((int (degrees(Deg[2]))) + Order[(3*leg)+2]);
}
PS.
you can do it with that too and see there is no diffrence:
port.write(1);
port.write(1);
port.write(1);
1