You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

519 lines
15 KiB

#include "header.h"
#include "global.h"
#include "sgn.h"
#include <error.h>
#include <errno.h>
#include <math.h>
#include "SvrData.h"
void printMat(Mat4f M)
{
cout << M << endl;
}
float* abctomat(float a, float b, float c)
{
Mat4f rx;
float ca = cos(c);
float sa = sin(c);
rx(0,0) = 1;
rx(1,1) = ca;
rx(1,2) = -sa;
rx(2,1) = sa;
rx(2,2) = ca;
rx(3,3) = 1;
Mat4f ry;
float cb = cos(b);
float sb = sin(b);
ry(0,0) = cb;
ry(0,2) = sb;
ry(1,1) = 1;
ry(2,0) = -sb;
ry(2,2) = cb;
ry(3,3) = 1;
Mat4f rz;
float cc = cos(a);
float sc = sin(a);
rz(0,0) = cc;
rz(0,1) = -sc;
rz(1,0) = sc;
rz(1,1) = cc;
rz(2,2) = 1;
rz(3,3) = 1;
Mat4f result;
Mat4f temp;
temp = rz * ry;
result = temp * rx;
#ifdef __DEBUG__
printMat(rx);
printMat(ry);
printMat(rz);
printMat(result);
#endif
float *res = new float[12];
//TODO simple converter for mat
for (int j=0;j<3;j++)
{
for (int k=0;k<4;k++)
{
res[j*4+k] = result(j,k);
}
}
return res;
}
float* mattoabc(float M[12])
{
float norm;
float sa;
float ca;
float *abc = new float[3];
norm = sqrt((M[0]*M[0])+(M[4]*M[4]));
if (norm>1e-5)
{
sa = M[4]/norm;
ca = M[0]/norm;
abc[0] = atan2(sa,ca);
}
else
{
sa = 0;
ca = 1;
abc[0] = 0;
}
abc[1] = atan2(-M[8],ca*M[0]+sa*M[4]);
abc[2] = atan2(sa*M[2]-ca*M[6],-sa*M[1]+ca*M[5]);
return abc;
}
void doRalf()
{
// Global definitions
float lowStiff[6] = { 0.01f, 0.01f, 0.01f, 0.01f, 0.01f, 0.01f};
float highStiff[6] = {1000.0f, 1000.0f, 1000.0f, 10.0f, 10.0f, 10.0f};
float slowDamp[6] = { 0.007f, 0.007f, 0.007f, 0.007f, 0.007f, 0.007f};
float fastDamp[6] = { 0.7f, 0.7f, 0.7f, 0.7f, 0.7f, 0.7f};
float pos1[12] = {
1.0f, 0.0f, 0.0f, 500.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 500.0f
};
float pos2[12] = {
1.0f, 0.0f, 0.0f , 500.0f,
0.0f, 1.0f, 0.0f , 0.0f,
0.0f, 0.0f ,1.0f , 500.0f
};
friRemote friInst;
double timeCounter=0;
friInst.doDataExchange();
float* cartPos;
while(true)
{
// Time
timeCounter+=friInst.getSampleTime();
friInst.doReceiveData();
// Get all variables from FRI
float* jntPos = friInst.getMsrMsrJntPosition();
float* jacobian = friInst.getJacobian();
float* ftTcp = friInst.getFTTcp();
if (timeCounter < 20)
cartPos = friInst.getMsrCartPosition();
//friInst.doDataExchange();
//friInst.doCartesianImpedanceControl(cartPos, highStiff, fastDamp, NULL, NULL, true);
#if 0
if (timeCounter>10 && timeCounter<10)
{
friInst.doCartesianImpedanceControl(cartPos, highStiff, fastDamp, NULL, NULL, false);
} else
{
friInst.doCartesianImpedanceControl(cartPos, highStiff, fastDamp, NULL, NULL, false);
}
#endif
float npos[12];
memcpy(npos,cartPos,12);
friInst.doCartesianImpedanceControl(npos, highStiff, fastDamp, NULL, NULL, false);
#if 0
if (timeCounter<20)
{
friInst.doCartesianImpedanceControl(Test, highStiff, fastDamp, NULL, NULL, true);
} else if (timeCounter<30)
{
friInst.doCartesianImpedanceControl(cartPos, highStiff, fastDamp, NULL, NULL, true);
} else if (timeCounter<40)
{
friInst.doCartesianImpedanceControl(cartPos , highStiff, fastDamp, NULL, NULL, true);
} else
{
friInst.doDataExchange();
}
#endif
friInst.doSendData();
}
}
Mat4f vecToMat2(float vec[12])
{
Mat4f result;
for (int i=0; i<3; i++)
{
for (int j=0; j<4; j++)
{
result(i,j) = (float)vec[i*4+j];
}
}
result(3,3)=(float)1;
return result;
}
float* matToVec2(Mat4f mat)
{
float* vec = new float[12];
for (int j=0;j<3;j++)
{
for (int k=0;k<4;k++)
{
vec[j*4+k] = mat(j,k);
}
}
return vec;
}
float* vectoquat(float vec[12])
{
float *quat = new float[4];
float diag[3];
int v; int w; int u;
diag[0] = vec[0];
diag[1] = vec[5];
diag[2] = vec[10];
float u_ = *std::max_element(diag,diag+2);
if (u_==diag[0])
{
u = 1;
v = 2;
w = 3;
} else if (u_==diag[1])
{
u = 2;
v = 3;
w = 1;
} else
{
u = 3;
v = 1;
w = 2;
}
float r = sqrt(1+vec[(u-1)*4+(u-1)] - vec[(v-1)*4+(v-1)] - vec[(w-1)*4+(w-1)]);
quat[0] = (vec[(w-1)*4+(v-1)] - vec[(v-1)*4+(w-1)]) / (2*r);
quat[u] = r/2;
quat[v] = (vec[(u-1)*4+(v-1)] + vec[(v-1)*4+(u-1)]) / (2*r);
quat[w] = (vec[(w-1)*4+(u-1)] + vec[(u-1)*4+(w-1)]) / (2*r);
return quat;
}
float* quattovec(float quat[4])
{
float *vec = new float[12];
vec[0] = quat[0]*quat[0]+quat[1]*quat[1]-quat[2]*quat[2]-quat[3]*quat[3];
vec[4] = 2*(quat[1]*quat[2]+quat[0]*quat[3]);
vec[8] = 2*(quat[1]*quat[3]-quat[0]*quat[2]);
vec[1] = 2*(quat[1]*quat[2]-quat[0]*quat[3]);
vec[5] = quat[0]*quat[0]-quat[1]*quat[1]+quat[2]*quat[2]-quat[3]*quat[3];
vec[9] = 2*(quat[2]*quat[3]+quat[0]*quat[1]);
vec[2] = 2*(quat[1]*quat[3]+quat[0]*quat[2]);
vec[6] = 2*(quat[2]*quat[3]-quat[0]*quat[1]);
vec[10] = quat[0]*quat[0]-quat[1]*quat[1]-quat[2]*quat[2]+quat[3]*quat[3];
return vec;
}
void *threadFriDataExchange(void *arg)
{
// unused
(void) arg;
//doRalf();
friRemote friInst;
while (1)
{
//#######################################
// Communication loop
// get current joint positions
for ( int i= 0; i < LBR_MNJ; i++)
{
MSRMSRJNTPOS[i] = friInst.getMsrCmdJntPosition()[i] + friInst.getMsrCmdJntPositionOffset()[i];
//MSRMSRJNTPOS[i] = friInst.getMsrMsrJntPosition()[i];
}
// get current jacobian
float* friJacobian = friInst.getJacobian();
if ( friJacobian == NULL)
{
fprintf(stderr,"Failed: could not get jacobian\n");
break;
}
for (int i = 0; i < FRI_CART_VEC*LBR_MNJ ; i++)
{
MSRMSRJACOBIAN[i] = friJacobian[i];
}
// get current messured carthesian position
float* friMsrCartPosition = friInst.getMsrCartPosition();
if ( friMsrCartPosition == NULL)
{
fprintf(stderr,"Failed: could not get Messurement in Carthesian Position\n");
break;
}
for ( int i =0; i < FRI_CART_FRM_DIM; i++)
{
MSRMSRCARTPOS[i] = friMsrCartPosition[i];
}
// get current force and torque
float* friFTTcp = friInst.getFTTcp();
for ( int i = 0; i < FRI_CART_VEC; i++)
{
MSRMSRFTTCP[i] = friFTTcp[i];
}
//#########################################################
// PTP Joint Movement
if (__MSRCMDJNTPOS)
{
double maxVelJntLocal[LBR_MNJ];
double maxAccJntLocal[LBR_MNJ];
double delta[LBR_MNJ];
double sampleTime = (double)0.005;
double deltaAbs[LBR_MNJ];
double dMaxSpeed[LBR_MNJ];
double lMaxSpeed[LBR_MNJ];
double dGesamt[LBR_MNJ];
double currentInk[LBR_MNJ] = {0};
double currentInkLast[LBR_MNJ] = {0};
double currentDist [LBR_MNJ] = {0};
double currentDistLast [LBR_MNJ] = {0};
Vec<float,LBR_MNJ> maxVelocity = SvrData::getInstance()->getMaxVelocity();
Vec<float,LBR_MNJ> maxAccelaration = SvrData::getInstance()->getMaxAcceleration();
// calculate delta positions of movement
for ( int i= 0; i < LBR_MNJ; i++)
{
delta[i] = MSRCMDJNTPOS[i]-(MSRMSRJNTPOS[i]*180/M_PI);
deltaAbs[i] = fabs(MSRCMDJNTPOS[i]-(MSRMSRJNTPOS[i]*180/M_PI));
maxVelJntLocal[i] = maxVelJnt[i]*sampleTime*(VELOCITY/100.0);
maxAccJntLocal[i] = maxAccJnt[i]*sampleTime*(ACCELARATION/100.0);
}
// calculate movement steps
for (int j=0; j<LBR_MNJ; j++)
{
dMaxSpeed[j] = maxVelJntLocal[j]/maxAccJntLocal[j];
lMaxSpeed[j] = dMaxSpeed[j]*dMaxSpeed[j]*maxAccJntLocal[j]*0.5;
if (lMaxSpeed[j] > deltaAbs[j]/(double)2.0)
{
dGesamt[j] = sqrt(deltaAbs[j]/maxAccJntLocal[j])*2.0;
} else
{
dGesamt[j] = dMaxSpeed[j]*2 + (deltaAbs[j]-lMaxSpeed[j]*2.0)/maxVelJntLocal[j];
}
}
int maxSteps =ceil(*max_element(dGesamt,(dGesamt+LBR_MNJ-1)));
if (maxSteps == 0 || maxSteps == 1)
goto end;
for (int j=0; j<LBR_MNJ; j++)
{
dMaxSpeed[j] = ceil(maxSteps/(double)2 - sqrt((maxSteps/(double)2)*(maxSteps/(double)2) - deltaAbs[j]/maxAccJntLocal[j]));
double temp = maxSteps/(double)2 - sqrt((maxSteps/(double)2)*(maxSteps/(double)2) - deltaAbs[j]/maxAccJntLocal[j]);
if (dMaxSpeed[j]==0)
{
maxAccJntLocal[j] = 0;
} else
{
maxAccJntLocal[j] = -deltaAbs[j]/(dMaxSpeed[j]*dMaxSpeed[j]-maxSteps*dMaxSpeed[j]);
}
maxVelJntLocal[j] = dMaxSpeed[j]*maxAccJntLocal[j];
}
for (int i=0;i<maxSteps;i++)
{
for (int j=0;j<LBR_MNJ;j++)
{
if (i+1 <= maxSteps/(double)2)
{
currentInk[j] = min(currentInkLast[j]+maxAccJntLocal[j],maxVelJntLocal[j]);
}else if (i+1 > maxSteps-dMaxSpeed[j])
{
currentInk[j] = max(currentInkLast[j]-maxAccJntLocal[j],(double)0);
}else
{
currentInk[j] = currentInkLast[j];
}
currentDist[j] = currentDistLast[j] + sgn(delta[j])*currentInk[j];
currentInkLast[j] = currentInk[j];
currentDistLast[j] = currentDist[j];
MSRMSRJNTPOS[j]+= sgn(delta[j])*currentInk[j]*(1./180*M_PI);
}
friInst.doPositionControl(MSRMSRJNTPOS);
}
end:
__MSRCMDJNTPOS = false;
} else if (__CARTMOVE)
{
//##########################################################
//Cartesian Movement
const float Stiff[6] = {1000.0, 1000.0, 1000.0, 150.0, 150.0, 150.0};
const float Damp[6] = {0.7, 0.7, 0.7, 0.7, 0.7, 0.7};
float th = 0.000;
float deltaCart[12];
float Pos[12];
float dist;
float rot;
float sum;
float b;
float c;
float t_fact = 1;
float t_outerT = 0.007;
float t_innerT = 0.000;
float t_outerR = 0.007;
float t_innerR = 0.000;
while (true)
{
for ( int i =0; i < FRI_CART_FRM_DIM; i++)
{
Pos[i] = friInst.getMsrCartPosition()[i];
MSRMSRCARTPOS[i] = friInst.getMsrCartPosition()[i];
deltaCart[i] = MSRCMDCARTPOS[i]-Pos[i];
}
sum = 0;
for (int i=0; i<3 ; i++)
{
sum = sum + (deltaCart[(i*4)+3]*deltaCart[(i*4)+3]);
}
dist = sqrt(sum);
sum = 0;
for (int i=0;i<12;i++)
{
if (i != 3 && i != 7 && i != 11)
{
sum = sum + deltaCart[i]*deltaCart[i];
}
}
rot = sqrt(sum);
b = t_fact*max((float)0.0,(float)min((float)1.0,(t_outerT-dist+(t_innerT-t_outerT))*(1/(t_innerT-t_outerT))));
c = t_fact*max((float)0.0,(float)min((float)1.0,(float)(t_outerR-rot+(t_innerR-t_outerR))*(float)(1/(t_innerR-t_outerR))));
if (dist < th)
{
for (int i=0; i<3; i++)
{
deltaCart[(i*4)+3] = 0;
}
}else
{
for (int i=0; i<3; i++)
{
deltaCart[(i*4)+3] = (deltaCart[(i*4)+3] / dist)*b;
}
}
// check offset to point
if (rot < th)
{
for (int i=0;i<12;i++)
{
if (i != 3 && i != 7 && i != 11)
{
deltaCart[i] = 0;
}
}
}else
{
for (int i=0;i<12;i++)
{
if (i != 3 && i != 7 && i != 11)
{
deltaCart[i] = (deltaCart[i] / rot)*c;
}
}
}
for (int i=0;i<12;i++)
{
Pos[i]+= deltaCart[i]*0.02;
}
friInst.doCartesianImpedanceControl(Pos, Stiff,Damp,NULL,NULL, true);
}
}
//float Stiff[6] = {1000.0, 1000.0, 1000.0, 10.0, 10.0, 10.0};
//float Damp[6] = {0.7, 0.7, 0.7, 0.7, 0.7, 0.7};
//float Test[12] = {MSRMSRCARTPOS[0],MSRMSRCARTPOS[1],MSRMSRCARTPOS[2],MSRMSRCARTPOS[3],MSRMSRCARTPOS[4],MSRMSRCARTPOS[5],MSRMSRCARTPOS[6],MSRMSRCARTPOS[7],MSRMSRCARTPOS[8],MSRMSRCARTPOS[9],MSRMSRCARTPOS[10],MSRMSRCARTPOS[11]};
//friInst.doCartesianImpedanceControl(Test,Stiff, Damp, NULL, NULL, true);
// innerhlab 5ms
friInst.doPositionControl(MSRMSRJNTPOS);
//i = 0;
//}
//friInst.doCartesianImpedanceControl(Test,Stiff, Damp, NULL, NULL, true);
}
return NULL;
}
int main()
{
int err = 0;
//Setting pthread for FRI interface
pthread_t fri_t;
//Start fri_thread
err = pthread_create(&fri_t,NULL,&threadFriDataExchange,NULL);
if (err > 0 )
{
cerr << "Failed: could not create thread\n" << endl;
return err;
}
//Start client handling
SvrHandling *svr = new SvrHandling();
if (svr == NULL)
{
cerr << "Failed: could not create server \n" << endl;
return -ENOMEM;
}
svr->run();
return 0;
}