ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org

# how can i get odometry data from wheel encoder motors for localization.

i am using the code which is mentioned below to get the ticks of wheel encoder motors but in order to get the localization i also need position and distance. please help me that how can i get odometry data for localization. What things are required in the code mentioned below.

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

# Encoder Pins of Motor A and Motor B
A_pin=20
B_pin=21
C_pin=5
D_pin=6

#Enable Pins to define speed of motors
en=25
en1=16
#Motor pins for forward or backward movement
in1 = 24
in2 = 23
in3 = 17
in4 = 27

GPIO.setup(en,GPIO.OUT)
GPIO.setup(en1,GPIO.OUT)
GPIO.setup(A_pin, GPIO.IN)
GPIO.setup(B_pin, GPIO.IN)
GPIO.setup(C_pin, GPIO.IN)
GPIO.setup(D_pin, GPIO.IN)

#motorA
GPIO.setup(in1,GPIO.OUT)
GPIO.setup(in2,GPIO.OUT)
GPIO.output(in1,GPIO.HIGH)
GPIO.output(in2,GPIO.LOW)

#Motor B
GPIO.setup(in3,GPIO.OUT)
GPIO.setup(in4,GPIO.OUT)
GPIO.output(in3,GPIO.HIGH)
GPIO.output(in4,GPIO.LOW)
p=GPIO.PWM(en,1000)
p1=GPIO.PWM(en1,1000)
p.start(45)
p1.start(45)

outcome=[0,-1,-1,0,-1,0,0,1,1,0,0,-1,0,-1,1,0]
#motor Left
last_AB=0b00
counter_L=0
#Motor Right
last_CD=0b00
counter_R=0

try:
while True:
A=GPIO.input(A_pin)
B=GPIO.input(B_pin)
start_AB=time.time()
C=GPIO.input(C_pin)
D=GPIO.input(D_pin)

current_AB= (A<<1) | B
start=time.time()
Position_L=(last_AB<<2) | current_AB
counter_L += outcome[Position_L]
last_AB= current_AB
stop_AB=time.time()
Duration=stop_AB-start_AB
print(Duration)
current_CD= (C<<1) | D
Position_R=(last_CD<<2) | current_CD
counter_R += outcome[Position_R]
last_CD= current_CD
time.sleep(1)
print ("Left = %.1f " % counter_L , "Right =%.1f " % counter_R)

except KeyboardInterrupt:
print("Stopped by user")
GPIO.cleanup()

edit retag close merge delete

Yoo, facing the exact same problem. any help regarding it will be highly appreciated😊

( 2020-07-26 05:21:01 -0600 )edit

Sort by » oldest newest most voted

This is a general robotics question, not a ROS-specific. Please refer to a textbook on mobile robotics or a Google search for how to calculate odometry from encoder ticks. You will need to know the diameters of your wheels and the size of your wheel base.

more

I was trying to generate values of my robot and was planning on publishing that data for mapping and path planning node to use so I think that makes it ROS-specific. p.s. Doing it this way because was unable to understand how to bind encoder pins with the odometry package(provided by ROS) .

( 2020-07-28 08:47:59 -0600 )edit