Advertisement
Guest User

nextone

a guest
Jan 16th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3.  
  4. #include <inttypes.h>
  5.  
  6. //Arduino.h has some Pi
  7. #include <Arduino.h>
  8. #include "Odometry.h"
  9.  
  10. volatile int right=0; //=0:no movement;-10:backwarts; 10:forwards
  11. volatile int left=0; //=0:no movement;-10:backwarts; 10:forwards
  12. int perimeter = PI*65/1000;
  13. static volatile uint8_t PINB_old=0;
  14.  
  15.  
  16.  
  17.  
  18.  
  19. //initialises Odometrie (Interrupts)
  20. void initOdom(){
  21. PCMSK0 = 0;
  22. PCMSK0 |= (1<<PCINT0) | (1<<PCINT3); //enable interrupts from ENC_1B and ENC_1A
  23.  
  24. PCICR =0;
  25. PCICR |= (1<<PCIE0);
  26.  
  27. PINB_old = PINB;
  28. }
  29.  
  30. ISR(PCINT0_vect){ //TODO: use of encoders has to be checked
  31. cli();
  32.  
  33.  
  34. if ((PINB & (1<<PB0)) >0){
  35. //nur steigende flanke
  36. /* if ((PINB_old & (1<<PB0) & (1<<PB7)) != (PINB & (1<<PB0) & (1<<PB7))){
  37. right = -10;
  38. //rechts rückwärts
  39. }
  40. else if ((PINB_old & (1<<PB0)) != (PINB & (1<<PB0))){
  41. right = 10;
  42. // rechts vorwärts
  43. }*/
  44.  
  45. if ((PINB_old & (1<<PB0)) != (PINB & (1<<PB0))){
  46. if ((PINB_old & (1<<PB7)) != (PINB & (1<<PB7))){
  47. Serial.println("rechts rück");
  48. right = -10;}
  49. else{
  50. Serial.println("rechts vor");
  51. right = 10;}
  52. }
  53. }
  54.  
  55. if ((PINB & (1<<PB3)) >0){
  56. //nur steigende flanke
  57. /*if ((PINB_old & (1<<PB3) & (1<<PB4)) != (PINB & (1<<PB3) & (1<<PB4))){
  58. left = 10;
  59. Serial.println("links vor");
  60. }
  61. else if ((PINB_old & (1<<PB3)) != (PINB & (1<<PB3))){
  62. left = -10;
  63. Serial.println("links rück");
  64. }*/
  65.  
  66. if ((PINB_old & (1<<PB3)) != (PINB & (1<<PB3))){
  67. if ((PINB_old & (1<<PB4)) != (PINB & (1<<PB4))){
  68. Serial.println("links vor");
  69. left = 10;}
  70.  
  71. else {
  72. left = -10;
  73. Serial.println("links rück");
  74. }
  75. }
  76. PINB_old = PINB;
  77. sei();
  78. }
  79. }
  80.  
  81. //writes count courrent of ticks to structure
  82. void odomTicks(struct OdomData& d){
  83. cli();
  84. if (right == 10)
  85. d.right++;
  86. else if (right == -10)
  87. d.right--;
  88. if (left == 10)
  89. d.left++;
  90. else if (left == -10)
  91. d.left++;
  92.  
  93. right = 0;
  94. left = 0;
  95. sei();
  96. }
  97.  
  98. //m <- count
  99. static float odomDistance( int32_t dticks)
  100. {
  101. float dist = 0;
  102.  
  103. if (dticks >0){ //moved forward
  104. while (dticks>224.2){
  105. dist += perimeter;
  106. }
  107. dist +=PI*(dticks*100/224.20);
  108. }
  109.  
  110. else{ //moved backward
  111. while (dticks<-224.2){
  112. dist -= perimeter;
  113. }
  114. dist +=PI*(dticks*100/224.20);
  115. }
  116. return dist;
  117. }
  118.  
  119. //m/s <- count, millis
  120. //you may want to use parts of millis for short messurements
  121. static float odomVelocity( int32_t dticks, float dtime)
  122. {
  123. //v=s/t
  124.  
  125. float distance = odomDistance(dticks); //in m
  126.  
  127. return distance/(dtime/1000); //dtime/1000 because time is probably in ms
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement