最近在用ESP32-CAM做一个小型机器人,使用的是arduino的IDE,因为刚刚开始,而且自己也是一个菜鸟小白,所以在这里记录一下学习的过程,以及向大家说明一下自己遇到的坑。
现在项目是进展到使用串口控制几个电机的转速。相关实物如图,比较简陋。
下面四个是电机。
但是目前遇到的问题就是这四个电机的运动会给串口通信带来乱码,目前还没找到具体原因。相应的代码如下:
#include<Arduino.h>
int channel1 = 1;//使用ESP32的第一个PWM通道
int channel2 = 2;//使用ESP32的第二个PWM通道
int channel3 = 3;//使用ESP32的第三个PWM通道
int channel4 = 4;//使用ESP32的第四个PWM通道
int freq = 50;//PWM频率
int resolution = 12;//PWM分辨率
int Spee;
const int motor1 = 12;//设置GPIO12口驱动电机1
const int motor2 = 13;//设置GPIO13口驱动电机2
const int motor3 = 14;//设置GPIO14口驱动电机3
const int motor4 = 15;//设置GPIO15口驱动电机4
void setup()
{
// put your setup code here, to run once:
pinMode(4,OUTPUT);//用于检测程序是否执行
// put your setup code here, to run once:
ledcSetup(channel1, freq, resolution); // 设置通道1
ledcAttachPin(motor1, channel1); // 将通道1与对应的电机1引脚关联
ledcSetup(channel2, freq, resolution); // 设置通道2
ledcAttachPin(motor2, channel2); // 将通道2与对应的电机2引脚关联
ledcSetup(channel3, freq, resolution); // 设置通道2
ledcAttachPin(motor3, channel3); // 将通道2与对应的电机2引脚关联
ledcSetup(channel4, freq, resolution); // 设置通道2
ledcAttachPin(motor4, channel4); // 将通道2与对应的电机2引脚关联
Serial.begin(56000);
while(Serial.read()>=0){}
}
void forth(int T,int Speed)
{
// for(int dutyCycle=307;dutyCycle>=250;dutyCycle=dutyCycle-10)
// {
// ledcWrite(channel1,dutyCycle);//设置通道1占空比
// ledcWrite(channel2,dutyCycle);//设置通道2占空比
// ledcWrite(channel3,dutyCycle);//设置通道3占空比
// ledcWrite(channel4,dutyCycle);//设置通道4占空比
// delay(30);
// }
ledcWrite(channel1,Speed);
ledcWrite(channel2,Speed);
ledcWrite(channel3,Speed);
ledcWrite(channel4,Speed);
digitalWrite(4,HIGH);
delay(T);
// for(int dutyCycle=250;dutyCycle<=307;dutyCycle=dutyCycle+10)
// {
// ledcWrite(channel1,dutyCycle);//设置通道1占空比
// ledcWrite(channel2,dutyCycle);//设置通道2占空比
// ledcWrite(channel3,dutyCycle);//设置通道3占空比
// ledcWrite(channel4,dutyCycle);//设置通道4占空比
// delay(30);
// }
}
int getSpeed()
{
int comInt;
if(Serial.available()>0){
delay(100);
comInt = Serial.parseInt();
Serial.print("the result of Serial is:");
Serial.println(comInt);
delay(100);
}
// clear serial buffer
while(Serial.read() >= 0){}
delay(5);
return comInt;
}
void loop()
{
// put your main code here, to run repeatedly:
//digitalWrite(4,HIGH);
if(Serial.available()>0)
{
Spee = getSpeed();
digitalWrite(4,LOW);
}
delay(5);
// while(Serial.available()<0)
forth(10000,Spee);
}
因为本人是个菜鸟,所以还没有养成给代码进行注释的好习惯,接下来会慢慢注意。
四个电机口分别是12、13、14、15。
在进行串口调试时,会出现如下乱码的情况:
为了搞清楚电机转动对串口的影响,我先用单个电机转动对其进行测试。测试代码如下:
#include<Arduino.h>
int number1;
int number2;
int result;
int channel1=1;
int freq = 50;//PWM频率
int resolution = 12;//PWM分辨率
int Spee;
const int motor1 = 12;//设置GPIO12口驱动电机1
void setup() {
// put your setup code here, to run once:
Serial.begin(56000);
pinMode(4,OUTPUT);
ledcSetup(channel1, freq, resolution); // 设置通道1
ledcAttachPin(motor1, channel1); // 将通道1与对应的电机1引脚关联
while(Serial.read() >= 0){}
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0)
{
delay(100);
number1=Serial.parseInt();
Spee=number1;
Serial.print("number1=");
Serial.println(number1);
delay(10);
while(Serial.read() >= 0){}
}
if(number1>300)
{
digitalWrite(4,HIGH);
ledcWrite(channel1,Spee);
}
else
{
digitalWrite(4,LOW);
ledcWrite(channel1,Spee);
}
}
通过ESP32-CAM上的LED灯来间接反映电机旋转的问题,通过几次测试,发现单个电机的转动有时也会出现串口乱码的问题,其中原因是USB口与电脑的接触不良。如果出现该问题,可以尝试将接口拔出来再重新插入。电机转动时的振动会加剧这个原因,所以很容易出现该问题。