当前位置: 首页>>代码示例>>Python>>正文


Python Engine.get_speed方法代码示例

本文整理汇总了Python中Engine.get_speed方法的典型用法代码示例。如果您正苦于以下问题:Python Engine.get_speed方法的具体用法?Python Engine.get_speed怎么用?Python Engine.get_speed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Engine的用法示例。


在下文中一共展示了Engine.get_speed方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: import Engine [as 别名]
# 或者: from Engine import get_speed [as 别名]
class Controller:
    def __init__(self):
        # Storing the engines of this car
        self.__leftengine = Engine("A")
        self.__rightengine = Engine("B")
        self.__engines = [self.__leftengine, self.__rightengine]
        # Storing the distance between the centers of the cars
        self.__widthcar = 2.6 + 11.1 - 0.2
        self.__gearratio = 1.0
        # Storing the perimeter of the wheels (2*pi*r)
        self.__perimeter = 2 * math.pi * 2.579
        # Storing a reference to a brickpi thread
        self.__brickpi = BrickPi_Thread(self.__engines)
        self.__command_going = False
        self.__command_thread = None

    # Start a commands
    # If there is already a command going, stop that first
    # Start thread
    # @post self.__command_going = True
    # @post self.__command_thread = new thread
    def start_command(self, command, arguments=None):
        if self.__command_going:
            self.stop_command()
        self.__brickpi.on()
        self.__command_going = True
        if arguments != None:
            thread = threading.Thread(target=command, args=arguments)
        else:
            thread = threading.Thread(target=command)
        self.__command_thread = thread
        self.__command_thread.setDaemon("True")
        self.__command_thread.start()

    # A method to stop the going command
    # if there is a command going stop it
    # @post self.__command_going = False
    # @post self.__command_thread = None
    def stop_command(self):
        if self.__command_going:
            self.__brickpi.off()
            self.__command_going = False
            self.__command_thread.join()
            self.__command_thread = None

    def BrickPi_start(self):
        self.__brickpi.on()

    # A method to set the speed of the engines
    # If the number of speeds don't equal the number of engines an error is raised
    # def set_speed_engines(self,speed):
    #    if len(speed) != len(self.__engines):
    #        raise Exception
    #    engines = self.__engines
    #    for i in range(engies):
    #        engines[i].set_speed(speed[i])
    # A method to flush the counter values of the engines
    def flush_engines(self):
        for engine in self.__engines:
            engine.reset_count()

    # return the distance traveled by the engines
    def get_engine_distance(self):
        result = []
        for engine in self.__engines:
            result.append(engine.get_count() * self.__perimeter * self.__gearratio)
        return result

    # Turn the thread back off
    def kill_threads(self):
        self.__brickpi.off()

    ## Return the values of the sensor in a dictionary with keys
    ## Distancesensor1 -> GPIO distance sensor
    ## Distancesensor2 -> Lego MINDSTORM distance sensor
    def get_sensor_data(self):
        result = dict()
        result["SpeedLeft"] = self.__leftengine.get_speed()
        result["SpeedRight"] = self.__rightengine.get_speed()
        ## result['Top angle'] = (self.__topengine.get_count()%1) *2*math.pi
        return result

    # A method to drive forward
    def forward(self):
        pid = PID.PID(15.0, 1 / 2.0, 2.0, 0.1)
        self.__leftengine.set_speed(240)
        self.__rightengine.set_speed(240)
        self.__leftengine.reset_count()
        self.__rightengine.reset_count()
        while self.__command_going:
            distance1 = self.__leftengine.get_count() * self.__perimeter * self.__gearratio
            distance2 = self.__rightengine.get_count() * self.__perimeter * self.__gearratio
            speeddif = pid.new_value(distance1 - distance2, 0.1)
            self.__rightengine.set_speed(240 + speeddif)
            time.sleep(0.05)
        self.__leftengine.set_speed(0)
        self.__rightengine.set_speed(0)

    # A mehtod to drive backward
    def backward(self):
#.........这里部分代码省略.........
开发者ID:PO-Purple,项目名称:po-purple,代码行数:103,代码来源:Controller.py


注:本文中的Engine.get_speed方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。