Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
机器人上车轮编码器的Python线程_Python_Python Multithreading_Robot - Fatal编程技术网

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

机器人上车轮编码器的Python线程,python,python-multithreading,robot,Python,Python Multithreading,Robot,我正在为一个机器人写代码,我的大学正在参加一个竞赛。我目前正试图建立一些车轮编码器使用反射传感器。不久前我意识到我可能需要使用线程来实现这一点,因为机器人需要同时监控左右编码器。以下代码是我目前掌握的代码: from __future__ import division import threading import time from sr import * R = Robot() class Encoder(threading.Thread): def __init__(self,

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

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=机器人()
类编码器(threading.Thread):
def _初始__(自身、电机、引脚、div=16):
自动马达
self.pin=pin
self.div=div
self.count=0
threading.Thread.\uuuuu init\uuuuuu(自)
def运行(自):
尽管如此:
等待(R.io[0]。输入[self.pin]。query.d)
self.count+=1
def旋转(自身、角度、启动速度=50):
seg=360/self.div
startcount=self.count
当前距离=角度#距离目标
R.电机[自身电机]。目标=启动速度
当前距离>360时:
newcount=self.count-startcount
当前距离=角度-新计数*分段
R.motors[自身电机]。目标=50
当abs(当前距离)>seg/2时:
newcount=self.count-startcount
当前距离=角度-新计数*分段
当前速度=启动速度*当前距离/360
如果当前速度小于5:
R.motors[self.motor]。目标=5
其他:
R.motors[self.motor]。目标=当前速度
R.motors[self.motor]。目标=0
左车轮=编码器(0,0)
左车轮。开始()
WheelRight=编码器(1,3)
WheelRight.start()
右转。转数(720)
左轮。旋转(720)
sr模块由南安普敦大学提供,南安普敦大学负责竞赛。它允许我们与机器人的硬件进行交互

现在,创建的线程似乎允许分别监控两个反射传感器。此代码位:
R.io[0]。输入[self.pin]。query.d
计算来自反射率传感器的值是否已更改。“旋转”方法通过不断检查车轮已经旋转了多少度,并在到达终点时减速,从而将车轮旋转一定角度。我想两个轮子开始转动时,我运行的程序,然后放慢速度,停止时,他们已经通过了两个旋转。但目前,当我运行程序时,一个轮子开始转动并减速并停止,接着是另一个轮子。在我看来,“旋转”方法不像“运行”方法那样在线程中运行。是仅在线程中运行的“run”方法下的代码,还是整个类

如果有帮助的话,我一直遵循本教程:

另外,我想知道为什么只能用
编码器(0,0).start()启动线程。对于要创建的新线程,为什么不必使用类(例如
Thread=Encoder(0,0).start()
)创建对象

很抱歉,如果我使用的术语不符合标准,您可能会发现我对线程和编程非常陌生。

Encoder(0,0).start()
是对启动线程的方法的调用。反过来,此方法调用
运行
实现,该实现不使用
旋转
方法。如果要这样做,则必须在
运行
的while循环中调用它

使用
Thread=Encoder(0,0).start()
可以存储从该调用中检索到的值(无),但要获取该值,必须首先启动新线程。

Encoder(0,0).start()
是对启动线程的方法的调用。反过来,此方法调用
运行
实现,该实现不使用
旋转
方法。如果要这样做,则必须在
运行
的while循环中调用它

使用
Thread=Encoder(0,0).start()
可以存储从该调用中检索到的值(无),但要获取该值,必须首先启动新线程。

run方法是执行线程

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

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

但是,您必须非常小心它的生存期,因为没有局部变量使它保持活动状态。

run方法是执行线程

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

哦,
Encoder(0,0).start()确实创建了一个对象。仅仅因为你没有将该对象绑定到局部变量并不意味着它不存在。如果它不存在,你就不能调用它的
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)
然后,您可以执行以下操作:

wheelLeft = Encoder(0,0)
wheelRight = Encoder(1,3)

wheelRight.rotations(720)
wheelLeft.rotations(720)

wait_for(wheelRight & wheelLeft)
请注意,编码器本身不是一个线程-它是一个“has a”关系,而不是一个“is a”关系

您可以从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)
然后,您可以执行以下操作:

wheelLeft = Encoder(0,0)
wheelRight = Encoder(1,3)

wheelRight.rotations(720)
wheelLeft.rotations(720)

wait_for(wheelRight & wheelLeft)
请注意,编码器本身并不是一个线程——它是一个“hasaa”关系,而不是一个“isa”关系