Navigate to a given point using the OGM and virtual forces
Dependencies: ISR_Mini-explorer mbed
Fork of VirtualForces by
main.cpp@44:e2bd06f94dc0, 2017-05-29 (annotated)
- Committer:
- Ludwigfr
- Date:
- Mon May 29 11:52:56 2017 +0000
- Revision:
- 44:e2bd06f94dc0
- Parent:
- 43:ffd5a6d4dd48
- Child:
- 45:fb07065a64a9
adjusted the main, tweaked calculate line
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 | |
geotsam | 34:128fc7aed957 | 5 | using namespace std; |
AurelienBernier | 4:8c56c3ba6e54 | 6 | |
Ludwigfr | 39:dd8326ec75ce | 7 | void initialise_parameters(); |
Ludwigfr | 22:ebb37a249b5f | 8 | //fill initialLogValues with the values we already know (here the bordurs) |
Ludwigfr | 22:ebb37a249b5f | 9 | void fill_initial_log_values(); |
Ludwigfr | 22:ebb37a249b5f | 10 | //generate a position randomly and makes the robot go there while updating the map |
Ludwigfr | 22:ebb37a249b5f | 11 | void randomize_and_map(); |
geotsam | 29:224e9e686f7b | 12 | //make the robot do a pi/2 flip |
geotsam | 29:224e9e686f7b | 13 | void do_half_flip(); |
Ludwigfr | 22:ebb37a249b5f | 14 | //go the the given position while updating the map |
Ludwigfr | 22:ebb37a249b5f | 15 | void go_to_point_with_angle(float target_x, float target_y, float target_angle); |
Ludwigfr | 43:ffd5a6d4dd48 | 16 | //Updates sonar values |
geotsam | 24:8f4b820d8de8 | 17 | void update_sonar_values(float leftMm, float frontMm, float rightMm); |
Ludwigfr | 22:ebb37a249b5f | 18 | //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 | 19 | float compute_probability_t(float x, float y,float xs,float ys, float angleFromSonarPosition, float distanceObstacleDetected); |
Ludwigfr | 22:ebb37a249b5f | 20 | //print the map |
Ludwigfr | 22:ebb37a249b5f | 21 | void print_final_map(); |
Ludwigfr | 25:572c9e9a8809 | 22 | //print the map with the robot marked on it |
Ludwigfr | 25:572c9e9a8809 | 23 | void print_final_map_with_robot_position(); |
Ludwigfr | 39:dd8326ec75ce | 24 | //print the map with the robot and the target marked on it |
Ludwigfr | 39:dd8326ec75ce | 25 | void print_final_map_with_robot_position_and_target(); |
Ludwigfr | 42:ab25bffdc32b | 26 | //go to a given line by updating angularLeft and angularRight |
Ludwigfr | 42:ab25bffdc32b | 27 | void go_to_line(float* angularLeft,float* angularRight,float line_a, float line_b, float line_c); |
geotsam | 33:78139f82ea74 | 28 | //calculate virtual force field and move |
Ludwigfr | 42:ab25bffdc32b | 29 | void vff(bool* reached); |
Ludwigfr | 43:ffd5a6d4dd48 | 30 | void test_got_to_line(bool* reached); |
geotsam | 0:8bffb51cc345 | 31 | |
Ludwigfr | 22:ebb37a249b5f | 32 | //MATHS heavy functions |
Ludwigfr | 22:ebb37a249b5f | 33 | float dist(float robot_x, float robot_y, float target_x, float target_y); |
Ludwigfr | 22:ebb37a249b5f | 34 | //returns the probability [0,1] that the cell is occupied from the log value lt |
Ludwigfr | 22:ebb37a249b5f | 35 | float log_to_proba(float lt); |
Ludwigfr | 22:ebb37a249b5f | 36 | //returns the log value that the cell is occupied from the probability value [0,1] |
Ludwigfr | 22:ebb37a249b5f | 37 | float proba_to_log(float p); |
Ludwigfr | 22:ebb37a249b5f | 38 | //returns the new log value |
Ludwigfr | 22:ebb37a249b5f | 39 | float compute_log_estimation_lt(float previousLogValue,float currentProbability,float originalLogvalue ); |
Ludwigfr | 22:ebb37a249b5f | 40 | //makes the angle inAngle between 0 and 2pi |
Ludwigfr | 22:ebb37a249b5f | 41 | float rad_angle_check(float inAngle); |
Ludwigfr | 22:ebb37a249b5f | 42 | //returns the angle between the vectors (x,y) and (xs,ys) |
Ludwigfr | 22:ebb37a249b5f | 43 | float compute_angle_between_vectors(float x, float y,float xs,float ys); |
Ludwigfr | 39:dd8326ec75ce | 44 | float x_robot_in_orthonormal_x(float x, float y); |
Ludwigfr | 39:dd8326ec75ce | 45 | float y_robot_in_orthonormal_y(float x, float y); |
Ludwigfr | 25:572c9e9a8809 | 46 | float robot_center_x_in_orthonormal_x(); |
Ludwigfr | 25:572c9e9a8809 | 47 | float robot_center_y_in_orthonormal_y(); |
Ludwigfr | 25:572c9e9a8809 | 48 | float robot_sonar_front_x_in_orthonormal_x(); |
Ludwigfr | 25:572c9e9a8809 | 49 | float robot_sonar_front_y_in_orthonormal_y(); |
Ludwigfr | 25:572c9e9a8809 | 50 | float robot_sonar_right_x_in_orthonormal_x(); |
Ludwigfr | 25:572c9e9a8809 | 51 | float robot_sonar_right_y_in_orthonormal_y(); |
Ludwigfr | 25:572c9e9a8809 | 52 | float robot_sonar_left_x_in_orthonormal_x(); |
Ludwigfr | 25:572c9e9a8809 | 53 | float robot_sonar_left_y_in_orthonormal_y(); |
Ludwigfr | 25:572c9e9a8809 | 54 | float estimated_width_indice_in_orthonormal_x(int i); |
Ludwigfr | 25:572c9e9a8809 | 55 | float estimated_height_indice_in_orthonormal_y(int j); |
Ludwigfr | 42:ab25bffdc32b | 56 | //update angleError,distanceFromTarget,d2, beta |
Ludwigfr | 42:ab25bffdc32b | 57 | void compute_angles_and_distance(float target_x, float target_y, float target_angle,float dt,float* angleError,float* distanceFromTarget,float* d2,float* beta); |
Ludwigfr | 42:ab25bffdc32b | 58 | //update angularLeft and angularRight |
Ludwigfr | 42:ab25bffdc32b | 59 | void compute_linear_angular_velocities(float angleError,float distanceFromTarget,float beta, float* angularLeft, float* angularRight); |
Ludwigfr | 32:d51928b58645 | 60 | //update foceX and forceY if necessary |
Ludwigfr | 43:ffd5a6d4dd48 | 61 | void update_force(int widthIndice, int heightIndice, float range, float* forceX, float* forceY, float xRobotOrtho, float yRobotOrtho ); |
Ludwigfr | 32:d51928b58645 | 62 | //compute the X and Y force |
Ludwigfr | 39:dd8326ec75ce | 63 | void compute_forceX_and_forceY(float* forceX, float* forceY); |
Ludwigfr | 42:ab25bffdc32b | 64 | //robotX and robotY are from Odometria, calculate line_a, line_b and line_c |
Ludwigfr | 42:ab25bffdc32b | 65 | void calculate_line(float forceX, float forceY, float robotX, float robotY,float *line_a, float *line_b, float *line_c); |
Ludwigfr | 43:ffd5a6d4dd48 | 66 | //return 1 if positiv, -1 if negativ |
Ludwigfr | 43:ffd5a6d4dd48 | 67 | float sign1(float value); |
Ludwigfr | 43:ffd5a6d4dd48 | 68 | //return 1 if positiv, 0 if negativ |
Ludwigfr | 43:ffd5a6d4dd48 | 69 | int sign2(float value); |
Ludwigfr | 43:ffd5a6d4dd48 | 70 | void set_target_to(float x, float y); |
Ludwigfr | 44:e2bd06f94dc0 | 71 | void try_to_reach_target(); |
AurelienBernier | 8:109314be5b68 | 72 | |
Ludwigfr | 22:ebb37a249b5f | 73 | const float pi=3.14159; |
Ludwigfr | 32:d51928b58645 | 74 | |
Ludwigfr | 22:ebb37a249b5f | 75 | //spec of the sonar |
Ludwigfr | 44:e2bd06f94dc0 | 76 | //TODO MEASURE THE DISTANCE on X and Y of the robot space, between each sonar and the center of the robot and add it to calculus in updateSonarValues |
geotsam | 24:8f4b820d8de8 | 77 | const float RANGE_SONAR=50;//cm |
geotsam | 24:8f4b820d8de8 | 78 | const float RANGE_SONAR_MIN=10;//Rmin cm |
geotsam | 24:8f4b820d8de8 | 79 | const float INCERTITUDE_SONAR=10;//cm |
geotsam | 24:8f4b820d8de8 | 80 | const float ANGLE_SONAR=pi/3;//Omega rad |
Ludwigfr | 22:ebb37a249b5f | 81 | |
Ludwigfr | 44:e2bd06f94dc0 | 82 | //those distance and angle are approximation in need of measurements, in the orthonormal space |
geotsam | 24:8f4b820d8de8 | 83 | const float ANGLE_FRONT_TO_LEFT=10*pi/36;//50 degrees |
Ludwigfr | 27:07bde633af72 | 84 | const float DISTANCE_SONAR_LEFT_X=-4; |
geotsam | 24:8f4b820d8de8 | 85 | const float DISTANCE_SONAR_LEFT_Y=4; |
Ludwigfr | 22:ebb37a249b5f | 86 | |
geotsam | 24:8f4b820d8de8 | 87 | const float ANGLE_FRONT_TO_RIGHT=-10*pi/36;//-50 degrees |
Ludwigfr | 27:07bde633af72 | 88 | const float DISTANCE_SONAR_RIGHT_X=4; |
geotsam | 24:8f4b820d8de8 | 89 | const float DISTANCE_SONAR_RIGHT_Y=4; |
AurelienBernier | 11:e641aa08c92e | 90 | |
Ludwigfr | 22:ebb37a249b5f | 91 | const float ANGLE_FRONT_TO_FRONT=0; |
Ludwigfr | 22:ebb37a249b5f | 92 | const float DISTANCE_SONAR_FRONT_X=0; |
Ludwigfr | 22:ebb37a249b5f | 93 | const float DISTANCE_SONAR_FRONT_Y=5; |
Ludwigfr | 22:ebb37a249b5f | 94 | |
Ludwigfr | 22:ebb37a249b5f | 95 | //TODO adjust the size of the map for computation time (25*25?) |
Ludwigfr | 32:d51928b58645 | 96 | const float WIDTH_ARENA=120;//cm |
Ludwigfr | 32:d51928b58645 | 97 | const float HEIGHT_ARENA=90;//cm |
geotsam | 24:8f4b820d8de8 | 98 | |
geotsam | 24:8f4b820d8de8 | 99 | //const int SIZE_MAP=25; |
Ludwigfr | 32:d51928b58645 | 100 | const int NB_CELL_WIDTH=24; |
Ludwigfr | 32:d51928b58645 | 101 | const int NB_CELL_HEIGHT=18; |
Ludwigfr | 22:ebb37a249b5f | 102 | |
Ludwigfr | 44:e2bd06f94dc0 | 103 | //position and orientation of the robot when put on the map (ODOMETRY doesn't know those) it's in the robot space |
Ludwigfr | 31:352be78e1aad | 104 | //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 | 35:c8f224ab153f | 105 | //const float DEFAULT_X=HEIGHT_ARENA/2; |
Ludwigfr | 35:c8f224ab153f | 106 | //const float DEFAULT_Y=WIDTH_ARENA/2; |
Ludwigfr | 40:f5e212d9f900 | 107 | const float DEFAULT_X=20;//lower right |
Ludwigfr | 40:f5e212d9f900 | 108 | const float DEFAULT_Y=20;//lower right |
Ludwigfr | 27:07bde633af72 | 109 | const float DEFAULT_THETA=0; |
Ludwigfr | 22:ebb37a249b5f | 110 | |
geotsam | 24:8f4b820d8de8 | 111 | //used to create the map 250 represent the 250cm of the square where the robot is tested |
geotsam | 24:8f4b820d8de8 | 112 | //float sizeCell=250/(float)SIZE_MAP; |
Ludwigfr | 27:07bde633af72 | 113 | float sizeCellWidth=WIDTH_ARENA/(float)NB_CELL_WIDTH; |
Ludwigfr | 27:07bde633af72 | 114 | float sizeCellHeight=HEIGHT_ARENA/(float)NB_CELL_HEIGHT; |
geotsam | 24:8f4b820d8de8 | 115 | |
geotsam | 24:8f4b820d8de8 | 116 | float map[NB_CELL_WIDTH][NB_CELL_HEIGHT];//contains the log values for each cell |
geotsam | 24:8f4b820d8de8 | 117 | float initialLogValues[NB_CELL_WIDTH][NB_CELL_HEIGHT]; |
Ludwigfr | 22:ebb37a249b5f | 118 | |
Ludwigfr | 22:ebb37a249b5f | 119 | //Diameter of a wheel and distance between the 2 |
Ludwigfr | 22:ebb37a249b5f | 120 | const float RADIUS_WHEELS=3.25; |
Ludwigfr | 22:ebb37a249b5f | 121 | const float DISTANCE_WHEELS=7.2; |
Ludwigfr | 22:ebb37a249b5f | 122 | |
Ludwigfr | 39:dd8326ec75ce | 123 | const int MAX_SPEED=200;//TODO TWEAK THE SPEED SO IT DOES NOT FUCK UP |
Ludwigfr | 39:dd8326ec75ce | 124 | |
Ludwigfr | 39:dd8326ec75ce | 125 | //TODO all those global variables are making me sad |
Ludwigfr | 43:ffd5a6d4dd48 | 126 | const float KRHO=12, KA=30, KB=-13, KV=300, KH=150; //Kappa values |
Ludwigfr | 43:ffd5a6d4dd48 | 127 | |
Ludwigfr | 43:ffd5a6d4dd48 | 128 | //CONSTANT FORCE FIELD |
Ludwigfr | 43:ffd5a6d4dd48 | 129 | const float FORCE_CONSTANT_REPULSION=50;//TODO tweak it |
Ludwigfr | 43:ffd5a6d4dd48 | 130 | const float FORCE_CONSTANT_ATTRACTION=25;//TODO tweak it |
Ludwigfr | 43:ffd5a6d4dd48 | 131 | const float RANGE_FORCE=50;//TODO tweak it |
geotsam | 33:78139f82ea74 | 132 | |
Ludwigfr | 39:dd8326ec75ce | 133 | //those target are in comparaison to the robot (for exemple a robot in 50,50 with a taget of 0,0 would not need to move) |
Ludwigfr | 44:e2bd06f94dc0 | 134 | float targetX;//this is in the robot space top left |
Ludwigfr | 44:e2bd06f94dc0 | 135 | float targetY;//this is in the robot space top left |
Ludwigfr | 43:ffd5a6d4dd48 | 136 | float targetXOrhto; |
Ludwigfr | 43:ffd5a6d4dd48 | 137 | float targetYOrhto; |
geotsam | 33:78139f82ea74 | 138 | |
geotsam | 0:8bffb51cc345 | 139 | int main(){ |
Ludwigfr | 44:e2bd06f94dc0 | 140 | initialise_parameters(); |
Ludwigfr | 44:e2bd06f94dc0 | 141 | //try to reach the target |
Ludwigfr | 44:e2bd06f94dc0 | 142 | set_target_to(50,0);//up right |
Ludwigfr | 44:e2bd06f94dc0 | 143 | print_final_map_with_robot_position_and_target(); |
Ludwigfr | 44:e2bd06f94dc0 | 144 | try_to_reach_target(); |
Ludwigfr | 44:e2bd06f94dc0 | 145 | set_target_to(-50,0);//lower right |
Ludwigfr | 44:e2bd06f94dc0 | 146 | print_final_map_with_robot_position_and_target(); |
Ludwigfr | 44:e2bd06f94dc0 | 147 | try_to_reach_target(); |
Ludwigfr | 44:e2bd06f94dc0 | 148 | set_target_to(50,50);//up left |
Ludwigfr | 44:e2bd06f94dc0 | 149 | print_final_map_with_robot_position_and_target(); |
Ludwigfr | 44:e2bd06f94dc0 | 150 | try_to_reach_target(); |
Ludwigfr | 44:e2bd06f94dc0 | 151 | //print the map forever |
Ludwigfr | 44:e2bd06f94dc0 | 152 | while(1){ |
Ludwigfr | 44:e2bd06f94dc0 | 153 | print_final_map_with_robot_position_and_target(); |
Ludwigfr | 44:e2bd06f94dc0 | 154 | } |
Ludwigfr | 44:e2bd06f94dc0 | 155 | } |
Ludwigfr | 44:e2bd06f94dc0 | 156 | |
Ludwigfr | 44:e2bd06f94dc0 | 157 | void try_to_reach_target(){ |
Ludwigfr | 44:e2bd06f94dc0 | 158 | bool reached=false; |
Ludwigfr | 44:e2bd06f94dc0 | 159 | int print=0; |
Ludwigfr | 39:dd8326ec75ce | 160 | while (!reached) { |
Ludwigfr | 43:ffd5a6d4dd48 | 161 | vff(&reached); |
Ludwigfr | 43:ffd5a6d4dd48 | 162 | //test_got_to_line(&reached); |
Ludwigfr | 44:e2bd06f94dc0 | 163 | if(print==10){ |
Ludwigfr | 44:e2bd06f94dc0 | 164 | leftMotor(1,0); |
Ludwigfr | 44:e2bd06f94dc0 | 165 | rightMotor(1,0); |
Ludwigfr | 44:e2bd06f94dc0 | 166 | pc.printf("\r\n theta=%f", theta); |
Ludwigfr | 44:e2bd06f94dc0 | 167 | pc.printf("\r\n IN ORTHO:"); |
Ludwigfr | 44:e2bd06f94dc0 | 168 | pc.printf("\r\n X Robot=%f", robot_center_x_in_orthonormal_x()); |
Ludwigfr | 44:e2bd06f94dc0 | 169 | pc.printf("\r\n Y Robot=%f", robot_center_y_in_orthonormal_y()); |
Ludwigfr | 44:e2bd06f94dc0 | 170 | pc.printf("\r\n X Target=%f", targetXOrhto); |
Ludwigfr | 44:e2bd06f94dc0 | 171 | pc.printf("\r\n Y Target=%f", targetYOrhto); |
Ludwigfr | 44:e2bd06f94dc0 | 172 | print_final_map_with_robot_position_and_target(); |
Ludwigfr | 44:e2bd06f94dc0 | 173 | print=0; |
Ludwigfr | 44:e2bd06f94dc0 | 174 | }else |
Ludwigfr | 44:e2bd06f94dc0 | 175 | print+=1; |
Ludwigfr | 39:dd8326ec75ce | 176 | } |
Ludwigfr | 39:dd8326ec75ce | 177 | //Stop at the end |
Ludwigfr | 39:dd8326ec75ce | 178 | leftMotor(1,0); |
Ludwigfr | 39:dd8326ec75ce | 179 | rightMotor(1,0); |
Ludwigfr | 44:e2bd06f94dc0 | 180 | pc.printf("\r\n target reached"); |
Ludwigfr | 39:dd8326ec75ce | 181 | } |
Ludwigfr | 39:dd8326ec75ce | 182 | |
Ludwigfr | 43:ffd5a6d4dd48 | 183 | void set_target_to(float x, float y){ |
Ludwigfr | 43:ffd5a6d4dd48 | 184 | targetX=x; |
Ludwigfr | 43:ffd5a6d4dd48 | 185 | targetY=y; |
Ludwigfr | 43:ffd5a6d4dd48 | 186 | targetXOrhto=x_robot_in_orthonormal_x(targetX,targetY); |
Ludwigfr | 43:ffd5a6d4dd48 | 187 | targetYOrhto=y_robot_in_orthonormal_y(targetX,targetY); |
Ludwigfr | 43:ffd5a6d4dd48 | 188 | } |
Ludwigfr | 43:ffd5a6d4dd48 | 189 | |
Ludwigfr | 39:dd8326ec75ce | 190 | void initialise_parameters(){ |
geotsam | 13:41f75c132135 | 191 | i2c1.frequency(100000); |
AurelienBernier | 2:ea61e801e81f | 192 | initRobot(); //Initializing the robot |
geotsam | 0:8bffb51cc345 | 193 | pc.baud(9600); // baud for the pc communication |
geotsam | 0:8bffb51cc345 | 194 | |
Ludwigfr | 22:ebb37a249b5f | 195 | measure_always_on();//TODO check if needed |
geotsam | 24:8f4b820d8de8 | 196 | wait(0.5); |
Ludwigfr | 39:dd8326ec75ce | 197 | //fill the map with the initial log values |
Ludwigfr | 22:ebb37a249b5f | 198 | fill_initial_log_values(); |
geotsam | 13:41f75c132135 | 199 | |
Ludwigfr | 31:352be78e1aad | 200 | theta=DEFAULT_THETA; |
Ludwigfr | 35:c8f224ab153f | 201 | X=DEFAULT_X; |
Ludwigfr | 35:c8f224ab153f | 202 | Y=DEFAULT_Y; |
AurelienBernier | 8:109314be5b68 | 203 | } |
AurelienBernier | 8:109314be5b68 | 204 | |
Ludwigfr | 22:ebb37a249b5f | 205 | //fill initialLogValues with the values we already know (here the bordurs) |
Ludwigfr | 22:ebb37a249b5f | 206 | void fill_initial_log_values(){ |
Ludwigfr | 31:352be78e1aad | 207 | //Fill map, we know the border are occupied |
geotsam | 24:8f4b820d8de8 | 208 | for (int i = 0; i<NB_CELL_WIDTH; i++) { |
geotsam | 24:8f4b820d8de8 | 209 | for (int j = 0; j<NB_CELL_HEIGHT; j++) { |
geotsam | 24:8f4b820d8de8 | 210 | if(j==0 || j==NB_CELL_HEIGHT-1 || i==0 || i==NB_CELL_WIDTH-1) |
Ludwigfr | 22:ebb37a249b5f | 211 | initialLogValues[i][j] = proba_to_log(1); |
Ludwigfr | 22:ebb37a249b5f | 212 | else |
Ludwigfr | 22:ebb37a249b5f | 213 | initialLogValues[i][j] = proba_to_log(0.5); |
AurelienBernier | 21:62154d644531 | 214 | } |
Ludwigfr | 22:ebb37a249b5f | 215 | } |
AurelienBernier | 8:109314be5b68 | 216 | } |
AurelienBernier | 8:109314be5b68 | 217 | |
Ludwigfr | 22:ebb37a249b5f | 218 | //generate a position randomly and makes the robot go there while updating the map |
Ludwigfr | 22:ebb37a249b5f | 219 | void randomize_and_map() { |
geotsam | 24:8f4b820d8de8 | 220 | //TODO check that it's aurelien's work |
Ludwigfr | 27:07bde633af72 | 221 | float target_x = (rand()%(int)(HEIGHT_ARENA*10))/10;//for decimal precision |
Ludwigfr | 27:07bde633af72 | 222 | float target_y = (rand()%(int)(WIDTH_ARENA*10))/10; |
geotsam | 30:95d8d3e2b81b | 223 | float target_angle = 2*((float)(rand()%31416)-15708)/10000.0; |
geotsam | 24:8f4b820d8de8 | 224 | |
Ludwigfr | 43:ffd5a6d4dd48 | 225 | go_to_point_with_angle(targetX, targetY, target_angle); |
AurelienBernier | 19:dbc5fbad4975 | 226 | } |
AurelienBernier | 19:dbc5fbad4975 | 227 | |
geotsam | 29:224e9e686f7b | 228 | |
geotsam | 29:224e9e686f7b | 229 | void do_half_flip(){ |
Ludwigfr | 28:f884979a02fa | 230 | Odometria(); |
geotsam | 29:224e9e686f7b | 231 | float theta_plus_h_pi=theta+pi/2;//theta is between -pi and pi |
geotsam | 29:224e9e686f7b | 232 | if(theta_plus_h_pi > pi) |
geotsam | 29:224e9e686f7b | 233 | theta_plus_h_pi=-(2*pi-theta_plus_h_pi); |
geotsam | 29:224e9e686f7b | 234 | leftMotor(0,100); |
geotsam | 29:224e9e686f7b | 235 | rightMotor(1,100); |
geotsam | 29:224e9e686f7b | 236 | while(abs(theta_plus_h_pi-theta)>0.05){ |
Ludwigfr | 28:f884979a02fa | 237 | Odometria(); |
geotsam | 29:224e9e686f7b | 238 | // pc.printf("\n\r diff=%f", abs(theta_plus_pi-theta)); |
Ludwigfr | 28:f884979a02fa | 239 | } |
Ludwigfr | 28:f884979a02fa | 240 | leftMotor(1,0); |
Ludwigfr | 28:f884979a02fa | 241 | rightMotor(1,0); |
Ludwigfr | 28:f884979a02fa | 242 | } |
Ludwigfr | 28:f884979a02fa | 243 | |
Ludwigfr | 22:ebb37a249b5f | 244 | //go the the given position while updating the map |
Ludwigfr | 22:ebb37a249b5f | 245 | //TODO clean this procedure it's ugly as hell and too long |
Ludwigfr | 22:ebb37a249b5f | 246 | void go_to_point_with_angle(float target_x, float target_y, float target_angle) { |
Ludwigfr | 43:ffd5a6d4dd48 | 247 | set_target_to(target_x,target_y); |
Ludwigfr | 28:f884979a02fa | 248 | Odometria(); |
Ludwigfr | 42:ab25bffdc32b | 249 | float angleError = atan2((target_y-Y),(target_x-X))-theta; |
Ludwigfr | 42:ab25bffdc32b | 250 | angleError = atan(sin(angleError)/cos(angleError)); |
Ludwigfr | 43:ffd5a6d4dd48 | 251 | float distanceFromTarget = dist(robot_center_x_in_orthonormal_x(),robot_center_y_in_orthonormal_y(),targetXOrhto,targetYOrhto); |
Ludwigfr | 42:ab25bffdc32b | 252 | float beta = -angleError-theta+target_angle; |
geotsam | 24:8f4b820d8de8 | 253 | //beta = atan(sin(beta)/cos(beta)); |
Ludwigfr | 27:07bde633af72 | 254 | bool keep_going=true; |
Ludwigfr | 40:f5e212d9f900 | 255 | float leftMm; |
Ludwigfr | 40:f5e212d9f900 | 256 | float frontMm; |
Ludwigfr | 40:f5e212d9f900 | 257 | float rightMm; |
Ludwigfr | 42:ab25bffdc32b | 258 | float angularLeft=0; |
Ludwigfr | 42:ab25bffdc32b | 259 | float angularRight=0; |
Ludwigfr | 42:ab25bffdc32b | 260 | Timer t; |
Ludwigfr | 42:ab25bffdc32b | 261 | float dt=0.5;//TODO better name please |
Ludwigfr | 42:ab25bffdc32b | 262 | float d2;//TODO better name please |
geotsam | 24:8f4b820d8de8 | 263 | do { |
AurelienBernier | 6:afde4b08166b | 264 | //Timer stuff |
AurelienBernier | 6:afde4b08166b | 265 | dt = t.read(); |
AurelienBernier | 6:afde4b08166b | 266 | t.reset(); |
AurelienBernier | 6:afde4b08166b | 267 | t.start(); |
AurelienBernier | 6:afde4b08166b | 268 | |
geotsam | 14:d58f2bdbf42e | 269 | //Updating X,Y and theta with the odometry values |
geotsam | 14:d58f2bdbf42e | 270 | Odometria(); |
geotsam | 24:8f4b820d8de8 | 271 | leftMm = get_distance_left_sensor(); |
geotsam | 24:8f4b820d8de8 | 272 | frontMm = get_distance_front_sensor(); |
geotsam | 24:8f4b820d8de8 | 273 | rightMm = get_distance_right_sensor(); |
geotsam | 24:8f4b820d8de8 | 274 | |
geotsam | 24:8f4b820d8de8 | 275 | //pc.printf("\n\r leftMm=%f", leftMm); |
geotsam | 24:8f4b820d8de8 | 276 | //pc.printf("\n\r frontMm=%f", frontMm); |
geotsam | 24:8f4b820d8de8 | 277 | //pc.printf("\n\r rightMm=%f", rightMm); |
Ludwigfr | 27:07bde633af72 | 278 | |
Ludwigfr | 27:07bde633af72 | 279 | //if in dangerzone |
geotsam | 29:224e9e686f7b | 280 | if(frontMm < 120 || leftMm <120 || rightMm <120){ |
geotsam | 24:8f4b820d8de8 | 281 | leftMotor(1,0); |
geotsam | 24:8f4b820d8de8 | 282 | rightMotor(1,0); |
Ludwigfr | 27:07bde633af72 | 283 | update_sonar_values(leftMm, frontMm, rightMm); |
geotsam | 29:224e9e686f7b | 284 | //TODO Giorgos maybe you can also test the do_half_flip() function |
geotsam | 24:8f4b820d8de8 | 285 | Odometria(); |
Ludwigfr | 27:07bde633af72 | 286 | //do a flip TODO |
Ludwigfr | 27:07bde633af72 | 287 | keep_going=false; |
geotsam | 29:224e9e686f7b | 288 | do_half_flip(); |
Ludwigfr | 27:07bde633af72 | 289 | }else{ |
Ludwigfr | 27:07bde633af72 | 290 | //if not in danger zone continue as usual |
Ludwigfr | 27:07bde633af72 | 291 | update_sonar_values(leftMm, frontMm, rightMm); |
Ludwigfr | 42:ab25bffdc32b | 292 | compute_angles_and_distance(target_x, target_y, target_angle,dt,&angleError,&distanceFromTarget,&d2,&beta);//Compute the angles and the distance from target |
Ludwigfr | 42:ab25bffdc32b | 293 | compute_linear_angular_velocities(angleError,distanceFromTarget,beta,&angularLeft,&angularRight); //Using the angles and distance, compute the velocities needed (linear & angular) |
Ludwigfr | 42:ab25bffdc32b | 294 | |
Ludwigfr | 27:07bde633af72 | 295 | //pc.printf("\n\r X=%f", X); |
Ludwigfr | 27:07bde633af72 | 296 | //pc.printf("\n\r Y=%f", Y); |
Ludwigfr | 27:07bde633af72 | 297 | |
Ludwigfr | 42:ab25bffdc32b | 298 | //pc.printf("\n\r a_r=%f", angularRight); |
Ludwigfr | 42:ab25bffdc32b | 299 | //pc.printf("\n\r a_l=%f", angularLeft); |
Ludwigfr | 27:07bde633af72 | 300 | |
Ludwigfr | 27:07bde633af72 | 301 | //Updating motor velocities |
Ludwigfr | 43:ffd5a6d4dd48 | 302 | leftMotor(sign2(angularLeft),abs(angularLeft)); |
Ludwigfr | 43:ffd5a6d4dd48 | 303 | rightMotor(sign2(angularRight),abs(angularRight)); |
Ludwigfr | 27:07bde633af72 | 304 | |
Ludwigfr | 27:07bde633af72 | 305 | wait(0.2); |
Ludwigfr | 27:07bde633af72 | 306 | //Timer stuff |
Ludwigfr | 27:07bde633af72 | 307 | t.stop(); |
AurelienBernier | 11:e641aa08c92e | 308 | } |
Ludwigfr | 27:07bde633af72 | 309 | } while(d2>1 && (abs(target_angle-theta)>0.01) && keep_going); |
geotsam | 24:8f4b820d8de8 | 310 | |
geotsam | 24:8f4b820d8de8 | 311 | //Stop at the end |
geotsam | 24:8f4b820d8de8 | 312 | leftMotor(1,0); |
geotsam | 24:8f4b820d8de8 | 313 | rightMotor(1,0); |
Ludwigfr | 22:ebb37a249b5f | 314 | } |
Ludwigfr | 22:ebb37a249b5f | 315 | |
Ludwigfr | 22:ebb37a249b5f | 316 | //Updates sonar values |
geotsam | 24:8f4b820d8de8 | 317 | void update_sonar_values(float leftMm, float frontMm, float rightMm){ |
Ludwigfr | 22:ebb37a249b5f | 318 | float currProba; |
Ludwigfr | 25:572c9e9a8809 | 319 | float i_in_orthonormal; |
Ludwigfr | 25:572c9e9a8809 | 320 | float j_in_orthonormal; |
geotsam | 24:8f4b820d8de8 | 321 | for(int i=0;i<NB_CELL_WIDTH;i++){ |
geotsam | 24:8f4b820d8de8 | 322 | for(int j=0;j<NB_CELL_HEIGHT;j++){ |
Ludwigfr | 44:e2bd06f94dc0 | 323 | //check if the point A(x,y) in the world space is within the range of the sonar (which has the coordinates xs, ys in the world space) |
geotsam | 24:8f4b820d8de8 | 324 | //check that again |
Ludwigfr | 22:ebb37a249b5f | 325 | //compute for front sonar |
Ludwigfr | 25:572c9e9a8809 | 326 | i_in_orthonormal=estimated_width_indice_in_orthonormal_x(i); |
Ludwigfr | 25:572c9e9a8809 | 327 | j_in_orthonormal=estimated_height_indice_in_orthonormal_y(j); |
Ludwigfr | 25:572c9e9a8809 | 328 | |
Ludwigfr | 25:572c9e9a8809 | 329 | 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 | 330 | 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 | 331 | //compute for right sonar |
Ludwigfr | 25:572c9e9a8809 | 332 | 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 | 333 | map[i][j]=map[i][j]+proba_to_log(currProba)+initialLogValues[i][j]; |
Ludwigfr | 22:ebb37a249b5f | 334 | //compute for left sonar |
Ludwigfr | 25:572c9e9a8809 | 335 | 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 | 336 | map[i][j]=map[i][j]+proba_to_log(currProba)+initialLogValues[i][j]; |
Ludwigfr | 22:ebb37a249b5f | 337 | } |
Ludwigfr | 22:ebb37a249b5f | 338 | } |
Ludwigfr | 22:ebb37a249b5f | 339 | } |
Ludwigfr | 22:ebb37a249b5f | 340 | |
Ludwigfr | 25:572c9e9a8809 | 341 | //ODOMETRIA MUST HAVE BEEN CALLED |
Ludwigfr | 22:ebb37a249b5f | 342 | //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 | 343 | float compute_probability_t(float x, float y,float xs,float ys, float angleFromSonarPosition, float distanceObstacleDetected){ |
Ludwigfr | 22:ebb37a249b5f | 344 | |
Ludwigfr | 42:ab25bffdc32b | 345 | float anglePointToSonar=compute_angle_between_vectors(x,y,xs,ys);//angle beetween the point and the sonar beam |
Ludwigfr | 42:ab25bffdc32b | 346 | float alphaBeforeAdjustment=anglePointToSonar-theta-angleFromSonarPosition; |
Ludwigfr | 42:ab25bffdc32b | 347 | anglePointToSonar=rad_angle_check(alphaBeforeAdjustment);//TODO I feel you don't need to do that but I m not sure |
Ludwigfr | 22:ebb37a249b5f | 348 | float distancePointToSonar=sqrt(pow(x-xs,2)+pow(y-ys,2)); |
Ludwigfr | 22:ebb37a249b5f | 349 | |
Ludwigfr | 22:ebb37a249b5f | 350 | //check if the distance between the cell and the robot is within the circle of range RADIUS_WHEELS |
Ludwigfr | 22:ebb37a249b5f | 351 | //check if absolute difference between the angles is no more than Omega/2 |
Ludwigfr | 42:ab25bffdc32b | 352 | if( distancePointToSonar < (RANGE_SONAR)&& (anglePointToSonar <= ANGLE_SONAR/2 || anglePointToSonar >= rad_angle_check(-ANGLE_SONAR/2))){ |
Ludwigfr | 22:ebb37a249b5f | 353 | if( distancePointToSonar < (distanceObstacleDetected - INCERTITUDE_SONAR)){ |
Ludwigfr | 22:ebb37a249b5f | 354 | //point before obstacle, probably empty |
Ludwigfr | 22:ebb37a249b5f | 355 | /*****************************************************************************/ |
Ludwigfr | 22:ebb37a249b5f | 356 | float Ea=1.f-pow((2*alphaBeforeAdjustment)/ANGLE_SONAR,2); |
Ludwigfr | 22:ebb37a249b5f | 357 | float Er; |
Ludwigfr | 22:ebb37a249b5f | 358 | if(distancePointToSonar < RANGE_SONAR_MIN){ |
Ludwigfr | 22:ebb37a249b5f | 359 | //point before minimum sonar range |
Ludwigfr | 22:ebb37a249b5f | 360 | Er=0.f; |
Ludwigfr | 22:ebb37a249b5f | 361 | }else{ |
Ludwigfr | 22:ebb37a249b5f | 362 | //point after minimum sonar range |
Ludwigfr | 22:ebb37a249b5f | 363 | Er=1.f-pow((distancePointToSonar-RANGE_SONAR_MIN)/(distanceObstacleDetected-INCERTITUDE_SONAR-RANGE_SONAR_MIN),2); |
Ludwigfr | 22:ebb37a249b5f | 364 | } |
Ludwigfr | 22:ebb37a249b5f | 365 | /*****************************************************************************/ |
Ludwigfr | 22:ebb37a249b5f | 366 | return (1.f-Er*Ea)/2.f; |
Ludwigfr | 22:ebb37a249b5f | 367 | }else{ |
Ludwigfr | 22:ebb37a249b5f | 368 | //probably occupied |
Ludwigfr | 22:ebb37a249b5f | 369 | /*****************************************************************************/ |
Ludwigfr | 22:ebb37a249b5f | 370 | float Oa=1.f-pow((2*alphaBeforeAdjustment)/ANGLE_SONAR,2); |
Ludwigfr | 22:ebb37a249b5f | 371 | float Or; |
Ludwigfr | 22:ebb37a249b5f | 372 | if( distancePointToSonar <= (distanceObstacleDetected + INCERTITUDE_SONAR)){ |
Ludwigfr | 22:ebb37a249b5f | 373 | //point between distanceObstacleDetected +- INCERTITUDE_SONAR |
Ludwigfr | 22:ebb37a249b5f | 374 | Or=1-pow((distancePointToSonar-distanceObstacleDetected)/(INCERTITUDE_SONAR),2); |
Ludwigfr | 22:ebb37a249b5f | 375 | }else{ |
Ludwigfr | 22:ebb37a249b5f | 376 | //point after in range of the sonar but after the zone detected |
Ludwigfr | 22:ebb37a249b5f | 377 | Or=0; |
Ludwigfr | 22:ebb37a249b5f | 378 | } |
Ludwigfr | 22:ebb37a249b5f | 379 | /*****************************************************************************/ |
Ludwigfr | 22:ebb37a249b5f | 380 | return (1+Or*Oa)/2; |
Ludwigfr | 22:ebb37a249b5f | 381 | } |
Ludwigfr | 22:ebb37a249b5f | 382 | }else{ |
Ludwigfr | 25:572c9e9a8809 | 383 | //not checked by the sonar |
Ludwigfr | 22:ebb37a249b5f | 384 | return 0.5; |
AurelienBernier | 19:dbc5fbad4975 | 385 | } |
Ludwigfr | 22:ebb37a249b5f | 386 | } |
Ludwigfr | 22:ebb37a249b5f | 387 | |
Ludwigfr | 25:572c9e9a8809 | 388 | void print_final_map() { |
Ludwigfr | 22:ebb37a249b5f | 389 | float currProba; |
geotsam | 24:8f4b820d8de8 | 390 | pc.printf("\n\r"); |
geotsam | 24:8f4b820d8de8 | 391 | for (int y = NB_CELL_HEIGHT -1; y>-1; y--) { |
geotsam | 24:8f4b820d8de8 | 392 | for (int x= 0; x<NB_CELL_WIDTH; x++) { |
geotsam | 24:8f4b820d8de8 | 393 | currProba=log_to_proba(map[x][y]); |
geotsam | 24:8f4b820d8de8 | 394 | if ( currProba < 0.5) { |
geotsam | 29:224e9e686f7b | 395 | pc.printf(" "); |
Ludwigfr | 22:ebb37a249b5f | 396 | } else { |
Ludwigfr | 22:ebb37a249b5f | 397 | if(currProba==0.5) |
geotsam | 24:8f4b820d8de8 | 398 | pc.printf(" . "); |
Ludwigfr | 22:ebb37a249b5f | 399 | else |
geotsam | 29:224e9e686f7b | 400 | pc.printf(" X "); |
Ludwigfr | 22:ebb37a249b5f | 401 | } |
Ludwigfr | 22:ebb37a249b5f | 402 | } |
geotsam | 24:8f4b820d8de8 | 403 | pc.printf("\n\r"); |
Ludwigfr | 22:ebb37a249b5f | 404 | } |
Ludwigfr | 22:ebb37a249b5f | 405 | } |
Ludwigfr | 22:ebb37a249b5f | 406 | |
Ludwigfr | 25:572c9e9a8809 | 407 | void print_final_map_with_robot_position() { |
geotsam | 24:8f4b820d8de8 | 408 | float currProba; |
Ludwigfr | 25:572c9e9a8809 | 409 | Odometria(); |
Ludwigfr | 25:572c9e9a8809 | 410 | float Xrobot=robot_center_x_in_orthonormal_x(); |
Ludwigfr | 25:572c9e9a8809 | 411 | float Yrobot=robot_center_y_in_orthonormal_y(); |
Ludwigfr | 25:572c9e9a8809 | 412 | |
Ludwigfr | 25:572c9e9a8809 | 413 | float heightIndiceInOrthonormal; |
Ludwigfr | 25:572c9e9a8809 | 414 | float widthIndiceInOrthonormal; |
Ludwigfr | 25:572c9e9a8809 | 415 | |
Ludwigfr | 27:07bde633af72 | 416 | float widthMalus=-(3*sizeCellWidth/2); |
Ludwigfr | 27:07bde633af72 | 417 | float widthBonus=sizeCellWidth/2; |
Ludwigfr | 25:572c9e9a8809 | 418 | |
Ludwigfr | 27:07bde633af72 | 419 | float heightMalus=-(3*sizeCellHeight/2); |
Ludwigfr | 27:07bde633af72 | 420 | float heightBonus=sizeCellHeight/2; |
Ludwigfr | 25:572c9e9a8809 | 421 | |
geotsam | 24:8f4b820d8de8 | 422 | pc.printf("\n\r"); |
geotsam | 24:8f4b820d8de8 | 423 | for (int y = NB_CELL_HEIGHT -1; y>-1; y--) { |
geotsam | 24:8f4b820d8de8 | 424 | for (int x= 0; x<NB_CELL_WIDTH; x++) { |
Ludwigfr | 25:572c9e9a8809 | 425 | heightIndiceInOrthonormal=estimated_height_indice_in_orthonormal_y(y); |
Ludwigfr | 25:572c9e9a8809 | 426 | widthIndiceInOrthonormal=estimated_width_indice_in_orthonormal_x(x); |
Ludwigfr | 27:07bde633af72 | 427 | if(Yrobot >= (heightIndiceInOrthonormal+heightMalus) && Yrobot <= (heightIndiceInOrthonormal+heightBonus) && Xrobot >= (widthIndiceInOrthonormal+widthMalus) && Xrobot <= (widthIndiceInOrthonormal+widthBonus)) |
Ludwigfr | 27:07bde633af72 | 428 | pc.printf(" R "); |
Ludwigfr | 25:572c9e9a8809 | 429 | else{ |
Ludwigfr | 25:572c9e9a8809 | 430 | currProba=log_to_proba(map[x][y]); |
Ludwigfr | 25:572c9e9a8809 | 431 | if ( currProba < 0.5) |
geotsam | 29:224e9e686f7b | 432 | pc.printf(" "); |
Ludwigfr | 25:572c9e9a8809 | 433 | else{ |
Ludwigfr | 25:572c9e9a8809 | 434 | if(currProba==0.5) |
Ludwigfr | 27:07bde633af72 | 435 | pc.printf(" . "); |
Ludwigfr | 25:572c9e9a8809 | 436 | else |
geotsam | 29:224e9e686f7b | 437 | pc.printf(" X "); |
Ludwigfr | 25:572c9e9a8809 | 438 | } |
geotsam | 24:8f4b820d8de8 | 439 | } |
geotsam | 24:8f4b820d8de8 | 440 | } |
geotsam | 24:8f4b820d8de8 | 441 | pc.printf("\n\r"); |
geotsam | 24:8f4b820d8de8 | 442 | } |
geotsam | 24:8f4b820d8de8 | 443 | } |
Ludwigfr | 22:ebb37a249b5f | 444 | |
Ludwigfr | 39:dd8326ec75ce | 445 | void print_final_map_with_robot_position_and_target() { |
Ludwigfr | 39:dd8326ec75ce | 446 | float currProba; |
Ludwigfr | 39:dd8326ec75ce | 447 | Odometria(); |
Ludwigfr | 39:dd8326ec75ce | 448 | float Xrobot=robot_center_x_in_orthonormal_x(); |
Ludwigfr | 39:dd8326ec75ce | 449 | float Yrobot=robot_center_y_in_orthonormal_y(); |
Ludwigfr | 39:dd8326ec75ce | 450 | |
Ludwigfr | 39:dd8326ec75ce | 451 | float heightIndiceInOrthonormal; |
Ludwigfr | 39:dd8326ec75ce | 452 | float widthIndiceInOrthonormal; |
Ludwigfr | 39:dd8326ec75ce | 453 | |
Ludwigfr | 39:dd8326ec75ce | 454 | float widthMalus=-(3*sizeCellWidth/2); |
Ludwigfr | 39:dd8326ec75ce | 455 | float widthBonus=sizeCellWidth/2; |
Ludwigfr | 39:dd8326ec75ce | 456 | |
Ludwigfr | 39:dd8326ec75ce | 457 | float heightMalus=-(3*sizeCellHeight/2); |
Ludwigfr | 39:dd8326ec75ce | 458 | float heightBonus=sizeCellHeight/2; |
Ludwigfr | 39:dd8326ec75ce | 459 | |
Ludwigfr | 39:dd8326ec75ce | 460 | pc.printf("\n\r"); |
Ludwigfr | 39:dd8326ec75ce | 461 | for (int y = NB_CELL_HEIGHT -1; y>-1; y--) { |
Ludwigfr | 39:dd8326ec75ce | 462 | for (int x= 0; x<NB_CELL_WIDTH; x++) { |
Ludwigfr | 39:dd8326ec75ce | 463 | heightIndiceInOrthonormal=estimated_height_indice_in_orthonormal_y(y); |
Ludwigfr | 39:dd8326ec75ce | 464 | widthIndiceInOrthonormal=estimated_width_indice_in_orthonormal_x(x); |
Ludwigfr | 39:dd8326ec75ce | 465 | if(Yrobot >= (heightIndiceInOrthonormal+heightMalus) && Yrobot <= (heightIndiceInOrthonormal+heightBonus) && Xrobot >= (widthIndiceInOrthonormal+widthMalus) && Xrobot <= (widthIndiceInOrthonormal+widthBonus)) |
Ludwigfr | 39:dd8326ec75ce | 466 | pc.printf(" R "); |
Ludwigfr | 39:dd8326ec75ce | 467 | else{ |
Ludwigfr | 43:ffd5a6d4dd48 | 468 | if(targetYOrhto >= (heightIndiceInOrthonormal+heightMalus) && targetYOrhto <= (heightIndiceInOrthonormal+heightBonus) && targetXOrhto >= (widthIndiceInOrthonormal+widthMalus) && targetXOrhto <= (widthIndiceInOrthonormal+widthBonus)) |
Ludwigfr | 39:dd8326ec75ce | 469 | pc.printf(" T "); |
Ludwigfr | 39:dd8326ec75ce | 470 | else{ |
Ludwigfr | 39:dd8326ec75ce | 471 | currProba=log_to_proba(map[x][y]); |
Ludwigfr | 39:dd8326ec75ce | 472 | if ( currProba < 0.5) |
Ludwigfr | 39:dd8326ec75ce | 473 | pc.printf(" "); |
Ludwigfr | 39:dd8326ec75ce | 474 | else{ |
Ludwigfr | 39:dd8326ec75ce | 475 | if(currProba==0.5) |
Ludwigfr | 39:dd8326ec75ce | 476 | pc.printf(" . "); |
Ludwigfr | 39:dd8326ec75ce | 477 | else |
Ludwigfr | 39:dd8326ec75ce | 478 | pc.printf(" X "); |
Ludwigfr | 39:dd8326ec75ce | 479 | } |
Ludwigfr | 39:dd8326ec75ce | 480 | } |
Ludwigfr | 39:dd8326ec75ce | 481 | } |
Ludwigfr | 39:dd8326ec75ce | 482 | } |
Ludwigfr | 39:dd8326ec75ce | 483 | pc.printf("\n\r"); |
Ludwigfr | 39:dd8326ec75ce | 484 | } |
Ludwigfr | 39:dd8326ec75ce | 485 | } |
Ludwigfr | 39:dd8326ec75ce | 486 | |
Ludwigfr | 22:ebb37a249b5f | 487 | //MATHS heavy functions |
Ludwigfr | 22:ebb37a249b5f | 488 | /**********************************************************************/ |
Ludwigfr | 22:ebb37a249b5f | 489 | //Distance computation function |
Ludwigfr | 22:ebb37a249b5f | 490 | float dist(float robot_x, float robot_y, float target_x, float target_y){ |
Ludwigfr | 22:ebb37a249b5f | 491 | return sqrt(pow(target_y-robot_y,2) + pow(target_x-robot_x,2)); |
Ludwigfr | 22:ebb37a249b5f | 492 | } |
Ludwigfr | 22:ebb37a249b5f | 493 | |
geotsam | 24:8f4b820d8de8 | 494 | //returns the probability [0,1] that the cell is occupied from the log valAue lt |
Ludwigfr | 22:ebb37a249b5f | 495 | float log_to_proba(float lt){ |
Ludwigfr | 22:ebb37a249b5f | 496 | return 1-1/(1+exp(lt)); |
Ludwigfr | 22:ebb37a249b5f | 497 | } |
Ludwigfr | 22:ebb37a249b5f | 498 | |
Ludwigfr | 22:ebb37a249b5f | 499 | //returns the log value that the cell is occupied from the probability value [0,1] |
Ludwigfr | 22:ebb37a249b5f | 500 | float proba_to_log(float p){ |
Ludwigfr | 22:ebb37a249b5f | 501 | return log(p/(1-p)); |
Ludwigfr | 22:ebb37a249b5f | 502 | } |
Ludwigfr | 22:ebb37a249b5f | 503 | |
Ludwigfr | 22:ebb37a249b5f | 504 | //returns the new log value |
Ludwigfr | 22:ebb37a249b5f | 505 | float compute_log_estimation_lt(float previousLogValue,float currentProbability,float originalLogvalue ){ |
Ludwigfr | 22:ebb37a249b5f | 506 | return previousLogValue+proba_to_log(currentProbability)-originalLogvalue; |
Ludwigfr | 22:ebb37a249b5f | 507 | } |
Ludwigfr | 22:ebb37a249b5f | 508 | |
Ludwigfr | 22:ebb37a249b5f | 509 | //makes the angle inAngle between 0 and 2pi |
Ludwigfr | 22:ebb37a249b5f | 510 | float rad_angle_check(float inAngle){ |
Ludwigfr | 22:ebb37a249b5f | 511 | //cout<<"before :"<<inAngle; |
Ludwigfr | 22:ebb37a249b5f | 512 | if(inAngle > 0){ |
Ludwigfr | 22:ebb37a249b5f | 513 | while(inAngle > (2*pi)) |
Ludwigfr | 22:ebb37a249b5f | 514 | inAngle-=2*pi; |
Ludwigfr | 22:ebb37a249b5f | 515 | }else{ |
Ludwigfr | 22:ebb37a249b5f | 516 | while(inAngle < 0) |
Ludwigfr | 22:ebb37a249b5f | 517 | inAngle+=2*pi; |
Ludwigfr | 22:ebb37a249b5f | 518 | } |
Ludwigfr | 22:ebb37a249b5f | 519 | //cout<<" after :"<<inAngle<<endl; |
Ludwigfr | 22:ebb37a249b5f | 520 | return inAngle; |
Ludwigfr | 22:ebb37a249b5f | 521 | } |
Ludwigfr | 22:ebb37a249b5f | 522 | |
Ludwigfr | 22:ebb37a249b5f | 523 | //returns the angle between the vectors (x,y) and (xs,ys) |
Ludwigfr | 22:ebb37a249b5f | 524 | float compute_angle_between_vectors(float x, float y,float xs,float ys){ |
Ludwigfr | 22:ebb37a249b5f | 525 | //alpha angle between ->x and ->SA |
Ludwigfr | 22:ebb37a249b5f | 526 | //vector S to A ->SA |
Ludwigfr | 22:ebb37a249b5f | 527 | float vSAx=x-xs; |
Ludwigfr | 22:ebb37a249b5f | 528 | float vSAy=y-ys; |
Ludwigfr | 22:ebb37a249b5f | 529 | //norme SA |
Ludwigfr | 22:ebb37a249b5f | 530 | float normeSA=sqrt(pow(vSAx,2)+pow(vSAy,2)); |
Ludwigfr | 22:ebb37a249b5f | 531 | //vector ->x (1,0) |
Ludwigfr | 22:ebb37a249b5f | 532 | float cosAlpha=1*vSAy/*+0*vSAx*//normeSA;; |
Ludwigfr | 22:ebb37a249b5f | 533 | //vector ->y (0,1) |
Ludwigfr | 22:ebb37a249b5f | 534 | float sinAlpha=/*0*vSAy+*/1*vSAx/normeSA;//+0*vSAx; |
Ludwigfr | 22:ebb37a249b5f | 535 | if (sinAlpha < 0) |
Ludwigfr | 22:ebb37a249b5f | 536 | return -acos(cosAlpha); |
Ludwigfr | 22:ebb37a249b5f | 537 | else |
Ludwigfr | 22:ebb37a249b5f | 538 | return acos(cosAlpha); |
Ludwigfr | 25:572c9e9a8809 | 539 | } |
Ludwigfr | 38:3c9f8cbf5250 | 540 | /* |
Ludwigfr | 25:572c9e9a8809 | 541 | |
Ludwigfr | 38:3c9f8cbf5250 | 542 | |
Ludwigfr | 44:e2bd06f94dc0 | 543 | Robot space: orthonormal space: |
Ludwigfr | 38:3c9f8cbf5250 | 544 | ^ ^ |
Ludwigfr | 38:3c9f8cbf5250 | 545 | |x |y |
Ludwigfr | 38:3c9f8cbf5250 | 546 | <- R O -> |
Ludwigfr | 38:3c9f8cbf5250 | 547 | y x |
Ludwigfr | 38:3c9f8cbf5250 | 548 | */ |
Ludwigfr | 38:3c9f8cbf5250 | 549 | //Odometria must bu up to date |
Ludwigfr | 36:c806c568720a | 550 | float x_robot_in_orthonormal_x(float x, float y){ |
Ludwigfr | 38:3c9f8cbf5250 | 551 | return robot_center_x_in_orthonormal_x()-y; |
Ludwigfr | 36:c806c568720a | 552 | } |
Ludwigfr | 36:c806c568720a | 553 | |
Ludwigfr | 38:3c9f8cbf5250 | 554 | //Odometria must bu up to date |
Ludwigfr | 36:c806c568720a | 555 | float y_robot_in_orthonormal_y(float x, float y){ |
Ludwigfr | 38:3c9f8cbf5250 | 556 | return robot_center_y_in_orthonormal_y()+x; |
Ludwigfr | 36:c806c568720a | 557 | } |
Ludwigfr | 36:c806c568720a | 558 | |
Ludwigfr | 25:572c9e9a8809 | 559 | float robot_center_x_in_orthonormal_x(){ |
Ludwigfr | 27:07bde633af72 | 560 | return NB_CELL_WIDTH*sizeCellWidth-Y; |
Ludwigfr | 25:572c9e9a8809 | 561 | } |
Ludwigfr | 25:572c9e9a8809 | 562 | |
Ludwigfr | 25:572c9e9a8809 | 563 | float robot_center_y_in_orthonormal_y(){ |
Ludwigfr | 27:07bde633af72 | 564 | return X; |
Ludwigfr | 25:572c9e9a8809 | 565 | } |
Ludwigfr | 25:572c9e9a8809 | 566 | |
Ludwigfr | 25:572c9e9a8809 | 567 | float robot_sonar_front_x_in_orthonormal_x(){ |
Ludwigfr | 27:07bde633af72 | 568 | return robot_center_x_in_orthonormal_x()+DISTANCE_SONAR_FRONT_X; |
Ludwigfr | 25:572c9e9a8809 | 569 | } |
Ludwigfr | 25:572c9e9a8809 | 570 | float robot_sonar_front_y_in_orthonormal_y(){ |
Ludwigfr | 27:07bde633af72 | 571 | return robot_center_y_in_orthonormal_y()+DISTANCE_SONAR_FRONT_Y; |
Ludwigfr | 25:572c9e9a8809 | 572 | } |
Ludwigfr | 25:572c9e9a8809 | 573 | |
Ludwigfr | 25:572c9e9a8809 | 574 | float robot_sonar_right_x_in_orthonormal_x(){ |
Ludwigfr | 27:07bde633af72 | 575 | return robot_center_x_in_orthonormal_x()+DISTANCE_SONAR_RIGHT_X; |
Ludwigfr | 25:572c9e9a8809 | 576 | } |
Ludwigfr | 25:572c9e9a8809 | 577 | float robot_sonar_right_y_in_orthonormal_y(){ |
Ludwigfr | 27:07bde633af72 | 578 | return robot_center_y_in_orthonormal_y()+DISTANCE_SONAR_RIGHT_Y; |
Ludwigfr | 25:572c9e9a8809 | 579 | } |
Ludwigfr | 25:572c9e9a8809 | 580 | |
Ludwigfr | 25:572c9e9a8809 | 581 | float robot_sonar_left_x_in_orthonormal_x(){ |
Ludwigfr | 27:07bde633af72 | 582 | return robot_center_x_in_orthonormal_x()+DISTANCE_SONAR_LEFT_X; |
Ludwigfr | 25:572c9e9a8809 | 583 | } |
Ludwigfr | 25:572c9e9a8809 | 584 | float robot_sonar_left_y_in_orthonormal_y(){ |
Ludwigfr | 27:07bde633af72 | 585 | return robot_center_y_in_orthonormal_y()+DISTANCE_SONAR_LEFT_Y; |
Ludwigfr | 25:572c9e9a8809 | 586 | } |
Ludwigfr | 25:572c9e9a8809 | 587 | |
Ludwigfr | 25:572c9e9a8809 | 588 | float estimated_width_indice_in_orthonormal_x(int i){ |
Ludwigfr | 27:07bde633af72 | 589 | return sizeCellWidth/2+i*sizeCellWidth; |
Ludwigfr | 25:572c9e9a8809 | 590 | } |
Ludwigfr | 25:572c9e9a8809 | 591 | float estimated_height_indice_in_orthonormal_y(int j){ |
Ludwigfr | 27:07bde633af72 | 592 | return sizeCellHeight/2+j*sizeCellHeight; |
geotsam | 26:b020cf253059 | 593 | } |
geotsam | 26:b020cf253059 | 594 | |
Ludwigfr | 42:ab25bffdc32b | 595 | //update angleError,distanceFromTarget,d2, beta |
Ludwigfr | 42:ab25bffdc32b | 596 | void compute_angles_and_distance(float target_x, float target_y, float target_angle,float dt,float* angleError,float* distanceFromTarget,float* d2,float* beta){ |
Ludwigfr | 42:ab25bffdc32b | 597 | *angleError = atan2((target_y-Y),(target_x-X))-theta; |
Ludwigfr | 42:ab25bffdc32b | 598 | *angleError = atan(sin(*angleError)/cos(*angleError)); |
Ludwigfr | 43:ffd5a6d4dd48 | 599 | *distanceFromTarget = dist(robot_center_x_in_orthonormal_x(),robot_center_y_in_orthonormal_y(),targetXOrhto,targetYOrhto); |
Ludwigfr | 42:ab25bffdc32b | 600 | *d2 = *distanceFromTarget; |
Ludwigfr | 42:ab25bffdc32b | 601 | *beta = -*angleError-theta+target_angle; |
geotsam | 26:b020cf253059 | 602 | |
geotsam | 26:b020cf253059 | 603 | //Computing angle error and distance towards the target value |
Ludwigfr | 42:ab25bffdc32b | 604 | *distanceFromTarget += dt*(-KRHO*cos(*angleError)**distanceFromTarget); |
Ludwigfr | 42:ab25bffdc32b | 605 | float temp = *angleError; |
Ludwigfr | 42:ab25bffdc32b | 606 | *angleError += dt*(KRHO*sin(*angleError)-KA**angleError-KB**beta); |
Ludwigfr | 42:ab25bffdc32b | 607 | *beta += dt*(-KRHO*sin(temp)); |
Ludwigfr | 27:07bde633af72 | 608 | //pc.printf("\n\r d2=%f", d2); |
Ludwigfr | 27:07bde633af72 | 609 | //pc.printf("\n\r dt=%f", dt); |
geotsam | 26:b020cf253059 | 610 | } |
geotsam | 26:b020cf253059 | 611 | |
Ludwigfr | 42:ab25bffdc32b | 612 | //update angularLeft and angularRight |
Ludwigfr | 42:ab25bffdc32b | 613 | void compute_linear_angular_velocities(float angleError,float distanceFromTarget,float beta,float* angularLeft, float* angularRight){ |
geotsam | 26:b020cf253059 | 614 | //Computing linear and angular velocities |
Ludwigfr | 42:ab25bffdc32b | 615 | float linear; |
Ludwigfr | 42:ab25bffdc32b | 616 | float angular; |
Ludwigfr | 42:ab25bffdc32b | 617 | if(angleError>=-1.5708 && angleError<=1.5708){ |
Ludwigfr | 42:ab25bffdc32b | 618 | linear=KRHO*distanceFromTarget; |
Ludwigfr | 42:ab25bffdc32b | 619 | angular=KA*angleError+KB*beta; |
geotsam | 26:b020cf253059 | 620 | } |
geotsam | 26:b020cf253059 | 621 | else{ |
Ludwigfr | 42:ab25bffdc32b | 622 | linear=-KRHO*distanceFromTarget; |
Ludwigfr | 42:ab25bffdc32b | 623 | angular=-KA*angleError-KB*beta; |
geotsam | 26:b020cf253059 | 624 | } |
Ludwigfr | 43:ffd5a6d4dd48 | 625 | //TODO check those signs |
Ludwigfr | 42:ab25bffdc32b | 626 | *angularLeft=(linear-0.5*DISTANCE_WHEELS*angular)/RADIUS_WHEELS; |
Ludwigfr | 42:ab25bffdc32b | 627 | *angularRight=(linear+0.5*DISTANCE_WHEELS*angular)/RADIUS_WHEELS; |
Ludwigfr | 43:ffd5a6d4dd48 | 628 | |
Ludwigfr | 43:ffd5a6d4dd48 | 629 | float aL=*angularLeft; |
Ludwigfr | 43:ffd5a6d4dd48 | 630 | float aR=*angularRight; |
Ludwigfr | 43:ffd5a6d4dd48 | 631 | //Normalize speed for motors |
Ludwigfr | 43:ffd5a6d4dd48 | 632 | if(abs(*angularLeft)>abs(*angularRight)) { |
Ludwigfr | 43:ffd5a6d4dd48 | 633 | *angularRight=MAX_SPEED*abs(aR/aL)*sign1(aR); |
Ludwigfr | 43:ffd5a6d4dd48 | 634 | *angularLeft=MAX_SPEED*sign1(aL); |
Ludwigfr | 43:ffd5a6d4dd48 | 635 | } |
Ludwigfr | 43:ffd5a6d4dd48 | 636 | else { |
Ludwigfr | 43:ffd5a6d4dd48 | 637 | *angularLeft=MAX_SPEED*abs(aL/aR)*sign1(aL); |
Ludwigfr | 43:ffd5a6d4dd48 | 638 | *angularRight=MAX_SPEED*sign1(aR); |
Ludwigfr | 43:ffd5a6d4dd48 | 639 | } |
Ludwigfr | 32:d51928b58645 | 640 | } |
Ludwigfr | 32:d51928b58645 | 641 | |
Ludwigfr | 43:ffd5a6d4dd48 | 642 | void update_force(int widthIndice, int heightIndice, float range, float* forceX, float* forceY, float xRobotOrtho, float yRobotOrtho ){ |
Ludwigfr | 44:e2bd06f94dc0 | 643 | //get the coordonate of the map and the robot in the ortonormal space |
Ludwigfr | 32:d51928b58645 | 644 | float xCenterCell=estimated_width_indice_in_orthonormal_x(widthIndice); |
Ludwigfr | 32:d51928b58645 | 645 | float yCenterCell=estimated_height_indice_in_orthonormal_y(heightIndice); |
Ludwigfr | 32:d51928b58645 | 646 | //compute the distance beetween the cell and the robot |
Ludwigfr | 32:d51928b58645 | 647 | float distanceCellToRobot=sqrt(pow(xCenterCell-xRobotOrtho,2)+pow(yCenterCell-yRobotOrtho,2)); |
Ludwigfr | 32:d51928b58645 | 648 | //check if the cell is in range |
Ludwigfr | 43:ffd5a6d4dd48 | 649 | if(distanceCellToRobot <= range) { |
Ludwigfr | 32:d51928b58645 | 650 | float probaCell=log_to_proba(map[widthIndice][heightIndice]); |
Ludwigfr | 32:d51928b58645 | 651 | float xForceComputed=FORCE_CONSTANT_REPULSION*probaCell*(xCenterCell-xRobotOrtho)/pow(distanceCellToRobot,3); |
Ludwigfr | 32:d51928b58645 | 652 | float yForceComputed=FORCE_CONSTANT_REPULSION*probaCell*(yCenterCell-yRobotOrtho)/pow(distanceCellToRobot,3); |
Ludwigfr | 32:d51928b58645 | 653 | *forceX+=xForceComputed; |
Ludwigfr | 32:d51928b58645 | 654 | *forceY+=yForceComputed; |
Ludwigfr | 32:d51928b58645 | 655 | } |
Ludwigfr | 32:d51928b58645 | 656 | } |
Ludwigfr | 32:d51928b58645 | 657 | |
Ludwigfr | 38:3c9f8cbf5250 | 658 | //compute the force on X and Y |
Ludwigfr | 39:dd8326ec75ce | 659 | void compute_forceX_and_forceY(float* forceX, float* forceY){ |
Ludwigfr | 44:e2bd06f94dc0 | 660 | //we put the position of the robot in an orthonormal space |
Ludwigfr | 32:d51928b58645 | 661 | float xRobotOrtho=robot_center_x_in_orthonormal_x(); |
Ludwigfr | 32:d51928b58645 | 662 | float yRobotOrtho=robot_center_y_in_orthonormal_y(); |
Ludwigfr | 39:dd8326ec75ce | 663 | |
geotsam | 37:462d19bb221f | 664 | float forceRepulsionComputedX=0; |
geotsam | 37:462d19bb221f | 665 | float forceRepulsionComputedY=0; |
Ludwigfr | 38:3c9f8cbf5250 | 666 | //for each cell of the map we compute a force of repulsion |
Ludwigfr | 32:d51928b58645 | 667 | for(int i=0;i<NB_CELL_WIDTH;i++){ |
Ludwigfr | 32:d51928b58645 | 668 | for(int j=0;j<NB_CELL_HEIGHT;j++){ |
Ludwigfr | 43:ffd5a6d4dd48 | 669 | update_force(i,j,RANGE_FORCE,&forceRepulsionComputedX,&forceRepulsionComputedY,xRobotOrtho,yRobotOrtho); |
Ludwigfr | 32:d51928b58645 | 670 | } |
Ludwigfr | 32:d51928b58645 | 671 | } |
geotsam | 34:128fc7aed957 | 672 | //update with attraction force |
geotsam | 37:462d19bb221f | 673 | *forceX=-forceRepulsionComputedX; |
geotsam | 37:462d19bb221f | 674 | *forceY=-forceRepulsionComputedY; |
Ludwigfr | 43:ffd5a6d4dd48 | 675 | float distanceTargetRobot=sqrt(pow(targetXOrhto-xRobotOrtho,2)+pow(targetYOrhto-yRobotOrtho,2)); |
Ludwigfr | 40:f5e212d9f900 | 676 | if(distanceTargetRobot != 0){ |
Ludwigfr | 43:ffd5a6d4dd48 | 677 | *forceX+=FORCE_CONSTANT_ATTRACTION*(targetXOrhto-xRobotOrtho)/distanceTargetRobot; |
Ludwigfr | 43:ffd5a6d4dd48 | 678 | *forceY+=FORCE_CONSTANT_ATTRACTION*(targetYOrhto-yRobotOrtho)/distanceTargetRobot; |
Ludwigfr | 40:f5e212d9f900 | 679 | } |
geotsam | 34:128fc7aed957 | 680 | float amplitude=sqrt(pow(*forceX,2)+pow(*forceY,2)); |
Ludwigfr | 40:f5e212d9f900 | 681 | if(amplitude!=0){//avoid division by 0 if forceX and forceY == 0 |
Ludwigfr | 36:c806c568720a | 682 | *forceX=*forceX/amplitude; |
Ludwigfr | 36:c806c568720a | 683 | *forceY=*forceY/amplitude; |
Ludwigfr | 36:c806c568720a | 684 | } |
geotsam | 33:78139f82ea74 | 685 | } |
geotsam | 33:78139f82ea74 | 686 | |
Ludwigfr | 43:ffd5a6d4dd48 | 687 | void test_got_to_line(bool* reached){ |
Ludwigfr | 43:ffd5a6d4dd48 | 688 | float line_a=1; |
Ludwigfr | 43:ffd5a6d4dd48 | 689 | float line_b=2; |
Ludwigfr | 43:ffd5a6d4dd48 | 690 | float line_c=-140; |
Ludwigfr | 43:ffd5a6d4dd48 | 691 | //we update the odometrie |
Ludwigfr | 43:ffd5a6d4dd48 | 692 | Odometria(); |
Ludwigfr | 43:ffd5a6d4dd48 | 693 | float angularRight=0; |
Ludwigfr | 43:ffd5a6d4dd48 | 694 | float angularLeft=0; |
Ludwigfr | 43:ffd5a6d4dd48 | 695 | |
Ludwigfr | 43:ffd5a6d4dd48 | 696 | go_to_line(&angularLeft,&angularRight,line_a,line_b,line_c); |
Ludwigfr | 43:ffd5a6d4dd48 | 697 | pc.printf("\r\n line: %f x + %f y + %f =0", line_a, line_b, line_c); |
Ludwigfr | 43:ffd5a6d4dd48 | 698 | |
Ludwigfr | 43:ffd5a6d4dd48 | 699 | leftMotor(sign2(angularLeft),abs(angularLeft)); |
Ludwigfr | 43:ffd5a6d4dd48 | 700 | rightMotor(sign2(angularRight),abs(angularRight)); |
Ludwigfr | 43:ffd5a6d4dd48 | 701 | |
Ludwigfr | 43:ffd5a6d4dd48 | 702 | pc.printf("\r\n dist=%f", dist(robot_center_x_in_orthonormal_x(),robot_center_y_in_orthonormal_y(),targetXOrhto,targetYOrhto)); |
Ludwigfr | 43:ffd5a6d4dd48 | 703 | |
Ludwigfr | 43:ffd5a6d4dd48 | 704 | //wait(0.1); |
Ludwigfr | 43:ffd5a6d4dd48 | 705 | Odometria(); |
Ludwigfr | 43:ffd5a6d4dd48 | 706 | if(dist(robot_center_x_in_orthonormal_x(),robot_center_y_in_orthonormal_y(),targetXOrhto,targetYOrhto)<10) |
Ludwigfr | 43:ffd5a6d4dd48 | 707 | *reached=true; |
Ludwigfr | 43:ffd5a6d4dd48 | 708 | } |
Ludwigfr | 42:ab25bffdc32b | 709 | void vff(bool* reached){ |
Ludwigfr | 42:ab25bffdc32b | 710 | float line_a; |
Ludwigfr | 42:ab25bffdc32b | 711 | float line_b; |
Ludwigfr | 42:ab25bffdc32b | 712 | float line_c; |
geotsam | 33:78139f82ea74 | 713 | //Updating X,Y and theta with the odometry values |
geotsam | 37:462d19bb221f | 714 | float forceX=0; |
geotsam | 37:462d19bb221f | 715 | float forceY=0; |
Ludwigfr | 38:3c9f8cbf5250 | 716 | //we update the odometrie |
geotsam | 33:78139f82ea74 | 717 | Odometria(); |
Ludwigfr | 38:3c9f8cbf5250 | 718 | //we check the sensors |
Ludwigfr | 40:f5e212d9f900 | 719 | float leftMm = get_distance_left_sensor(); |
Ludwigfr | 40:f5e212d9f900 | 720 | float frontMm = get_distance_front_sensor(); |
Ludwigfr | 40:f5e212d9f900 | 721 | float rightMm = get_distance_right_sensor(); |
Ludwigfr | 42:ab25bffdc32b | 722 | float angularRight=0; |
Ludwigfr | 42:ab25bffdc32b | 723 | float angularLeft=0; |
Ludwigfr | 38:3c9f8cbf5250 | 724 | //update the probabilities values |
geotsam | 33:78139f82ea74 | 725 | update_sonar_values(leftMm, frontMm, rightMm); |
Ludwigfr | 38:3c9f8cbf5250 | 726 | //we compute the force on X and Y |
Ludwigfr | 39:dd8326ec75ce | 727 | compute_forceX_and_forceY(&forceX, &forceY); |
Ludwigfr | 38:3c9f8cbf5250 | 728 | //we compute a new ine |
Ludwigfr | 42:ab25bffdc32b | 729 | calculate_line(forceX, forceY, X, Y,&line_a,&line_b,&line_c); |
Ludwigfr | 42:ab25bffdc32b | 730 | go_to_line(&angularLeft,&angularRight,line_a,line_b,line_c); |
geotsam | 33:78139f82ea74 | 731 | |
geotsam | 33:78139f82ea74 | 732 | //Updating motor velocities |
Ludwigfr | 43:ffd5a6d4dd48 | 733 | |
Ludwigfr | 43:ffd5a6d4dd48 | 734 | leftMotor(sign2(angularLeft),abs(angularLeft)); |
Ludwigfr | 43:ffd5a6d4dd48 | 735 | rightMotor(sign2(angularRight),abs(angularRight)); |
geotsam | 33:78139f82ea74 | 736 | |
Ludwigfr | 40:f5e212d9f900 | 737 | //wait(0.1); |
geotsam | 33:78139f82ea74 | 738 | Odometria(); |
Ludwigfr | 43:ffd5a6d4dd48 | 739 | if(dist(robot_center_x_in_orthonormal_x(),robot_center_y_in_orthonormal_y(),targetXOrhto,targetYOrhto)<10) |
Ludwigfr | 42:ab25bffdc32b | 740 | *reached=true; |
geotsam | 33:78139f82ea74 | 741 | } |
geotsam | 33:78139f82ea74 | 742 | |
Ludwigfr | 44:e2bd06f94dc0 | 743 | //return 1 if positiv, -1 if negativ |
Ludwigfr | 44:e2bd06f94dc0 | 744 | float sign1(float value){ |
Ludwigfr | 44:e2bd06f94dc0 | 745 | if(value>=0) |
Ludwigfr | 44:e2bd06f94dc0 | 746 | return 1; |
Ludwigfr | 44:e2bd06f94dc0 | 747 | else |
Ludwigfr | 44:e2bd06f94dc0 | 748 | return -1; |
Ludwigfr | 44:e2bd06f94dc0 | 749 | } |
Ludwigfr | 44:e2bd06f94dc0 | 750 | |
Ludwigfr | 44:e2bd06f94dc0 | 751 | //return 1 if positiv, 0 if negativ |
Ludwigfr | 44:e2bd06f94dc0 | 752 | int sign2(float value){ |
Ludwigfr | 44:e2bd06f94dc0 | 753 | if(value>=0) |
Ludwigfr | 44:e2bd06f94dc0 | 754 | return 1; |
Ludwigfr | 44:e2bd06f94dc0 | 755 | else |
Ludwigfr | 44:e2bd06f94dc0 | 756 | return 0; |
Ludwigfr | 44:e2bd06f94dc0 | 757 | } |
Ludwigfr | 44:e2bd06f94dc0 | 758 | |
Ludwigfr | 44:e2bd06f94dc0 | 759 | //currently line_c is not used |
Ludwigfr | 42:ab25bffdc32b | 760 | void go_to_line(float* angularLeft,float* angularRight,float line_a, float line_b, float line_c){ |
Ludwigfr | 42:ab25bffdc32b | 761 | float lineAngle; |
Ludwigfr | 42:ab25bffdc32b | 762 | float angleError; |
Ludwigfr | 42:ab25bffdc32b | 763 | float linear; |
Ludwigfr | 42:ab25bffdc32b | 764 | float angular; |
geotsam | 33:78139f82ea74 | 765 | if(line_b!=0){ |
Ludwigfr | 42:ab25bffdc32b | 766 | lineAngle=atan(-line_a/line_b); |
geotsam | 33:78139f82ea74 | 767 | } |
geotsam | 33:78139f82ea74 | 768 | else{ |
Ludwigfr | 42:ab25bffdc32b | 769 | lineAngle=1.5708; |
geotsam | 33:78139f82ea74 | 770 | } |
Ludwigfr | 43:ffd5a6d4dd48 | 771 | |
geotsam | 33:78139f82ea74 | 772 | //Computing angle error |
Ludwigfr | 42:ab25bffdc32b | 773 | angleError = lineAngle-theta; |
Ludwigfr | 42:ab25bffdc32b | 774 | angleError = atan(sin(angleError)/cos(angleError)); |
geotsam | 33:78139f82ea74 | 775 | |
geotsam | 33:78139f82ea74 | 776 | //Calculating velocities |
Ludwigfr | 42:ab25bffdc32b | 777 | linear=KV*(3.1416); |
Ludwigfr | 42:ab25bffdc32b | 778 | angular=KH*angleError; |
Ludwigfr | 43:ffd5a6d4dd48 | 779 | //TODO if we put it like the poly says it fails, if we switch the plus and minus it works ... |
Ludwigfr | 44:e2bd06f94dc0 | 780 | *angularLeft=(linear-0.5*DISTANCE_WHEELS*angular)/RADIUS_WHEELS; |
Ludwigfr | 44:e2bd06f94dc0 | 781 | *angularRight=(linear+0.5*DISTANCE_WHEELS*angular)/RADIUS_WHEELS; |
Ludwigfr | 43:ffd5a6d4dd48 | 782 | |
Ludwigfr | 43:ffd5a6d4dd48 | 783 | float aL=*angularLeft; |
Ludwigfr | 43:ffd5a6d4dd48 | 784 | float aR=*angularRight; |
Ludwigfr | 43:ffd5a6d4dd48 | 785 | //Normalize speed for motors |
Ludwigfr | 43:ffd5a6d4dd48 | 786 | if(abs(*angularLeft)>abs(*angularRight)) { |
Ludwigfr | 43:ffd5a6d4dd48 | 787 | *angularRight=MAX_SPEED*abs(aR/aL)*sign1(aR); |
Ludwigfr | 43:ffd5a6d4dd48 | 788 | *angularLeft=MAX_SPEED*sign1(aL); |
Ludwigfr | 43:ffd5a6d4dd48 | 789 | } |
Ludwigfr | 43:ffd5a6d4dd48 | 790 | else { |
Ludwigfr | 43:ffd5a6d4dd48 | 791 | *angularLeft=MAX_SPEED*abs(aL/aR)*sign1(aL); |
Ludwigfr | 43:ffd5a6d4dd48 | 792 | *angularRight=MAX_SPEED*sign1(aR); |
Ludwigfr | 43:ffd5a6d4dd48 | 793 | } |
Ludwigfr | 44:e2bd06f94dc0 | 794 | pc.printf("\r\n line: %f x + %f y + %f =0 , X=%f; Y=%f", line_a, line_b, line_c,robot_center_x_in_orthonormal_x(),robot_center_y_in_orthonormal_y()); |
Ludwigfr | 43:ffd5a6d4dd48 | 795 | } |
Ludwigfr | 43:ffd5a6d4dd48 | 796 | |
Ludwigfr | 44:e2bd06f94dc0 | 797 | //robotX and robotY are from Odometria, calculate line_a, line_b and line_c |
Ludwigfr | 44:e2bd06f94dc0 | 798 | void calculate_line(float forceX, float forceY, float robotX, float robotY,float *line_a, float *line_b, float *line_c){ |
Ludwigfr | 44:e2bd06f94dc0 | 799 | /* |
Ludwigfr | 44:e2bd06f94dc0 | 800 | in the teachers maths it is |
Ludwigfr | 44:e2bd06f94dc0 | 801 | |
Ludwigfr | 44:e2bd06f94dc0 | 802 | *line_a=forceY; |
Ludwigfr | 44:e2bd06f94dc0 | 803 | *line_b=-forceX; |
Ludwigfr | 44:e2bd06f94dc0 | 804 | |
Ludwigfr | 44:e2bd06f94dc0 | 805 | because a*x+b*y+c=0 |
Ludwigfr | 44:e2bd06f94dc0 | 806 | a impact the vertical and b the horizontal |
Ludwigfr | 44:e2bd06f94dc0 | 807 | and he has to put them like this because |
Ludwigfr | 44:e2bd06f94dc0 | 808 | Robot space: orthonormal space: |
Ludwigfr | 44:e2bd06f94dc0 | 809 | ^ ^ |
Ludwigfr | 44:e2bd06f94dc0 | 810 | |x |y |
Ludwigfr | 44:e2bd06f94dc0 | 811 | <- R O -> |
Ludwigfr | 44:e2bd06f94dc0 | 812 | y x |
Ludwigfr | 44:e2bd06f94dc0 | 813 | but since our forceX, forceY are already computed in the orthonormal space I m not sure we need to |
Ludwigfr | 44:e2bd06f94dc0 | 814 | */ |
Ludwigfr | 44:e2bd06f94dc0 | 815 | *line_a=forceX; |
Ludwigfr | 44:e2bd06f94dc0 | 816 | *line_b=forceY; |
Ludwigfr | 44:e2bd06f94dc0 | 817 | //TODO check that |
Ludwigfr | 44:e2bd06f94dc0 | 818 | //because the line computed always pass by the robot center we dont need lince_c |
Ludwigfr | 44:e2bd06f94dc0 | 819 | //float xRobotOrtho=robot_center_x_in_orthonormal_x(); |
Ludwigfr | 44:e2bd06f94dc0 | 820 | //float yRobotOrtho=robot_center_y_in_orthonormal_y(); |
Ludwigfr | 44:e2bd06f94dc0 | 821 | //*line_c=forceX*yRobotOrtho+forceY*xRobotOrtho; |
Ludwigfr | 44:e2bd06f94dc0 | 822 | *line_c=0; |
geotsam | 24:8f4b820d8de8 | 823 | } |