在 Python 3 中将齿轮箱的转数转换为 RPM

Convert revolutions of a gearbox to RPM in Python 3

我正在 Python3 中构建一个机器人。我有一个引擎报告其变速箱在更长时间的完全停止之间的总转数:

import ftrobopy    
Motor1 = Robo.motor(1)
Motor1.getCurrentDistance()

所以在暂停之后,这个值将从 0 开始,然后上升到任何值(它很快达到六位数),直到它在某个时候重置。

现在我想 运行 一些依赖于 rpm 的代码。当 RPM 超过一定值时,IE 换到更高档位。所以我需要找到一种方法将电机报告的转数转换为每个时间间隔的转数。

我需要一些与 rpm 的短期变化密切相关的东西,反映过去几秒钟发生的事情。我不确定如何实现它。

我已尝试实施此解决方案:

Calculating revolutions per minute in Python from an Arduino

删除序列部分并使用 Motor1.getCurrentDistance() for = a.

但它似乎根本无法满足我的要求。如果您能给我任何指示,将不胜感激。

EDIT2:这是当前的状态:

from __future__ import print_function
import time
import ftrobopy
import threading


txt=ftrobopy.ftrobopy('auto')

joystick1 = txt.joystick(0,1,1)
joystick2 = txt.joystick(1,1,1)
Motor1 = txt.motor(1)
Motor1.setDistance(0)
txt.updateWait()

def infiniteloop1():  # this controls the motor via a remote
    while True:
        Motor1.setSpeed(joystick1.leftright() * 512)


def infiniteloop2():  # this is for the rpm meter
    while True:
        curRev = Motor1.getCurrentDistance()
        t0 = time.time()
        lastRev = 0

        while (curRev == Motor1.getCurrentDistance()):
            t1 = time.time()
            try:
                rpm = (curRev - lastRev / ((t1 - t0) / 4320))
                print("RPM: " + "%.1f" % rpm)
                print("RevsTotal: " + str(Motor1.getCurrentDistance()))
                time.sleep(5)
                txt.updateWait()

            except ZeroDivisionError:
                pass
            t0 = t1
            lastRev = curRev
            txt.updateWait()






thread1 = threading.Thread(target=infiniteloop1)
thread1.start()

thread2 = threading.Thread(target=infiniteloop2)
thread2.start()

当我执行此操作时,它将以 RPM 和 TotalRev = 0 开始,然后 RPM 和 TotalRev 保持相同的值 (100..200..)。当我停止电机时,TotalRev 保持在最后一个值,RPM 变成一个负数,随着时间的推移会慢慢变大。我一定是哪里搞砸了?

您不计算电机执行的转数,只计算经过的时间

rpm = (1 / ((t1 - t0) / 60))

您需要包含一个变量来存储最后的转数,然后从当前转数中减去该数字。

def infiniteloop2():  # this is for the rpm meter
    while True:
        Motor1 = txt.motor(1)
        a = Motor1.getCurrentDistance()
        t0 = time.time()
        lastRev = 0

        while (curRev == Motor1.getCurrentDistance()):
            t1 = time.time()
            try:
                rpm = (curRev - lastRev / ((t1 - t0) / 60))
                print("RPM: " + "%.1f" % rpm)

            except ZeroDivisionError:
                pass
            time.sleep(2)
            t0 = t1
            lastRev = curRev