首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >修复nxt-python中的turn方法

修复nxt-python中的turn方法
EN

Stack Overflow用户
提问于 2012-10-17 13:54:11
回答 1查看 1.1K关注 0票数 1

我希望当我的机器人遇到障碍物时能够阻止它移动。但是,motor.py下的SynchronizedMotors类中的turn方法不允许我这样做。我该怎么解决这个问题呢?有什么直接的解决方案吗?我确实尝试过使用线程,但它不起作用。

EN

回答 1

Stack Overflow用户

发布于 2013-03-12 10:29:13

我可以毫不费力地让伺服系统对传感器输入做出反应。下面的操作将使机器人全速前进,直到一个或多个触摸传感器被按下,然后它停止,等待片刻,然后切断伺服系统的所有电源。

代码语言:javascript
复制
from time import sleep
import nxt

from nxt.motor import Motor, PORT_B, PORT_C
from nxt.sensor import Touch, PORT_1, PORT_2

class Robot(object):

    def __init__(self, brick):

        self.brick = brick

        # left and right motors
        self.lmotor = Motor(brick, PORT_B)
        self.rmotor = Motor(brick, PORT_C)

        # left and right touch sensors
        self.ltouch = Touch(brick, PORT_1)
        self.rtouch = Touch(brick, PORT_2)

    def roll(self, left, right):
        '''
        Non-blocking function for modifying power to servos.
        The servos will continue to operate at these power
        levels until told to do otherwise. Values are given
        as percentages of full power, so torque will change
        as the battery loses charge. A negative value will
        reverse the rotation of the respective servo.
        For example, to spin on the spot, clockwise and at
        full power, do

            self.roll(100, -100)

        '''
        self.lmotor.run(left)
        self.rmotor.run(right)

    def halt(self):
        '''
        Stops and then holds both servos steady for
        0.2 seconds, then cuts power to both servos.
        '''
        self.lmotor.brake()
        self.rmotor.brake() 
        sleep(0.2)
        self.lmotor.idle()
        self.rmotor.idle()


brick = nxt.find_one_brick(name='R2')
robot = Robot(brick)

robot.roll(100, 100)
while True:
    if robot.ltouch.is_pressed() or robot.rtouch.is_pressed():
        robot.halt()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12927956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档