Advertisement
HDElectronics

Odometrie

May 12th, 2020
1,465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*****************************************************************
  2. *   Author: KHADRAOUI Ibrahim
  3. *   Company: USTHB FEI
  4. *   Date:   July 2019
  5. *   Project: Odometry on my proto robot
  6. ***********
  7. *   Description: Print on the serial monitor the x,y coordinates
  8. *               of the current position of the robot and the
  9. *               angle of the robot by using odometry and
  10. *               wheele encodeurs.
  11. *
  12. ******************************************************************/
  13.  
  14. #include <math.h>
  15. #define pi 3.141592
  16.  
  17. ////////////////////////////////////////////////////
  18. // Variables Robot
  19. ////////////////////////////////////////////////////
  20. const float N=231;  //nombre de tick par tour (encodeur)
  21. const float D=6.7;  //diametre roues
  22. const float L=17;   //distance entre les deux roues
  23. ////////////////////////////////////////////////////
  24.  
  25. ////////////////////////////////////////////////////
  26. // Variables Odometerie
  27. ////////////////////////////////////////////////////
  28. volatile float countD,countG;
  29. float distanceD,distanceG,distanceC;
  30. float x,y,phi;
  31. float countDLast,countGLast,deltaCountD,deltaCountG;
  32. ////////////////////////////////////////////////////
  33.  
  34.  
  35.  
  36. void setup() {
  37.  
  38.   pinMode(2,INPUT);pinMode(11,INPUT);
  39.   pinMode(3,INPUT);pinMode(12,INPUT);
  40.   attachInterrupt(digitalPinToInterrupt(2), tikG, FALLING);
  41.   attachInterrupt(digitalPinToInterrupt(3), tikD, FALLING);
  42.   Serial.begin(9600);
  43.   delay(1000);
  44.  
  45.   distanceD = distanceG = distanceC = x = y = phi = 0;
  46.   countDLast = countGLast = 0;
  47.  
  48.   currentTime=millis();
  49. }
  50.  
  51. void loop() {
  52.  
  53. while(millis()-currentTime<50){ //50ms calcule de la distance parcourue par les deux roues
  54.     interrupts();
  55.     }
  56.    
  57.  noInterrupts();
  58.  odom();
  59.  currentTime=millis();
  60. }
  61.  
  62. void tikG() {
  63.   noInterrupts();
  64.   if(digitalRead(11)==HIGH) countG++;
  65.   else countG--;
  66. }
  67.  
  68. void tikD() {
  69.   noInterrupts();
  70.   if(digitalRead(12)==HIGH) countD--;
  71.   else countD++;
  72. }
  73.  
  74. void odom(){
  75.  
  76. //Delta count:
  77. //number of count since the last sample
  78. deltaCountD = countD-countDLast;
  79. deltaCountG = countG-countGLast;
  80.  
  81. // distance traveled by the two wheels
  82. distanceD = pi*D*(deltaCountD/N);
  83. distanceG = pi*D*(deltaCountG/N);
  84.  
  85. // distance traveled by the robot
  86. distanceC = (distanceD+distanceG)/2;
  87.  
  88. // x&y coordinates of the robot
  89. x = x+(distanceC*cos(phi));
  90. y = y+(distanceC*sin(phi));
  91.  
  92. // angle of the robot
  93. phi = phi+((distanceD-distanceG)/L);
  94. phi = atan2(sin(phi),cos(phi));
  95.  
  96.  
  97. Serial.print("x:   ");Serial.print(x);
  98. Serial.print("                ");
  99. Serial.print("y:   ");Serial.print(y);
  100. Serial.print("                ");
  101. Serial.print("phi:   ");Serial.println(phi);
  102.  
  103. /*
  104. Serial.print("D:   ");Serial.print(distanceD);
  105. Serial.print("                ");
  106. Serial.print("G:   ");Serial.print(distanceG);
  107. Serial.print("                ");
  108. Serial.print("C:   ");Serial.println(distanceC);
  109. */
  110. countDLast=countD;
  111. countGLast=countG;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement