#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "Timer.h" #define pi 3.1415926535897932384626433 using namespace std; using namespace PlayerCc; using namespace Eigen; typedef Matrix MatrixXld; long double randn(long double mu=0.0, long double sigma=1.0) { // source: http://www.dreamincode.net/code/snippet1446.htm static bool deviateAvailable=false; // flag static float storedDeviate; // deviate from previous calculation long double polar, rsquared, var1, var2; // If no deviate has been stored, the polar Box-Muller transformation is // performed, producing two independent normally-distributed random // deviates. One is stored for the next round, and one is returned. if (!deviateAvailable) { // choose pairs of uniformly distributed deviates, discarding those // that don't fall within the unit circle do { var1=2.0*( (long double)(rand())/(long double)(RAND_MAX) ) - 1.0; var2=2.0*( (long double)(rand())/(long double)(RAND_MAX) ) - 1.0; rsquared=var1*var1+var2*var2; } while ( rsquared>=1.0 || rsquared == 0.0); // calculate polar tranformation for each deviate polar=sqrt(-2.0*log(rsquared)/rsquared); // store first deviate and set flag storedDeviate=var1*polar; deviateAvailable=true; // return second deviate return var2*polar*sigma + mu; } // If a deviate is available from a previous call to this function, it is // returned, and the flag is set to false. else { deviateAvailable=false; return storedDeviate*sigma + mu; } } double radiansToDegrees(double rad) { return rad*(180/pi); } double degreesToRadians(double deg) { return deg*pi/180.0; } long double pi_to_pi(long double angle) { long double tmp = 2.0*pi; angle = fmod(angle,tmp); if(angle>pi) { angle = angle - 2.0*pi; } if(angle<-pi) { angle = angle + 2.0*pi; } return angle; } MatrixXld calculateRealPos(long double relX, long double relY, long double x, long double y, long double angle) { MatrixXld pos(1,2); pos(0,0) = x + relX*cosl(angle) - relY*sinl(angle); pos(0,1) = y + relX*sinl(angle) - relY*cosl(angle); return pos; } void drive(double *forwardSpeed, double *turnSpeed, RangerProxy &rp) { double avoidDistance = 0.5; double corridorWidth = 1; int avoidTurnSpeed = 45; double minSpeed = 0.1; static bool tracking = false; if(!tracking && (rp.GetRange(2) < avoidDistance + corridorWidth)) { tracking = true; cout << "tracking on" << endl; } cout << "bb" << endl; if(rp.GetRangeCount()==0) { cout << "fuck" << endl; return; } cout << "cc" << endl; if(rp.GetRange(1) < avoidDistance) //left { *forwardSpeed = std::max(minSpeed,*forwardSpeed * 0.9); *turnSpeed = (-1)*avoidTurnSpeed; // draai naar rechts return; } else if(rp.GetRange(2) < avoidDistance) //right { *forwardSpeed = std::max(minSpeed,*forwardSpeed * 0.9); *turnSpeed = avoidTurnSpeed; // draai naar links return; } else if(rp.GetRange(0) < avoidDistance * 1.5) //center { *forwardSpeed = std::max(minSpeed,*forwardSpeed * 0.7); *turnSpeed = avoidTurnSpeed * 1.5; // draai naar links return; } else // geen enkele sensor meet iets binnen de avoidDistance // er is ruimte. if(rp.GetRange(1) < *forwardSpeed) // linkerafstand < snelheid vooruit { // cout << "obstacle @ left, reduce speed from " << *forwardSpeed << " to "; *forwardSpeed = std::max(minSpeed,rp.GetRange(1) * 0.5); //halve afstand links // cout << *forwardSpeed << "m/s"<< endl; return; } else if(rp.GetRange(2) < *forwardSpeed) { // cout << "obstacle @ right, reduce speed from " << *forwardSpeed << " to "; *forwardSpeed = std::max(minSpeed,rp.GetRange(2) * 0.5); //halve afstand rechts // cout << *forwardSpeed << "m/s"<< endl; return; } else if(rp.GetRange(0) < *forwardSpeed) //halve afstand rechtdoor { // cout << "obstacle @ mid, reduce speed from " << *forwardSpeed << " to "; *forwardSpeed = std::max(minSpeed,rp.GetRange(0) * 0.5); // cout << *forwardSpeed << "m/s"<< endl; return; } *turnSpeed = 0; if(tracking) { if(rp.GetRange(2) > corridorWidth) { *turnSpeed = (-1)*avoidTurnSpeed; // draai naar rechts // cout << "Stay in corridor!" < 3) { P.block(0,3,3,P.rows()-3) = Gv*P.block(0,3,3,P.rows()-3); P.block(3,0,P.cols()-3,3) = P.block(0,3,3,P.rows()-3).transpose(); } x(0,0) += vtc; x(1,0) += vts; x(2,0) = x(2,0) + Vn*dt*sinl(Gn)/4.0; dtsum += dt; if(dtsum >= 0.5) { cout << "observe my dear" << endl; dtsum = 0; fidsFound = fidProxy.GetCount(); for(int i=0;i::max(); outer=std::numeric_limits::max(); for(int j=1;j<=Nf;j++) { caz(0,0) = z(0,i); caz(1,0) = z(1,i); fpos = Nxv + j*2 -1; HHH = MatrixXld::Zero(2,x.rows()); dX = x(fpos-1) -x(0,0); dY = x(fpos) -x(1,0); d2 = pow(dX,2) + pow(dY,2); d=sqrt(d2); xd = dX/d; yd = dY/d; xd2 = dX/d2; yd2 = dY/d2; zp(0,0) = d; zp(1,0) = atan2(dY,dX) - x(2,0); HHH(0,0) = -xd; HHH(0,1) = -yd; HHH(0,2) = 0; HHH(1,0) = yd2; HHH(1,1) = -xd2; HHH(1,2) = -1; HHH(0,fpos-1) = xd; HHH(0,fpos) = yd; HHH(1,fpos-1) = -yd2; HHH(1,fpos) = xd2; v=caz-zp; v(1,0)=pi_to_pi(v(1,0)); S=HHH*P*HHH.transpose() + RE; Eigennis = v.transpose()*S.inverse()*v; nis = Eigennis(0,0); nd = nis + log(S.determinant()); if (nis < 4 && nd < nbest) { nbest = nd; jbest = j; } else if(nis < outer) { outer = nis; } } if(jbest!=0) { zf.conservativeResize(2,zf.cols()+1); zf(0,zf.cols()-1) = z(0,i); zf(1,zf.cols()-1) = z(1,i); daidf.conservativeResize(1,daidf.cols()+1); daidf(0,daidf.cols()-1) = jbest; } else if(outer > 25) { zn.conservativeResize(2,zn.cols()+1); zn(0,zn.cols()-1) = z(0,i); zn(1,zn.cols()-1) = z(1,i); } } if((bool)z.rows()) idf = daidf; lenz = zf.cols(); lenx = x.rows(); HA = MatrixXld::Zero(2*lenz,lenx); v2 = MatrixXld::Zero(2*lenz,1); RR = MatrixXld::Zero(2*lenz,2*lenz); for(int i=0;i 0) { PHt = P*HA.transpose(); S2 = HA*PHt + RR; S2 = (S2+S2.transpose())*0.5; LLT llt; llt.compute(S2); SChol = llt.matrixL().transpose(); SCholInv = SChol.inverse(); W1 = PHt * SCholInv; W = W1 * SCholInv.transpose(); x = x + W*v2; P = P - W1*W1.transpose(); } for(int i=0;i3) { rnm(0,0) = 4; rnm(0,1) = len; P.block(rng(0,0)-1,3,2,rnm(0,1)-rnm(0,0)+1) = Gvv*P.block(0,3,3,rnm(0,1)-rnm(0,0)+1); P.block(3,rng(0,0)-1,rnm(0,1)-rnm(0,0)+1,2) = P.block(rng(0,0)-1,3,2,rnm(0,1)-rnm(0,0)+1).transpose(); } } } simProxy.SetPose2d("rg",(double)x(0,0)+startX,(double)x(1,0)+startY,(double)x(2,0)); sleep(0.05); } } catch(PlayerError error) { cout << "Error: " << error.GetErrorStr() << endl; } }