Advertisement
Guest User

Robot Position

a guest
Jul 22nd, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. void poseUpdate(int NL, int NR, int r, int b, int C);
  2.  
  3. // Global variable
  4. float robotPosition[3] = {0.0,0.0,0.0};
  5.  
  6. void setup()
  7. {
  8.   Serial.begin(9600);
  9.  
  10.   int r = 5;
  11.   int b = 20;
  12.   int C = 100; // Pulses per revolution
  13.    
  14.   int oldEncL, oldEncR;
  15.   oldEncL = 0;
  16.   oldEncR = 0;
  17.   int newEncL, newEncR;
  18.   int diffL, diffR;
  19.  
  20.  
  21.   for (int i = 0; i < 10; i++) {
  22.     int *encVals;
  23.     encVals = encUpdate(i);
  24.     newEncL = encVals[0];
  25.     newEncR = encVals[1];
  26.    
  27.     diffL = newEncL - oldEncL;
  28.     diffR = newEncR - oldEncR;
  29.    
  30.     oldEncL = newEncL;
  31.     oldEncR = newEncR;
  32.    
  33.     poseUpdate(diffL, diffR, r, b, C);
  34.     // Print robotPosition
  35.     Serial.print("Step "); Serial.print(i+1);
  36.     Serial.print(", x: "); Serial.print(robotPosition[0]);
  37.     Serial.print(", y: "); Serial.print(robotPosition[1]);
  38.     Serial.print(", theta: "); Serial.println(robotPosition[2]);
  39.   }
  40.  
  41. }
  42.  
  43. void loop()
  44. {
  45.  
  46. }
  47.  
  48. int* encUpdate(int t) {
  49.   static int matrix[10][2] = {
  50.     {0, 0},
  51.     {100, 100},
  52.     {300, 200},
  53.     {500, 300},
  54.     {700, 400},
  55.     {900, 500},
  56.     {1000, 600},
  57.     {1100, 700},
  58.     {1200, 800},
  59.     {1200, 800}
  60.   };
  61.  
  62.   int *encVals;
  63.   encVals = matrix[t];
  64.  
  65.   return encVals;
  66. }
  67.  
  68. void poseUpdate(int NL, int NR, int r, int b, int C) {
  69.  
  70.  
  71.   float dl = (2*M_PI*r / (float)C) * NL;
  72.   float dr = (2*M_PI*r / (float)C) * NR;
  73.   float d = (dr + dl) / 2.0;
  74.  
  75.   float theta = robotPosition[2] + ((dr - dl) / (float)b);
  76.   float x = robotPosition[0] + d * cos(theta);
  77.   float y = robotPosition[1] + d * sin(theta);
  78.  
  79.   // Update robotPosition
  80.   robotPosition[0] = x;
  81.   robotPosition[1] = y;
  82.   robotPosition[2] = theta;
  83.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement