自学内容网 自学内容网

Arduino UNO R3自学笔记20 之 Arduino如何测定电机速度?

注意:学习和写作过程中,部分资料搜集于互联网,如有侵权请联系删除。

前言:在学习了Arduino的相关基础知识后,现在做个综合应用,给旋转的电机测速。

1.实验目的

        测定旋转电机的转速

2.实验器材-编码器

        要想测定电机的转速,那么就要有个器件在电机旋转过程中其转动的角速度与这个器件的输出线性相关的。这个器件呢就是编码器。

        我们先说一下编码器,简单来说,电机的主轴旋转一圈,编码器输出n个脉冲,信号中有n个上升沿,n个高电平,n个下降沿,n个低电平。

        这样的话,对于Arduino有四种出发方式:RISING 、FALLING、LOW 、CHANGE 。则Arduino采用上述编码器时,电机旋转1圈对应的触发数量如图所示:

                                RISING             n个

                                FALLING           n个

                                LOW                  n个

                                CHANGE           2n个

注:此处采用中断测量触发数量,更加准确。

3.实验思路

        既然要测速,那么就需要知道在一定时间T内电机转过的圈数,则转速n_{motor}就是:

                                                        n_{motor}=\frac{n_{0}}{t},其中n_{0}为圈数,t为对应的时间。

        圈数n_{0}则根据电机旋转时测得的脉冲数n_{t}与电机转一圈脉冲的数量n计算:

                                        ​​​​​​​        ​​​​​​​        n_{0}=\frac{n_{t}}{n}

        则转速n_{motor}就是:

        ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​        n_{motor}=\frac{n_{t}}{t*n}                  单位:r/s​​​​​​​

                                                         n_{motor}=\frac{n_{t}}{t*n}*60       单位:r/min

最终,计算n_{motor}我们需要知道:

                ​​​​​​​        ​​​​​​​        ​​​​​​​                       n_{t} 测得的脉冲数

                                                         ​​​​​​​t:为对应的时间

                                                        n:机转一圈脉冲的数量

上述三个量,其中,我们可以通过程序在t时间内测定n_{t},n为电机编码器的参数,一般会给定。

4.实验电路(Proteus仿真图)

电机参数:

5.仿真程序

#include<MsTimer2.h>

#define moto_in1 7
#define moto_in2 8
#define moto_pwm 9

#define moto_num1 2
#define moto_num2 3

float num1=0;
float num2=0;

int interrupt_time = 40;
bool num_over_flag=0;

void speed_c1();
void speed_c2();
void int_time();

void setup()
 { // put your setup code here, to run once:
    Serial.begin(9600);
pinMode(moto_in1,OUTPUT);
pinMode(moto_in2,OUTPUT);
pinMode(moto_pwm,OUTPUT);
pinMode(moto_num1,INPUT);
pinMode(moto_num2,INPUT);
    noInterrupts();
    attachInterrupt( 0,speed_c1,CHANGE );
    attachInterrupt( 1,speed_c2,CHANGE);
    MsTimer2::set(interrupt_time,int_time);   
    MsTimer2::start(); 
    interrupts();//开所有中断,准备进入程序运行模式
 }

void loop()
 { // put your main code here, to run repeatedly:
 
   digitalWrite(moto_in1,HIGH);
   digitalWrite(moto_in2,LOW);
   analogWrite(moto_pwm,150);

 }

void speed_c1()
{
  num1++;
}

void speed_c2()
{
  num2++;
}

void int_time()
{
float speed_real = (num1+num2)/(1200*0.04)*60;
 Serial.print(speed_real);
 Serial.print("   ");
 num1=0;
 num2=0;
}

6.测试视频

proteus 仿真电机测速

上一篇:Arduino UNO R3自学笔记19 之 PID基础知识简介?

下一篇:​​​​​​​Arduino UNO R3自学笔记21 之 Arduino电机的闭环控制​​​​​​​


原文地址:https://blog.csdn.net/FKL1054934525/article/details/142611456

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!