定时器不跳到默认值或下一个间隔
Timer does not hop to default value or next interval
在下面的代码中。该代码执行 运行 所有单独的行。
Interval 1 行将 运行 在 21.00 和 21.05hr 之间
Interval 2 行将 运行 在 22.00 和 22.05hr 之间
标准脉冲线将在所有其他时间范围内 运行。
问题:
该代码确实不会从间隔 1 -> 标准脉冲 -> 间隔 2 等跳跃。它保持 运行 代码开始到 运行.
的时间范围
有人可以帮我解决这个 python 时间问题吗?
这是代码:
from __future__ import division
from datetime import datetime, time
# Import the PCA9685 module.
import Adafruit_PCA9685
now = datetime.now()
# Initialise the PWM device using the default address
pwm = Adafruit_PCA9685.PCA9685()
# Note if you'd like more debug output you can instead run:
#pwm = PWM(0x40, debug=True)
servo_min = 300 # Min pulse length out of 4096
servo_max = 600 # Max pulse length out of 4096
def setServoPulse(channel, pulse):
pulseLength = 1000000 # 1,000,000 us per second
pulseLength /= 60 # 60 Hz
print "%d us per period" % pulseLength
pulseLength /= 4096 # 12 bits of resolution
print "%d us per bit" % pulseLength
pulse *= 1000
pulse /= pulseLength
pwm.set_pwm(channel, 0, pulse)
# Set frequency to 60hz, good for servos.
pwm.set_pwm_freq(60)
while True:
if now.time() >= time(21, 00, 00) and now.time() <= time(21, 05, 0):
print "Interval 1"
pwm.set_pwm(0, 0, servo_min)
elif now.time() >= time(22, 00, 0) and now.time() <= time(22, 05, 0):
print "Interval 2"
pwm.set_pwm(0, 0, servo_min)
else:
print "Standard pulse"
pwm.set_pwm(0, 0, servo_max)
根据文档,datetime.now()
returns 当前时间,因此在 now
中,变量始终仅在您启动程序时存储。尝试将 now = datetime.now()
放在 while 循环的顶部。
...
# Set frequency to 60hz, good for servos.
pwm.set_pwm_freq(60)
while True:
now = datetime.now()
if now.time() >= time(21, 00, 00) and now.time() <= time(21, 05, 0):
print "Interval 1"
pwm.set_pwm(0, 0, servo_min)
elif now.time() >= time(22, 00, 0) and now.time() <= time(22, 05, 0):
print "Interval 2"
pwm.set_pwm(0, 0, servo_min)
else:
print "Standard pulse"
pwm.set_pwm(0, 0, servo_max)
在下面的代码中。该代码执行 运行 所有单独的行。 Interval 1 行将 运行 在 21.00 和 21.05hr 之间 Interval 2 行将 运行 在 22.00 和 22.05hr 之间 标准脉冲线将在所有其他时间范围内 运行。
问题: 该代码确实不会从间隔 1 -> 标准脉冲 -> 间隔 2 等跳跃。它保持 运行 代码开始到 运行.
的时间范围有人可以帮我解决这个 python 时间问题吗?
这是代码:
from __future__ import division
from datetime import datetime, time
# Import the PCA9685 module.
import Adafruit_PCA9685
now = datetime.now()
# Initialise the PWM device using the default address
pwm = Adafruit_PCA9685.PCA9685()
# Note if you'd like more debug output you can instead run:
#pwm = PWM(0x40, debug=True)
servo_min = 300 # Min pulse length out of 4096
servo_max = 600 # Max pulse length out of 4096
def setServoPulse(channel, pulse):
pulseLength = 1000000 # 1,000,000 us per second
pulseLength /= 60 # 60 Hz
print "%d us per period" % pulseLength
pulseLength /= 4096 # 12 bits of resolution
print "%d us per bit" % pulseLength
pulse *= 1000
pulse /= pulseLength
pwm.set_pwm(channel, 0, pulse)
# Set frequency to 60hz, good for servos.
pwm.set_pwm_freq(60)
while True:
if now.time() >= time(21, 00, 00) and now.time() <= time(21, 05, 0):
print "Interval 1"
pwm.set_pwm(0, 0, servo_min)
elif now.time() >= time(22, 00, 0) and now.time() <= time(22, 05, 0):
print "Interval 2"
pwm.set_pwm(0, 0, servo_min)
else:
print "Standard pulse"
pwm.set_pwm(0, 0, servo_max)
根据文档,datetime.now()
returns 当前时间,因此在 now
中,变量始终仅在您启动程序时存储。尝试将 now = datetime.now()
放在 while 循环的顶部。
...
# Set frequency to 60hz, good for servos.
pwm.set_pwm_freq(60)
while True:
now = datetime.now()
if now.time() >= time(21, 00, 00) and now.time() <= time(21, 05, 0):
print "Interval 1"
pwm.set_pwm(0, 0, servo_min)
elif now.time() >= time(22, 00, 0) and now.time() <= time(22, 05, 0):
print "Interval 2"
pwm.set_pwm(0, 0, servo_min)
else:
print "Standard pulse"
pwm.set_pwm(0, 0, servo_max)