使用Arduino和TB6612驱动编码电机实现测速功能

编码电机是一种常见的电机类型,它可以通过内置的编码器精确地测量电机的转速和位置。在本文中,我们将介绍如何使用Arduino和TB6612驱动编码电机,并实现测速功能。TB6612是一种双路直流电机驱动器,适用于控制小型直流电机。我们将使用Arduino作为主控制器,TB6612作为电机驱动器,并通过编码器获取电机的转速信息。

1.材料清单

Arduino Mega2560开发板
TB6612电机驱动模块
编码电机
杜邦线
电源(根据电机和驱动器的要求选择)

2.硬件连接:

将Arduino与TB6612驱动器和编码电机连接起来。根据驱动器和电机的引脚定义,将其正确连接到Arduino的数字引脚。确保连接正确无误,并检查供电是否稳定。
Mega2560引脚图
编码电机

arduino与编码电机接线图接线图:

Arduino --------- TB6612 -------- 编码电机
5V ---------------- VCC
GND -------------- GND
D3-------------------PWMA
D10-----------------STBY
D9 ----------------- AIN1
D8 ----------------- AIN2
NONE--------------A01--------------M-
NONE--------------A02--------------M+
D20 ----------------------------------- Channel A
D21 ----------------------------------- Channel B
5V---------------------------------------5V
GND------------------------------------GND

TB6612模块需要额外的12V电源来外部供电
12V--------------VM
GND------------GND

这只是一个示例接线图,实际的接线可能会根据使用的具体电机和驱动器而有所不同。请根据您的具体硬件和引脚定义来进行正确的连接。

编码电机与普通直流电机的区别就是编码电机带有编码器,可以实时反馈电机运动状态和位置信息,可以实现更精准的控制和定位,普通直流电机没有编码器,只能进行简单的启动和停止。所以编码电机比普通的电机更加精准,能更好的控制电机的运动。

编码电机上的编码器需要额外供电,arduino上恰好有外部输出口,可以直接将编码电机的5v和GND与arduino的5v和GND相连接

接下来我们试一下,控制编码电机正反转

int pwma = 3;
int ain1 = 9;
int ain2 = 8;
int stby = 10;
int pwmb = 5;
int bin1 = 6;
int bin2 = 7;
int led = 13;

void Stop(){
    
    
  digitalWrite(ain1,LOW);
  digitalWrite(ain2,LOW);
  digitalWrite(bin1,LOW);
  digitalWrite(bin2,LOW);
}
void up(){
    
    
  digitalWrite(ain1,HIGH);
  digitalWrite(ain2,LOW);
  digitalWrite(bin1,HIGH);
  digitalWrite(bin2,LOW);
}
void back(){
    
    
  digitalWrite(ain1,LOW);
  digitalWrite(ain2,HIGH);
  digitalWrite(bin1,HIGH);
  digitalWrite(bin2,LOW);
  delay(2000);
}
void setup() {
    
    
  // put your setup code here, to run once:
  pinMode(pwma,OUTPUT);
  pinMode(ain1,OUTPUT);
  pinMode(ain2,OUTPUT);
  pinMode(pwmb,OUTPUT);
  pinMode(bin1,OUTPUT);
  pinMode(bin2,OUTPUT);
  pinMode(stby,OUTPUT);
  pinMode(led,OUTPUT);
  digitalWrite(stby,HIGH);
  analogWrite(pwma,255);
  analogWrite(pwmb,255);
  Serial.begin(9600);
}
void loop() {
    
    
  // put your main code here, to run repeatedly:
  if(Serial.available()>0){
    
    
    char command = Serial.read();  // 读取蓝牙串口数据
    Serial.println(command);
    switch (command){
    
    
      case 'u':
      up();
      delay(2000);
      break;

      case 's':
      Stop();
      break;
      
      case 'b':
      back();
      delay(2000);
      break;
    }
}
}




这里我只接了一个编码电机,因此
int pwmb = 5;
int bin1 = 6;
int bin2 = 7;这部分代码可以不用管。

打开串口监视器,输入’u’,编码电机正转,‘b’,编码电机反转,'s’编码电机停止

完成这部分,我们来试下测速功能

int encoderA = 21;
int encoderB = 20;
volatile int count = 0;
void count_a(){
    
    
  if (digitalRead(encoderA)==HIGH){
    
    
    if (digitalRead(encoderB)==HIGH){
    
    
      count++;
    }
    else{
    
    
      count--;
    }
  }
}

void setup() {
    
    
  // put your setup code here, to run once:
  Serial.begin(57600);//串口波特率设置为57600
  pinMode(encoderA,INPUT);
  pinMode(encoderB,INPUT);
  attachInterrupt(2,count_a,CHANGE);
  
  

}

void loop() {
    
    
  // put your main code here, to run repeatedly:
  delay(2000);
  Serial.println(count);

}

测速方式采用的是单倍频测速,还有双倍频和四倍频可以采用

打开串口监视器,将波特率设为57600,用手转动电机,可以看到

在这里插入图片描述

说明运行成功了!!!

补充更加完善的测速代码,接线跟上面一样

int motor_A = 21;//中端口是2
int motor_B = 20;//中断口是3
volatile int count = 0;//如果是正转,那么每计数一次自增1,如果是反转,那么每计数一次自减1 


void count_A(){
    
    
  //单频计数实现
  //手动旋转电机一圈,输出结果为 一圈脉冲数 * 减速比
  /*if(digitalRead(motor_A) == HIGH){

    if(digitalRead(motor_B) == LOW){//A 高 B 低
      count++;  
    } else {//A 高 B 高
      count--;  
    }


  }*/

  //2倍频计数实现
  //手动旋转电机一圈,输出结果为 一圈脉冲数 * 减速比 * 2
  if(digitalRead(motor_A) == HIGH){
    
    

    if(digitalRead(motor_B) == HIGH){
    
    //A 高 B 高
      count++;  
    } else {
    
    //A 高 B 低
      count--;  
    }


  } else {
    
    

    if(digitalRead(motor_B) == LOW){
    
    //A 低 B 低
      count++;  
    } else {
    
    //A 低 B 高
      count--;  
    }  

  }

}

//与A实现类似
//4倍频计数实现
//手动旋转电机一圈,输出结果为 一圈脉冲数 * 减速比 * 4
void count_B(){
    
    
  if(digitalRead(motor_B) == HIGH){
    
    

    if(digitalRead(motor_A) == LOW){
    
    //B 高 A 低
      count++;
    } else {
    
    //B 高 A 高
      count--;
    }


  } else {
    
    

    if(digitalRead(motor_A) == HIGH){
    
    //B 低 A 高
      count++;
    } else {
    
    //B 低 A 低
      count--;
    }

  }

}

void setup() {
    
    
  Serial.begin(57600);//设置波特率  
  pinMode(motor_A,INPUT);
  pinMode(motor_B,INPUT);
  attachInterrupt(2,count_A,CHANGE);//当电平发生改变时触发中断函数
  //四倍频统计需要为B相也添加中断
  attachInterrupt(3,count_B,CHANGE);
}
int reducation = 90;//减速比,根据电机参数设置,比如 15 | 30 | 60
int pulse = 11; //编码器旋转一圈产生的脉冲数该值需要参考商家电机参数
int per_round = pulse * reducation * 4;//车轮旋转一圈产生的脉冲数 
long start_time = millis();//一个计算周期的开始时刻,初始值为 millis();
long interval_time = 50;//一个计算周期 50ms
double current_vel;

//获取当前转速的函数
void get_current_vel(){
    
    
  long right_now = millis();  
  long past_time = right_now - start_time;//计算逝去的时间
  if(past_time >= interval_time){
    
    //如果逝去时间大于等于一个计算周期
    //1.禁止中断
    noInterrupts();
    //2.计算转速 转速单位可以是秒,也可以是分钟... 自定义即可
    current_vel = (double)count / per_round / past_time * 1000 * 60;
    //3.重置计数器
    count = 0;
    //4.重置开始时间
    start_time = right_now;
    //5.重启中断
    interrupts();

    Serial.println(current_vel);

  }
}

void loop() {
    
    

  delay(10);
  get_current_vel();

}

猜你喜欢

转载自blog.csdn.net/m0_63715549/article/details/131511751