从 Arduino 计算 Python 中的每分钟转数 [英] Calculating revolutions per minute in Python from an Arduino

查看:19
本文介绍了从 Arduino 计算 Python 中的每分钟转数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 arduino 的簧片开关在 python 中计算 rpm.我似乎做对了.有人可以帮我把代码弄对吗.我的结果不一致.

I'm trying to calculate rpm's in python via a reed switch from the arduino. i cant seem to get it right. can some one please help me get the code right. my results are not consistent.

我的问题是我试图计算每分钟的转数,但我对分钟部分有问题.每次通过簧片开关时,转数计数器工作正常,计数增加 1、2、3、4、5、6(等).但是,rpm 部分是 rev/((time.time() - t)/60) 给我的结果是,根据旋转速度,该值的增加或减少速度不够快.所以当簧片开关以每分钟 30 转的速度通过时,它应该发出 30 RPM;当簧片开关以每分钟 90 转的速度通过时,它应该发出 90 RPM.我知道我需要计算每一分钟的转数,我想这是我很难编程的每一分钟部分.

my problem is I'm trying to count the revolution per minute but I'm having issues with the minute part. the revolution counter works OK every time the reed switch is passed the count increases 1,2,3,4,5,6(etc). but, the rpm part witch is rev / ((time.time() - t)/60) gives me a result that keeps increases the value does not increase or decrease fast enough according to the speed of the revolution. so when when the reed switch is passed at 30 rev per minute it should reed out 30 RPM; when when the reed switch is passed at 90 rev per minute it should reed out 90 RPM. i know that i need to get a count of revolution for every minute, i guess its the ever minute part that i having a hard time programing.

感谢您帮助我解决这困扰我数月的问题.

thanks for helping me out this has been bothering me for months now.

import serial
import time
ser = serial.Serial('/dev/ttyACM0', 9600) 
t = time.time()
a = ser.read()

rev = 0

while  (a == ser.read()):


    rev += 1
    print rev / ((time.time() - t)/60)

新问题,现在我正在尝试添加两个簧片开关.我可以让两者分别工作,但是当我通过第二个簧片开关时,我的脚本终止了.我现在为什么要终止脚本(一个如果为真,另一个为假终止),但我不确定如何正确编码.b'1' 和 b'2' 是来自 arduino 的

New problem, now I'm trying to add two reed switches. i can get both to work separately, but when i go to pass the second reed switch i get script terminated. i now why I'm getting the script termination( one if true other is false terminate), but I'm not sure how to code it properly. the b'1' and b'2' is what is coming in from the arduino

我的目标是让两个芦苇同时阅读.

my goal is to get both reeds to read at the same time.

import serial
import time
ser = serial.Serial('/dev/ttyACM0', 9600) 
a = ser.read()
t0 = time.time()


while(a == ser.read()):

    if a == b'2':
        t1 = time.time()
        try:
            print(1 / ((t1-t0)/60))
        except ZeroDivisionError:
            pass
        t0 = t1
    if a == b'1':
        t1 = time.time()
        try:
            print(1 / ((t1-t0)/60))
        except ZeroDivisionError:
            pass
        t0 = t1

我没有工作的代码,所以你可以了解我正在尝试做什么.如果我能以某种方式打破 b'1' 和 b'2'.在字体对象部分的下方,您将看到 m 和 r 需要去

my none working code so you can get an idea of what I'm trying to do. if i could somehow break out the b'1' and b'2'. down below in the font object part you will see were m and r will need to go

import serial
import time
ser = serial.Serial('/dev/ttyACM0', 9600)
import pygame, sys
from pygame.locals import *
t0 = time.time()

pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('MPH!')

WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
BLUE = (148, 228, 255)
BLACK = (0, 0, 0)


while True:
    a = ser.read()

    if a == b'2':

        t1 = time.time()
        try:
            rpmm = (1 / ((t1-t0)/60))
            m = str((7.065 *rpmm * 60)/5280).strip("\n,\r")

        except ZeroDivisionError:
            pass
        t0 = t1
    if a == b'1':
        t1 = time.time()
        try:
            r = str((1 / ((t1-t0)/60))).strip("\n,\r")


        except ZeroDivisionError:
            pass
        t0 = t1



    fontObj = pygame.font.SysFont('digitaldream', 40)
    textSurfaceObj = fontObj.render('MPH:'+m, True, BLUE, BLACK)
    textRectObj = textSurfaceObj.get_rect()
    textRectObj.center = (200, 150)

    fontObj2 = pygame.font.SysFont('digitaldream', 40)
    textSurfaceObj2 = fontObj2.render('RPM:'+r, True, BLUE, BLACK)
    textRectObj2 = textSurfaceObj2.get_rect()
    textRectObj2.center = (200, 200)




    DISPLAYSURF.fill(BLACK)
    DISPLAYSURF.blit(textSurfaceObj, textRectObj)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()

推荐答案

现在,您的代码将输出自阅读开始以来的平均 RPM.如果 ser.read() 每次旋转返回一次,你可以做这样的事情来获得瞬时旋转速度:

For now, your code will output the average RPM since the reading started. If ser.read() returns one time for every rotation, you can do something like this to get the instantaneous rotation speed:

import serial
import time
ser = serial.Serial('/dev/ttyACM0', 9600) 
a = ser.read()
t0 = time.time()

while(a == ser.read()):
    t1 = time.time()
    try:
        print(1 / ((t1-t0)/60))
    except ZeroDivisionError:
        pass
    t0 = t1

如果你看看你的新代码,你应该意识到变量 a 在 while 循环中没有分配任何新值.您只需检查 ser.read() 是否等于它在进入 for 循环之前的值.这意味着如果 ser.read() 取另一个值而不是它在进入 while 循环之前的值,则语句 a == ser.read() 将是 <代码>假.

If you take a look at your new code, you should realize that the variable a is not assigned any new value during the while loop. You only check if ser.read() is equal to the value it had before you entered the for loop. This means that if ser.read() takes another value than what it had prior to entering the while loop, the statement a == ser.read() will be False.

此代码适用于 ser.read() 的两个值.

This code will work for both values of ser.read().

import serial
import time


ser = serial.Serial('/dev/ttyACM0', 9600) 
t0 = time.time()

while True:
    a = ser.read()
    if a == b'2' or a == b'1':
        t1 = time.time()
        try:
            print(1 / ((t1-t0)/60))
        except ZeroDivisionError:
            pass
        t0 = t1
    else:
        break

这篇关于从 Arduino 计算 Python 中的每分钟转数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆