Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma config(Sensor, S1, L, sensorEV3_Color)
- #pragma config(Sensor, S2, sonar, sensorEV3_Ultrasonic)
- #pragma config(Sensor, S3, R, sensorEV3_Color)
- #pragma config(Sensor, S4, GyroA, sensorEV3_Gyro)
- #pragma config(Motor, motorB, L, tmotorEV3_Large, PIDControl, encoder)
- #pragma config(Motor, motorC, R, tmotorEV3_Large, PIDControl, encoder)
- #pragma config(Motor, motorD, sonarM, tmotorEV3_Medium, PIDControl, encoder)
- //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
- #define max 9
- #define goal 5
- #define rDegree 88
- #define lDegree -89
- //north-0 east-90
- int thres = 27;
- typedef struct node{
- short x,y;
- short title;
- bool pass; //check aleady pass?
- bool n,e,w,s; //check goable direction
- bool left,center,right;
- short fromX,fromY; //
- bool known; //
- short distance; //
- }node;
- node table[max][max];
- short x,y;
- short run; //n-0 e-1 s-2 w-3
- short count;
- //short goal = 0;
- short countNode;
- int degree;
- /*init node for first run loop*/
- void init(){
- short k,j;
- for(k = 0;k<max;k++){
- for(j = 0;j<max;j++){
- table[k][j].title = k*max + j;
- //displayTextLine(5,"title : %d",table[k][j].title);
- //wait1Msec(1500);
- table[k][j].x = k;
- table[k][j].y = j;
- table[k][j].pass = false;
- table[k][j].left = false;
- table[k][j].center = false;
- table[k][j].right = false;
- table[k][j].n = false;
- table[k][j].e = false;
- table[k][j].w = false;
- table[k][j].s = false;
- table[k][j].fromX = -1;
- table[k][j].fromY = -1;
- table[k][j].known = false;
- table[k][j].distance = -1;
- }
- }
- x = 0;
- y = 0;
- count = 0;
- countNode = 0;
- resetGyro(GyroA);
- run = 0; //north
- degree = 0;
- /*while(true){
- goal = 2;
- displayTextLine(10,"goal = %d",goal);
- wait1Msec(2000);
- if(SensorValue(t) == 1) break;
- goal = 3;
- displayTextLine(10,"goal = %d",goal);
- wait1Msec(2000);
- if(SensorValue(t) == 1) break;
- goal = 4;
- displayTextLine(10,"goal = %d",goal);
- wait1Msec(2000);
- if(SensorValue(t) == 1) break;
- goal = 5;
- displayTextLine(10,"goal = %d",goal);
- wait1Msec(2000);
- if(SensorValue(t) == 1) break;
- }*/
- }
- void forward(){
- switch (run){
- case 0: y++; break;
- case 1: x++; break;
- case 2: y--; break;
- case 3: x--; break;
- }
- //walk until 1 of sensor see line
- while(1){
- //displayBigTextLine(1, "Reflected: %d", SensorValue[S1]);
- //displayBigTextLine(2, "Reflected: %d", SensorValue[S3]);
- if((SensorValue[S1] > thres )&&( SensorValue[S3] < thres)){
- //displayBigTextLine(5, "LEFT");
- motor[motorB]=-1;
- motor[motorC]=1;
- }
- else if((SensorValue[S1] < thres )&&( SensorValue[S3] > thres)){
- // displayBigTextLine(5, "RIGHT");
- motor[motorB]=1;
- motor[motorC]=-1;
- }
- else if((SensorValue[S1] < thres )&& (SensorValue[S3] < thres)){
- // displayBigTextLine(5, "forward");
- motor[motorB]=10;
- motor[motorC]=10;
- }
- else if((SensorValue[S1] > thres )&& (SensorValue[S3] > thres)){
- //displayBigTextLine(5, "break");
- break;
- }
- //if((SensorValue[S1] > thres) || (SensorValue[S3] > thres)) break;
- }
- motor[motorB] = 0;
- motor[motorC] = 0;
- moveMotorTarget(L,455,20);
- moveMotorTarget(R,455,20);
- waitUntilMotorStop(R);
- waitUntilMotorStop(L);
- //make two of sensor in line
- /*while(1){
- if((SensorValue[S1] > thres) && (SensorValue[S3] > thres)){
- displayTextLine(9,"sensor %d",SensorValue[S1]);
- displayTextLine(10,"sensor %d",SensorValue[S3]);
- //break;
- }
- else if((SensorValue[S1] > thres) && (SensorValue[S3] < thres)){
- motor(L) = 0;
- motor(R) = 1;
- }
- else if((SensorValue[S1] < thres )&&( SensorValue[S3] > thres)){
- motor(L) = 1;
- motor(R) = 0;
- }
- motor(R) = 0;
- motor(L) = 0;
- }
- setMotorSpeed(L,0);
- setMotorSpeed(R,0);
- return ;
- //walk out of yellow line with 90degree line
- while(1){
- if(SensorValue[S3] < thres && SensorValue[S1] < thres && -2 < SensorValue[S3] - SensorValue[S1] && SensorValue[S3] - SensorValue[S1] < 2){
- break;
- }
- else if(SensorValue[S3] < thres && SensorValue[S1] < thres){
- motor(L) = -1;
- motor(R) = -1;
- }
- else if(SensorValue[S1] > thres && SensorValue[S3] > thres){
- motor(L) = 1;
- motor(R) = 1;
- }
- else if(SensorValue[S1] > thres && SensorValue[S3] < thres){
- motor(L) = 1;
- motor(R) = -1;
- }
- else if(SensorValue[S1] < thres && SensorValue[S3] > thres){
- motor(L) = -1;
- motor(R) = 1;
- }
- }
- motor(L) = 0;
- motor(R) = 0;
- //walk half line
- */
- return ;
- /*
- moveMotorTarget(L,619,20.98);
- moveMotorTarget(R,619,20);
- waitUntilMotorStop(R);
- waitUntilMotorStop(L);
- /* int n = degree;
- repeatUntil(getGyroDegrees(GyroA) == n){
- if(getGyroDegrees(GyroA)<n){
- setMotorSpeed(L,5);
- setMotorSpeed(R,-5);
- }
- else{
- setMotorSpeed(L,-5);
- setMotorSpeed(R,5);
- }
- displayTextLine(1,"GyroValue = %d",getGyroDegrees(GyroA));
- }
- */
- setMotorSpeed(L,0);
- setMotorSpeed(R,0);
- }
- void right(){
- //displayTextLine(10, "Gyro = %d",getGyroDegrees(GyroA));
- //wait1Msec(3000);
- resetGyro(GyroA);
- int n = rDegree;
- /*repeatUntil(getGyroDegrees(GyroA) > n-1 /*&& getGyroDegrees(GyroA) < n+1){
- if(getGyroDegrees(GyroA)<n ){
- setMotorSpeed(L,5);
- setMotorSpeed(R,-5);
- }
- }*/
- repeatUntil(getGyroDegrees(GyroA) == n){
- if(getGyroDegrees(GyroA)<n){
- setMotorSpeed(L,5);
- setMotorSpeed(R,-5);
- }
- else{
- setMotorSpeed(L,-5);
- setMotorSpeed(R,5);
- }
- displayTextLine(1,"GyroValue = %d",getGyroDegrees(GyroA));
- }
- setMotorSpeed(L,0);
- setMotorSpeed(R,0);
- if(run == 3) run = 0; //change run to right direction
- else run++;
- degree = getGyroDegrees(GyroA);
- //displayTextLine(10, "Gyro = %d",getGyroDegrees(GyroA));
- //wait1Msec(3000);
- }
- void left(){
- resetGyro(GyroA);
- int n = lDegree;
- /*repeatUntil(getGyroDegrees(GyroA) <= n+1){
- if(getGyroDegrees(GyroA)>n ){
- setMotorSpeed(L,-5);
- setMotorSpeed(R,5);
- }
- }*/
- repeatUntil(getGyroDegrees(GyroA) == n){
- if(getGyroDegrees(GyroA)>n ){
- setMotorSpeed(L,-5);
- setMotorSpeed(R,5);
- }
- else{
- setMotorSpeed(L,5);
- setMotorSpeed(R,-5);
- }
- displayTextLine(1,"GyroValue = %d",getGyroDegrees(GyroA));
- }
- setMotorSpeed(L,0);
- setMotorSpeed(R,0);
- if(run == 0) run = 3; //change run to left direction
- else run--;
- degree = getGyroDegrees(GyroA);
- }
- void turnAround(){
- int n = getGyroDegrees(GyroA) + rDegree*2;
- /*repeatUntil(getGyroDegrees(GyroA) >= n-1){
- if(getGyroDegrees(GyroA)<n ){
- setMotorSpeed(L,5);
- setMotorSpeed(R,-5);
- }
- }*/
- repeatUntil(getGyroDegrees(GyroA) == n){
- if(getGyroDegrees(GyroA)<n){
- setMotorSpeed(L,5);
- setMotorSpeed(R,-5);
- }
- else{
- setMotorSpeed(L,-5);
- setMotorSpeed(R,5);
- }
- displayTextLine(1,"GyroValue = %d",getGyroDegrees(GyroA));
- }
- setMotorSpeed(L,0);
- setMotorSpeed(R,0);
- if(run == 3) run = 0; //change run to right direction
- else run++;
- if(run == 3) run = 0; //change run to right direction
- else run++;
- degree = getGyroDegrees(GyroA);
- }
- void turnDirection(short from,short to){
- displayTextLine(8, "from : %d , to : %d",from,to);
- if((from==0&&to==1)||(from==1&&to==2)||(from==2&&to==3)||(from==3&&to==0)){
- displayTextLine(9, "turn right");
- right();
- }
- else if((from==0&&to==3)||(from==1&&to==0)||(from==2&&to==1)||(from==3&&to==2)){
- displayTextLine(9, "turn left");
- left();
- }
- else if((from==0&&to==2)||(from==1&&to==3)||(from==2&&to==0)||(from==3&&to==1)){
- displayTextLine(9, "turn turnAround");
- turnAround();
- }
- }
- void checkWall(short thisX,short thisY){
- //turn right
- motor(sonarM) = 20;
- wait1Msec(350);
- motor(sonarM) = 0;
- /*
- switch (run){
- case 0: table[thisX][thisY].s = true; break;
- case 1: table[thisX][thisY].w = true; break;
- case 2: table[thisX][thisY].n = true; break;
- case 3: table[thisX][thisY].e = true;
- }*/
- if(SensorValue(sonar) > 20){
- displayTextLine(10, "sonar = %d",SensorValue(sonar));
- //wait1Msec(3000);
- table[thisX][thisY].right = true;
- switch (run){
- case 0: table[thisX][thisY].e = true; break;
- case 1: table[thisX][thisY].s = true; break;
- case 2: table[thisX][thisY].w = true; break;
- case 3: table[thisX][thisY].n = true;
- }
- }
- //turn center
- motor(sonarM) = -20;
- wait1Msec(350);
- motor(sonarM) = 0;
- if(SensorValue(sonar) > 20){
- displayTextLine(10, "sonar = %d",SensorValue(sonar));
- //wait1Msec(3000);
- table[thisX][thisY].center = true;
- switch (run){
- case 0: table[thisX][thisY].n = true; break;
- case 1: table[thisX][thisY].e = true; break;
- case 2: table[thisX][thisY].s = true; break;
- case 3: table[thisX][thisY].w = true;
- }
- }
- //turn left
- motor(sonarM) = -20;
- wait1Msec(350);
- motor(sonarM) = 0;
- if(SensorValue(sonar) > 20){
- displayTextLine(10, "sonar = %d",SensorValue(sonar));
- //wait1Msec(3000);
- table[thisX][thisY].left = true;
- switch (run){
- case 0: table[thisX][thisY].w = true; break;
- case 1: table[thisX][thisY].n = true; break;
- case 2: table[thisX][thisY].e = true; break;
- case 3: table[thisX][thisY].s = true;
- }
- }
- //turn back to center
- motor(sonarM) = 20;
- wait1Msec(340);
- motor(sonarM) = 0;
- }
- void travelAll(short thisX,short thisY){
- // ( x , y )
- switch (run){
- case 0 : table[thisX][thisY].s = true; break;
- case 1 : table[thisX][thisY].w = true; break;
- case 2 : table[thisX][thisY].n = true; break;
- case 3 : table[thisX][thisY].e = true;
- }
- checkWall(thisX,thisY);
- table[thisX][thisY].pass = true; //not use now
- countNode++; //count Node ++
- displayTextLine(1, "node %d,%d pass",thisX,thisY);
- short runtemp = run;
- short tempX,tempY;
- if((table[thisX][thisY].right || table[thisX][thisY].center || table[thisX][thisY].left)/* && !table[thisX][thisY].pass*/){
- /////////////////////////////right//////////////////////////////////
- if(table[thisX][thisY].right){
- //turn right
- right(); //turn right
- tempX = x;
- tempY = y;
- switch (run){
- case 0: tempY = y+1; break;
- case 1: tempX = x+1; break;
- case 2: tempY = y-1; break;
- case 3: tempX = x-1;
- }
- if(table[tempX][tempY].pass){
- displayTextLine(2, "node %d,%d pass!!",x,y);
- wait1Msec(1000);
- left();
- }
- else{
- forward();
- travelAll(x,y);
- //next node
- displayTextLine(5, "run : %d , runtemp : %d",run,(runtemp+3)%4);
- displayTextLine(6, "run : %d , runtemp : %d",run, runtemp);
- //wait1Msec(3000);
- turnDirection(run,(runtemp+3)%4);
- forward();
- //this node
- //right();
- }
- }
- /////////////////////////////center//////////////////////////////////
- if(table[thisX][thisY].center){
- displayTextLine(3, "run : %d , runtemp : %d",run,runtemp);
- //wait1Msec(3000);
- turnDirection(run,runtemp);
- tempX = x;
- tempY = y;
- switch (run){
- case 0: tempY = y+1; break;
- case 1: tempX = x+1; break;
- case 2: tempY = y-1; break;
- case 3: tempX = x-1;
- }
- if(table[tempX][tempY].pass){
- displayTextLine(2, "node %d,%d pass!!",x,y);
- }
- else{
- forward();
- travelAll(x,y);
- //next node
- displayTextLine(5, "run : %d , runtemp : %d",run,(runtemp+2)%4);
- displayTextLine(6, "run : %d , runtemp : %d",run, runtemp);
- //wait1Msec(3000);
- turnDirection(run,(runtemp+2)%4);
- //wait1Msec(3000);
- forward();
- //this node
- //turnAround();
- }
- }
- /////////////////////////////left//////////////////////////////////
- if(table[thisX][thisY].left){
- //turn left
- displayTextLine(3, "run : %d , runtemp : %d",run,(runtemp+3)%4);
- //wait1Msec(3000);
- turnDirection(run,(runtemp+3)%4); //left
- tempX = x;
- tempY = y;
- switch (run){
- case 0: tempY = y+1; break;
- case 1: tempX = x+1; break;
- case 2: tempY = y-1; break;
- case 3: tempX = x-1;
- }
- if(table[tempX][tempY].pass){
- displayTextLine(2, "node %d,%d pass!!",x,y);
- turnDirection(run,runtemp);
- }
- else{
- forward();
- travelAll(x,y);
- //next node
- displayTextLine(5, "run : %d , runtemp : %d",run,(runtemp+1)%4);
- //wait1Msec(3000);
- turnDirection(run,(runtemp+1)%4);
- forward();
- //this node
- //left();
- }
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- // find shortest path //
- ////////////////////////////////////////////////////////////////////////////////////////////////
- short findNext(){
- short dist = 999;
- short indexX,indexY;
- short retNode = -1;
- //displayTextLine(2,"in function findNext");
- //wait1Msec(1500);
- for(indexX = 0;indexX < max;indexX++){
- for(indexY = 0;indexY < max;indexY++){
- //displayTextLine(5,"name:%d %d %d",table[indexX][indexY].title,indexX,indexY);
- //wait1Msec(2000);
- if(!table[indexX][indexY].known && table[indexX][indexY].distance != -1){
- //displayTextLine(7,"Unknown!!");
- //wait1Msec(1500);
- //displayTextLine(8,"dist: %d",table[indexX][indexY].distance);
- //wait1Msec(1500);
- if(table[indexX][indexY].distance < dist){
- retNode = table[indexX][indexY].title;
- dist = table[indexX][indexY].distance;
- //displayTextLine(5,"name:%d %d %d",table[indexX][indexY].title,indexX,indexY);
- //displayTextLine(1,"retNode: %d",retNode);
- //wait1Msec(1500);
- }
- //else{
- // displayTextLine(8,"dist == NULL");
- // wait1Msec(1500);
- //}
- }
- }
- }
- displayTextLine(2,"retnode : %d",retNode);
- //wait1Msec(2000);
- return retNode;
- }
- void findPath(){
- displayTextLine(2,"in function findPath");
- table[0][0].distance = 0;
- table[0][0].s = false;
- table[0][0].fromX = -1;
- table[0][0].fromY = -1;
- while(true){
- short nodep = findNext();
- if(nodep == -1){
- break;
- }
- x = nodep/max;
- y = nodep%max;
- displayTextLine(8,"this node %d",table[x][y].title);
- //wait1Msec(1500);
- displayTextLine(10,"");
- displayTextLine(11,"");
- displayTextLine(12,"");
- displayTextLine(13,"");
- table[x][y].known = true;
- if(x == goal-1 && y == goal-1){
- displayTextLine(10,"found!!!!!!!");
- break;
- }
- //short count;
- //for(count = 0;count<4;count++){
- //}
- if(table[x][y].n){
- displayTextLine(10,"can go n");
- if(!table[x][y+1].known && ((table[x][y+1].distance == -1) || (table[x][y+1].distance > table[x][y].distance + 1))){
- table[x][y+1].distance = table[x][y].distance + 1;
- table[x][y+1].fromX = x;
- table[x][y+1].fromY = y;
- displayTextLine(10,"next node %d %d",table[x][y+1].title,table[x][y+1].distance);
- //wait1Msec(1000);
- }
- }
- else displayTextLine(10,"cannot go n");
- if(table[x][y].e){
- displayTextLine(11,"can go e");
- if(!table[x+1][y].known && ((table[x+1][y].distance == -1) || table[x+1][y].distance > table[x][y].distance + 1)){
- table[x+1][y].distance = table[x][y].distance + 1;
- table[x+1][y].fromX = x;
- table[x+1][y].fromY = y;
- displayTextLine(11,"next node %d %d",table[x+1][y].title,table[x+1][y].distance);
- //wait1Msec(1000);
- }
- }
- else displayTextLine(10,"cannot go e");
- if(table[x][y].s){
- displayTextLine(12,"can go s");
- if(!table[x][y-1].known && ((table[x][y-1].distance == -1) || table[x][y-1].distance > table[x][y].distance + 1)){
- table[x][y-1].distance = table[x][y].distance + 1;
- table[x][y-1].fromX = x;
- table[x][y-1].fromY = y;
- displayTextLine(12,"next node %d %d",table[x][y-1].title,table[x][y-1].distance);
- //wait1Msec(1000);
- }
- }
- else displayTextLine(10,"cannot go s");
- if(table[x][y].w){
- displayTextLine(13,"can go w");
- if(!table[x-1][y].known && ((table[x-1][y].distance == -1) || table[x-1][y].distance > table[x][y].distance + 1)){
- table[x-1][y].distance = table[x][y].distance + 1;
- table[x-1][y].fromX = x;
- table[x-1][y].fromY = y;
- displayTextLine(13,"next node %d %d",table[x-1][y].title,table[x-1][y].distance);
- //wait1Msec(1500);
- }
- }
- else displayTextLine(10,"cannot go w");
- }
- // end loop while
- }
- void nodeTonode(short x2,short y2,short x1,short y1){ //from node(x1,y1) to node(x2,y2)
- if(x2 == 0 && y2 == 0)
- return ;
- if(y2>y1){ //go to north
- turnDirection(run,0);
- forward();
- }
- else if(y2<y1){ //go to south
- turnDirection(run,2);
- forward();
- }
- else if(x2>x1){ //go to east
- turnDirection(run,1);
- forward();
- }
- else{ //go to west
- turnDirection(run,3);
- forward();
- }
- }
- void shortPath(short thisX,short thisY){ //walk from start to end node
- displayTextLine(2,"in func short path");
- if(thisX >= 0 && thisY >= 0){
- shortPath(table[thisX][thisY].fromX,table[thisX][thisY].fromY);
- nodeTonode(thisX,thisY,table[thisX][thisY].fromX,table[thisX][thisY].fromY);
- displayTextLine(4, "node title = %d",table[thisX][thisY].title);
- displayTextLine(5, "node x,y = %d %d",thisX,thisY);
- //wait1Msec(1500);
- }
- }
- task main()
- {
- init();
- //forward();
- travelAll(x,y);
- findPath();
- shortPath(goal-1,goal-1);
- //displayTextLine(10, "Gyross = %d",getGyroDegrees(Gyross));
- for(count = 0;count<=20;count++){
- displayTextLine(count,"");
- }
- displayTextLine(8, " Shortest Path Block = %d",table[goal-1][goal-1].distance);
- displayTextLine(10, " Available Block = %d",countNode);
- while(1){}
- /*while(SensorValue(t)!= 1){*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement