![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
with class
Dependencies: ISR_Mini-explorer mbed
Fork of VirtualForces by
Sonar.cpp@37:b4c45e43ad29, 2017-06-11 (annotated)
- Committer:
- Ludwigfr
- Date:
- Sun Jun 11 23:22:28 2017 +0000
- Revision:
- 37:b4c45e43ad29
- Parent:
- 35:68f9edbb3cff
- Child:
- 38:5ed7c79fb724
changed a few thing, notably tried to simplify the mapping funciton
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ludwigfr | 33:814bcd7d3cfe | 1 | #include "Sonar.hpp" |
Ludwigfr | 33:814bcd7d3cfe | 2 | |
Ludwigfr | 34:c208497dd079 | 3 | #define PI 3.14159 |
Ludwigfr | 34:c208497dd079 | 4 | |
Ludwigfr | 37:b4c45e43ad29 | 5 | Sonar::Sonar(float angleFromOrigin, float distanceXFromRobotCenter, float distanceYFromRobotCenter ){ |
Ludwigfr | 37:b4c45e43ad29 | 6 | this->angleFromOrigin=angleFromOrigin; |
Ludwigfr | 35:68f9edbb3cff | 7 | this->distanceX=distanceXFromRobotCenter; |
Ludwigfr | 35:68f9edbb3cff | 8 | this->distanceY=distanceYFromRobotCenter; |
Ludwigfr | 33:814bcd7d3cfe | 9 | this->maxRange=50;//cm |
Ludwigfr | 33:814bcd7d3cfe | 10 | this->minRange=10;//Rmin cm |
Ludwigfr | 33:814bcd7d3cfe | 11 | this->incertitudeRange=10;//cm |
Ludwigfr | 33:814bcd7d3cfe | 12 | this->angleRange=3.14159/3;//Omega rad |
Ludwigfr | 33:814bcd7d3cfe | 13 | } |
Ludwigfr | 33:814bcd7d3cfe | 14 | |
Ludwigfr | 34:c208497dd079 | 15 | //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] |
Ludwigfr | 37:b4c45e43ad29 | 16 | float Sonar::compute_probability_t(float distanceObstacleDetected, float xCell, float yCell, float xRobotWorld, float yRobotWorld, float thetaWorld){ |
Ludwigfr | 34:c208497dd079 | 17 | float xSonar=xRobotWorld+this->distanceX; |
Ludwigfr | 34:c208497dd079 | 18 | float ySonar=yRobotWorld+this->distanceY; |
Ludwigfr | 34:c208497dd079 | 19 | float distancePointToSonar=sqrt(pow(xCell-xSonar,2)+pow(yCell-ySonar,2)); |
Ludwigfr | 37:b4c45e43ad29 | 20 | //check if the distance between the cell and the robot is within the circle of range RADIUS_WHEELS |
Ludwigfr | 33:814bcd7d3cfe | 21 | if( distancePointToSonar < this->maxRange){ |
Ludwigfr | 37:b4c45e43ad29 | 22 | //float anglePointToSonar=this->compute_angle_between_vectors(xCell,yCell,xSonar,ySonar);//angle beetween the point and the sonar beam |
Ludwigfr | 37:b4c45e43ad29 | 23 | float anglePointToSonar=atan2(yCell-yRobotWorld,xCell-xRobotWorld);//like world system |
Ludwigfr | 33:814bcd7d3cfe | 24 | |
Ludwigfr | 37:b4c45e43ad29 | 25 | float angleOriginToMidleOfBeam=thetaWorld+this->angleFromOrigin;// |
Ludwigfr | 33:814bcd7d3cfe | 26 | |
Ludwigfr | 37:b4c45e43ad29 | 27 | float angleDifference=anglePointToSonar-angleOriginToMidleOfBeam; |
Ludwigfr | 33:814bcd7d3cfe | 28 | //check if absolute difference between the angles is no more than Omega/2 |
Ludwigfr | 37:b4c45e43ad29 | 29 | if(abs(angleDifference) <= this->angleRange/2){ |
Ludwigfr | 33:814bcd7d3cfe | 30 | if( distancePointToSonar < (distanceObstacleDetected - this->incertitudeRange)){ |
Ludwigfr | 33:814bcd7d3cfe | 31 | //point before obstacle, probably empty |
Ludwigfr | 33:814bcd7d3cfe | 32 | /*****************************************************************************/ |
Ludwigfr | 37:b4c45e43ad29 | 33 | float Ea=1.f-pow((2*angleDifference)/this->angleRange,2); |
Ludwigfr | 33:814bcd7d3cfe | 34 | float Er; |
Ludwigfr | 33:814bcd7d3cfe | 35 | if(distancePointToSonar < this->minRange){ |
Ludwigfr | 33:814bcd7d3cfe | 36 | //point before minimum sonar range |
Ludwigfr | 33:814bcd7d3cfe | 37 | Er=0.f; |
Ludwigfr | 33:814bcd7d3cfe | 38 | }else{ |
Ludwigfr | 33:814bcd7d3cfe | 39 | //point after minimum sonar range |
Ludwigfr | 33:814bcd7d3cfe | 40 | Er=1.f-pow((distancePointToSonar-this->minRange)/(distanceObstacleDetected-this->incertitudeRange-this->minRange),2); |
Ludwigfr | 33:814bcd7d3cfe | 41 | } |
Ludwigfr | 33:814bcd7d3cfe | 42 | /*****************************************************************************/ |
Ludwigfr | 33:814bcd7d3cfe | 43 | //if((1.f-Er*Ea)/2.f >1 || (1.f-Er*Ea)/2.f < 0) |
Ludwigfr | 37:b4c45e43ad29 | 44 | // pc.printf("\n\r return value=%f,Er=%f,Ea=%f,angleDifference=%f",(1.f-Er*Ea)/2.f,Er,Ea,angleDifference); |
Ludwigfr | 33:814bcd7d3cfe | 45 | return (1.f-Er*Ea)/2.f; |
Ludwigfr | 33:814bcd7d3cfe | 46 | }else{ |
Ludwigfr | 34:c208497dd079 | 47 | //probably occuPIed |
Ludwigfr | 33:814bcd7d3cfe | 48 | /*****************************************************************************/ |
Ludwigfr | 37:b4c45e43ad29 | 49 | float Oa=1.f-pow((2*angleDifference)/this->angleRange,2); |
Ludwigfr | 33:814bcd7d3cfe | 50 | float Or; |
Ludwigfr | 33:814bcd7d3cfe | 51 | if( distancePointToSonar <= (distanceObstacleDetected + this->incertitudeRange)){ |
Ludwigfr | 33:814bcd7d3cfe | 52 | //point between distanceObstacleDetected +- INCERTITUDE_SONAR |
Ludwigfr | 33:814bcd7d3cfe | 53 | Or=1-pow((distancePointToSonar-distanceObstacleDetected)/(this->incertitudeRange),2); |
Ludwigfr | 33:814bcd7d3cfe | 54 | }else{ |
Ludwigfr | 33:814bcd7d3cfe | 55 | //point after in range of the sonar but after the zone detected |
Ludwigfr | 33:814bcd7d3cfe | 56 | Or=0; |
Ludwigfr | 33:814bcd7d3cfe | 57 | } |
Ludwigfr | 33:814bcd7d3cfe | 58 | /*****************************************************************************/ |
Ludwigfr | 33:814bcd7d3cfe | 59 | //if((1+Or*Oa)/2 >1 || (1+Or*Oa)/2 < 0) |
Ludwigfr | 37:b4c45e43ad29 | 60 | // pc.printf("\n\r return value=%f,Er=%f,Ea=%f,angleDifference=%f",(1+Or*Oa)/2,Or,Oa,angleDifference); |
Ludwigfr | 33:814bcd7d3cfe | 61 | return (1+Or*Oa)/2; |
Ludwigfr | 33:814bcd7d3cfe | 62 | } |
Ludwigfr | 33:814bcd7d3cfe | 63 | } |
Ludwigfr | 33:814bcd7d3cfe | 64 | } |
Ludwigfr | 33:814bcd7d3cfe | 65 | //not checked by the sonar |
Ludwigfr | 33:814bcd7d3cfe | 66 | return 0.5; |
Ludwigfr | 33:814bcd7d3cfe | 67 | } |
Ludwigfr | 33:814bcd7d3cfe | 68 | |
Ludwigfr | 33:814bcd7d3cfe | 69 | //returns the angle between the vectors (x,y) and (xs,ys) |
Ludwigfr | 33:814bcd7d3cfe | 70 | float Sonar::compute_angle_between_vectors(float x, float y,float xs,float ys){ |
Ludwigfr | 33:814bcd7d3cfe | 71 | //alpha angle between ->x and ->SA |
Ludwigfr | 33:814bcd7d3cfe | 72 | //vector S to A ->SA |
Ludwigfr | 33:814bcd7d3cfe | 73 | float vSAx=x-xs; |
Ludwigfr | 33:814bcd7d3cfe | 74 | float vSAy=y-ys; |
Ludwigfr | 33:814bcd7d3cfe | 75 | //norme SA |
Ludwigfr | 33:814bcd7d3cfe | 76 | float normeSA=sqrt(pow(vSAx,2)+pow(vSAy,2)); |
Ludwigfr | 33:814bcd7d3cfe | 77 | //vector ->x (1,0) |
Ludwigfr | 33:814bcd7d3cfe | 78 | float cosAlpha=1*vSAy/*+0*vSAx*//normeSA;; |
Ludwigfr | 33:814bcd7d3cfe | 79 | //vector ->y (0,1) |
Ludwigfr | 33:814bcd7d3cfe | 80 | float sinAlpha=/*0*vSAy+*/1*vSAx/normeSA;//+0*vSAx; |
Ludwigfr | 33:814bcd7d3cfe | 81 | if (sinAlpha < 0) |
Ludwigfr | 33:814bcd7d3cfe | 82 | return -acos(cosAlpha); |
Ludwigfr | 33:814bcd7d3cfe | 83 | else |
Ludwigfr | 33:814bcd7d3cfe | 84 | return acos(cosAlpha); |
Ludwigfr | 33:814bcd7d3cfe | 85 | } |
Ludwigfr | 33:814bcd7d3cfe | 86 | |
Ludwigfr | 34:c208497dd079 | 87 | //makes the angle inAngle between 0 and 2PI |
Ludwigfr | 33:814bcd7d3cfe | 88 | float Sonar::rad_angle_check(float inAngle){ |
Ludwigfr | 33:814bcd7d3cfe | 89 | //cout<<"before :"<<inAngle; |
Ludwigfr | 33:814bcd7d3cfe | 90 | if(inAngle > 0){ |
Ludwigfr | 34:c208497dd079 | 91 | while(inAngle > (2*PI)) |
Ludwigfr | 34:c208497dd079 | 92 | inAngle-=2*PI; |
Ludwigfr | 33:814bcd7d3cfe | 93 | }else{ |
Ludwigfr | 33:814bcd7d3cfe | 94 | while(inAngle < 0) |
Ludwigfr | 34:c208497dd079 | 95 | inAngle+=2*PI; |
Ludwigfr | 33:814bcd7d3cfe | 96 | } |
Ludwigfr | 33:814bcd7d3cfe | 97 | //cout<<" after :"<<inAngle<<endl; |
Ludwigfr | 33:814bcd7d3cfe | 98 | return inAngle; |
Ludwigfr | 33:814bcd7d3cfe | 99 | } |
Ludwigfr | 33:814bcd7d3cfe | 100 |