Advertisement
Guest User

odometry

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