ELEC2645 (2018/19) / Mbed 2 deprecated el16y2m

Dependencies:   mbed

Committer:
MYY
Date:
Thu May 09 03:28:55 2019 +0000
Revision:
12:777558372c67
Child:
17:9f7ff626210b
Test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MYY 12:777558372c67 1 #include"Detect.h"
MYY 12:777558372c67 2
MYY 12:777558372c67 3 /*
MYY 12:777558372c67 4 The idea behind Detect:
MYY 12:777558372c67 5
MYY 12:777558372c67 6 Due to the size of the screen, it will be too hard if we only say that the hook has hooked an object when the hook touches the object.
MYY 12:777558372c67 7 Instead we can draw a circle at hook point and another one at the centre of Gold/Rock. when this two circles are tangent with each other
MYY 12:777558372c67 8 we consider the hook has hooked a Gold/Rock. The radius for two circles are both 3.5.
MYY 12:777558372c67 9
MYY 12:777558372c67 10 After confirmation that the hook has hooked a Gold/Rock, the hookpoint will be assigned the value of the centre point
MYY 12:777558372c67 11 and we use function pull_back()(within Pull class) to draw a line between (42,0) and pull back the Gold/Rock.
MYY 12:777558372c67 12
MYY 12:777558372c67 13 level_1_x, level_1_y : Coordinates for gold/rock in level 1. Random coordinates will mess up with the difficulty of game.
MYY 12:777558372c67 14 level_1_X, level_1_Y : centre point for gold/rock which we will draw a circle with.
MYY 12:777558372c67 15 They are global variables stored in "Global_my"
MYY 12:777558372c67 16
MYY 12:777558372c67 17 This is same for level 2 and level 3.
MYY 12:777558372c67 18 */
MYY 12:777558372c67 19
MYY 12:777558372c67 20
MYY 12:777558372c67 21 int Detect::detect(Pull &Pull, N5110 &lcd, Image &Image, float angle, float distance_detect, InterruptIn &A, Tool &Tool, Rotate &Rotate)
MYY 12:777558372c67 22 {
MYY 12:777558372c67 23 for(int i=0; i<7; i=i+1) {//The number of rock and gold in level 1 altogether is 7.
MYY 12:777558372c67 24 float distance = lcd.calDistance(Rotate.hookpoint[0],Rotate.hookpoint[1],level_1_X[i],level_1_Y[i]);//calculate the distance between hookpoint and thecentre point.
MYY 12:777558372c67 25 if (distance < 7) {//if distance<7 which means two circles are tangent with each other.
MYY 12:777558372c67 26 float cos_value=(42-level_1_X[i])/lcd.calDistance(level_1_X[i],level_1_Y[i],42,0);//find out the cos value between the centre point and (42,0)
MYY 12:777558372c67 27 Rotate.hookpoint[0]= level_1_X[i],//let the hookpoint equals to the centre point
MYY 12:777558372c67 28 Rotate.hookpoint[1]= level_1_Y[i],
MYY 12:777558372c67 29 level_1_x[i]=100;//set the centre point and coordinates out of range so the Gold/Rock will not be drawed nor detected.
MYY 12:777558372c67 30 level_1_y[i]=100;
MYY 12:777558372c67 31 level_1_X[i]=100;
MYY 12:777558372c67 32 level_1_Y[i]=100;
MYY 12:777558372c67 33 angle = acos(cos_value);//use arccos function to calculate the angle bwtween the centre point and (42,0)
MYY 12:777558372c67 34 //printf("%f\n",angle);are used to test the angle
MYY 12:777558372c67 35 Pull.pull_back(lcd, Image, i, distance_detect, angle, A, Tool, Rotate);//pass the angle to pull_back function.
MYY 12:777558372c67 36 return 1;//if we detect a Gold/Rock and pull it back, we can return 1 so Release function before will break so we can go back to rotate stage again.
MYY 12:777558372c67 37 } else {
MYY 12:777558372c67 38 continue;
MYY 12:777558372c67 39 }
MYY 12:777558372c67 40 }
MYY 12:777558372c67 41 return 0;//if nothing is detected, the hook will keep going until it reached maximum length
MYY 12:777558372c67 42 }
MYY 12:777558372c67 43
MYY 12:777558372c67 44
MYY 12:777558372c67 45 // detect_2 and detect_3 use another set of coordinates and center point, draw different maps.
MYY 12:777558372c67 46 int Detect::detect_2(Pull &Pull, N5110 &lcd, Image &Image, float angle, float distance_detect, InterruptIn &A, Tool &Tool, Rotate &Rotate)
MYY 12:777558372c67 47 {
MYY 12:777558372c67 48 for(int i=0; i<10; i=i+1) {
MYY 12:777558372c67 49 float distance = lcd.calDistance(Rotate.hookpoint[0],Rotate.hookpoint[1],level_2_X[i],level_2_Y[i]);
MYY 12:777558372c67 50 if (distance < 7) {
MYY 12:777558372c67 51 float cos_value=(42-level_2_X[i])/lcd.calDistance(level_2_X[i],level_2_Y[i],42,0);
MYY 12:777558372c67 52 Rotate.hookpoint[0]= level_2_X[i],
MYY 12:777558372c67 53 Rotate.hookpoint[1]= level_2_Y[i],
MYY 12:777558372c67 54 level_2_x[i]=100;
MYY 12:777558372c67 55 level_2_y[i]=100;
MYY 12:777558372c67 56 level_2_X[i]=100;
MYY 12:777558372c67 57 level_2_Y[i]=100;
MYY 12:777558372c67 58 angle = acos(cos_value);
MYY 12:777558372c67 59 Pull.pull_back_2(lcd, Image, i, distance_detect, angle, A, Tool, Rotate);
MYY 12:777558372c67 60 return 1;
MYY 12:777558372c67 61 } else {
MYY 12:777558372c67 62 continue;
MYY 12:777558372c67 63 }
MYY 12:777558372c67 64 }
MYY 12:777558372c67 65 return 0;
MYY 12:777558372c67 66 }
MYY 12:777558372c67 67
MYY 12:777558372c67 68
MYY 12:777558372c67 69
MYY 12:777558372c67 70 int Detect::detect_3(Pull &Pull, N5110 &lcd, Image &Image, float angle, float distance_detect, InterruptIn &A, Tool &Tool, Rotate &Rotate)
MYY 12:777558372c67 71 {
MYY 12:777558372c67 72 for(int i=0; i<12; i=i+1) {
MYY 12:777558372c67 73 float distance = lcd.calDistance(Rotate.hookpoint[0],Rotate.hookpoint[1],level_3_X[i],level_3_Y[i]);
MYY 12:777558372c67 74 if (distance < 7) {
MYY 12:777558372c67 75 float cos_value=(42-level_3_X[i])/lcd.calDistance(level_3_X[i],level_3_Y[i],42,0);
MYY 12:777558372c67 76 Rotate.hookpoint[0]= level_3_X[i],
MYY 12:777558372c67 77 Rotate.hookpoint[1]= level_3_Y[i],
MYY 12:777558372c67 78 level_3_x[i]=100;
MYY 12:777558372c67 79 level_3_y[i]=100;
MYY 12:777558372c67 80 level_3_X[i]=100;
MYY 12:777558372c67 81 level_3_Y[i]=100;
MYY 12:777558372c67 82 angle = acos(cos_value);
MYY 12:777558372c67 83 Pull.pull_back_3(lcd, Image, i, distance_detect, angle, A, Tool, Rotate);
MYY 12:777558372c67 84 return 1;
MYY 12:777558372c67 85 } else {
MYY 12:777558372c67 86 continue;
MYY 12:777558372c67 87 }
MYY 12:777558372c67 88 }
MYY 12:777558372c67 89 return 0;
MYY 12:777558372c67 90 }