Mapping for TP2

Dependencies:   ISR_Mini-explorer mbed

Fork of GoToPointWithAngle by Georgios Tsamis

Committer:
geotsam
Date:
Wed Apr 19 22:20:56 2017 +0000
Revision:
24:8f4b820d8de8
Parent:
23:901fc468b8a7
Child:
25:572c9e9a8809
the file from the afternoon

Who changed what in which revision?

UserRevisionLine numberNew contents of line
geotsam 0:8bffb51cc345 1 #include "mbed.h"
geotsam 0:8bffb51cc345 2 #include "robot.h" // Initializes the robot. This include should be used in all main.cpp!
geotsam 0:8bffb51cc345 3 #include "math.h"
Ludwigfr 21:ebb37a249b5f 4
Ludwigfr 21:ebb37a249b5f 5 using namespace std;
AurelienBernier 4:8c56c3ba6e54 6
Ludwigfr 21:ebb37a249b5f 7 //fill initialLogValues with the values we already know (here the bordurs)
Ludwigfr 21:ebb37a249b5f 8 void fill_initial_log_values();
Ludwigfr 21:ebb37a249b5f 9 //generate a position randomly and makes the robot go there while updating the map
Ludwigfr 21:ebb37a249b5f 10 void randomize_and_map();
Ludwigfr 21:ebb37a249b5f 11 //go the the given position while updating the map
Ludwigfr 21:ebb37a249b5f 12 void go_to_point_with_angle(float target_x, float target_y, float target_angle);
Ludwigfr 21:ebb37a249b5f 13 //Updates sonar values
geotsam 24:8f4b820d8de8 14 void update_sonar_values(float leftMm, float frontMm, float rightMm);
Ludwigfr 21:ebb37a249b5f 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 21:ebb37a249b5f 16 float compute_probability_t(float x, float y,float xs,float ys, float angleFromSonarPosition, float distanceObstacleDetected);
Ludwigfr 21:ebb37a249b5f 17 //print the map
Ludwigfr 21:ebb37a249b5f 18 void print_final_map();
geotsam 0:8bffb51cc345 19
Ludwigfr 21:ebb37a249b5f 20 //MATHS heavy functions
Ludwigfr 21:ebb37a249b5f 21 float dist(float robot_x, float robot_y, float target_x, float target_y);
Ludwigfr 21:ebb37a249b5f 22 //returns the probability [0,1] that the cell is occupied from the log value lt
Ludwigfr 21:ebb37a249b5f 23 float log_to_proba(float lt);
Ludwigfr 21:ebb37a249b5f 24 //returns the log value that the cell is occupied from the probability value [0,1]
Ludwigfr 21:ebb37a249b5f 25 float proba_to_log(float p);
Ludwigfr 21:ebb37a249b5f 26 //returns the new log value
Ludwigfr 21:ebb37a249b5f 27 float compute_log_estimation_lt(float previousLogValue,float currentProbability,float originalLogvalue );
Ludwigfr 21:ebb37a249b5f 28 //makes the angle inAngle between 0 and 2pi
Ludwigfr 21:ebb37a249b5f 29 float rad_angle_check(float inAngle);
Ludwigfr 21:ebb37a249b5f 30 //returns the angle between the vectors (x,y) and (xs,ys)
Ludwigfr 21:ebb37a249b5f 31 float compute_angle_between_vectors(float x, float y,float xs,float ys);
AurelienBernier 8:109314be5b68 32
Ludwigfr 21:ebb37a249b5f 33 const float pi=3.14159;
Ludwigfr 21:ebb37a249b5f 34 //spec of the sonar
Ludwigfr 21:ebb37a249b5f 35 //TODO MEASURE THE DISTANCE on X and Y of the robot frame, between each sonar and the center of the robot and add it to calculus in updateSonarValues
geotsam 24:8f4b820d8de8 36 const float RANGE_SONAR=50;//cm
geotsam 24:8f4b820d8de8 37 const float RANGE_SONAR_MIN=10;//Rmin cm
geotsam 24:8f4b820d8de8 38 const float INCERTITUDE_SONAR=10;//cm
geotsam 24:8f4b820d8de8 39 const float ANGLE_SONAR=pi/3;//Omega rad
geotsam 24:8f4b820d8de8 40 const float SECURITY_DISTANCE=150;//mm
Ludwigfr 21:ebb37a249b5f 41
Ludwigfr 21:ebb37a249b5f 42 //those distance and angle are approximation in need of measurements
geotsam 24:8f4b820d8de8 43 const float ANGLE_FRONT_TO_LEFT=10*pi/36;//50 degrees
geotsam 24:8f4b820d8de8 44 const float DISTANCE_SONAR_LEFT_X=4;
geotsam 24:8f4b820d8de8 45 const float DISTANCE_SONAR_LEFT_Y=4;
Ludwigfr 21:ebb37a249b5f 46
geotsam 24:8f4b820d8de8 47 const float ANGLE_FRONT_TO_RIGHT=-10*pi/36;//-50 degrees
geotsam 24:8f4b820d8de8 48 const float DISTANCE_SONAR_RIGHT_X=-4;
geotsam 24:8f4b820d8de8 49 const float DISTANCE_SONAR_RIGHT_Y=4;
AurelienBernier 11:e641aa08c92e 50
Ludwigfr 21:ebb37a249b5f 51 const float ANGLE_FRONT_TO_FRONT=0;
Ludwigfr 21:ebb37a249b5f 52 const float DISTANCE_SONAR_FRONT_X=0;
Ludwigfr 21:ebb37a249b5f 53 const float DISTANCE_SONAR_FRONT_Y=5;
Ludwigfr 21:ebb37a249b5f 54
Ludwigfr 21:ebb37a249b5f 55 //TODO adjust the size of the map for computation time (25*25?)
geotsam 24:8f4b820d8de8 56 const float WIDTH_ARENA=120;//cm
geotsam 24:8f4b820d8de8 57 const float HEIGHT_ARENA=90;//cm
geotsam 24:8f4b820d8de8 58
geotsam 24:8f4b820d8de8 59 //const int SIZE_MAP=25;
geotsam 24:8f4b820d8de8 60 const int NB_CELL_WIDTH=24;
geotsam 24:8f4b820d8de8 61 const int NB_CELL_HEIGHT=18;
Ludwigfr 21:ebb37a249b5f 62
Ludwigfr 21:ebb37a249b5f 63 //position and orientation of the robot when put on the map (ODOMETRY doesn't know those)
geotsam 24:8f4b820d8de8 64 const float DEFAULT_X=WIDTH_ARENA/2;
geotsam 24:8f4b820d8de8 65 const float DEFAULT_Y=HEIGHT_ARENA/2;
Ludwigfr 21:ebb37a249b5f 66 const float DEFAULT_THETA=pi/2;
Ludwigfr 21:ebb37a249b5f 67
geotsam 24:8f4b820d8de8 68 //used to create the map 250 represent the 250cm of the square where the robot is tested
geotsam 24:8f4b820d8de8 69 //float sizeCell=250/(float)SIZE_MAP;
geotsam 24:8f4b820d8de8 70 float sizeCellX=WIDTH_ARENA/(float)NB_CELL_WIDTH;
geotsam 24:8f4b820d8de8 71 float sizeCellY=HEIGHT_ARENA/(float)NB_CELL_HEIGHT;
geotsam 24:8f4b820d8de8 72
geotsam 24:8f4b820d8de8 73 float map[NB_CELL_WIDTH][NB_CELL_HEIGHT];//contains the log values for each cell
geotsam 24:8f4b820d8de8 74 float initialLogValues[NB_CELL_WIDTH][NB_CELL_HEIGHT];
Ludwigfr 21:ebb37a249b5f 75
Ludwigfr 21:ebb37a249b5f 76 //Diameter of a wheel and distance between the 2
Ludwigfr 21:ebb37a249b5f 77 const float RADIUS_WHEELS=3.25;
Ludwigfr 21:ebb37a249b5f 78 const float DISTANCE_WHEELS=7.2;
Ludwigfr 21:ebb37a249b5f 79
geotsam 24:8f4b820d8de8 80 const int MAX_SPEED=500;//TODO TWEAK THE SPEED SO IT DOES NOT FUCK UP
AurelienBernier 8:109314be5b68 81
geotsam 24:8f4b820d8de8 82 bool tooClose=false;
geotsam 24:8f4b820d8de8 83 bool turnFromObstacle=false;
AurelienBernier 8:109314be5b68 84
geotsam 0:8bffb51cc345 85 int main(){
geotsam 17:caf393b63e27 86
geotsam 13:41f75c132135 87 i2c1.frequency(100000);
AurelienBernier 2:ea61e801e81f 88 initRobot(); //Initializing the robot
geotsam 0:8bffb51cc345 89 pc.baud(9600); // baud for the pc communication
geotsam 0:8bffb51cc345 90
Ludwigfr 21:ebb37a249b5f 91 measure_always_on();//TODO check if needed
geotsam 24:8f4b820d8de8 92 wait(0.5);
AurelienBernier 18:dbc5fbad4975 93
Ludwigfr 21:ebb37a249b5f 94 fill_initial_log_values();
geotsam 13:41f75c132135 95
geotsam 24:8f4b820d8de8 96 theta=DEFAULT_THETA;
geotsam 24:8f4b820d8de8 97 X=DEFAULT_X;
geotsam 24:8f4b820d8de8 98 Y=DEFAULT_Y;
geotsam 24:8f4b820d8de8 99
geotsam 24:8f4b820d8de8 100 for (int i = 0; i<20; i++) {
Ludwigfr 21:ebb37a249b5f 101 randomize_and_map();
geotsam 24:8f4b820d8de8 102 wait(2);
geotsam 24:8f4b820d8de8 103 print_final_map();
geotsam 17:caf393b63e27 104 }
AurelienBernier 8:109314be5b68 105
AurelienBernier 8:109314be5b68 106 }
AurelienBernier 8:109314be5b68 107
Ludwigfr 21:ebb37a249b5f 108 //fill initialLogValues with the values we already know (here the bordurs)
Ludwigfr 21:ebb37a249b5f 109 void fill_initial_log_values(){
Ludwigfr 21:ebb37a249b5f 110 //Fill map, we know the border are occupied
geotsam 24:8f4b820d8de8 111 for (int i = 0; i<NB_CELL_WIDTH; i++) {
geotsam 24:8f4b820d8de8 112 for (int j = 0; j<NB_CELL_HEIGHT; j++) {
geotsam 24:8f4b820d8de8 113 if(j==0 || j==NB_CELL_HEIGHT-1 || i==0 || i==NB_CELL_WIDTH-1)
Ludwigfr 21:ebb37a249b5f 114 initialLogValues[i][j] = proba_to_log(1);
Ludwigfr 21:ebb37a249b5f 115 else
Ludwigfr 21:ebb37a249b5f 116 initialLogValues[i][j] = proba_to_log(0.5);
AurelienBernier 20:62154d644531 117 }
Ludwigfr 21:ebb37a249b5f 118 }
AurelienBernier 8:109314be5b68 119 }
AurelienBernier 8:109314be5b68 120
Ludwigfr 21:ebb37a249b5f 121 //generate a position randomly and makes the robot go there while updating the map
Ludwigfr 21:ebb37a249b5f 122 void randomize_and_map() {
geotsam 24:8f4b820d8de8 123 //TODO check that it's aurelien's work
geotsam 24:8f4b820d8de8 124 float target_x = (rand()%(int)(WIDTH_ARENA*10))/10;//for decimal precision
geotsam 24:8f4b820d8de8 125 float target_y = (rand()%(int)(HEIGHT_ARENA*10))/10;
Ludwigfr 21:ebb37a249b5f 126 float target_angle = ((float)(rand()%31416)-15708)/10000.0;
geotsam 24:8f4b820d8de8 127
geotsam 24:8f4b820d8de8 128 //TODO comment that
geotsam 24:8f4b820d8de8 129 //pc.printf("\n\r targ_X=%f", target_x);
geotsam 24:8f4b820d8de8 130 //pc.printf("\n\r targ_Y=%f", target_y);
geotsam 24:8f4b820d8de8 131 //pc.printf("\n\r targ_Angle=%f", target_angle);
geotsam 24:8f4b820d8de8 132
Ludwigfr 21:ebb37a249b5f 133 go_to_point_with_angle(target_x, target_y, target_angle);
AurelienBernier 18:dbc5fbad4975 134 }
AurelienBernier 18:dbc5fbad4975 135
Ludwigfr 21:ebb37a249b5f 136 //go the the given position while updating the map
Ludwigfr 21:ebb37a249b5f 137 //TODO clean this procedure it's ugly as hell and too long
Ludwigfr 21:ebb37a249b5f 138 void go_to_point_with_angle(float target_x, float target_y, float target_angle) {
geotsam 24:8f4b820d8de8 139 if(tooClose){
geotsam 24:8f4b820d8de8 140 target_x=X;
geotsam 24:8f4b820d8de8 141 target_y=Y;
geotsam 24:8f4b820d8de8 142 target_angle=theta+pi/2;
geotsam 24:8f4b820d8de8 143 target_angle = atan(sin(target_angle)/cos(target_angle));
geotsam 24:8f4b820d8de8 144 pc.printf("\n\rShould just turn now, new target_angle=%f\n\r", target_angle);
geotsam 24:8f4b820d8de8 145 }
geotsam 24:8f4b820d8de8 146
geotsam 24:8f4b820d8de8 147 //TODO ugly old stuff
geotsam 24:8f4b820d8de8 148 float alpha; //angle error
geotsam 24:8f4b820d8de8 149 float rho; //distance from target
geotsam 24:8f4b820d8de8 150 float beta;
geotsam 24:8f4b820d8de8 151 float kRho=12, ka=30, kb=-13; //Kappa values
geotsam 24:8f4b820d8de8 152 float linear, angular, angular_left, angular_right;
geotsam 24:8f4b820d8de8 153 float dt=0.5;
geotsam 24:8f4b820d8de8 154 float temp;
geotsam 24:8f4b820d8de8 155 float d2;
geotsam 24:8f4b820d8de8 156 Timer t;
geotsam 24:8f4b820d8de8 157
geotsam 24:8f4b820d8de8 158 int speed=MAX_SPEED; // Max speed at beggining of movement
geotsam 24:8f4b820d8de8 159
geotsam 24:8f4b820d8de8 160 float leftMm;
geotsam 24:8f4b820d8de8 161 float frontMm;
geotsam 24:8f4b820d8de8 162 float rightMm;
geotsam 24:8f4b820d8de8 163
geotsam 24:8f4b820d8de8 164 alpha = atan2((target_y-Y),(target_x-X))-theta;
geotsam 24:8f4b820d8de8 165 alpha = atan(sin(alpha)/cos(alpha));
geotsam 24:8f4b820d8de8 166 rho = dist(X, Y, target_x, target_y);
geotsam 24:8f4b820d8de8 167
geotsam 24:8f4b820d8de8 168 beta = -alpha-theta+target_angle;
geotsam 24:8f4b820d8de8 169 //beta = atan(sin(beta)/cos(beta));
geotsam 24:8f4b820d8de8 170
geotsam 24:8f4b820d8de8 171 do {
geotsam 24:8f4b820d8de8 172 //pc.printf("\n\n\r entered while");
AurelienBernier 2:ea61e801e81f 173
AurelienBernier 6:afde4b08166b 174 //Timer stuff
AurelienBernier 6:afde4b08166b 175 dt = t.read();
AurelienBernier 6:afde4b08166b 176 t.reset();
AurelienBernier 6:afde4b08166b 177 t.start();
AurelienBernier 6:afde4b08166b 178
geotsam 15:d58f2bdbf42e 179 //Updating X,Y and theta with the odometry values
geotsam 15:d58f2bdbf42e 180 Odometria();
geotsam 24:8f4b820d8de8 181
geotsam 24:8f4b820d8de8 182 leftMm = get_distance_left_sensor();
geotsam 24:8f4b820d8de8 183 frontMm = get_distance_front_sensor();
geotsam 24:8f4b820d8de8 184 rightMm = get_distance_right_sensor();
geotsam 24:8f4b820d8de8 185
geotsam 24:8f4b820d8de8 186 //pc.printf("\n\r leftMm=%f", leftMm);
geotsam 24:8f4b820d8de8 187 //pc.printf("\n\r frontMm=%f", frontMm);
geotsam 24:8f4b820d8de8 188 //pc.printf("\n\r rightMm=%f", rightMm);
Ludwigfr 21:ebb37a249b5f 189
geotsam 24:8f4b820d8de8 190 update_sonar_values(leftMm, frontMm, rightMm);
geotsam 24:8f4b820d8de8 191
geotsam 24:8f4b820d8de8 192 if ((leftMm < SECURITY_DISTANCE || frontMm < SECURITY_DISTANCE || rightMm < SECURITY_DISTANCE) && turnFromObstacle==false) {
AurelienBernier 20:62154d644531 193 tooClose = true;
geotsam 24:8f4b820d8de8 194 turnFromObstacle = true;
geotsam 24:8f4b820d8de8 195 pc.printf("\n\r TOO CLOSE \n\r");
geotsam 24:8f4b820d8de8 196 leftMotor(1,0);
geotsam 24:8f4b820d8de8 197 rightMotor(1,0);
geotsam 24:8f4b820d8de8 198 Odometria();
geotsam 24:8f4b820d8de8 199 go_to_point_with_angle(X, Y, rad_angle_check(theta+pi));
geotsam 15:d58f2bdbf42e 200 break;
AurelienBernier 11:e641aa08c92e 201 }
AurelienBernier 11:e641aa08c92e 202
geotsam 24:8f4b820d8de8 203 alpha = atan2((target_y-Y),(target_x-X))-theta;
geotsam 24:8f4b820d8de8 204 alpha = atan(sin(alpha)/cos(alpha));
geotsam 24:8f4b820d8de8 205 rho = dist(X, Y, target_x, target_y);
geotsam 24:8f4b820d8de8 206 d2 = rho;
geotsam 24:8f4b820d8de8 207 beta = -alpha-theta+target_angle;
geotsam 24:8f4b820d8de8 208
geotsam 24:8f4b820d8de8 209 //Computing angle error and distance towards the target value
geotsam 24:8f4b820d8de8 210 rho += dt*(-kRho*cos(alpha)*rho);
geotsam 24:8f4b820d8de8 211 temp = alpha;
geotsam 24:8f4b820d8de8 212 alpha += dt*(kRho*sin(alpha)-ka*alpha-kb*beta);
geotsam 24:8f4b820d8de8 213 beta += dt*(-kRho*sin(temp));
geotsam 24:8f4b820d8de8 214 //pc.printf("\n\r d2=%f", d2);
geotsam 24:8f4b820d8de8 215 //pc.printf("\n\r dt=%f", dt);
geotsam 0:8bffb51cc345 216
geotsam 24:8f4b820d8de8 217 //Computing linear and angular velocities
geotsam 24:8f4b820d8de8 218 if(alpha>=-1.5708 && alpha<=1.5708){
geotsam 24:8f4b820d8de8 219 linear=kRho*rho;
geotsam 24:8f4b820d8de8 220 angular=ka*alpha+kb*beta;
geotsam 24:8f4b820d8de8 221 }
geotsam 24:8f4b820d8de8 222 else{
geotsam 24:8f4b820d8de8 223 linear=-kRho*rho;
geotsam 24:8f4b820d8de8 224 angular=-ka*alpha-kb*beta;
geotsam 24:8f4b820d8de8 225 }
geotsam 24:8f4b820d8de8 226 angular_left=(linear-0.5*DISTANCE_WHEELS*angular)/RADIUS_WHEELS;
geotsam 24:8f4b820d8de8 227 angular_right=(linear+0.5*DISTANCE_WHEELS*angular)/RADIUS_WHEELS;
geotsam 24:8f4b820d8de8 228
geotsam 24:8f4b820d8de8 229 pc.printf("\n\r a_r=%f", angular_right);
geotsam 24:8f4b820d8de8 230 pc.printf("\n\r a_l=%f", angular_left);
geotsam 24:8f4b820d8de8 231
geotsam 24:8f4b820d8de8 232 //Slowing down at the end for more precision
geotsam 24:8f4b820d8de8 233 //if (d2<25) {
geotsam 24:8f4b820d8de8 234 // speed = d2*30;
geotsam 24:8f4b820d8de8 235 // }
geotsam 24:8f4b820d8de8 236
geotsam 24:8f4b820d8de8 237 //Normalize speed for motors
geotsam 24:8f4b820d8de8 238 if(angular_left>angular_right) {
geotsam 24:8f4b820d8de8 239 angular_right=speed*angular_right/angular_left;
geotsam 24:8f4b820d8de8 240 angular_left=speed;
geotsam 24:8f4b820d8de8 241 } else {
geotsam 24:8f4b820d8de8 242 angular_left=speed*angular_left/angular_right;
geotsam 24:8f4b820d8de8 243 angular_right=speed;
geotsam 24:8f4b820d8de8 244 }
geotsam 24:8f4b820d8de8 245
geotsam 24:8f4b820d8de8 246 //pc.printf("\n\r X=%f", X);
geotsam 24:8f4b820d8de8 247 //pc.printf("\n\r Y=%f", Y);
geotsam 24:8f4b820d8de8 248
geotsam 24:8f4b820d8de8 249 pc.printf("\n\r a_r=%f", angular_right);
geotsam 24:8f4b820d8de8 250 pc.printf("\n\r a_l=%f", angular_left);
geotsam 0:8bffb51cc345 251
AurelienBernier 2:ea61e801e81f 252 //Updating motor velocities
AurelienBernier 1:f0807d5c5a4b 253 leftMotor(1,angular_left);
AurelienBernier 1:f0807d5c5a4b 254 rightMotor(1,angular_right);
geotsam 0:8bffb51cc345 255
AurelienBernier 7:c94070f9af78 256 wait(0.2);
AurelienBernier 6:afde4b08166b 257 //Timer stuff
AurelienBernier 6:afde4b08166b 258 t.stop();
geotsam 24:8f4b820d8de8 259 } while(d2>1 && (abs(target_angle-theta)>0.01) && tooClose==false);
geotsam 24:8f4b820d8de8 260
geotsam 24:8f4b820d8de8 261 //Stop at the end
geotsam 24:8f4b820d8de8 262 leftMotor(1,0);
geotsam 24:8f4b820d8de8 263 rightMotor(1,0);
geotsam 24:8f4b820d8de8 264
geotsam 24:8f4b820d8de8 265 if(turnFromObstacle){
geotsam 24:8f4b820d8de8 266 turnFromObstacle=false;
geotsam 24:8f4b820d8de8 267 tooClose=false;
geotsam 24:8f4b820d8de8 268 }
Ludwigfr 21:ebb37a249b5f 269 }
Ludwigfr 21:ebb37a249b5f 270
Ludwigfr 21:ebb37a249b5f 271 //Updates sonar values
geotsam 24:8f4b820d8de8 272 void update_sonar_values(float leftMm, float frontMm, float rightMm){
Ludwigfr 21:ebb37a249b5f 273
Ludwigfr 21:ebb37a249b5f 274 float currProba;
geotsam 24:8f4b820d8de8 275 for(int i=0;i<NB_CELL_WIDTH;i++){
geotsam 24:8f4b820d8de8 276 for(int j=0;j<NB_CELL_HEIGHT;j++){
geotsam 24:8f4b820d8de8 277 //check if the point A(x,y) in the world frame is within the range of the sonar (which has the coordinates xs, ys in the world frame)
geotsam 24:8f4b820d8de8 278 //check that again
Ludwigfr 21:ebb37a249b5f 279 //compute for front sonar
geotsam 24:8f4b820d8de8 280 currProba=compute_probability_t(sizeCellX/2+i*sizeCellX,sizeCellY/2+j*sizeCellY,Y+DISTANCE_SONAR_FRONT_Y,NB_CELL_WIDTH*sizeCellX-X+DISTANCE_SONAR_FRONT_X,ANGLE_FRONT_TO_FRONT,frontMm/10);
Ludwigfr 21:ebb37a249b5f 281 map[i][j]=map[i][j]+proba_to_log(currProba)+initialLogValues[i][j];//map is filled as map[0][0] get the data for the point closest to the origin
Ludwigfr 21:ebb37a249b5f 282 //compute for right sonar
geotsam 24:8f4b820d8de8 283 currProba=compute_probability_t(sizeCellX/2+i*sizeCellX,sizeCellY/2+j*sizeCellY,Y+DISTANCE_SONAR_RIGHT_Y,NB_CELL_WIDTH*sizeCellX-X+DISTANCE_SONAR_RIGHT_X,ANGLE_FRONT_TO_RIGHT,rightMm/10);
Ludwigfr 21:ebb37a249b5f 284 map[i][j]=map[i][j]+proba_to_log(currProba)+initialLogValues[i][j];
Ludwigfr 21:ebb37a249b5f 285 //compute for left sonar
geotsam 24:8f4b820d8de8 286 currProba=compute_probability_t(sizeCellX/2+i*sizeCellX,sizeCellY/2+j*sizeCellY,Y+DISTANCE_SONAR_LEFT_Y,NB_CELL_WIDTH*sizeCellX-X+DISTANCE_SONAR_LEFT_X,ANGLE_FRONT_TO_LEFT,leftMm/10);
Ludwigfr 21:ebb37a249b5f 287 map[i][j]=map[i][j]+proba_to_log(currProba)+initialLogValues[i][j];
Ludwigfr 21:ebb37a249b5f 288 }
Ludwigfr 21:ebb37a249b5f 289 }
Ludwigfr 21:ebb37a249b5f 290 }
Ludwigfr 21:ebb37a249b5f 291
Ludwigfr 21:ebb37a249b5f 292 //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 21:ebb37a249b5f 293 float compute_probability_t(float x, float y,float xs,float ys, float angleFromSonarPosition, float distanceObstacleDetected){
Ludwigfr 21:ebb37a249b5f 294
Ludwigfr 21:ebb37a249b5f 295 float alpha=compute_angle_between_vectors(x,y,xs,ys);//angle beetween the point and the sonar beam
geotsam 24:8f4b820d8de8 296 float alphaBeforeAdjustment=alpha-theta-angleFromSonarPosition;
Ludwigfr 21:ebb37a249b5f 297 alpha=rad_angle_check(alphaBeforeAdjustment);//TODO I feel you don't need to do that but I m not sure
Ludwigfr 21:ebb37a249b5f 298 float distancePointToSonar=sqrt(pow(x-xs,2)+pow(y-ys,2));
Ludwigfr 21:ebb37a249b5f 299
Ludwigfr 21:ebb37a249b5f 300 //check if the distance between the cell and the robot is within the circle of range RADIUS_WHEELS
Ludwigfr 21:ebb37a249b5f 301 //check if absolute difference between the angles is no more than Omega/2
Ludwigfr 21:ebb37a249b5f 302 if( distancePointToSonar < (RANGE_SONAR)&& (alpha <= ANGLE_SONAR/2 || alpha >= rad_angle_check(-ANGLE_SONAR/2))){
Ludwigfr 21:ebb37a249b5f 303 if( distancePointToSonar < (distanceObstacleDetected - INCERTITUDE_SONAR)){
Ludwigfr 21:ebb37a249b5f 304 //point before obstacle, probably empty
Ludwigfr 21:ebb37a249b5f 305 /*****************************************************************************/
Ludwigfr 21:ebb37a249b5f 306 float Ea=1.f-pow((2*alphaBeforeAdjustment)/ANGLE_SONAR,2);
Ludwigfr 21:ebb37a249b5f 307 float Er;
Ludwigfr 21:ebb37a249b5f 308 if(distancePointToSonar < RANGE_SONAR_MIN){
Ludwigfr 21:ebb37a249b5f 309 //point before minimum sonar range
Ludwigfr 21:ebb37a249b5f 310 Er=0.f;
Ludwigfr 21:ebb37a249b5f 311 }else{
Ludwigfr 21:ebb37a249b5f 312 //point after minimum sonar range
Ludwigfr 21:ebb37a249b5f 313 Er=1.f-pow((distancePointToSonar-RANGE_SONAR_MIN)/(distanceObstacleDetected-INCERTITUDE_SONAR-RANGE_SONAR_MIN),2);
Ludwigfr 21:ebb37a249b5f 314 }
Ludwigfr 21:ebb37a249b5f 315 /*****************************************************************************/
Ludwigfr 21:ebb37a249b5f 316 return (1.f-Er*Ea)/2.f;
Ludwigfr 21:ebb37a249b5f 317 }else{
Ludwigfr 21:ebb37a249b5f 318 //probably occupied
Ludwigfr 21:ebb37a249b5f 319 /*****************************************************************************/
Ludwigfr 21:ebb37a249b5f 320 float Oa=1.f-pow((2*alphaBeforeAdjustment)/ANGLE_SONAR,2);
Ludwigfr 21:ebb37a249b5f 321 float Or;
Ludwigfr 21:ebb37a249b5f 322 if( distancePointToSonar <= (distanceObstacleDetected + INCERTITUDE_SONAR)){
Ludwigfr 21:ebb37a249b5f 323 //point between distanceObstacleDetected +- INCERTITUDE_SONAR
Ludwigfr 21:ebb37a249b5f 324 Or=1-pow((distancePointToSonar-distanceObstacleDetected)/(INCERTITUDE_SONAR),2);
Ludwigfr 21:ebb37a249b5f 325 }else{
Ludwigfr 21:ebb37a249b5f 326 //point after in range of the sonar but after the zone detected
Ludwigfr 21:ebb37a249b5f 327 Or=0;
Ludwigfr 21:ebb37a249b5f 328 }
Ludwigfr 21:ebb37a249b5f 329 /*****************************************************************************/
Ludwigfr 21:ebb37a249b5f 330 return (1+Or*Oa)/2;
Ludwigfr 21:ebb37a249b5f 331 }
Ludwigfr 21:ebb37a249b5f 332 }else{
Ludwigfr 21:ebb37a249b5f 333 //not in range of the sonar
Ludwigfr 21:ebb37a249b5f 334 return 0.5;
AurelienBernier 18:dbc5fbad4975 335 }
Ludwigfr 21:ebb37a249b5f 336 }
Ludwigfr 21:ebb37a249b5f 337
geotsam 24:8f4b820d8de8 338 void print_final_map_1() {
Ludwigfr 21:ebb37a249b5f 339 float currProba;
geotsam 24:8f4b820d8de8 340 pc.printf("\n\r");
geotsam 24:8f4b820d8de8 341 for (int y = NB_CELL_HEIGHT -1; y>-1; y--) {
geotsam 24:8f4b820d8de8 342 for (int x= 0; x<NB_CELL_WIDTH; x++) {
geotsam 24:8f4b820d8de8 343 currProba=log_to_proba(map[x][y]);
geotsam 24:8f4b820d8de8 344 if ( currProba < 0.5) {
geotsam 24:8f4b820d8de8 345 pc.printf(" 0 ");
Ludwigfr 21:ebb37a249b5f 346 } else {
Ludwigfr 21:ebb37a249b5f 347 if(currProba==0.5)
geotsam 24:8f4b820d8de8 348 pc.printf(" . ");
Ludwigfr 21:ebb37a249b5f 349 else
geotsam 24:8f4b820d8de8 350 pc.printf(" + ");
Ludwigfr 21:ebb37a249b5f 351 }
Ludwigfr 21:ebb37a249b5f 352 }
geotsam 24:8f4b820d8de8 353 pc.printf("\n\r");
Ludwigfr 21:ebb37a249b5f 354 }
Ludwigfr 21:ebb37a249b5f 355 }
Ludwigfr 21:ebb37a249b5f 356
geotsam 24:8f4b820d8de8 357 void print_final_map_2() {
geotsam 24:8f4b820d8de8 358 float currProba;
geotsam 24:8f4b820d8de8 359 pc.printf("\n\r");
geotsam 24:8f4b820d8de8 360 for (int y = NB_CELL_HEIGHT -1; y>-1; y--) {
geotsam 24:8f4b820d8de8 361 for (int x= 0; x<NB_CELL_WIDTH; x++) {
geotsam 24:8f4b820d8de8 362 currProba=log_to_proba(map[x][y]);
geotsam 24:8f4b820d8de8 363 if(x => && x <= && y=> && y<= ){
geotsam 24:8f4b820d8de8 364 pc.printf(" R ");
geotsam 24:8f4b820d8de8 365 }else{
geotsam 24:8f4b820d8de8 366 if ( currProba < 0.5) {
geotsam 24:8f4b820d8de8 367 pc.printf(" 0 ");
geotsam 24:8f4b820d8de8 368 } else {
geotsam 24:8f4b820d8de8 369 if(currProba==0.5)
geotsam 24:8f4b820d8de8 370 pc.printf(" . ");
geotsam 24:8f4b820d8de8 371 else
geotsam 24:8f4b820d8de8 372 pc.printf(" + ");
geotsam 24:8f4b820d8de8 373 }
geotsam 24:8f4b820d8de8 374 }
geotsam 24:8f4b820d8de8 375
geotsam 24:8f4b820d8de8 376 }
geotsam 24:8f4b820d8de8 377 pc.printf("\n\r");
geotsam 24:8f4b820d8de8 378 }
geotsam 24:8f4b820d8de8 379 }
Ludwigfr 21:ebb37a249b5f 380
Ludwigfr 21:ebb37a249b5f 381
Ludwigfr 21:ebb37a249b5f 382 //MATHS heavy functions
Ludwigfr 21:ebb37a249b5f 383 /**********************************************************************/
Ludwigfr 21:ebb37a249b5f 384 //Distance computation function
Ludwigfr 21:ebb37a249b5f 385 float dist(float robot_x, float robot_y, float target_x, float target_y){
Ludwigfr 21:ebb37a249b5f 386 return sqrt(pow(target_y-robot_y,2) + pow(target_x-robot_x,2));
Ludwigfr 21:ebb37a249b5f 387 }
Ludwigfr 21:ebb37a249b5f 388
geotsam 24:8f4b820d8de8 389 //returns the probability [0,1] that the cell is occupied from the log valAue lt
Ludwigfr 21:ebb37a249b5f 390 float log_to_proba(float lt){
Ludwigfr 21:ebb37a249b5f 391 return 1-1/(1+exp(lt));
Ludwigfr 21:ebb37a249b5f 392 }
Ludwigfr 21:ebb37a249b5f 393
Ludwigfr 21:ebb37a249b5f 394 //returns the log value that the cell is occupied from the probability value [0,1]
Ludwigfr 21:ebb37a249b5f 395 float proba_to_log(float p){
Ludwigfr 21:ebb37a249b5f 396 return log(p/(1-p));
Ludwigfr 21:ebb37a249b5f 397 }
Ludwigfr 21:ebb37a249b5f 398
Ludwigfr 21:ebb37a249b5f 399 //returns the new log value
Ludwigfr 21:ebb37a249b5f 400 float compute_log_estimation_lt(float previousLogValue,float currentProbability,float originalLogvalue ){
Ludwigfr 21:ebb37a249b5f 401 return previousLogValue+proba_to_log(currentProbability)-originalLogvalue;
Ludwigfr 21:ebb37a249b5f 402 }
Ludwigfr 21:ebb37a249b5f 403
Ludwigfr 21:ebb37a249b5f 404 //makes the angle inAngle between 0 and 2pi
Ludwigfr 21:ebb37a249b5f 405 float rad_angle_check(float inAngle){
Ludwigfr 21:ebb37a249b5f 406 //cout<<"before :"<<inAngle;
Ludwigfr 21:ebb37a249b5f 407 if(inAngle > 0){
Ludwigfr 21:ebb37a249b5f 408 while(inAngle > (2*pi))
Ludwigfr 21:ebb37a249b5f 409 inAngle-=2*pi;
Ludwigfr 21:ebb37a249b5f 410 }else{
Ludwigfr 21:ebb37a249b5f 411 while(inAngle < 0)
Ludwigfr 21:ebb37a249b5f 412 inAngle+=2*pi;
Ludwigfr 21:ebb37a249b5f 413 }
Ludwigfr 21:ebb37a249b5f 414 //cout<<" after :"<<inAngle<<endl;
Ludwigfr 21:ebb37a249b5f 415 return inAngle;
Ludwigfr 21:ebb37a249b5f 416 }
Ludwigfr 21:ebb37a249b5f 417
Ludwigfr 21:ebb37a249b5f 418 //returns the angle between the vectors (x,y) and (xs,ys)
Ludwigfr 21:ebb37a249b5f 419 float compute_angle_between_vectors(float x, float y,float xs,float ys){
Ludwigfr 21:ebb37a249b5f 420 //alpha angle between ->x and ->SA
Ludwigfr 21:ebb37a249b5f 421 //vector S to A ->SA
Ludwigfr 21:ebb37a249b5f 422 float vSAx=x-xs;
Ludwigfr 21:ebb37a249b5f 423 float vSAy=y-ys;
Ludwigfr 21:ebb37a249b5f 424 //norme SA
Ludwigfr 21:ebb37a249b5f 425 float normeSA=sqrt(pow(vSAx,2)+pow(vSAy,2));
Ludwigfr 21:ebb37a249b5f 426 //vector ->x (1,0)
Ludwigfr 21:ebb37a249b5f 427 float cosAlpha=1*vSAy/*+0*vSAx*//normeSA;;
Ludwigfr 21:ebb37a249b5f 428 //vector ->y (0,1)
Ludwigfr 21:ebb37a249b5f 429 float sinAlpha=/*0*vSAy+*/1*vSAx/normeSA;//+0*vSAx;
Ludwigfr 21:ebb37a249b5f 430 if (sinAlpha < 0)
Ludwigfr 21:ebb37a249b5f 431 return -acos(cosAlpha);
Ludwigfr 21:ebb37a249b5f 432 else
Ludwigfr 21:ebb37a249b5f 433 return acos(cosAlpha);
geotsam 24:8f4b820d8de8 434 }