with class

Dependencies:   ISR_Mini-explorer mbed

Fork of VirtualForces by Georgios Tsamis

Committer:
Ludwigfr
Date:
Fri Jun 09 14:30:21 2017 +0000
Revision:
34:c208497dd079
Parent:
33:814bcd7d3cfe
Child:
35:68f9edbb3cff
okay it compiles

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ludwigfr33:814bcd7d3cfe 1#include "Sonar.hpp"
Ludwigfr33:814bcd7d3cfe 2
Ludwigfr34:c208497dd079 3#define PI 3.14159
Ludwigfr34:c208497dd079 4
Ludwigfr34:c208497dd079 5Sonar::Sonar(float angleFromCenter, float distanceXFromRobotCenter, float distanceYFromRobotCenter ){
Ludwigfr34:c208497dd079 6 this->angleFromCenter=angleFromCenter;
Ludwigfr34:c208497dd079 7 this->distanceX=-distanceYFromRobotCenter;
Ludwigfr34:c208497dd079 8 this->distanceY=distanceXFromRobotCenter;
Ludwigfr33:814bcd7d3cfe 9 this->maxRange=50;//cm
Ludwigfr33:814bcd7d3cfe 10 this->minRange=10;//Rmin cm
Ludwigfr33:814bcd7d3cfe 11 this->incertitudeRange=10;//cm
Ludwigfr33:814bcd7d3cfe 12 this->angleRange=3.14159/3;//Omega rad
Ludwigfr33:814bcd7d3cfe 13}
Ludwigfr33:814bcd7d3cfe 14
Ludwigfr33:814bcd7d3cfe 15//ODOMETRIA MUST HAVE BEEN CALLED
Ludwigfr34:c208497dd079 16//function that check if a cell A(x,y) is in the range of the front sonar S(xs,ys) (with an angle depending on the sonar used, front 0, left PI/3, right -PI/3) returns the probability it's occuPIed/empty [0;1]
Ludwigfr34:c208497dd079 17float Sonar::compute_probability_t(float distanceObstacleDetected, float xCell, float yCell, float xRobotWorld, float yRobotWorld, float theta){
Ludwigfr34:c208497dd079 18 float xSonar=xRobotWorld+this->distanceX;
Ludwigfr34:c208497dd079 19 float ySonar=yRobotWorld+this->distanceY;
Ludwigfr34:c208497dd079 20 float distancePointToSonar=sqrt(pow(xCell-xSonar,2)+pow(yCell-ySonar,2));
Ludwigfr33:814bcd7d3cfe 21 if( distancePointToSonar < this->maxRange){
Ludwigfr34:c208497dd079 22 float anglePointToSonar=this->compute_angle_between_vectors(xCell,yCell,xSonar,ySonar);//angle beetween the point and the sonar beam
Ludwigfr34:c208497dd079 23 float alphaBeforeAdjustment=anglePointToSonar-theta-this->angleFromCenter;
Ludwigfr33:814bcd7d3cfe 24 anglePointToSonar=rad_angle_check(alphaBeforeAdjustment);//TODO I feel you don't need to do that but I m not sure
Ludwigfr33:814bcd7d3cfe 25
Ludwigfr34:c208497dd079 26 if(alphaBeforeAdjustment>PI)
Ludwigfr34:c208497dd079 27 alphaBeforeAdjustment=alphaBeforeAdjustment-2*PI;
Ludwigfr34:c208497dd079 28 if(alphaBeforeAdjustment<-PI)
Ludwigfr34:c208497dd079 29 alphaBeforeAdjustment=alphaBeforeAdjustment+2*PI;
Ludwigfr33:814bcd7d3cfe 30
Ludwigfr33:814bcd7d3cfe 31 //float anglePointToSonar2=atan2(y-ys,x-xs)-theta;
Ludwigfr33:814bcd7d3cfe 32
Ludwigfr33:814bcd7d3cfe 33 //check if the distance between the cell and the robot is within the circle of range RADIUS_WHEELS
Ludwigfr33:814bcd7d3cfe 34 //check if absolute difference between the angles is no more than Omega/2
Ludwigfr33:814bcd7d3cfe 35 if(anglePointToSonar <= this->angleRange/2 || anglePointToSonar >= this->rad_angle_check(-this->angleRange/2)){
Ludwigfr33:814bcd7d3cfe 36 if( distancePointToSonar < (distanceObstacleDetected - this->incertitudeRange)){
Ludwigfr33:814bcd7d3cfe 37 //point before obstacle, probably empty
Ludwigfr33:814bcd7d3cfe 38 /*****************************************************************************/
Ludwigfr33:814bcd7d3cfe 39 float Ea=1.f-pow((2*alphaBeforeAdjustment)/this->angleRange,2);
Ludwigfr33:814bcd7d3cfe 40 float Er;
Ludwigfr33:814bcd7d3cfe 41 if(distancePointToSonar < this->minRange){
Ludwigfr33:814bcd7d3cfe 42 //point before minimum sonar range
Ludwigfr33:814bcd7d3cfe 43 Er=0.f;
Ludwigfr33:814bcd7d3cfe 44 }else{
Ludwigfr33:814bcd7d3cfe 45 //point after minimum sonar range
Ludwigfr33:814bcd7d3cfe 46 Er=1.f-pow((distancePointToSonar-this->minRange)/(distanceObstacleDetected-this->incertitudeRange-this->minRange),2);
Ludwigfr33:814bcd7d3cfe 47 }
Ludwigfr33:814bcd7d3cfe 48 /*****************************************************************************/
Ludwigfr33:814bcd7d3cfe 49 //if((1.f-Er*Ea)/2.f >1 || (1.f-Er*Ea)/2.f < 0)
Ludwigfr33:814bcd7d3cfe 50 // pc.printf("\n\r return value=%f,Er=%f,Ea=%f,alphaBeforeAdjustment=%f",(1.f-Er*Ea)/2.f,Er,Ea,alphaBeforeAdjustment);
Ludwigfr33:814bcd7d3cfe 51 return (1.f-Er*Ea)/2.f;
Ludwigfr33:814bcd7d3cfe 52 }else{
Ludwigfr34:c208497dd079 53 //probably occuPIed
Ludwigfr33:814bcd7d3cfe 54 /*****************************************************************************/
Ludwigfr34:c208497dd079 55 float Oa=1.f-pow((2*alphaBeforeAdjustment)/this->angleRange,2);
Ludwigfr33:814bcd7d3cfe 56 float Or;
Ludwigfr33:814bcd7d3cfe 57 if( distancePointToSonar <= (distanceObstacleDetected + this->incertitudeRange)){
Ludwigfr33:814bcd7d3cfe 58 //point between distanceObstacleDetected +- INCERTITUDE_SONAR
Ludwigfr33:814bcd7d3cfe 59 Or=1-pow((distancePointToSonar-distanceObstacleDetected)/(this->incertitudeRange),2);
Ludwigfr33:814bcd7d3cfe 60 }else{
Ludwigfr33:814bcd7d3cfe 61 //point after in range of the sonar but after the zone detected
Ludwigfr33:814bcd7d3cfe 62 Or=0;
Ludwigfr33:814bcd7d3cfe 63 }
Ludwigfr33:814bcd7d3cfe 64 /*****************************************************************************/
Ludwigfr33:814bcd7d3cfe 65 //if((1+Or*Oa)/2 >1 || (1+Or*Oa)/2 < 0)
Ludwigfr33:814bcd7d3cfe 66 // pc.printf("\n\r return value=%f,Er=%f,Ea=%f,alphaBeforeAdjustment=%f",(1+Or*Oa)/2,Or,Oa,alphaBeforeAdjustment);
Ludwigfr33:814bcd7d3cfe 67 return (1+Or*Oa)/2;
Ludwigfr33:814bcd7d3cfe 68 }
Ludwigfr33:814bcd7d3cfe 69 }
Ludwigfr33:814bcd7d3cfe 70 }
Ludwigfr33:814bcd7d3cfe 71 //not checked by the sonar
Ludwigfr33:814bcd7d3cfe 72 return 0.5;
Ludwigfr33:814bcd7d3cfe 73}
Ludwigfr33:814bcd7d3cfe 74
Ludwigfr33:814bcd7d3cfe 75//returns the angle between the vectors (x,y) and (xs,ys)
Ludwigfr33:814bcd7d3cfe 76float Sonar::compute_angle_between_vectors(float x, float y,float xs,float ys){
Ludwigfr33:814bcd7d3cfe 77 //alpha angle between ->x and ->SA
Ludwigfr33:814bcd7d3cfe 78 //vector S to A ->SA
Ludwigfr33:814bcd7d3cfe 79 float vSAx=x-xs;
Ludwigfr33:814bcd7d3cfe 80 float vSAy=y-ys;
Ludwigfr33:814bcd7d3cfe 81 //norme SA
Ludwigfr33:814bcd7d3cfe 82 float normeSA=sqrt(pow(vSAx,2)+pow(vSAy,2));
Ludwigfr33:814bcd7d3cfe 83 //vector ->x (1,0)
Ludwigfr33:814bcd7d3cfe 84 float cosAlpha=1*vSAy/*+0*vSAx*//normeSA;;
Ludwigfr33:814bcd7d3cfe 85 //vector ->y (0,1)
Ludwigfr33:814bcd7d3cfe 86 float sinAlpha=/*0*vSAy+*/1*vSAx/normeSA;//+0*vSAx;
Ludwigfr33:814bcd7d3cfe 87 if (sinAlpha < 0)
Ludwigfr33:814bcd7d3cfe 88 return -acos(cosAlpha);
Ludwigfr33:814bcd7d3cfe 89 else
Ludwigfr33:814bcd7d3cfe 90 return acos(cosAlpha);
Ludwigfr33:814bcd7d3cfe 91}
Ludwigfr33:814bcd7d3cfe 92
Ludwigfr34:c208497dd079 93//makes the angle inAngle between 0 and 2PI
Ludwigfr33:814bcd7d3cfe 94float Sonar::rad_angle_check(float inAngle){
Ludwigfr33:814bcd7d3cfe 95 //cout<<"before :"<<inAngle;
Ludwigfr33:814bcd7d3cfe 96 if(inAngle > 0){
Ludwigfr34:c208497dd079 97 while(inAngle > (2*PI))
Ludwigfr34:c208497dd079 98 inAngle-=2*PI;
Ludwigfr33:814bcd7d3cfe 99 }else{
Ludwigfr33:814bcd7d3cfe 100 while(inAngle < 0)
Ludwigfr34:c208497dd079 101 inAngle+=2*PI;
Ludwigfr33:814bcd7d3cfe 102 }
Ludwigfr33:814bcd7d3cfe 103 //cout<<" after :"<<inAngle<<endl;
Ludwigfr33:814bcd7d3cfe 104 return inAngle;
Ludwigfr33:814bcd7d3cfe 105}
Ludwigfr33:814bcd7d3cfe 106