零知四轮小车入门版(ESP32主控)教程2-让小车跑起来

本系列教程讲解零知小车套件以零知esp32为主控,使用零知开源平台进行智能小车开发。

使用的硬件:零知智能小车套件

实现的功能:红外循迹、超声避障、直线运行、手机蓝牙控制等基本功能。

扩展:可以在小车上加入其它传感器模块进行扩展,比如加入摄像头模块进行监控、加入火焰传感器做灭火小车、加入更多超声波模块实现自动跟随...


本系列教程目录

零知四轮小车入门版(零知esp32主控)系列教程-小车组装


零知四轮小车入门版(零知esp32主控)系列教程-程序控制电机开跑


零知四轮小车入门版(零知esp32主控)系列教程-加入蓝牙实现手机控制


零知四轮小车入门版(零知esp32主控)系列教程-红外循迹走S弯


零知四轮小车入门版(零知esp32主控)系列教程-超声波自动避障


一、准备工作

我们需要如下工具和材料:

组装好的智能小车


二、程序代码

仅仅是组装好的小车还无法跑起来,要想让其跑起来,就得写程序告诉它怎么跑,什么时候跑。这里我们首先使用串口来试着控制小车,4WD小车代码实现如下:

							
							
	
	/**********************************************************
	*    文件: 4WD_car.ino      by 零知实验室([url=http://www.lingzhilab.com]www.lingzhilab.com[/url])
	*    -^^- 零知开源,让电子制作变得更简单! -^^-
	*    时间: 2019/11/19 16:35
	*    说明: 
	************************************************************/
	  
	#define LEFT_MOTO1  27
	#define LEFT_MOTO2  14
	#define LEFT2_MOTO1 32
	#define LEFT2_MOTO2 33
	#define RIGHT_MOTO1 12
	#define RIGHT_MOTO2 13
	#define RIGHT2_MOTO1 26
	#define RIGHT2_MOTO2 25
	  
	int pitch,roll;
	  
	void setup() {
	    Serial.begin(9600);
	    pinMode(LEFT_MOTO1,OUTPUT);
	    pinMode(LEFT_MOTO2,OUTPUT);
	    pinMode(LEFT2_MOTO1,OUTPUT);
	    pinMode(LEFT2_MOTO2,OUTPUT);
	    pinMode(RIGHT_MOTO1,OUTPUT);
	    pinMode(RIGHT_MOTO2,OUTPUT);
	    pinMode(RIGHT2_MOTO1,OUTPUT);
	    pinMode(RIGHT2_MOTO2,OUTPUT);
	   //设置引脚输出PWM的通道
	    ledcAttachPin(LEFT_MOTO1,1);
	    ledcAttachPin(LEFT_MOTO2,2);
	    ledcAttachPin(LEFT2_MOTO1,3);
	    ledcAttachPin(LEFT2_MOTO2,4);
	    ledcAttachPin(RIGHT_MOTO1,5);
	    ledcAttachPin(RIGHT_MOTO2,6);
	    ledcAttachPin(RIGHT2_MOTO1,7);
	    ledcAttachPin(RIGHT2_MOTO2,8);
	    ledcSetup(1, 12000, 8);
	    ledcSetup(2, 12000, 8);
	    ledcSetup(3, 12000, 8);
	    ledcSetup(4, 12000, 8);
	    ledcSetup(5, 12000, 8);
	    ledcSetup(6, 12000, 8);
	    ledcSetup(7, 12000, 8);
	    ledcSetup(8, 12000, 8);
	  
	}
	  
             void loop() {   
             //接收串口发送的数据
            if(Serial.available()>0){
          	pitch = Serial.parseInt();
                Serial.print("pitch=");
                Serial.print(pitch);
                motoRun(pitch);
        	}
          }
	  
	
void motoRun(int type){
	//1 前进 2后退 3右转弯 4左转弯
	//其中 pwm通道1 2 控制左后轮  3 4通道控制左前轮  5 6通道控制右后轮  7 8通道右前轮
	if(type==1){
		ledcWrite(1,180);
		ledcWrite(2,0);
		ledcWrite(3,-180);
		ledcWrite(4,0);
		ledcWrite(5,0);
		ledcWrite(6,180);
		ledcWrite(7,180);
		ledcWrite(8,0);
		Serial.println("-----前进----");
	}else if(type==2){
		ledcWrite(1,0);
		ledcWrite(2,180);
		ledcWrite(3,0);
		ledcWrite(4,180);
		ledcWrite(5,180);
		ledcWrite(6,0);
		ledcWrite(7,0);
		ledcWrite(8,180);
		Serial.println("-----后退----");
	}else if(type==3){
		ledcWrite(1,0);
		ledcWrite(2,0);
		ledcWrite(3,0);
		ledcWrite(4,0);
		ledcWrite(5,0);
		ledcWrite(6,180);
		ledcWrite(7,180);
		ledcWrite(8,0);
		Serial.println("-----左转----");
	}else {
		ledcWrite(1,180);
		ledcWrite(2,0);
		ledcWrite(3,-180);
		ledcWrite(4,0);
		ledcWrite(5,0);
		ledcWrite(6,0);
		ledcWrite(7,0);
		ledcWrite(8,0);
		Serial.println("-----右转----");
	}
}
	  
	void motoStop(){
	    ledcWrite(1,0);
	    ledcWrite(2,0);
	    ledcWrite(3,0);
	    ledcWrite(4,0);
	    ledcWrite(5,0);
	    ledcWrite(6,0);
	    ledcWrite(7,0);
	    ledcWrite(8,0);
	}							
											
								

注意:由于每个人的接线都会有些不同,所以电机的引脚需要根据自己的小车的情况来更改。

将以上代码验证上传至零知ESP32上,然后通过串口发送1,2,3,4就可以控制小车前进,后退,左转,右转了,我们可以根据轮子的转动情况来修改代码中电机的引脚。