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