Tutorial II: Motor With Encoder Control Demo 2

From Waveshare Wiki
Revision as of 04:01, 25 March 2024 by Eng52 (talk | contribs) (Created page with "<div class="wiki-pages jet-green-color"> ==Motor With Encoder Control Demo 2== This tutorial is for controlling the forward and reverse, fast and slow rotation of a motor, the...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Motor With Encoder Control Demo 2

This tutorial is for controlling the forward and reverse, fast and slow rotation of a motor, the following provides demos for controlling the rotation of a motor.

Demo

Upload Demo

After downloading the motorCtrl.ino, use the USB cable to connect the multifunctional driver board and the computer (here inserted into the USB Type-C port of the multifunctional driver board), click on "Tools" → "Ports", and then click on the newly appeared COM port.
UGV1 doenload03EN.png
In Arduino IDE, click "Tools" → "Development Board" → "ESP32" → "ESP32 Dev Module", select the development board and the port and then upload the demo. After uploading the demo, connect the motor interface PH2.0 2P on the motor driver board to the motor without an encoder. Connect the XH2.54 power port to the power supply. Upon doing this, you will observe the motor rapidly rotating in the positive direction for 3 seconds, then slowly rotating in the opposite direction for 3 seconds, followed by a pause of 3 seconds, all in a continuous loop.

Demo Analysis

// 以下定义了用于控制TB6612的ESP32引脚
// A路电机
const uint16_t PWMA = 25;         // Motor A PWM control  Orange
const uint16_t AIN2 = 17;         // Motor A input 2      Brown
const uint16_t AIN1 = 21;         // Motor A input 1      Green

// B路电机
const uint16_t BIN1 = 22;        // Motor B input 1       Yellow
const uint16_t BIN2 = 23;        // Motor B input 2       Purple
const uint16_t PWMB = 26;        // Motor B PWM control   White

// 用于PWM输出的引脚的PWM频率
int freq = 100000;

// 定义PWM通道
int channel_A = 5;
int channel_B = 6;

// 定义PWM的精度,精度为8时,PWM数值在0-255(2^8-1)
const uint16_t ANALOG_WRITE_BITS = 8;
// 最大的PWM数值
const uint16_t MAX_PWM = pow(2, ANALOG_WRITE_BITS)-1;
// 最小的PWM数值,由于直流电机的低速特性一般比较差,过低的PWM达不到电机的转动阈值
const uint16_t MIN_PWM = MAX_PWM/5;


void setup(){
  // 设置用于控制TB6612FNG的ESP32引脚的工作模式
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
  pinMode(PWMB, OUTPUT);

  // 设置用于PWM输出的ESP32引脚的通道、频率和精度
  ledcSetup(channel_A, freq, ANALOG_WRITE_BITS);
  ledcAttachPin(PWMA, channel_A);

  ledcSetup(channel_B, freq, ANALOG_WRITE_BITS);
  ledcAttachPin(PWMB, channel_B);

  // 用于控制转动的引脚置于低电平,电机停止转动,避免初始化后立即开始转动
  digitalWrite(AIN1, LOW);
  digitalWrite(AIN2, LOW);
  digitalWrite(BIN1, LOW);
  digitalWrite(BIN2, LOW);
}


// A路电机控制
void channel_A_Ctrl(float pwmInputA){
  // 将pwmInput值取整
  int pwmIntA = round(pwmInputA);
  if(pwmIntA == 0){
    digitalWrite(AIN1, LOW);
    digitalWrite(AIN2, LOW);
    return;
  }

  // 判断pwmInput值的正负来确定旋转方向
  if(pwmIntA > 0){
    digitalWrite(AIN1, LOW);
    digitalWrite(AIN2, HIGH);
    // constrain()函数用于将pwmIntA的值限制在MIN_PWM与MAX_PWM之间
    ledcWrite(channel_A, constrain(pwmIntA, MIN_PWM, MAX_PWM));
  }
  else{
    digitalWrite(AIN1, HIGH);
    digitalWrite(AIN2, LOW);
    ledcWrite(channel_A,-constrain(pwmIntA, -MAX_PWM, -MIN_PWM));
  }
}

// B路电机控制
void channel_B_Ctrl(float pwmInputB){
  int pwmIntB = round(pwmInputB);
  if(pwmIntB == 0){
    digitalWrite(BIN1, LOW);
    digitalWrite(BIN2, LOW);
    return;
  }

  if(pwmIntB > 0){
    digitalWrite(BIN1, LOW);
    digitalWrite(BIN2, HIGH);
    ledcWrite(channel_B, constrain(pwmIntB, MIN_PWM, MAX_PWM));
  }
  else{
    digitalWrite(BIN1, HIGH);
    digitalWrite(BIN2, LOW);
    ledcWrite(channel_B,-constrain(pwmIntB, -MAX_PWM, -MIN_PWM));
  }
}


void loop(){
  // 电机停转3秒钟
  channel_A_Ctrl(0);
  channel_B_Ctrl(0);
  delay(3000);

  // 电机反方向,以低速转3秒钟
  channel_A_Ctrl(-64);
  channel_B_Ctrl(-64);
  delay(3000);

  // 电机正方向,以最高转速转3秒钟
  channel_A_Ctrl(255);
  channel_B_Ctrl(255);
  delay(3000);
}

资料