All the lab works are here!

Dependencies:   ISR_Mini-explorer mbed

Fork of VirtualForces by Georgios Tsamis

Committer:
Ludwigfr
Date:
Tue May 30 15:51:38 2017 +0000
Revision:
48:cd3463dcca04
Parent:
47:f0fe58571e4a
Child:
49:d61da2bc8882
kind of work

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 48:cd3463dcca04 357
Ludwigfr 48:cd3463dcca04 358 if(alphaBeforeAdjustment>pi)
Ludwigfr 48:cd3463dcca04 359 alphaBeforeAdjustment=alphaBeforeAdjustment-2*pi;
Ludwigfr 48:cd3463dcca04 360 if(alphaBeforeAdjustment<-pi)
Ludwigfr 48:cd3463dcca04 361 alphaBeforeAdjustment=alphaBeforeAdjustment+2*pi;
Ludwigfr 48:cd3463dcca04 362
Ludwigfr 48:cd3463dcca04 363 //float anglePointToSonar2=atan2(y-ys,x-xs)-theta;
Ludwigfr 48:cd3463dcca04 364
Ludwigfr 22:ebb37a249b5f 365 //check if the distance between the cell and the robot is within the circle of range RADIUS_WHEELS
Ludwigfr 22:ebb37a249b5f 366 //check if absolute difference between the angles is no more than Omega/2
Ludwigfr 42:ab25bffdc32b 367 if( distancePointToSonar < (RANGE_SONAR)&& (anglePointToSonar <= ANGLE_SONAR/2 || anglePointToSonar >= rad_angle_check(-ANGLE_SONAR/2))){
Ludwigfr 22:ebb37a249b5f 368 if( distancePointToSonar < (distanceObstacleDetected - INCERTITUDE_SONAR)){
Ludwigfr 22:ebb37a249b5f 369 //point before obstacle, probably empty
Ludwigfr 22:ebb37a249b5f 370 /*****************************************************************************/
Ludwigfr 22:ebb37a249b5f 371 float Ea=1.f-pow((2*alphaBeforeAdjustment)/ANGLE_SONAR,2);
Ludwigfr 22:ebb37a249b5f 372 float Er;
Ludwigfr 22:ebb37a249b5f 373 if(distancePointToSonar < RANGE_SONAR_MIN){
Ludwigfr 22:ebb37a249b5f 374 //point before minimum sonar range
Ludwigfr 22:ebb37a249b5f 375 Er=0.f;
Ludwigfr 22:ebb37a249b5f 376 }else{
Ludwigfr 22:ebb37a249b5f 377 //point after minimum sonar range
Ludwigfr 22:ebb37a249b5f 378 Er=1.f-pow((distancePointToSonar-RANGE_SONAR_MIN)/(distanceObstacleDetected-INCERTITUDE_SONAR-RANGE_SONAR_MIN),2);
Ludwigfr 22:ebb37a249b5f 379 }
Ludwigfr 22:ebb37a249b5f 380 /*****************************************************************************/
Ludwigfr 47:f0fe58571e4a 381 if((1.f-Er*Ea)/2.f >1 || (1.f-Er*Ea)/2.f < 0)
Ludwigfr 47:f0fe58571e4a 382 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 383 return (1.f-Er*Ea)/2.f;
Ludwigfr 22:ebb37a249b5f 384 }else{
Ludwigfr 22:ebb37a249b5f 385 //probably occupied
Ludwigfr 22:ebb37a249b5f 386 /*****************************************************************************/
Ludwigfr 22:ebb37a249b5f 387 float Oa=1.f-pow((2*alphaBeforeAdjustment)/ANGLE_SONAR,2);
Ludwigfr 22:ebb37a249b5f 388 float Or;
Ludwigfr 22:ebb37a249b5f 389 if( distancePointToSonar <= (distanceObstacleDetected + INCERTITUDE_SONAR)){
Ludwigfr 22:ebb37a249b5f 390 //point between distanceObstacleDetected +- INCERTITUDE_SONAR
Ludwigfr 22:ebb37a249b5f 391 Or=1-pow((distancePointToSonar-distanceObstacleDetected)/(INCERTITUDE_SONAR),2);
Ludwigfr 22:ebb37a249b5f 392 }else{
Ludwigfr 22:ebb37a249b5f 393 //point after in range of the sonar but after the zone detected
Ludwigfr 22:ebb37a249b5f 394 Or=0;
Ludwigfr 22:ebb37a249b5f 395 }
Ludwigfr 22:ebb37a249b5f 396 /*****************************************************************************/
Ludwigfr 47:f0fe58571e4a 397 if((1+Or*Oa)/2 >1 || (1+Or*Oa)/2 < 0)
Ludwigfr 47:f0fe58571e4a 398 pc.printf("\n\r return value=%f,Er=%f,Ea=%f,alphaBeforeAdjustment=%f",(1+Or*Oa)/2,Or,Oa,alphaBeforeAdjustment);
Ludwigfr 22:ebb37a249b5f 399 return (1+Or*Oa)/2;
Ludwigfr 22:ebb37a249b5f 400 }
Ludwigfr 22:ebb37a249b5f 401 }else{
Ludwigfr 25:572c9e9a8809 402 //not checked by the sonar
Ludwigfr 22:ebb37a249b5f 403 return 0.5;
AurelienBernier 19:dbc5fbad4975 404 }
Ludwigfr 22:ebb37a249b5f 405 }
Ludwigfr 22:ebb37a249b5f 406
Ludwigfr 25:572c9e9a8809 407 void print_final_map() {
Ludwigfr 22:ebb37a249b5f 408 float currProba;
geotsam 24:8f4b820d8de8 409 pc.printf("\n\r");
geotsam 24:8f4b820d8de8 410 for (int y = NB_CELL_HEIGHT -1; y>-1; y--) {
geotsam 24:8f4b820d8de8 411 for (int x= 0; x<NB_CELL_WIDTH; x++) {
geotsam 24:8f4b820d8de8 412 currProba=log_to_proba(map[x][y]);
geotsam 24:8f4b820d8de8 413 if ( currProba < 0.5) {
geotsam 29:224e9e686f7b 414 pc.printf(" ");
Ludwigfr 22:ebb37a249b5f 415 } else {
Ludwigfr 22:ebb37a249b5f 416 if(currProba==0.5)
geotsam 24:8f4b820d8de8 417 pc.printf(" . ");
Ludwigfr 22:ebb37a249b5f 418 else
geotsam 29:224e9e686f7b 419 pc.printf(" X ");
Ludwigfr 22:ebb37a249b5f 420 }
Ludwigfr 22:ebb37a249b5f 421 }
geotsam 24:8f4b820d8de8 422 pc.printf("\n\r");
Ludwigfr 22:ebb37a249b5f 423 }
Ludwigfr 22:ebb37a249b5f 424 }
Ludwigfr 22:ebb37a249b5f 425
Ludwigfr 25:572c9e9a8809 426 void print_final_map_with_robot_position() {
geotsam 24:8f4b820d8de8 427 float currProba;
Ludwigfr 25:572c9e9a8809 428 Odometria();
Ludwigfr 25:572c9e9a8809 429 float Xrobot=robot_center_x_in_orthonormal_x();
Ludwigfr 25:572c9e9a8809 430 float Yrobot=robot_center_y_in_orthonormal_y();
Ludwigfr 25:572c9e9a8809 431
Ludwigfr 25:572c9e9a8809 432 float heightIndiceInOrthonormal;
Ludwigfr 25:572c9e9a8809 433 float widthIndiceInOrthonormal;
Ludwigfr 25:572c9e9a8809 434
Ludwigfr 27:07bde633af72 435 float widthMalus=-(3*sizeCellWidth/2);
Ludwigfr 27:07bde633af72 436 float widthBonus=sizeCellWidth/2;
Ludwigfr 25:572c9e9a8809 437
Ludwigfr 27:07bde633af72 438 float heightMalus=-(3*sizeCellHeight/2);
Ludwigfr 27:07bde633af72 439 float heightBonus=sizeCellHeight/2;
Ludwigfr 25:572c9e9a8809 440
geotsam 24:8f4b820d8de8 441 pc.printf("\n\r");
geotsam 24:8f4b820d8de8 442 for (int y = NB_CELL_HEIGHT -1; y>-1; y--) {
geotsam 24:8f4b820d8de8 443 for (int x= 0; x<NB_CELL_WIDTH; x++) {
Ludwigfr 25:572c9e9a8809 444 heightIndiceInOrthonormal=estimated_height_indice_in_orthonormal_y(y);
Ludwigfr 25:572c9e9a8809 445 widthIndiceInOrthonormal=estimated_width_indice_in_orthonormal_x(x);
Ludwigfr 27:07bde633af72 446 if(Yrobot >= (heightIndiceInOrthonormal+heightMalus) && Yrobot <= (heightIndiceInOrthonormal+heightBonus) && Xrobot >= (widthIndiceInOrthonormal+widthMalus) && Xrobot <= (widthIndiceInOrthonormal+widthBonus))
Ludwigfr 27:07bde633af72 447 pc.printf(" R ");
Ludwigfr 25:572c9e9a8809 448 else{
Ludwigfr 25:572c9e9a8809 449 currProba=log_to_proba(map[x][y]);
Ludwigfr 25:572c9e9a8809 450 if ( currProba < 0.5)
geotsam 29:224e9e686f7b 451 pc.printf(" ");
Ludwigfr 25:572c9e9a8809 452 else{
Ludwigfr 25:572c9e9a8809 453 if(currProba==0.5)
Ludwigfr 27:07bde633af72 454 pc.printf(" . ");
Ludwigfr 25:572c9e9a8809 455 else
geotsam 29:224e9e686f7b 456 pc.printf(" X ");
Ludwigfr 25:572c9e9a8809 457 }
geotsam 24:8f4b820d8de8 458 }
geotsam 24:8f4b820d8de8 459 }
geotsam 24:8f4b820d8de8 460 pc.printf("\n\r");
geotsam 24:8f4b820d8de8 461 }
geotsam 24:8f4b820d8de8 462 }
Ludwigfr 22:ebb37a249b5f 463
Ludwigfr 39:dd8326ec75ce 464 void print_final_map_with_robot_position_and_target() {
Ludwigfr 39:dd8326ec75ce 465 float currProba;
Ludwigfr 39:dd8326ec75ce 466 Odometria();
Ludwigfr 39:dd8326ec75ce 467 float Xrobot=robot_center_x_in_orthonormal_x();
Ludwigfr 39:dd8326ec75ce 468 float Yrobot=robot_center_y_in_orthonormal_y();
Ludwigfr 39:dd8326ec75ce 469
Ludwigfr 39:dd8326ec75ce 470 float heightIndiceInOrthonormal;
Ludwigfr 39:dd8326ec75ce 471 float widthIndiceInOrthonormal;
Ludwigfr 39:dd8326ec75ce 472
Ludwigfr 39:dd8326ec75ce 473 float widthMalus=-(3*sizeCellWidth/2);
Ludwigfr 39:dd8326ec75ce 474 float widthBonus=sizeCellWidth/2;
Ludwigfr 39:dd8326ec75ce 475
Ludwigfr 39:dd8326ec75ce 476 float heightMalus=-(3*sizeCellHeight/2);
Ludwigfr 39:dd8326ec75ce 477 float heightBonus=sizeCellHeight/2;
Ludwigfr 39:dd8326ec75ce 478
Ludwigfr 39:dd8326ec75ce 479 pc.printf("\n\r");
Ludwigfr 39:dd8326ec75ce 480 for (int y = NB_CELL_HEIGHT -1; y>-1; y--) {
Ludwigfr 39:dd8326ec75ce 481 for (int x= 0; x<NB_CELL_WIDTH; x++) {
Ludwigfr 39:dd8326ec75ce 482 heightIndiceInOrthonormal=estimated_height_indice_in_orthonormal_y(y);
Ludwigfr 39:dd8326ec75ce 483 widthIndiceInOrthonormal=estimated_width_indice_in_orthonormal_x(x);
Ludwigfr 39:dd8326ec75ce 484 if(Yrobot >= (heightIndiceInOrthonormal+heightMalus) && Yrobot <= (heightIndiceInOrthonormal+heightBonus) && Xrobot >= (widthIndiceInOrthonormal+widthMalus) && Xrobot <= (widthIndiceInOrthonormal+widthBonus))
Ludwigfr 39:dd8326ec75ce 485 pc.printf(" R ");
Ludwigfr 39:dd8326ec75ce 486 else{
Ludwigfr 43:ffd5a6d4dd48 487 if(targetYOrhto >= (heightIndiceInOrthonormal+heightMalus) && targetYOrhto <= (heightIndiceInOrthonormal+heightBonus) && targetXOrhto >= (widthIndiceInOrthonormal+widthMalus) && targetXOrhto <= (widthIndiceInOrthonormal+widthBonus))
Ludwigfr 39:dd8326ec75ce 488 pc.printf(" T ");
Ludwigfr 39:dd8326ec75ce 489 else{
Ludwigfr 39:dd8326ec75ce 490 currProba=log_to_proba(map[x][y]);
Ludwigfr 39:dd8326ec75ce 491 if ( currProba < 0.5)
Ludwigfr 39:dd8326ec75ce 492 pc.printf(" ");
Ludwigfr 39:dd8326ec75ce 493 else{
Ludwigfr 39:dd8326ec75ce 494 if(currProba==0.5)
Ludwigfr 39:dd8326ec75ce 495 pc.printf(" . ");
Ludwigfr 39:dd8326ec75ce 496 else
Ludwigfr 39:dd8326ec75ce 497 pc.printf(" X ");
Ludwigfr 39:dd8326ec75ce 498 }
Ludwigfr 39:dd8326ec75ce 499 }
Ludwigfr 39:dd8326ec75ce 500 }
Ludwigfr 39:dd8326ec75ce 501 }
Ludwigfr 39:dd8326ec75ce 502 pc.printf("\n\r");
Ludwigfr 39:dd8326ec75ce 503 }
Ludwigfr 39:dd8326ec75ce 504 }
Ludwigfr 39:dd8326ec75ce 505
Ludwigfr 22:ebb37a249b5f 506 //MATHS heavy functions
Ludwigfr 22:ebb37a249b5f 507 /**********************************************************************/
Ludwigfr 22:ebb37a249b5f 508 //Distance computation function
Ludwigfr 22:ebb37a249b5f 509 float dist(float robot_x, float robot_y, float target_x, float target_y){
Ludwigfr 22:ebb37a249b5f 510 return sqrt(pow(target_y-robot_y,2) + pow(target_x-robot_x,2));
Ludwigfr 22:ebb37a249b5f 511 }
Ludwigfr 22:ebb37a249b5f 512
geotsam 24:8f4b820d8de8 513 //returns the probability [0,1] that the cell is occupied from the log valAue lt
Ludwigfr 22:ebb37a249b5f 514 float log_to_proba(float lt){
Ludwigfr 22:ebb37a249b5f 515 return 1-1/(1+exp(lt));
Ludwigfr 22:ebb37a249b5f 516 }
Ludwigfr 22:ebb37a249b5f 517
Ludwigfr 22:ebb37a249b5f 518 //returns the log value that the cell is occupied from the probability value [0,1]
Ludwigfr 22:ebb37a249b5f 519 float proba_to_log(float p){
Ludwigfr 22:ebb37a249b5f 520 return log(p/(1-p));
Ludwigfr 22:ebb37a249b5f 521 }
Ludwigfr 22:ebb37a249b5f 522
Ludwigfr 22:ebb37a249b5f 523 //returns the new log value
Ludwigfr 22:ebb37a249b5f 524 float compute_log_estimation_lt(float previousLogValue,float currentProbability,float originalLogvalue ){
Ludwigfr 22:ebb37a249b5f 525 return previousLogValue+proba_to_log(currentProbability)-originalLogvalue;
Ludwigfr 22:ebb37a249b5f 526 }
Ludwigfr 22:ebb37a249b5f 527
Ludwigfr 22:ebb37a249b5f 528 //makes the angle inAngle between 0 and 2pi
Ludwigfr 22:ebb37a249b5f 529 float rad_angle_check(float inAngle){
Ludwigfr 22:ebb37a249b5f 530 //cout<<"before :"<<inAngle;
Ludwigfr 22:ebb37a249b5f 531 if(inAngle > 0){
Ludwigfr 22:ebb37a249b5f 532 while(inAngle > (2*pi))
Ludwigfr 22:ebb37a249b5f 533 inAngle-=2*pi;
Ludwigfr 22:ebb37a249b5f 534 }else{
Ludwigfr 22:ebb37a249b5f 535 while(inAngle < 0)
Ludwigfr 22:ebb37a249b5f 536 inAngle+=2*pi;
Ludwigfr 22:ebb37a249b5f 537 }
Ludwigfr 22:ebb37a249b5f 538 //cout<<" after :"<<inAngle<<endl;
Ludwigfr 22:ebb37a249b5f 539 return inAngle;
Ludwigfr 22:ebb37a249b5f 540 }
Ludwigfr 22:ebb37a249b5f 541
Ludwigfr 47:f0fe58571e4a 542 //
Ludwigfr 22:ebb37a249b5f 543 //returns the angle between the vectors (x,y) and (xs,ys)
Ludwigfr 22:ebb37a249b5f 544 float compute_angle_between_vectors(float x, float y,float xs,float ys){
Ludwigfr 22:ebb37a249b5f 545 //alpha angle between ->x and ->SA
Ludwigfr 22:ebb37a249b5f 546 //vector S to A ->SA
Ludwigfr 22:ebb37a249b5f 547 float vSAx=x-xs;
Ludwigfr 22:ebb37a249b5f 548 float vSAy=y-ys;
Ludwigfr 22:ebb37a249b5f 549 //norme SA
Ludwigfr 22:ebb37a249b5f 550 float normeSA=sqrt(pow(vSAx,2)+pow(vSAy,2));
Ludwigfr 22:ebb37a249b5f 551 //vector ->x (1,0)
Ludwigfr 22:ebb37a249b5f 552 float cosAlpha=1*vSAy/*+0*vSAx*//normeSA;;
Ludwigfr 22:ebb37a249b5f 553 //vector ->y (0,1)
Ludwigfr 22:ebb37a249b5f 554 float sinAlpha=/*0*vSAy+*/1*vSAx/normeSA;//+0*vSAx;
Ludwigfr 22:ebb37a249b5f 555 if (sinAlpha < 0)
Ludwigfr 22:ebb37a249b5f 556 return -acos(cosAlpha);
Ludwigfr 22:ebb37a249b5f 557 else
Ludwigfr 22:ebb37a249b5f 558 return acos(cosAlpha);
Ludwigfr 25:572c9e9a8809 559 }
Ludwigfr 38:3c9f8cbf5250 560 /*
Ludwigfr 25:572c9e9a8809 561
Ludwigfr 38:3c9f8cbf5250 562
Ludwigfr 44:e2bd06f94dc0 563 Robot space: orthonormal space:
Ludwigfr 38:3c9f8cbf5250 564 ^ ^
Ludwigfr 38:3c9f8cbf5250 565 |x |y
Ludwigfr 38:3c9f8cbf5250 566 <- R O ->
Ludwigfr 38:3c9f8cbf5250 567 y x
Ludwigfr 38:3c9f8cbf5250 568 */
Ludwigfr 38:3c9f8cbf5250 569 //Odometria must bu up to date
Ludwigfr 36:c806c568720a 570 float x_robot_in_orthonormal_x(float x, float y){
Ludwigfr 38:3c9f8cbf5250 571 return robot_center_x_in_orthonormal_x()-y;
Ludwigfr 36:c806c568720a 572 }
Ludwigfr 36:c806c568720a 573
Ludwigfr 38:3c9f8cbf5250 574 //Odometria must bu up to date
Ludwigfr 36:c806c568720a 575 float y_robot_in_orthonormal_y(float x, float y){
Ludwigfr 38:3c9f8cbf5250 576 return robot_center_y_in_orthonormal_y()+x;
Ludwigfr 36:c806c568720a 577 }
Ludwigfr 36:c806c568720a 578
Ludwigfr 25:572c9e9a8809 579 float robot_center_x_in_orthonormal_x(){
Ludwigfr 27:07bde633af72 580 return NB_CELL_WIDTH*sizeCellWidth-Y;
Ludwigfr 25:572c9e9a8809 581 }
Ludwigfr 25:572c9e9a8809 582
Ludwigfr 25:572c9e9a8809 583 float robot_center_y_in_orthonormal_y(){
Ludwigfr 27:07bde633af72 584 return X;
Ludwigfr 25:572c9e9a8809 585 }
Ludwigfr 25:572c9e9a8809 586
Ludwigfr 25:572c9e9a8809 587 float robot_sonar_front_x_in_orthonormal_x(){
Ludwigfr 27:07bde633af72 588 return robot_center_x_in_orthonormal_x()+DISTANCE_SONAR_FRONT_X;
Ludwigfr 25:572c9e9a8809 589 }
Ludwigfr 25:572c9e9a8809 590 float robot_sonar_front_y_in_orthonormal_y(){
Ludwigfr 27:07bde633af72 591 return robot_center_y_in_orthonormal_y()+DISTANCE_SONAR_FRONT_Y;
Ludwigfr 25:572c9e9a8809 592 }
Ludwigfr 25:572c9e9a8809 593
Ludwigfr 25:572c9e9a8809 594 float robot_sonar_right_x_in_orthonormal_x(){
Ludwigfr 27:07bde633af72 595 return robot_center_x_in_orthonormal_x()+DISTANCE_SONAR_RIGHT_X;
Ludwigfr 25:572c9e9a8809 596 }
Ludwigfr 25:572c9e9a8809 597 float robot_sonar_right_y_in_orthonormal_y(){
Ludwigfr 27:07bde633af72 598 return robot_center_y_in_orthonormal_y()+DISTANCE_SONAR_RIGHT_Y;
Ludwigfr 25:572c9e9a8809 599 }
Ludwigfr 25:572c9e9a8809 600
Ludwigfr 25:572c9e9a8809 601 float robot_sonar_left_x_in_orthonormal_x(){
Ludwigfr 27:07bde633af72 602 return robot_center_x_in_orthonormal_x()+DISTANCE_SONAR_LEFT_X;
Ludwigfr 25:572c9e9a8809 603 }
Ludwigfr 25:572c9e9a8809 604 float robot_sonar_left_y_in_orthonormal_y(){
Ludwigfr 27:07bde633af72 605 return robot_center_y_in_orthonormal_y()+DISTANCE_SONAR_LEFT_Y;
Ludwigfr 25:572c9e9a8809 606 }
Ludwigfr 25:572c9e9a8809 607
Ludwigfr 25:572c9e9a8809 608 float estimated_width_indice_in_orthonormal_x(int i){
Ludwigfr 27:07bde633af72 609 return sizeCellWidth/2+i*sizeCellWidth;
Ludwigfr 25:572c9e9a8809 610 }
Ludwigfr 25:572c9e9a8809 611 float estimated_height_indice_in_orthonormal_y(int j){
Ludwigfr 27:07bde633af72 612 return sizeCellHeight/2+j*sizeCellHeight;
geotsam 26:b020cf253059 613 }
geotsam 26:b020cf253059 614
Ludwigfr 42:ab25bffdc32b 615 //update angleError,distanceFromTarget,d2, beta
Ludwigfr 42:ab25bffdc32b 616 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 617 *angleError = atan2((target_y-Y),(target_x-X))-theta;
Ludwigfr 42:ab25bffdc32b 618 *angleError = atan(sin(*angleError)/cos(*angleError));
Ludwigfr 43:ffd5a6d4dd48 619 *distanceFromTarget = dist(robot_center_x_in_orthonormal_x(),robot_center_y_in_orthonormal_y(),targetXOrhto,targetYOrhto);
Ludwigfr 42:ab25bffdc32b 620 *d2 = *distanceFromTarget;
Ludwigfr 42:ab25bffdc32b 621 *beta = -*angleError-theta+target_angle;
geotsam 26:b020cf253059 622
geotsam 26:b020cf253059 623 //Computing angle error and distance towards the target value
Ludwigfr 42:ab25bffdc32b 624 *distanceFromTarget += dt*(-KRHO*cos(*angleError)**distanceFromTarget);
Ludwigfr 42:ab25bffdc32b 625 float temp = *angleError;
Ludwigfr 42:ab25bffdc32b 626 *angleError += dt*(KRHO*sin(*angleError)-KA**angleError-KB**beta);
Ludwigfr 42:ab25bffdc32b 627 *beta += dt*(-KRHO*sin(temp));
Ludwigfr 27:07bde633af72 628 //pc.printf("\n\r d2=%f", d2);
Ludwigfr 27:07bde633af72 629 //pc.printf("\n\r dt=%f", dt);
geotsam 26:b020cf253059 630 }
geotsam 26:b020cf253059 631
Ludwigfr 42:ab25bffdc32b 632 //update angularLeft and angularRight
Ludwigfr 42:ab25bffdc32b 633 void compute_linear_angular_velocities(float angleError,float distanceFromTarget,float beta,float* angularLeft, float* angularRight){
geotsam 26:b020cf253059 634 //Computing linear and angular velocities
Ludwigfr 42:ab25bffdc32b 635 float linear;
Ludwigfr 42:ab25bffdc32b 636 float angular;
Ludwigfr 42:ab25bffdc32b 637 if(angleError>=-1.5708 && angleError<=1.5708){
Ludwigfr 42:ab25bffdc32b 638 linear=KRHO*distanceFromTarget;
Ludwigfr 42:ab25bffdc32b 639 angular=KA*angleError+KB*beta;
geotsam 26:b020cf253059 640 }
geotsam 26:b020cf253059 641 else{
Ludwigfr 42:ab25bffdc32b 642 linear=-KRHO*distanceFromTarget;
Ludwigfr 42:ab25bffdc32b 643 angular=-KA*angleError-KB*beta;
geotsam 26:b020cf253059 644 }
Ludwigfr 43:ffd5a6d4dd48 645 //TODO check those signs
Ludwigfr 42:ab25bffdc32b 646 *angularLeft=(linear-0.5*DISTANCE_WHEELS*angular)/RADIUS_WHEELS;
Ludwigfr 42:ab25bffdc32b 647 *angularRight=(linear+0.5*DISTANCE_WHEELS*angular)/RADIUS_WHEELS;
Ludwigfr 43:ffd5a6d4dd48 648
Ludwigfr 43:ffd5a6d4dd48 649 float aL=*angularLeft;
Ludwigfr 43:ffd5a6d4dd48 650 float aR=*angularRight;
Ludwigfr 43:ffd5a6d4dd48 651 //Normalize speed for motors
Ludwigfr 43:ffd5a6d4dd48 652 if(abs(*angularLeft)>abs(*angularRight)) {
Ludwigfr 43:ffd5a6d4dd48 653 *angularRight=MAX_SPEED*abs(aR/aL)*sign1(aR);
Ludwigfr 43:ffd5a6d4dd48 654 *angularLeft=MAX_SPEED*sign1(aL);
Ludwigfr 43:ffd5a6d4dd48 655 }
Ludwigfr 43:ffd5a6d4dd48 656 else {
Ludwigfr 43:ffd5a6d4dd48 657 *angularLeft=MAX_SPEED*abs(aL/aR)*sign1(aL);
Ludwigfr 43:ffd5a6d4dd48 658 *angularRight=MAX_SPEED*sign1(aR);
Ludwigfr 43:ffd5a6d4dd48 659 }
Ludwigfr 32:d51928b58645 660 }
Ludwigfr 32:d51928b58645 661
Ludwigfr 43:ffd5a6d4dd48 662 void update_force(int widthIndice, int heightIndice, float range, float* forceX, float* forceY, float xRobotOrtho, float yRobotOrtho ){
Ludwigfr 44:e2bd06f94dc0 663 //get the coordonate of the map and the robot in the ortonormal space
Ludwigfr 32:d51928b58645 664 float xCenterCell=estimated_width_indice_in_orthonormal_x(widthIndice);
Ludwigfr 32:d51928b58645 665 float yCenterCell=estimated_height_indice_in_orthonormal_y(heightIndice);
Ludwigfr 32:d51928b58645 666 //compute the distance beetween the cell and the robot
Ludwigfr 32:d51928b58645 667 float distanceCellToRobot=sqrt(pow(xCenterCell-xRobotOrtho,2)+pow(yCenterCell-yRobotOrtho,2));
Ludwigfr 32:d51928b58645 668 //check if the cell is in range
Ludwigfr 43:ffd5a6d4dd48 669 if(distanceCellToRobot <= range) {
Ludwigfr 32:d51928b58645 670 float probaCell=log_to_proba(map[widthIndice][heightIndice]);
Ludwigfr 32:d51928b58645 671 float xForceComputed=FORCE_CONSTANT_REPULSION*probaCell*(xCenterCell-xRobotOrtho)/pow(distanceCellToRobot,3);
Ludwigfr 32:d51928b58645 672 float yForceComputed=FORCE_CONSTANT_REPULSION*probaCell*(yCenterCell-yRobotOrtho)/pow(distanceCellToRobot,3);
Ludwigfr 32:d51928b58645 673 *forceX+=xForceComputed;
Ludwigfr 32:d51928b58645 674 *forceY+=yForceComputed;
Ludwigfr 32:d51928b58645 675 }
Ludwigfr 32:d51928b58645 676 }
Ludwigfr 32:d51928b58645 677
Ludwigfr 38:3c9f8cbf5250 678 //compute the force on X and Y
Ludwigfr 39:dd8326ec75ce 679 void compute_forceX_and_forceY(float* forceX, float* forceY){
Ludwigfr 44:e2bd06f94dc0 680 //we put the position of the robot in an orthonormal space
Ludwigfr 32:d51928b58645 681 float xRobotOrtho=robot_center_x_in_orthonormal_x();
Ludwigfr 32:d51928b58645 682 float yRobotOrtho=robot_center_y_in_orthonormal_y();
Ludwigfr 39:dd8326ec75ce 683
geotsam 37:462d19bb221f 684 float forceRepulsionComputedX=0;
geotsam 37:462d19bb221f 685 float forceRepulsionComputedY=0;
Ludwigfr 38:3c9f8cbf5250 686 //for each cell of the map we compute a force of repulsion
Ludwigfr 32:d51928b58645 687 for(int i=0;i<NB_CELL_WIDTH;i++){
Ludwigfr 32:d51928b58645 688 for(int j=0;j<NB_CELL_HEIGHT;j++){
Ludwigfr 43:ffd5a6d4dd48 689 update_force(i,j,RANGE_FORCE,&forceRepulsionComputedX,&forceRepulsionComputedY,xRobotOrtho,yRobotOrtho);
Ludwigfr 32:d51928b58645 690 }
Ludwigfr 32:d51928b58645 691 }
geotsam 34:128fc7aed957 692 //update with attraction force
geotsam 37:462d19bb221f 693 *forceX=-forceRepulsionComputedX;
geotsam 37:462d19bb221f 694 *forceY=-forceRepulsionComputedY;
Ludwigfr 43:ffd5a6d4dd48 695 float distanceTargetRobot=sqrt(pow(targetXOrhto-xRobotOrtho,2)+pow(targetYOrhto-yRobotOrtho,2));
Ludwigfr 40:f5e212d9f900 696 if(distanceTargetRobot != 0){
Ludwigfr 43:ffd5a6d4dd48 697 *forceX+=FORCE_CONSTANT_ATTRACTION*(targetXOrhto-xRobotOrtho)/distanceTargetRobot;
Ludwigfr 43:ffd5a6d4dd48 698 *forceY+=FORCE_CONSTANT_ATTRACTION*(targetYOrhto-yRobotOrtho)/distanceTargetRobot;
Ludwigfr 40:f5e212d9f900 699 }
geotsam 34:128fc7aed957 700 float amplitude=sqrt(pow(*forceX,2)+pow(*forceY,2));
Ludwigfr 40:f5e212d9f900 701 if(amplitude!=0){//avoid division by 0 if forceX and forceY == 0
Ludwigfr 36:c806c568720a 702 *forceX=*forceX/amplitude;
Ludwigfr 36:c806c568720a 703 *forceY=*forceY/amplitude;
Ludwigfr 36:c806c568720a 704 }
geotsam 33:78139f82ea74 705 }
geotsam 33:78139f82ea74 706
Ludwigfr 43:ffd5a6d4dd48 707 void test_got_to_line(bool* reached){
Ludwigfr 43:ffd5a6d4dd48 708 float line_a=1;
Ludwigfr 43:ffd5a6d4dd48 709 float line_b=2;
Ludwigfr 43:ffd5a6d4dd48 710 float line_c=-140;
Ludwigfr 43:ffd5a6d4dd48 711 //we update the odometrie
Ludwigfr 43:ffd5a6d4dd48 712 Odometria();
Ludwigfr 43:ffd5a6d4dd48 713 float angularRight=0;
Ludwigfr 43:ffd5a6d4dd48 714 float angularLeft=0;
Ludwigfr 43:ffd5a6d4dd48 715
Ludwigfr 43:ffd5a6d4dd48 716 go_to_line(&angularLeft,&angularRight,line_a,line_b,line_c);
Ludwigfr 48:cd3463dcca04 717 //pc.printf("\r\n line: %f x + %f y + %f =0", line_a, line_b, line_c);
Ludwigfr 43:ffd5a6d4dd48 718
Ludwigfr 43:ffd5a6d4dd48 719 leftMotor(sign2(angularLeft),abs(angularLeft));
Ludwigfr 43:ffd5a6d4dd48 720 rightMotor(sign2(angularRight),abs(angularRight));
Ludwigfr 43:ffd5a6d4dd48 721
Ludwigfr 43:ffd5a6d4dd48 722 pc.printf("\r\n dist=%f", dist(robot_center_x_in_orthonormal_x(),robot_center_y_in_orthonormal_y(),targetXOrhto,targetYOrhto));
Ludwigfr 43:ffd5a6d4dd48 723
Ludwigfr 43:ffd5a6d4dd48 724 //wait(0.1);
Ludwigfr 43:ffd5a6d4dd48 725 Odometria();
Ludwigfr 43:ffd5a6d4dd48 726 if(dist(robot_center_x_in_orthonormal_x(),robot_center_y_in_orthonormal_y(),targetXOrhto,targetYOrhto)<10)
Ludwigfr 43:ffd5a6d4dd48 727 *reached=true;
Ludwigfr 43:ffd5a6d4dd48 728 }
Ludwigfr 42:ab25bffdc32b 729 void vff(bool* reached){
Ludwigfr 42:ab25bffdc32b 730 float line_a;
Ludwigfr 42:ab25bffdc32b 731 float line_b;
Ludwigfr 42:ab25bffdc32b 732 float line_c;
geotsam 33:78139f82ea74 733 //Updating X,Y and theta with the odometry values
geotsam 37:462d19bb221f 734 float forceX=0;
geotsam 37:462d19bb221f 735 float forceY=0;
Ludwigfr 38:3c9f8cbf5250 736 //we update the odometrie
geotsam 33:78139f82ea74 737 Odometria();
Ludwigfr 38:3c9f8cbf5250 738 //we check the sensors
Ludwigfr 40:f5e212d9f900 739 float leftMm = get_distance_left_sensor();
Ludwigfr 40:f5e212d9f900 740 float frontMm = get_distance_front_sensor();
Ludwigfr 40:f5e212d9f900 741 float rightMm = get_distance_right_sensor();
Ludwigfr 42:ab25bffdc32b 742 float angularRight=0;
Ludwigfr 42:ab25bffdc32b 743 float angularLeft=0;
Ludwigfr 38:3c9f8cbf5250 744 //update the probabilities values
geotsam 33:78139f82ea74 745 update_sonar_values(leftMm, frontMm, rightMm);
Ludwigfr 38:3c9f8cbf5250 746 //we compute the force on X and Y
Ludwigfr 39:dd8326ec75ce 747 compute_forceX_and_forceY(&forceX, &forceY);
Ludwigfr 38:3c9f8cbf5250 748 //we compute a new ine
Ludwigfr 42:ab25bffdc32b 749 calculate_line(forceX, forceY, X, Y,&line_a,&line_b,&line_c);
Ludwigfr 42:ab25bffdc32b 750 go_to_line(&angularLeft,&angularRight,line_a,line_b,line_c);
geotsam 33:78139f82ea74 751
geotsam 33:78139f82ea74 752 //Updating motor velocities
Ludwigfr 43:ffd5a6d4dd48 753
Ludwigfr 43:ffd5a6d4dd48 754 leftMotor(sign2(angularLeft),abs(angularLeft));
Ludwigfr 43:ffd5a6d4dd48 755 rightMotor(sign2(angularRight),abs(angularRight));
geotsam 33:78139f82ea74 756
Ludwigfr 40:f5e212d9f900 757 //wait(0.1);
geotsam 33:78139f82ea74 758 Odometria();
Ludwigfr 43:ffd5a6d4dd48 759 if(dist(robot_center_x_in_orthonormal_x(),robot_center_y_in_orthonormal_y(),targetXOrhto,targetYOrhto)<10)
Ludwigfr 42:ab25bffdc32b 760 *reached=true;
geotsam 33:78139f82ea74 761 }
geotsam 33:78139f82ea74 762
Ludwigfr 44:e2bd06f94dc0 763 //return 1 if positiv, -1 if negativ
Ludwigfr 44:e2bd06f94dc0 764 float sign1(float value){
Ludwigfr 44:e2bd06f94dc0 765 if(value>=0)
Ludwigfr 44:e2bd06f94dc0 766 return 1;
Ludwigfr 44:e2bd06f94dc0 767 else
Ludwigfr 44:e2bd06f94dc0 768 return -1;
Ludwigfr 44:e2bd06f94dc0 769 }
Ludwigfr 44:e2bd06f94dc0 770
Ludwigfr 44:e2bd06f94dc0 771 //return 1 if positiv, 0 if negativ
Ludwigfr 44:e2bd06f94dc0 772 int sign2(float value){
Ludwigfr 44:e2bd06f94dc0 773 if(value>=0)
Ludwigfr 44:e2bd06f94dc0 774 return 1;
Ludwigfr 44:e2bd06f94dc0 775 else
Ludwigfr 44:e2bd06f94dc0 776 return 0;
Ludwigfr 44:e2bd06f94dc0 777 }
Ludwigfr 44:e2bd06f94dc0 778
Ludwigfr 44:e2bd06f94dc0 779 //currently line_c is not used
Ludwigfr 42:ab25bffdc32b 780 void go_to_line(float* angularLeft,float* angularRight,float line_a, float line_b, float line_c){
Ludwigfr 42:ab25bffdc32b 781 float lineAngle;
Ludwigfr 42:ab25bffdc32b 782 float angleError;
Ludwigfr 42:ab25bffdc32b 783 float linear;
Ludwigfr 42:ab25bffdc32b 784 float angular;
Ludwigfr 47:f0fe58571e4a 785
Ludwigfr 47:f0fe58571e4a 786 bool inFront=true;
Ludwigfr 47:f0fe58571e4a 787 //atan2 gives the angle beetween pi and -pi
Ludwigfr 47:f0fe58571e4a 788 float angleToTarget=atan2(targetYOrhtoNotMap,targetXOrhtoNotMap);
Ludwigfr 48:cd3463dcca04 789 //pc.printf("angleToTarget=%f",angleToTarget);
Ludwigfr 47:f0fe58571e4a 790 if(angleToTarget < 0)//the target is in front
Ludwigfr 47:f0fe58571e4a 791 inFront=false;
Ludwigfr 47:f0fe58571e4a 792
Ludwigfr 47:f0fe58571e4a 793 bool direction=true;
Ludwigfr 47:f0fe58571e4a 794
Ludwigfr 47:f0fe58571e4a 795 if(theta > 0){
Ludwigfr 47:f0fe58571e4a 796 //the robot is oriented to the left
Ludwigfr 47:f0fe58571e4a 797 if(theta > pi/2){
Ludwigfr 47:f0fe58571e4a 798 //the robot is oriented lower left
Ludwigfr 47:f0fe58571e4a 799 if(inFront)
Ludwigfr 47:f0fe58571e4a 800 direction=false;//robot and target not oriented the same way
Ludwigfr 47:f0fe58571e4a 801 }else{
Ludwigfr 47:f0fe58571e4a 802 //the robot is oriented upper left
Ludwigfr 47:f0fe58571e4a 803 if(!inFront)
Ludwigfr 47:f0fe58571e4a 804 direction=false;//robot and target not oriented the same way
Ludwigfr 47:f0fe58571e4a 805 }
Ludwigfr 47:f0fe58571e4a 806 }else{
Ludwigfr 47:f0fe58571e4a 807 //the robot is oriented to the right
Ludwigfr 47:f0fe58571e4a 808 if(theta < -pi/2){
Ludwigfr 47:f0fe58571e4a 809 //the robot is oriented lower right
Ludwigfr 47:f0fe58571e4a 810 if(inFront)
Ludwigfr 47:f0fe58571e4a 811 direction=false;//robot and target not oriented the same way
Ludwigfr 47:f0fe58571e4a 812 }else{
Ludwigfr 47:f0fe58571e4a 813 //the robot is oriented upper right
Ludwigfr 47:f0fe58571e4a 814 if(!inFront)
Ludwigfr 47:f0fe58571e4a 815 direction=false;//robot and target not oriented the same way
Ludwigfr 47:f0fe58571e4a 816 }
Ludwigfr 47:f0fe58571e4a 817 }
Ludwigfr 47:f0fe58571e4a 818
Ludwigfr 47:f0fe58571e4a 819 if(line_b!=0){
Ludwigfr 47:f0fe58571e4a 820 if(!direction)
Ludwigfr 47:f0fe58571e4a 821 lineAngle=atan(-line_a/line_b);
Ludwigfr 47:f0fe58571e4a 822 else
Ludwigfr 47:f0fe58571e4a 823 lineAngle=atan(line_a/-line_b);
Ludwigfr 47:f0fe58571e4a 824 }
Ludwigfr 47:f0fe58571e4a 825 else{
Ludwigfr 47:f0fe58571e4a 826 lineAngle=1.5708;
Ludwigfr 47:f0fe58571e4a 827 }
Ludwigfr 47:f0fe58571e4a 828
Ludwigfr 47:f0fe58571e4a 829 /*
geotsam 33:78139f82ea74 830 if(line_b!=0){
Ludwigfr 42:ab25bffdc32b 831 lineAngle=atan(-line_a/line_b);
geotsam 33:78139f82ea74 832 }
geotsam 33:78139f82ea74 833 else{
Ludwigfr 42:ab25bffdc32b 834 lineAngle=1.5708;
geotsam 33:78139f82ea74 835 }
Ludwigfr 47:f0fe58571e4a 836 */
geotsam 33:78139f82ea74 837 //Computing angle error
Ludwigfr 42:ab25bffdc32b 838 angleError = lineAngle-theta;
Ludwigfr 42:ab25bffdc32b 839 angleError = atan(sin(angleError)/cos(angleError));
geotsam 33:78139f82ea74 840
geotsam 33:78139f82ea74 841 //Calculating velocities
Ludwigfr 42:ab25bffdc32b 842 linear=KV*(3.1416);
Ludwigfr 42:ab25bffdc32b 843 angular=KH*angleError;
Ludwigfr 43:ffd5a6d4dd48 844 //TODO if we put it like the poly says it fails, if we switch the plus and minus it works ...
Ludwigfr 44:e2bd06f94dc0 845 *angularLeft=(linear-0.5*DISTANCE_WHEELS*angular)/RADIUS_WHEELS;
Ludwigfr 44:e2bd06f94dc0 846 *angularRight=(linear+0.5*DISTANCE_WHEELS*angular)/RADIUS_WHEELS;
Ludwigfr 43:ffd5a6d4dd48 847
Ludwigfr 43:ffd5a6d4dd48 848 float aL=*angularLeft;
Ludwigfr 43:ffd5a6d4dd48 849 float aR=*angularRight;
Ludwigfr 43:ffd5a6d4dd48 850 //Normalize speed for motors
Ludwigfr 43:ffd5a6d4dd48 851 if(abs(*angularLeft)>abs(*angularRight)) {
Ludwigfr 43:ffd5a6d4dd48 852 *angularRight=MAX_SPEED*abs(aR/aL)*sign1(aR);
Ludwigfr 43:ffd5a6d4dd48 853 *angularLeft=MAX_SPEED*sign1(aL);
Ludwigfr 43:ffd5a6d4dd48 854 }
Ludwigfr 43:ffd5a6d4dd48 855 else {
Ludwigfr 43:ffd5a6d4dd48 856 *angularLeft=MAX_SPEED*abs(aL/aR)*sign1(aL);
Ludwigfr 43:ffd5a6d4dd48 857 *angularRight=MAX_SPEED*sign1(aR);
Ludwigfr 43:ffd5a6d4dd48 858 }
Ludwigfr 44:e2bd06f94dc0 859 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 860 }
Ludwigfr 43:ffd5a6d4dd48 861
Ludwigfr 44:e2bd06f94dc0 862 //robotX and robotY are from Odometria, calculate line_a, line_b and line_c
Ludwigfr 44:e2bd06f94dc0 863 void calculate_line(float forceX, float forceY, float robotX, float robotY,float *line_a, float *line_b, float *line_c){
Ludwigfr 44:e2bd06f94dc0 864 /*
Ludwigfr 44:e2bd06f94dc0 865 in the teachers maths it is
Ludwigfr 44:e2bd06f94dc0 866
Ludwigfr 44:e2bd06f94dc0 867 *line_a=forceY;
Ludwigfr 44:e2bd06f94dc0 868 *line_b=-forceX;
Ludwigfr 44:e2bd06f94dc0 869
Ludwigfr 44:e2bd06f94dc0 870 because a*x+b*y+c=0
Ludwigfr 44:e2bd06f94dc0 871 a impact the vertical and b the horizontal
Ludwigfr 44:e2bd06f94dc0 872 and he has to put them like this because
Ludwigfr 44:e2bd06f94dc0 873 Robot space: orthonormal space:
Ludwigfr 44:e2bd06f94dc0 874 ^ ^
Ludwigfr 44:e2bd06f94dc0 875 |x |y
Ludwigfr 44:e2bd06f94dc0 876 <- R O ->
Ludwigfr 44:e2bd06f94dc0 877 y x
Ludwigfr 44:e2bd06f94dc0 878 but since our forceX, forceY are already computed in the orthonormal space I m not sure we need to
Ludwigfr 44:e2bd06f94dc0 879 */
Ludwigfr 44:e2bd06f94dc0 880 *line_a=forceX;
Ludwigfr 44:e2bd06f94dc0 881 *line_b=forceY;
Ludwigfr 44:e2bd06f94dc0 882 //TODO check that
Ludwigfr 44:e2bd06f94dc0 883 //because the line computed always pass by the robot center we dont need lince_c
Ludwigfr 44:e2bd06f94dc0 884 //float xRobotOrtho=robot_center_x_in_orthonormal_x();
Ludwigfr 44:e2bd06f94dc0 885 //float yRobotOrtho=robot_center_y_in_orthonormal_y();
Ludwigfr 44:e2bd06f94dc0 886 //*line_c=forceX*yRobotOrtho+forceY*xRobotOrtho;
Ludwigfr 44:e2bd06f94dc0 887 *line_c=0;
geotsam 24:8f4b820d8de8 888 }