我目前正在使用树莓派3使用AlphaBot进行一个小项目。我正在尝试使用车轮编码器状态转换或刻度来使机器人进行90度转弯。这是我的尝试:
import RPi.GPIO as GPIO # Importing Libraries
import time
import math
GPIO.setmode(GPIO.BCM) # Choosing BCM numbering
GPIO.setwarnings(False) # Disables Warnings when setting the pins
r = 4.85 #width of robot chasis in inches
d = 8.5 #diameter of wheel in inches
C = math.pi*d #circumference of wheel in inches
slits = 20 #number of slits on wheel encoders
state_trans = 2*slits #total # of state transitions of wheel encoders
inpertick = C/state_trans #total inches per tick
def pivot(degrees,turn):
s = 0 #used to count ticks needed to pivot a certain number of degrees
L = 0 #used for number of pulses on left encoder
R = 0 #used for number of pulses on right encoder
ticks = (degrees*math.pi*r)/180
final_ticks = int(round(ticks/inpertick))
while(s != final_ticks):
if (turn == "left"):
L += 1
R -= 1
elif (turn == "right"):
R += 1
L -= 1
s += 1
if (turn == "left"):
GPIO.output(IN1,GPIO.LOW)
GPIO.output(IN2,GPIO.HIGH)
GPIO.output(IN3,GPIO.LOW)
GPIO.output(IN4,GPIO.HIGH)
elif (turn == "right"):
GPIO.output(IN1,GPIO.HIGH)
GPIO.output(IN2,GPIO.LOW)
GPIO.output(IN3,GPIO.HIGH)
GPIO.output(IN4,GPIO.LOW)
PWMA.ChangeDutyCycle(R)
PWMB.ChangeDutyCycle(L)
IN1 = 12
IN2 = 13
IN3 = 20
IN4 = 21
ENA = 6
ENB = 26
GPIO.setup(IN1,GPIO.OUT) # Set GPIO pin 12 as an output
GPIO.setup(IN2,GPIO.OUT) # Set GPIO pin 13 as an output
GPIO.setup(IN3,GPIO.OUT) # Set GPIO pin 20 as an output
GPIO.setup(IN4,GPIO.OUT) # Set GPIO pin 21 as an output
GPIO.setup(ENA,GPIO.OUT) # Set GPIO pin 6 as an output
GPIO.setup(ENB,GPIO.OUT) # Set GPIO pin 26 as an output
PWMA = GPIO.PWM(ENA,50) # Setup ENA as PWM at 50 Hertz
PWMB = GPIO.PWM(ENB,50) # Setup ENB as PWM at 50 Hertz
PWMA.start(45)
PWMB.start(50)
pivot(90,"right")
time.sleep(2)
PWMA.stop()
PWMB.stop()
正如您在pivot()函数中看到的,我计算了转90度所需的刻度或状态转换的数量。我尝试采用这些滴答数并应用它来改变占空比。我确定这是错的,但我不知道如何采用滴答数并将其转换为编码器应该做的事情。有什么想法吗?
答案 0 :(得分:0)
我录制了有关如何使用车轮编码器以及如何计算机器人线性和角速度的视频。 (https://youtu.be/ktZTN5aUYPY)
如果您观看视频,您将在项目doc文件夹中看到一个包含所有公式的ppt文件。 (https://github.com/gusbots/mark01/tree/v3.1/doc)
这是它的快照:
其中vr和vl是角速度(以rad / s为单位),R是车轮的半径,L是车轮之间的距离。