机器人车轮编码器的Python线程

2024-04-19 03:35:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为一个机器人编写代码,我的大学正在参加一个竞赛。我目前正在尝试建立一些车轮编码器使用反射传感器。我意识到我可能需要使用线程来实现这一点,因为机器人需要同时监控左右编码器。以下代码是我目前掌握的代码:

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计算出来自反射率传感器的值是否已更改。“旋转”方法通过不断检查轮子已经转动了多少度,从而使轮子转动一个特定的角度,并在到达终点时减慢速度。我希望两个轮子开始转动时,我运行的程序,然后减速和停止时,他们已经通过2个旋转。但目前,当我运行这个程序时,一个轮子开始转动,减速并停止,然后是另一个轮子。在我看来,“rotations”方法不像“run”方法那样在线程中运行。在线程中运行的只是“run”方法下的代码,还是整个类?在

如果有帮助的话,我一直遵循这个教程:http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/1/

另外,我想知道为什么只能用Encoder(0,0).start()启动线程。为什么不必使用类(例如Thread = Encoder(0,0).start())创建一个对象来创建一个新线程?在

抱歉,如果我使用的术语不符合要求,你可能会告诉我,我是线程和编程的新手。在


Tags: 代码importselftargetencoderdistcountpin
3条回答

Encoder(0,0).start()是对启动线程的方法的调用。反过来,这个方法调用您的run实现,它不使用rotations方法。如果您想这样做,那么您必须在run的while循环中调用它。在

{start}你需要从第一个调用中获取一个新的值。在

您可以从SR的^{}类扩展,以便可以在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)

这样你就可以:

^{pr2}$

请注意,编码器本身不是一个线程-它是一个“有”关系,而不是“是”关系

run方法是执行线程。在

如果您希望在该线程中发生其他事情,则必须从Encoder.run()调用它。在

哦,而且Encoder(0,0).start()会创建一个对象。仅仅因为你没有将对象绑定到一个局部变量并不意味着它不存在。如果它不存在,就不能调用它的start方法。在

但是,你必须非常小心它的生命周期,没有一个局部变量使它存活。在

相关问题 更多 >