我正在为我的大学正在参加竞赛的机器人编写代码。我目前正在尝试使用反射传感器构建一些车轮编码器。不久前我意识到我可能需要使用线程来实现这一点,因为机器人需要同时监控左右编码器。下面的代码是我到目前为止所拥有的:
from __future__ import division
import threading
import time
from sr import *
R = Robot()
class Encoder(threading.Thread):
def __init__(self, motor, pin, div=16):
self.motor = motor
self.pin = pin
self.div = div
self.count = 0
threading.Thread.__init__(self)
def run(self):
while True:
wait_for(R.io[0].input[self.pin].query.d)
self.count += 1
def rotations(self, angle, start_speed=50):
seg = 360/self.div
startcount = self.count
current_dist = angle #Distance away from target
R.motors[self.motor].target = start_speed
while current_dist > 360:
newcount = self.count - startcount
current_dist = angle - newcount*seg
R.motors[self.motor].target = 50
while abs(current_dist) > seg/2:
newcount = self.count - startcount
current_dist = angle - newcount*seg
current_speed = start_speed * current_dist / 360
if current_speed < 5:
R.motors[self.motor].target = 5
else:
R.motors[self.motor].target = current_speed
R.motors[self.motor].target = 0
WheelLeft = Encoder(0,0)
WheelLeft.start()
WheelRight = Encoder(1,3)
WheelRight.start()
WheelRight.rotations(720)
WheelLeft.rotations(720)
sr模块由主办本次比赛的南安普顿大学提供。它允许我们与机器人的硬件进行交互。
现在,创建的线程似乎允许单独监视两个反射传感器。这段代码:
R.io[0].input[self.pin].query.d
计算出来自反射传感器的值是否已经改变。 “旋转”方法通过不断检查轮子已经转动了多少度来将轮子转动一定的角度,并在到达终点时减慢速度。我希望两个轮子在运行程序时都开始转动,然后在转动两圈后减速并停止。但目前,当我运行该程序时,一个轮子开始转动,然后减速并停止,然后另一个轮子也跟着转动。在我看来,“旋转”方法不像“运行”方法那样在线程中运行。是只有“run”方法下的代码在线程中运行,还是整个类?
如果有帮助,我一直在关注本教程:http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/1/
另外,我想知道为什么只能用
Encoder(0,0).start()
来启动线程。为什么不必使用类创建对象(例如 Thread = Encoder(0,0).start()
来创建新线程?
如果我使用的术语不符合标准,我很抱歉,因为你可能会说我对线程和一般编程很陌生。
Encoder(0,0).start()
是调用启动线程的方法。反过来,此方法调用您的 run
实现,该实现不使用 rotations
方法。如果你想这样做,那么你必须在run
的while循环中调用它。
使用
Thread = Encoder(0,0).start()
,您可以存储从该调用中检索到的值(None),但要获取它,您无论如何都需要先启动新线程。
run方法是执行线程。
如果您希望该线程中发生其他事情,您必须从
Encoder.run()
调用它。
哦,
Encoder(0,0).start()
确实创建了一个对象。仅仅因为您没有将该对象绑定到局部变量并不意味着它不存在。如果它不存在,则无法调用其 start
方法。
不过,你必须非常小心它的生命周期,没有局部变量可以让它保持活动状态。
Poll
类进行扩展,以便可以在 wait_for
中使用它:
import poll
class Encoder(poll.Poll):
def __init__(self, motor, pin, div=16):
self.motor = motor
self.pin = pin
self.div = div
self.count = 0
self.target_reached = False
# kick off a thread to count the encoder ticks
self.counter_thread = threading.Thread(target=self._update_count)
self.counter_thread.start()
def _update_count(self):
while True:
wait_for(R.io[0].input[self.pin].query.d)
self.count += 1
def rotations(self, angle, start_speed=50):
if not self.target_reached:
raise Exception("Last motion still in progress!")
self.target_reached = False
# kick off a thread to control the speed
self.angle_thread = threading.Thread(
target=self._update_speeds,
args=(angle, start_speed)
)
self.angle_thread.start()
def _update_speeds(self, angle, start_speed):
# control the motor speed as before
...
# let things know we're done
self.target_reached = True
# implement poll methods
def eval(self):
return (self.target_reached, None)
然后你可以这样做:
wheelLeft = Encoder(0,0)
wheelRight = Encoder(1,3)
wheelRight.rotations(720)
wheelLeft.rotations(720)
wait_for(wheelRight & wheelLeft)
请注意,编码器本身并不是线程 - 它是“有一个”关系,而不是“是一个”关系