KRITSADA

POP-7 Test RQ-ROBOT

Apr 4th, 2018
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.67 KB | None | 0 0
  1. #include <pop7.h>
  2. void setup() {}
  3. void loop() {menu();}
  4. void menu(){
  5.   setTextSize(2);
  6.   glcdMode(3);
  7.   setTextColor(color[5]);glcd(2,0,"Check");
  8.   setTextSize(3);
  9.   while(!sw_OK()){
  10.     switch (knob(4)){
  11.       case 0 : setTextColor(color[1]);glcd(2,1,"Motor   "); break;
  12.       case 1 : setTextColor(color[2]);glcd(2,1,"Color   ");break;
  13.       case 2 : setTextColor(color[3]);glcd(2,1,"Encoder ");break;
  14.       case 3 : setTextColor(color[6]);glcd(2,1,"GP2Y0A41");break;
  15.       default : setTextColor(color[7]);glcd(2,1,"Servo   ");break;
  16.     }
  17.   }
  18.   setTextColor(GLCD_GREEN);
  19.   switch (knob(4)){
  20.       case 0 : chkMotor(); break;
  21.       case 1 : chkColor();break;
  22.       case 2 : chkEnc();break;
  23.       case 3 : chkGP2();break;
  24.       default : chkServo();break;
  25.   }  
  26. }
  27. void left() {
  28.   tlEnc(30, 36); fdEnc(30, 30); sKeep(); bkWall(50);
  29. }
  30. void right() {
  31.   trEnc(30, 36); fdEnc(30, 30); sKeep(); bkWall(50);
  32. }
  33.  
  34. void run1(){
  35.   sHome();
  36.   fd(50); while(readColor()==3); ao();beep();
  37.   fdEnc(50,75);beep();
  38.   if (readColor()==2){glcdFillScreen(GLCD_YELLOW);left();}
  39.   else{glcdFillScreen(GLCD_GREEN);right();}
  40. }
  41. void ShowColor(){
  42.   while(!sw1()){
  43.     if(readColor()==1){ glcdFillScreen(GLCD_WHITE);}
  44.     else if(readColor()==2){ glcdFillScreen(GLCD_YELLOW);}
  45.     if(readColor()==3){ glcdFillScreen(GLCD_RED);}
  46.     if(readColor()==4){ glcdFillScreen(GLCD_GRAY);}
  47.     if(readColor()==5){ glcdFillScreen(GLCD_VIOLET);}
  48.     if(readColor()==6){ glcdFillScreen(GLCD_GREEN);}
  49.   }  
  50. }
  51. void chkMotor(){
  52.   glcdClear();setTextSize(2);
  53.   setTextColor(GLCD_RED);   glcd(1,0,"Forward   ");fd(100);delay(1000);ao();delay(200);
  54.   setTextColor(GLCD_YELLOW);glcd(1,0,"Backward  ");bk(100);delay(1000);ao();delay(200);
  55.   setTextColor(GLCD_VIOLET);glcd(1,0,"Spin Left ");sl(100);delay(1000);ao();delay(200);
  56.   setTextColor(GLCD_PINK);  glcd(1,0,"Spin Right");sr(100);delay(1000);ao();delay(200);
  57.   setTextColor(GLCD_GREEN); glcd(1,0,"Turn Left ");tl(100);delay(1000);ao();delay(200);
  58.   setTextColor(GLCD_BLUE);  glcd(1,0,"Turn Right");tr(100);delay(1000);ao();delay(200);
  59.   glcdClear();
  60. }
  61. void chkColor() {
  62.   glcdClear();
  63.   unsigned int I,R,G,B,a;
  64.   setTextSize(2);
  65.   setTextColor(GLCD_WHITE);glcd(0,0,"SW1 Exit");
  66.   while(!sw1()){
  67.     I=0;for(a=0;a<40;a++){I=analog(0)+I;}I=I/40;
  68.     R=0;for(a=0;a<40;a++){R=analog(1)+R;}R=R/40;
  69.     G=0;for(a=0;a<40;a++){G=analog(2)+G;}G=G/40;
  70.     B=0;for(a=0;a<40;a++){B=analog(3)+B;}B=B/40;
  71.     setTextColor(GLCD_VIOLET);glcd(2,0,"I=%d ",I);
  72.     setTextColor(GLCD_RED);glcd(3,0,"R=%d ",R);
  73.     setTextColor(GLCD_GREEN);glcd(4,0,"G=%d ",G);
  74.     setTextColor(GLCD_BLUE);glcd(5,0,"B=%d ",B);
  75.   }
  76.   glcdClear();
  77. }
  78. void chkEnc(){
  79.   glcdClear();setTextSize(2);
  80.   long t;
  81.   setTextColor(GLCD_RED);   glcd(1,0,"Forward   ");fdEnc(60,80);
  82.   setTextColor(GLCD_GREEN); glcd(1,0,"Turn Left ");tlEnc(60,80);
  83.   setTextColor(GLCD_BLUE);  glcd(1,0,"Turn Right");trEnc(60,80);
  84.   setTextColor(GLCD_YELLOW);glcd(1,0,"Backward  ");bkEnc(60,80);
  85.   setTextColor(GLCD_VIOLET);glcd(1,0,"Spin Left ");slEnc(60,40);
  86.   setTextColor(GLCD_PINK);  glcd(1,0,"Spin Right");srEnc(60,40);
  87.   glcdClear();
  88. }
  89. void chkGP2(){
  90.   glcdClear();
  91.   setTextSize(2);
  92.   setTextColor(GLCD_WHITE);glcd(0,0,"SW1 Exit");
  93.   setTextColor(GLCD_RED);
  94.     while(!sw1()){
  95.       setTextColor(GLCD_RED);glcd(2,0,"Raw=%d  ",GP2AV());
  96.       setTextColor(GLCD_GREEN);glcd(3,0,"CW=%d  ",getdist(4));    
  97.     }
  98.   glcdClear();
  99. }
  100. void chkServo(){
  101.   glcdClear();  setTextSize(2);
  102.   setTextColor(GLCD_WHITE);glcd(0,0,"SW1 Exit");
  103.   setTextSize(1);
  104.   setTextColor(GLCD_GREEN);glcd(3,0,"ZX-Switch for Test");
  105.   sHome();delay(300);
  106.   sKeep();delay(300);
  107.   sHome();delay(300);
  108.   servo(1,70);delay(300);
  109.   servo(2,95);delay(300);
  110.   servo(1,160);delay(300);
  111.   servo(2,5);delay(300);
  112.  
  113.   setTextSize(2);
  114.   int x=0,y=0;;
  115.   while(!sw1()){
  116.     if(!in(8)){
  117.        beep();
  118.        if(x){setTextColor(GLCD_YELLOW);glcd(3,0,"ServoL Home");servo(1,160);x=0;}
  119.        else{setTextColor(GLCD_GREEN);glcd(3,0,"ServoL Keep");servo(1,70);x=1;}}
  120.     if(!in(9)){
  121.       beep();
  122.       if(y){setTextColor(GLCD_BLUE);glcd(4,0,"ServoR Home");servo(2,5);y=0;}
  123.       else{setTextColor(GLCD_RED);glcd(4,0,"ServoR Keep");servo(2,95);y=1;}}
  124.   }  
  125.   glcdClear();
  126. }
  127.  
  128. void fdWall(int D){
  129.   while(GP2AV()<D){
  130.     fd(50);
  131.   }
  132.   ao();
  133. }
  134. int GP2AV(){
  135. int R=0,i;  
  136.   for(i=0;i<20;i++){
  137.       R=analog(4)+R;
  138.   }
  139.   return (R/20);
  140. }
  141. void fdEnc(int Speed,int ENC){
  142.   encoder(3,0);encoder(2,0);fd(Speed);
  143.   while((encoder(3)<ENC)||(encoder(2)<ENC));
  144.   ao();
  145. }
  146. void bkEnc(int Speed,int ENC){
  147.   encoder(3,0);encoder(2,0);bk(Speed);
  148.   while((encoder(3)<ENC)||(encoder(2)<ENC));
  149.   ao();
  150. }
  151. void slEnc(int Speed,int ENC){
  152.   encoder(3,0);sl(Speed);
  153.   while(encoder(3)<ENC);ao();
  154. }
  155. void tlEnc(int Speed,int ENC){
  156.   encoder(3,0);tl(Speed);
  157.   while(encoder(3)<ENC);ao();
  158. }
  159. void trEnc(int Speed,int ENC){
  160.   encoder(2,0);tr(Speed);
  161.   while(encoder(2)<ENC);ao();
  162. }
  163. void srEnc(int Speed,int ENC){
  164.   encoder(2,0);sr(Speed);
  165.   while(encoder(2)<ENC);ao();
  166. }
  167. void bkWall(int Speed){
  168.   bk(Speed);
  169.   while(1){
  170.    if(!in(8)){
  171.       while(in(9)){
  172.         motor(1,0);motor(2,-100);
  173.           }
  174.       ao();break;
  175.    }
  176.    else if(!in(9)){
  177.      while(in(8)){
  178.         motor(2,0);motor(1,-100);}
  179.       ao();break;
  180.    }
  181. }
  182. }
  183. void sHome(){
  184.   servo(1,170);servo(2,5);  
  185. }
  186. void sKeep(){
  187.   servo(1,70);servo(2,95);  
  188. }
  189. int readColor(){
  190.   unsigned int R,G,i,C;
  191.   R=0;for(i=0;i<40;i++){R=analog(1)+R;}R=R/40;
  192.   G=0;for(i=0;i<40;i++){G=analog(2)+G;}G=G/40;
  193.   if(R>900){
  194.       if(G>900){C=1;}
  195.       else if(G>400){C=2;}
  196.       else {C=3;}
  197.    }
  198.   else if(R>500){
  199.       if (G>500){C=4;}
  200.       else{C=5;}
  201.       }
  202.   else { C=6;  }
  203.   return C;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment