本文整理汇总了Python中motor.Motor.set_speed方法的典型用法代码示例。如果您正苦于以下问题:Python Motor.set_speed方法的具体用法?Python Motor.set_speed怎么用?Python Motor.set_speed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类motor.Motor
的用法示例。
在下文中一共展示了Motor.set_speed方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MotorPot
# 需要导入模块: from motor import Motor [as 别名]
# 或者: from motor.Motor import set_speed [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()
#.........这里部分代码省略.........