當前位置: 首頁>>代碼示例>>Python>>正文


Python Motor.set_dir方法代碼示例

本文整理匯總了Python中motor.Motor.set_dir方法的典型用法代碼示例。如果您正苦於以下問題:Python Motor.set_dir方法的具體用法?Python Motor.set_dir怎麽用?Python Motor.set_dir使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在motor.Motor的用法示例。


在下文中一共展示了Motor.set_dir方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: MotorPot

# 需要導入模塊: from motor import Motor [as 別名]
# 或者: from motor.Motor import set_dir [as 別名]
class MotorPot(Thread):
    def __init__(self, bus, adc_addr=0x48, motor_pin1=L293_1, motor_pin2=L293_2, motor_enable = STEREO_L293_ENABLE, dirmult=1, verbose=False):
        Thread.__init__(self)

        self.motor = Motor(pin1=motor_pin1, pin2=motor_pin2, enable = motor_enable)
        self.motor.set_speed(0)

        self.adc = ADS1015(bus, adc_addr)

        self.adc.write_config(MUX_AIN0 | PGA_4V | MODE_CONT | DATA_1600 | COMP_MODE_TRAD | COMP_POL_LOW | COMP_NON_LAT | COMP_QUE_DISABLE)

        self.dirmult = dirmult

        self.setPoint = None
        self.newSetPoint = False
        self.moving = False

        self.daemon = True
        self.verbose = verbose

        self.lastStopTime = time.time()

        self.start()

    def set(self, value):
        self.setPoint = value
        self.newSetPoint = True

    def check_for_request(self):
        pass

    def handle_value(self):
        pass

    def run(self):
        lastStallValue = -1
        stallStack = []
        setPoint = None
        while True:
            self.check_for_request()

            self.value = self.adc.read_conversion()

            self.handle_value()

            if self.newSetPoint:
                setPoint = self.setPoint
                self.newSetPoint=False
                settle = 0
                stallStack = []

            if setPoint is not None:
                if (self.value < setPoint):
                    dir = 1
                else:
                    dir = -1

                # the 'P' part of a PID controller...
                error = abs(self.value - setPoint)
                if (error <= 1):
                    speed = 0

                    # are we done yet?
                    settle=settle+1
                    if (settle>32):
                        setPoint = None
                else:
                    settle = 0
                    if (error < 10):
                        speed = 50
                    elif (error < 25):
                        speed = 55
                    elif (error < 50):
                        speed = 65
                    elif (error < 100):
                        speed = 75
                    else:
                        speed = 100

                stallStack.insert(0, self.value)
                stallStack = stallStack[:250]
                minv = min(stallStack)
                maxv = max(stallStack)
                if (len(stallStack)>=250) and ((maxv-minv) < 25):
                    print "stalled at", self.value, maxv, minv
                    speed = 0
                    setPoint = None

                if self.verbose:
                    print "moving", self.value, setPoint, dir, speed

                self.motor.set_dir(dir * self.dirmult)
                self.motor.set_speed(speed)

                self.moving = True

                time.sleep(0.001)
            else:
                if self.moving:
                    self.lastStopTime = time.time()
#.........這裏部分代碼省略.........
開發者ID:sbelectronics,項目名稱:pi-stereo,代碼行數:103,代碼來源:motorpot.py


注:本文中的motor.Motor.set_dir方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。