Найти - Пользователи
Полная версия: Время и математика
Начало » Python для новичков » Время и математика
1
xorkrus
Здравствуйте, имею такой скрипт:
#!/usr/bin/python3
# Test interrupts.
import select, time, sys
pin_base = '/sys/class/gpio/gpio17/'
def write_once(path, value):
    f = open('/tmp/spd_tmp', 'w')
    f.write(value)
    f.close()
    return
f = open(pin_base + 'value', 'r')
write_once(pin_base + 'direction', 'in')
write_once(pin_base + 'edge', 'falling')
po = select.poll()
po.register(f, select.POLLPRI)
state_last = f.read(1)
t1 = time.time()
sys.stdout.write('Initial pin value = {}\n'.format(repr(state_last)))
while 1:
    events = po.poll(60000)
    t2 = time.time()
    f.seek(0)
    state_last = f.read(1)
    if len(events) == 0:
        sys.stdout.write('  timeout  delta = {:8.4f} seconds\n'.format(t2 - t1))
    else:
        RPM = t2 - t1
        speed = RPM * 10 / 1000
        sys.stdout.write('T1={:8.4f}'.format(t1) +' T2={:8.4f}'.format(t2) + ' T2-T1={:8.4f}'.format(RPM) + ' RPM = {:8.4f} '.format(RPM) + ' Speed = {:8.4f}'.format(speed) + 'm/s [60/t2-t1={:8.4f}'.format(60/(t2 - t1)) +']\n')
        speed = 0
        t1 = t2
Прошу пояснения, почему у меня в выводе T2-T1 всегда пусто?
А ещё, как сделать чтобы на разницу T2-T1 < 0.01 скрипт не реагировал. Ну или реагировал по варианту if len(events) == 0:
Я пытался сделать что-то типа if t2-t1 < 0.01:, но t2-t1 у меня пусто и в итоге всякая фигня получается
С Уважением,
Shaman
Проблемы с точностью time.time()
http://stackoverflow.com/questions/85451/python-time-clock-vs-time-time-accuracy
xorkrus
Аналогично и с time.clock() - t2-t1 - выводится “пустота”
sys.stdout.write(' timeout delta = {:8.4f} seconds\n'.format(t2 - t1))
Тут выводится
sys.stdout.write('T1={:8.4f}'.format(t1) +' T2={:8.4f}'.format(t2) + ‘ T2-T1={:8.4f}’.format(RPM) + ‘ RPM = {:8.4f} ’.format(RPM) + ‘ Speed = {:8.4f}’.format(speed) + 'm/s \n')
А тут уже нет
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB