with class

Dependencies:   ISR_Mini-explorer mbed

Fork of VirtualForces by Georgios Tsamis

Committer:
geotsam
Date:
Wed Apr 26 17:06:00 2017 +0000
Revision:
30:95d8d3e2b81b
Parent:
29:224e9e686f7b
Child:
31:352be78e1aad
target_angle now is double

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