All the lab works are here!

Dependencies:   ISR_Mini-explorer mbed

Fork of VirtualForces by Georgios Tsamis

Committer:
Ludwigfr
Date:
Tue May 30 15:17:19 2017 +0000
Revision:
47:f0fe58571e4a
Parent:
45:fb07065a64a9
Child:
48:cd3463dcca04
added the direction stuff, trying to fix compute_proba

Who changed what in which revision?

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