Advertisement
tyridge77

RobotSource

Nov 4th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1.  
  2. float power = 12;
  3. float dec = 0;
  4. float lastrevcount = 0;
  5. float twopi = PI*2;
  6. float dir = 1;
  7. float x_count = 0;
  8. float y_count = 0;
  9. float lasttime = 0;
  10. float lasttime2 = 0;
  11. float result_t = 1;
  12. bool Mapped = false;
  13. bool Active = true;
  14.  
  15. int mapY[3700/2];
  16. int mapX[3700/2];
  17.  
  18.  
  19. float Revolutions()
  20. {
  21.         float current = (float)SensorValue(I2C_1);
  22.         float raw =  (current/650);
  23.         float percent = (raw-lastrevcount);
  24.         //writeDebugStreamLine("%f",percent);
  25.         if(abs(percent)>=.9)
  26.         {
  27.                 // erase map data
  28.                 dir=-dir;
  29.                 Mapped = true;
  30.                 lastrevcount=raw;
  31.  
  32.                 //for(int i = 0;i<3700/2;i++)
  33.                 //{
  34.                    //     mapX[i]=NULL;
  35.                  //       mapY[i]=NULL;
  36.                //         x_count=0;
  37.              //           y_count=0;
  38.            //     }
  39.         }
  40.         return percent;
  41. }
  42. void RecordSurrounding(float rev,float dist)
  43. {
  44.         float theta = rev*twopi;
  45.         float distance = dist/4;
  46.         float y = distance*cos(theta);
  47.         float x = dir*(-distance*sin(theta));
  48.  
  49.         if(x_count!=(3700/2-1))
  50.         {
  51.                 y_count++;
  52.                 x_count++;
  53.                 mapX[x_count]=x;
  54.                 mapY[y_count]=y;
  55.                // writeDebugStreamLine("(%f,%f)",x,y);
  56.         }
  57.         else
  58.         {
  59.          WriteDebugStreamLine("%s","Passed threshold!");
  60.          Mapped = true;
  61.         }
  62. }
  63.  
  64. //float rev = 0;
  65. float mapOutput = 0;
  66. task main()
  67. {
  68.  
  69.         while(Active)
  70.         {
  71.             if(nPgmTime>5000)
  72.         {
  73.                 if(!Mapped)
  74.           {
  75.                 float dist = SensorValue(Output);
  76.                 //writeDebugStreamLine("%f",dist);
  77.  
  78.                 float rev=Revolutions();
  79.                 float time = nPGmTime*1000;
  80.                 //100000 = .1 sec
  81.                 if(time-lasttime>=10000)
  82.                 {
  83.                         lasttime=time;
  84.                        RecordSurrounding(rev,dist);
  85.                 }
  86.  
  87.  
  88.                 motor[Neck]=(dist<5?0:(dir*power));
  89.     }
  90.       else
  91.     {
  92.      motor[Neck]=0;
  93.      float time2 = time100[T2];
  94.      if (time2-lasttime2 >= .1)
  95.        //WriteDebugStreamLine("%d",1);
  96.      {
  97.       lasttime2=time2;
  98.       if(mapOutput < 3700/2-1)
  99.       {
  100.       mapOutput++;
  101.       writeDebugStreamLine("%f,%f",mapX[mapOutput],mapY[mapOutput]);
  102.       }
  103.       else
  104.       {
  105.        Active = false;
  106.       }
  107.      }
  108.     }
  109.  
  110.                 // VectorX = RevolutionCount
  111.                 // VectorY = Distance
  112.  
  113.                 //start at 0,
  114.                 //-1 for left until full revolution
  115.                 // +1 for right until full revolution
  116.  
  117.  
  118.         }
  119.       }
  120.         motor[Neck]=0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement