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