|
本帖最后由 gyq-svip 于 2021-8-19 18:39 编辑
第一次发帖还是一个很简单的程序
代码很长 其实就是动作太多 所以............
原理很简单 其实就是手机app 给blinker服务器(MQTT)发消息,也就是发布与订阅
MQTT
发布者就是手机APP,订阅者就是ESP8266,这样手机端发消息小车(esp8266)收到指令后进行相关操作,
只要安找接线图接线都可以的.如图:
L298N接线图
因为麦克纳姆轮是四驱车所以需要4个轮子,在这里我给取名为ABCD,ABCD四个电机的正负极分别为A1A2,B1B2,C1C2,D1D2
如图所示L298N,A1A2接直流电机1口 简单点就是如图的的方向 从上到下接线分别为A1A2,B1B2,C1C2,D1D2,这个接线很重要 这是电机的接线,也可以按键btn-s 这个按键就是测试轮子正反转的 如果每个轮子都是先正转在反转 那就恭喜你接线没问题了 不对的话就调整下就可以了
开发版的接线图在在左边IN1对应的就是A1,IN2对应的就是A2 也就是当IN1输出高电平并且IN2输出低电平时轮子是正传 车子向前移动,(温馨提示 这里不能同时输出高电平会烧的)其他电机原理也是一样,麦克纳姆轮方向全靠四个轮子配合 才能达到8个方向平移,最后演变成18个动作,L298N建议用12V电源 不然肯能带不动 我之前就是用充电宝 搞了好久都没搞明白,有时轮子转 有时轮子不转,最后才发现是电压的问题,
用到的配件有
ESP8266 和L298N 四路电机驱动(也可以是esp32都可以驱动电机也可以是 2个2路的呢98n 便宜不少)主要是4路的后面扩展
所有的配置都在图中标注
直接复制就可以
后面会改成4G小车
目前没有4G模块先用WIFI或者手机热点
所有方向都是在手机横屏下进行的 (因我app 不支持旋转)
个别图片来源于网上 如有侵权请联系我删除
链接在代码中
[pre]
- #define BLINKER_WIFI
- #include <Blinker.h>
- //资料链接:https://pan.baidu.com/s/14D4XDXt986y_RJ1lYNFvXw
- //提取码:flgj
- //------需要修改为自己的信息------
- char auth[] = "xxxxxxxxxxxx";//自己app的key
- char ssid[] = "GYQ-2.4G";//自己的wifi名字
- char pswd[] = "762340778";//WIFI密码
- //-----------------------------
- /*按键 btn
- 5 1 6
- 3 4
- 7 2 8
- */
- BlinkerButton But1("btn-a");//这行代码就是But1绑定app山的btn-a 这个按键
- BlinkerButton But2("btn-b");
- BlinkerButton But3("btn-c");
- BlinkerButton But4("btn-d");
- BlinkerButton But5("btn-e");
- BlinkerButton But6("btn-f");
- BlinkerButton But7("btn-g");
- BlinkerButton But8("btn-h");
- BlinkerButton But9("btn-i");
- BlinkerButton But10("btn-j");
- BlinkerButton But11("btn-k");
- BlinkerButton But12("btn-l");
- BlinkerButton But13("btn-m");
- BlinkerButton But14("btn-n");
- BlinkerButton But15("btn-o");
- BlinkerButton But16("btn-p");
- BlinkerButton But17("btn-q");
- BlinkerButton But18("btn-r");
- BlinkerButton But19("btn-s");
- BlinkerButton But20("btn-t");
- BlinkerSlider S1("run-a");
- BlinkerText Txt1("tex-a");
- // analogWrite(S1, pwn_v);
- //---------------------------
- /*车轮/接线
- A(正负) B(正负)
- C(正负) D(正负)
- */
- //引脚
- #define A1 16 //这里定义引脚 主要是为了可以在多个esp型号上可用方便修改, A1对应引脚16 只需改这里 方向动作就不用去修改方便迁移
- #define A2 5
- #define B1 4
- #define B2 0
- #define C1 2
- #define C2 14
- #define D1 12
- #define D2 13
- #define PWM 15
- //const int PWM = run-a.value;
- //小车十八方向控制
- void stop_run()
- { //停止
- Serial.println("停止---------------");
- digitalWrite(A1, LOW);
- digitalWrite(A2, LOW);
- digitalWrite(B1, LOW);
- digitalWrite(B2, LOW);
- digitalWrite(C1, LOW);
- digitalWrite(C2, LOW);
- digitalWrite(D1, LOW);
- digitalWrite(D2, LOW);
- }
- void run_a()
- { //上
- Serial.println("前进---------------");
- delay(50);
- digitalWrite(A1, HIGH);
- digitalWrite(A2, LOW);
- digitalWrite(B1, HIGH);
- digitalWrite(B2, LOW);
- digitalWrite(C1, HIGH);
- digitalWrite(C2, LOW);
- digitalWrite(D1, HIGH);
- digitalWrite(D2, LOW);
- }
- void run_b()
- { //下
- Serial.println("后退---------------");
- delay(50);
- digitalWrite(A1, LOW);
- digitalWrite(A2, HIGH);
- digitalWrite(B1, LOW);
- digitalWrite(B2, HIGH);
- digitalWrite(C1, LOW);
- digitalWrite(C2, HIGH);
- digitalWrite(D1, LOW);
- digitalWrite(D2, HIGH);
- }
- void run_c()
- { //左
- Serial.println("左---------------");
- delay(50);
- digitalWrite(A1, LOW);
- digitalWrite(A2, HIGH);
- digitalWrite(B1, HIGH);
- digitalWrite(B2, LOW);
- digitalWrite(C1, HIGH);
- digitalWrite(C2, LOW);
- digitalWrite(D1, LOW);
- digitalWrite(D2, HIGH);
- }
- void run_d()
- { //右
- Serial.println("右---------------");
- delay(50);
- digitalWrite(A1, HIGH);
- digitalWrite(A2, LOW);
- digitalWrite(B1, LOW);
- digitalWrite(B2, HIGH);
- digitalWrite(C1, LOW);
- digitalWrite(C2, HIGH);
- digitalWrite(D1, HIGH);
- digitalWrite(D2, LOW);
- }
- void run_e()
- { //左前
- Serial.println("左前---------------");
- delay(50);
- digitalWrite(A1, LOW);
- digitalWrite(A2, LOW);
- digitalWrite(B1, HIGH);
- digitalWrite(B2, LOW);
- digitalWrite(C1, HIGH);
- digitalWrite(C2, LOW);
- digitalWrite(D1, LOW);
- digitalWrite(D2, LOW);
- }
- void run_f()
- { //右前
- Serial.println("右前---------------");
- delay(50);
- digitalWrite(A1, HIGH);
- digitalWrite(A2, LOW);
- digitalWrite(B1, LOW);
- digitalWrite(B2, LOW);
- digitalWrite(C1, LOW);
- digitalWrite(C2, LOW);
- digitalWrite(D1, HIGH);
- digitalWrite(D2, LOW);
- }
- void run_g()
- { //左后
- Serial.println("左后---------------");
- delay(50);
- digitalWrite(A1, LOW);
- digitalWrite(A2, HIGH);
- digitalWrite(B1, LOW);
- digitalWrite(B2, LOW);
- digitalWrite(C1, LOW);
- digitalWrite(C2, LOW);
- digitalWrite(D1, LOW);
- digitalWrite(D2, HIGH);
- }
- void run_h()
- { //右后
- Serial.println("右后---------------");
- delay(50);
- digitalWrite(A1, LOW);
- digitalWrite(A2, LOW);
- digitalWrite(B1, LOW);
- digitalWrite(B2, HIGH);
- digitalWrite(C1, LOW);
- digitalWrite(C2, HIGH);
- digitalWrite(D1, LOW);
- digitalWrite(D2, LOW);
- }
- void run_i()
- { //原地右旋转
- Serial.println("原地右旋转---------------");
- delay(50);
- digitalWrite(A1, HIGH);
- digitalWrite(A2, LOW);
- digitalWrite(B1, LOW);
- digitalWrite(B2, HIGH);
- digitalWrite(C1, HIGH);
- digitalWrite(C2, LOW);
- digitalWrite(D1, LOW);
- digitalWrite(D2, HIGH);
- }
- void run_j()
- { //原地左旋转
- Serial.println("原地左旋转---------------");
- delay(50);
- digitalWrite(A1, LOW);
- digitalWrite(A2, HIGH);
- digitalWrite(B1, HIGH);
- digitalWrite(B2, LOW);
- digitalWrite(C1, LOW);
- digitalWrite(C2, HIGH);
- digitalWrite(D1, HIGH);
- digitalWrite(D2, LOW);
- }
- void run_k()
- { //右转
- Serial.println("右前转---------------");
- delay(50);
- digitalWrite(A1, HIGH);
- digitalWrite(A2, LOW);
- digitalWrite(B1, LOW);
- digitalWrite(B2, LOW);
- digitalWrite(C1, HIGH);
- digitalWrite(C2, LOW);
- digitalWrite(D1, LOW);
- digitalWrite(D2, LOW);
- }
- void run_l()
- { //左转
- Serial.println("左前转---------------");
- delay(50);
- digitalWrite(A1, LOW);
- digitalWrite(A2, LOW);
- digitalWrite(B1, HIGH);
- digitalWrite(B2, LOW);
- digitalWrite(C1, LOW);
- digitalWrite(C2, LOW);
- digitalWrite(D1, HIGH);
- digitalWrite(D2, LOW);
- }
- void run_m()
- { //右后转
- Serial.println("右后转---------------");
- delay(50);
- digitalWrite(A1, LOW);
- digitalWrite(A2, HIGH);
- digitalWrite(B1, LOW);
- digitalWrite(B2, LOW);
- digitalWrite(C1, LOW);
- digitalWrite(C2, HIGH);
- digitalWrite(D1, LOW);
- digitalWrite(D2, LOW);
- }
- void run_n()
- { //左后转
- Serial.println("左后转---------------");
- delay(50);
- digitalWrite(A1, LOW);
- digitalWrite(A2, LOW);
- digitalWrite(B1, LOW);
- digitalWrite(B2, HIGH);
- digitalWrite(C1, LOW);
- digitalWrite(C2, LOW);
- digitalWrite(D1, LOW);
- digitalWrite(D2, HIGH);
- }
- void run_o()
- { //后轴右转
- Serial.println("后轴右转---------------");
- delay(50);
- digitalWrite(A1, HIGH);
- digitalWrite(A2, LOW);
- digitalWrite(B1, LOW);
- digitalWrite(B2, HIGH);
- digitalWrite(C1, LOW);
- digitalWrite(C2, LOW);
- digitalWrite(D1, LOW);
- digitalWrite(D2, LOW);
- }
- void run_p()
- { //后轴左转
- Serial.println("后轴左转---------------");
- delay(50);
- digitalWrite(A1, LOW);
- digitalWrite(A2, HIGH);
- digitalWrite(B1, HIGH);
- digitalWrite(B2, LOW);
- digitalWrite(C1, LOW);
- digitalWrite(C2, LOW);
- digitalWrite(D1, LOW);
- digitalWrite(D2, LOW);
- }
- void run_q()
- { //前轴右转
- Serial.println("后轴左转---------------");
- delay(50);
- digitalWrite(A1, LOW);
- digitalWrite(A2, LOW);
- digitalWrite(B1, LOW);
- digitalWrite(B2, LOW);
- digitalWrite(C1, LOW);
- digitalWrite(C2, HIGH);
- digitalWrite(D1, HIGH);
- digitalWrite(D2, LOW);
- }
- void run_r()
- { //前轴左转
- Serial.println("后轴左转---------------");
- delay(50);
- digitalWrite(A1, LOW);
- digitalWrite(A2, LOW);
- digitalWrite(B1, LOW);
- digitalWrite(B2, LOW);
- digitalWrite(C1, HIGH);
- digitalWrite(C2, LOW);
- digitalWrite(D1, LOW);
- digitalWrite(D2, HIGH);
- }
- void run_s()
- { //测试
- Serial.println("测试A轮子正转");
- delay(50);
- digitalWrite(A1, HIGH);
- digitalWrite(A2, LOW);
- delay(500);
- digitalWrite(A1, LOW);
- digitalWrite(A2, LOW);
- Serial.println("测试A轮子反转");
- delay(100);
- digitalWrite(A1, LOW);
- digitalWrite(A2, HIGH);
- delay(500);
- digitalWrite(A1, LOW);
- digitalWrite(A2, LOW);
- delay(50);
- Serial.println("测试B轮子正转");
- digitalWrite(B1, HIGH);
- digitalWrite(B2, LOW);
- delay(500);
- digitalWrite(B1, LOW);
- digitalWrite(B2, LOW);
- delay(100);
- Serial.println("测试B轮子反转");
- digitalWrite(B1, LOW);
- digitalWrite(B2, HIGH);
- delay(500);
- digitalWrite(B1, LOW);
- digitalWrite(B2, LOW);
- Serial.println("测试C轮子正转");
- digitalWrite(C1, HIGH);
- digitalWrite(C2, LOW);
- delay(500);
- digitalWrite(C1, LOW);
- digitalWrite(C2, LOW);
- delay(100);
- Serial.println("测试C轮子反转");
- digitalWrite(C1, LOW);
- digitalWrite(C2, HIGH);
- delay(500);
- digitalWrite(C1, LOW);
- digitalWrite(C2, LOW);
- Serial.println("测试D轮子正转");
- digitalWrite(D1, HIGH);
- digitalWrite(D2, LOW);
- delay(500);
- digitalWrite(D1, LOW);
- digitalWrite(D2, LOW);
- delay(100);
- Serial.println("测试D轮子反转");
- digitalWrite(D1, LOW);
- digitalWrite(D2, HIGH);
- delay(500);
- digitalWrite(D1, LOW);
- digitalWrite(D2, LOW);
- }
- void run_t()
- { //右后
- Serial.println("右后---------------");
- delay(50);
- digitalWrite(A1, HIGH);
- digitalWrite(A2, LOW);
- digitalWrite(B1, HIGH);
- digitalWrite(B2, LOW);
- digitalWrite(C1, HIGH);
- digitalWrite(C2, LOW);
- digitalWrite(D1, HIGH);
- digitalWrite(D2, LOW);
- }
- void dataRead(const String & data)
- {
- BLINKER_LOG("Blinker readString: ", data);
- Blinker.vibrate();
- uint32_t BlinkerTime = millis();
- Blinker.print("millis", BlinkerTime);
- }
- void btn1_callback(const String & state) //点按键上触发
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") { //按下按键上
- run_a();//执行方法run_a()的方法也就是上面的上 ,按住按键btn-a时就一直执行上的方法也就是一直前进, 下面都是一样的道理
- }
- else if (state == "pressup") {
- stop_run(); //松开按键上 全部低电平也就是停止
- }
- }
- void btn2_callback(const String & state) //点按键下触发
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") { //按下按键下
- run_b(); // 执行方法run_b()的方法也就是上面的上 ,按住按键btn-b时就一直执行上的方法也就是一直后退,下面都是一样的道理,一下类似只是方向不一样
- }
- else if (state == "pressup") {
- stop_run(); //松开按键下 停止
- }
- }
- void btn3_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_c(); //左
- }
- else if (state == "pressup") {
- stop_run();
- }
- }
- void btn4_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_d();//右
- }
- else if (state == "pressup") {
- stop_run();
- }
- }
- void btn5_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_e();//左前
- }
- else if (state == "pressup") {
- stop_run();
- }
- }
- void btn6_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_f();//右前
- }
- else if (state == "pressup") {
- stop_run();
- }
- }
- void btn7_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_g();//左后
- }
- else if (state == "pressup") {
- stop_run();
- }
- }
- void btn8_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_h();//右后
- }
- else if (state == "pressup") {
- stop_run();
- }
- }
- void btn9_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_i();//原地右旋转
- }
- else if (state == "pressup") {
- stop_run();
- }
- }
- void btn10_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_j();//原地左旋转
- }
- else if (state == "pressup") {
- stop_run();
- }
- }
- void btn11_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_k();//右转
- }
- else if (state == "pressup") {
- stop_run();
- }
- }
- void btn12_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_l();//左转
- }
- else if (state == "pressup") {
- stop_run();
- }
- }
- void btn13_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_m();//右后转
- }
- else if (state == "pressup") {
- stop_run();
- }
- }
- void btn14_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_n();//左后转
- }
- else if (state == "pressup") {
- stop_run();
- }
- }
- void btn15_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_o();//后轴右转
- }
- else if (state == "pressup") {
- stop_run();
- }
- }
- void btn16_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_p();//后轴左转
- }
- else if (state == "pressup") {
- stop_run();
- }
- }
- void btn17_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_q();//前轴右转
- }
- else if (state == "pressup") {
- stop_run();
- }
- }
- void btn18_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_r();//前轴左转
- }
- else if (state == "pressup") {
- stop_run();
- }
- }
- void btn19_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_s();//测试 测试每个轮子 从ABCD依次正传500毫秒反正500毫秒 此方法也是测试轮子接线和IN的接线 方便调整
- }
- else if (state == "pressup") {
- stop_run();
- }
- }
- void btn20_callback(const String & state)
- {
- BLINKER_LOG("get button state: ", state);
- if (state == "press") {
- run_t();//右后
- }
- }
- // 心跳包函数
- void heartbeat() {
- //Blinker readString: {"ran-a":177}
- }
- void setup()
- {
- Serial.begin(115200);
- BLINKER_DEBUG.stream(Serial);
- BLINKER_DEBUG.debugAll();
- //定义引脚为输出
- pinMode(A1, OUTPUT);
- pinMode(A2, OUTPUT);
- pinMode(B1, OUTPUT);
- pinMode(B2, OUTPUT);
- pinMode(C1, OUTPUT);
- pinMode(C2, OUTPUT);
- pinMode(D1, OUTPUT);
- pinMode(D2, OUTPUT);
- //初始化引脚
- digitalWrite(A1, LOW);
- digitalWrite(A2, LOW);
- digitalWrite(B1, LOW);
- digitalWrite(B2, LOW);
- digitalWrite(C1, LOW);
- digitalWrite(C2, LOW);
- digitalWrite(D1, LOW);
- digitalWrite(D2, LOW);
- Blinker.begin(auth, ssid, pswd);
- Blinker.attachData(dataRead);
- But1.attach(btn1_callback);
- But2.attach(btn2_callback);
- But3.attach(btn3_callback);
- But4.attach(btn4_callback);
- But5.attach(btn5_callback);
- But6.attach(btn6_callback);
- But7.attach(btn7_callback);
- But8.attach(btn8_callback);
- But9.attach(btn9_callback);
- But10.attach(btn10_callback);
- But11.attach(btn11_callback);
- But12.attach(btn12_callback);
- But13.attach(btn13_callback);
- But14.attach(btn14_callback);
- But15.attach(btn15_callback);
- But16.attach(btn16_callback);
- But17.attach(btn17_callback);
- But18.attach(btn18_callback);
- But19.attach(btn19_callback);
- But20.attach(btn20_callback);
- //注册一个心跳包
- Blinker.attachHeartbeat(heartbeat);
- }
- void loop()
- {
- Blinker.run();
- }
复制代码 [/pre]
--------------------------[新增功能]----------------------
新增 利用避障,雷达 妈妈在也不怕小车撞坏了
删除 403和404行代码出现网页代码 导致烧写失败。删除即可
-----------------------------------------------------------
功能慢慢跟新中新增代码晚一点补上(突然有事)
[pre]//esp32
#define BLINKER_WIFI
#include <Blinker.h>
#include <ESP32Servo.h>
//资料链接:https://pan.baidu.com/s/14D4XDXt986y_RJ1lYNFvXw
//提取码:flgj
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
//------需要修改为自己的信息----------------
char auth[] = "XXXXXXXXXX";//自己app的key
char ssid[] = "gyq";//自己的wifi名字
char pswd[] = "762340778";//WIFI密码
int distance ;
int LD ; //雷达
int JL = 30 ; //距离30厘米
Servo myservo; //创建伺服对象以控制服务
int pos = 0; //变量来存储伺服位置
int servoPin = 26;//雷达舵机引脚
//引脚
#define A_1 23 //这里定义引脚 主要是为了可以在多个esp型号上可用方便修改, A_1对应引脚16 只需改这里 方向动作就不用去修改方便迁移
#define A_2 22
#define B_1 21
#define B_2 19
#define C_1 18
#define C_2 5
#define D_1 4
#define D_2 2
#define PWM 13
#define trig 27
#define echo 14
#define fl 15//红外避障
#define LED1 12//红外避障灯
#define LED2 13//红外避障灯
#define MG1 25//
#define MG2 33//
//const int PWM = run-a.value;
//------------------------------------------------------------------------------------------------
BlinkerButton But1("btn-a");//这行代码就是But1绑定app山的btn-a 这个按键
BlinkerButton But2("btn-b");
BlinkerButton But3("btn-c");
BlinkerButton But4("btn-d");
BlinkerButton But5("btn-e");
BlinkerButton But6("btn-f");
BlinkerButton But7("btn-g");
BlinkerButton But8("btn-h");
BlinkerButton But9("btn-i");
BlinkerButton But10("btn-j");
BlinkerButton But11("btn-k");
BlinkerButton But12("btn-l");
BlinkerButton But13("btn-m");
BlinkerButton But14("btn-n");
BlinkerButton But15("btn-o");
BlinkerButton But16("btn-p");
BlinkerButton But17("btn-q");
BlinkerButton But18("btn-r");
BlinkerButton But19("btn-s");
BlinkerButton But20("btn-t");
BlinkerSlider S1("run-a");
BlinkerText Txt1("tex-a");
//---------------------------
//小车十八方向控制
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
void stop_run()
{ //停止
Serial.println("停止---------------");
digitalWrite(A_1, LOW);
digitalWrite(A_2, LOW);
digitalWrite(B_1, LOW);
digitalWrite(B_2, LOW);
digitalWrite(C_1, LOW);
digitalWrite(C_2, LOW);
digitalWrite(D_1, LOW);
digitalWrite(D_2, LOW);
}
void run_a()
{ //上
Serial.println("前进---------------");
delay(50);
digitalWrite(A_1, HIGH);
digitalWrite(A_2, LOW);
digitalWrite(B_1, HIGH);
digitalWrite(B_2, LOW);
digitalWrite(C_1, HIGH);
digitalWrite(C_2, LOW);
digitalWrite(D_1, HIGH);
digitalWrite(D_2, LOW);
}
void run_b()
{ //下
Serial.println("后退---------------");
delay(50);
digitalWrite(A_1, LOW);
digitalWrite(A_2, HIGH);
digitalWrite(B_1, LOW);
digitalWrite(B_2, HIGH);
digitalWrite(C_1, LOW);
digitalWrite(C_2, HIGH);
digitalWrite(D_1, LOW);
digitalWrite(D_2, HIGH);
}
void run_c()
{ //左
Serial.println("左---------------");
delay(50);
digitalWrite(A_1, LOW);
digitalWrite(A_2, HIGH);
digitalWrite(B_1, HIGH);
digitalWrite(B_2, LOW);
digitalWrite(C_1, HIGH);
digitalWrite(C_2, LOW);
digitalWrite(D_1, LOW);
digitalWrite(D_2, HIGH);
}
void run_d()
{ //右
Serial.println("右---------------");
delay(50);
digitalWrite(A_1, HIGH);
digitalWrite(A_2, LOW);
digitalWrite(B_1, LOW);
digitalWrite(B_2, HIGH);
digitalWrite(C_1, LOW);
digitalWrite(C_2, HIGH);
digitalWrite(D_1, HIGH);
digitalWrite(D_2, LOW);
}
void run_e()
{ //左前
Serial.println("左前---------------");
delay(50);
digitalWrite(A_1, LOW);
digitalWrite(A_2, LOW);
digitalWrite(B_1, HIGH);
digitalWrite(B_2, LOW);
digitalWrite(C_1, HIGH);
digitalWrite(C_2, LOW);
digitalWrite(D_1, LOW);
digitalWrite(D_2, LOW);
}
void run_f()
{ //右前
Serial.println("右前---------------");
delay(50);
digitalWrite(A_1, HIGH);
digitalWrite(A_2, LOW);
digitalWrite(B_1, LOW);
digitalWrite(B_2, LOW);
digitalWrite(C_1, LOW);
digitalWrite(C_2, LOW);
digitalWrite(D_1, HIGH);
digitalWrite(D_2, LOW);
}
void run_g()
{ //左后
Serial.println("左后---------------");
delay(50);
digitalWrite(A_1, LOW);
digitalWrite(A_2, HIGH);
digitalWrite(B_1, LOW);
digitalWrite(B_2, LOW);
digitalWrite(C_1, LOW);
digitalWrite(C_2, LOW);
digitalWrite(D_1, LOW);
digitalWrite(D_2, HIGH);
}
void run_h()
{ //右后
Serial.println("右后---------------");
delay(50);
digitalWrite(A_1, LOW);
digitalWrite(A_2, LOW);
digitalWrite(B_1, LOW);
digitalWrite(B_2, HIGH);
digitalWrite(C_1, LOW);
digitalWrite(C_2, HIGH);
digitalWrite(D_1, LOW);
digitalWrite(D_2, LOW);
}
void run_i()
{ //原地右旋转
Serial.println("原地右旋转---------------");
delay(50);
digitalWrite(A_1, HIGH);
digitalWrite(A_2, LOW);
digitalWrite(B_1, LOW);
digitalWrite(B_2, HIGH);
digitalWrite(C_1, HIGH);
digitalWrite(C_2, LOW);
digitalWrite(D_1, LOW);
digitalWrite(D_2, HIGH);
}
void run_j()
{ //原地左旋转
Serial.println("原地左旋转---------------");
delay(50);
digitalWrite(A_1, LOW);
digitalWrite(A_2, HIGH);
digitalWrite(B_1, HIGH);
digitalWrite(B_2, LOW);
digitalWrite(C_1, LOW);
digitalWrite(C_2, HIGH);
digitalWrite(D_1, HIGH);
digitalWrite(D_2, LOW);
}
void run_k()
{ //右转
Serial.println("右前转---------------");
delay(50);
digitalWrite(A_1, HIGH);
digitalWrite(A_2, LOW);
digitalWrite(B_1, LOW);
digitalWrite(B_2, LOW);
digitalWrite(C_1, HIGH);
digitalWrite(C_2, LOW);
digitalWrite(D_1, LOW);
digitalWrite(D_2, LOW);
}
void run_l()
{ //左转
Serial.println("左前转---------------");
delay(50);
digitalWrite(A_1, LOW);
digitalWrite(A_2, LOW);
digitalWrite(B_1, HIGH);
digitalWrite(B_2, LOW);
digitalWrite(C_1, LOW);
digitalWrite(C_2, LOW);
digitalWrite(D_1, HIGH);
digitalWrite(D_2, LOW);
}
void run_m()
{ //右后转
Serial.println("右后转---------------");
delay(50);
digitalWrite(A_1, LOW);
digitalWrite(A_2, HIGH);
digitalWrite(B_1, LOW);
digitalWrite(B_2, LOW);
digitalWrite(C_1, LOW);
digitalWrite(C_2, HIGH);
digitalWrite(D_1, LOW);
digitalWrite(D_2, LOW);
}
void run_n()
{ //左后转
Serial.println("左后转---------------");
delay(50);
digitalWrite(A_1, LOW);
digitalWrite(A_2, LOW);
digitalWrite(B_1, LOW);
digitalWrite(B_2, HIGH);
digitalWrite(C_1, LOW);
digitalWrite(C_2, LOW);
digitalWrite(D_1, LOW);
digitalWrite(D_2, HIGH);
}
void run_o()
{ //后轴右转
Serial.println("后轴右转---------------");
delay(50);
digitalWrite(A_1, HIGH);
digitalWrite(A_2, LOW);
digitalWrite(B_1, LOW);
digitalWrite(B_2, HIGH);
digitalWrite(C_1, LOW);
digitalWrite(C_2, LOW);
digitalWrite(D_1, LOW);
digitalWrite(D_2, LOW);
}
void run_p()
{ //后轴左转
Serial.println("后轴左转---------------");
delay(50);
digitalWrite(A_1, LOW);
digitalWrite(A_2, HIGH);
digitalWrite(B_1, HIGH);
digitalWrite(B_2, LOW);
digitalWrite(C_1, LOW);
digitalWrite(C_2, LOW);
digitalWrite(D_1, LOW);
digitalWrite(D_2, LOW);
}
void run_q()
{ //前轴右转
Serial.println("后轴左转---------------");
delay(50);
digitalWrite(A_1, LOW);
digitalWrite(A_2, LOW);
digitalWrite(B_1, LOW);
digitalWrite(B_2, LOW);
digitalWrite(C_1, LOW);
digitalWrite(C_2, HIGH);
digitalWrite(D_1, HIGH);
digitalWrite(D_2, LOW);
}
void run_r()
{ //前轴左转
Serial.println("后轴左转---------------");
delay(50);
digitalWrite(A_1, LOW);
digitalWrite(A_2, LOW);
digitalWrite(B_1, LOW);
digitalWrite(B_2, LOW);
digitalWrite(C_1, HIGH);
digitalWrite(C_2, LOW);
digitalWrite(D_1, LOW);
digitalWrite(D_2, HIGH);
}
void run_s()
{ //测试
Serial.println("测试A轮子正转");
delay(50);
digitalWrite(A_1, HIGH);
digitalWrite(A_2, LOW);
delay(500);
digitalWrite(A_1, LOW);
digitalWrite(A_2, LOW);
Serial.println("测试A轮子反转");
delay(100);
digitalWrite(A_1, LOW);
digitalWrite(A_2, HIGH);
delay(500);
digitalWrite(A_1, LOW);
digitalWrite(A_2, LOW);
delay(50);
Serial.println("测试B轮子正转");
digitalWrite(B_1, HIGH);
digitalWrite(B_2, LOW);
delay(500);
digitalWrite(B_1, LOW);
digitalWrite(B_2, LOW);
delay(100);
Serial.println("测试B轮子反转");
digitalWrite(B_1, LOW);
digitalWrite(B_2, HIGH);
delay(500);
digitalWrite(B_1, LOW);
digitalWrite(B_2, LOW);
Serial.println("测试C轮子正转");
digitalWrite(C_1, HIGH);
digitalWrite(C_2, LOW);
delay(500);
digitalWrite(C_1, LOW);
digitalWrite(C_2, LOW);
delay(100);
Serial.println("测试C轮子反转");
digitalWrite(C_1, LOW);
digitalWrite(C_2, HIGH);
delay(500);
digitalWrite(C_1, LOW);
digitalWrite(C_2, LOW);
Serial.println("测试D轮子正转");
digitalWrite(D_1, HIGH);
digitalWrite(D_2, LOW);
delay(500);
digitalWrite(D_1, LOW);
digitalWrite(D_2, LOW);
delay(100);
Serial.println("测试D轮子反转");
digitalWrite(D_1, LOW);
digitalWrite(D_2, HIGH);
delay(500);
digitalWrite(D_1, LOW);
digitalWrite(D_2, LOW);
}
void run_t()
{ //右后
Serial.println("右后---------------");
delay(50);
digitalWrite(A_1, HIGH);
digitalWrite(A_2, LOW);
digitalWrite(B_1, HIGH);
digitalWrite(B_2, LOW);
digitalWrite(C_1, HIGH);
digitalWrite(C_2, LOW);
digitalWrite(D_1, HIGH);
digitalWrite(D_2, LOW);
}
void leida()//雷达测距方法
{
digitalWrite(trig, LOW);
delayMicroseconds(20);
digitalWrite(trig, HIGH);
delayMicroseconds(20);
digitalWrite(trig, LOW); //发一个20US的高脉冲去触发Trig
distance = pulseIn(echo, HIGH); //计数接收高电平时间
distance = distance * 340 / 2 / 10000; //计算距离 1:声速:340M/S 2:实际距离1/2声速距离 3:计数时钟为1US
Serial.print("距离: ");
Serial.print(distance);
Serial.println("cm");
Serial.println("距离+++++++++++++++");
Serial.println(".");
Serial.println(".");
Serial.println(".");
Serial.println(".");
Serial.print(distance);
Serial.println("厘米");
Serial.println(".");
Serial.println(".");
Serial.println(".");
if (distance > JL) {
Serial.println("j距离大于30厘米");
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
}
if (distance < JL) {
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
Serial.println("j距离小于30厘米");
//------------------舵机----------
delay(800);
myservo.write(0);
delay(800);
myservo.write(60);
delay(800);
myservo.write(45);
delay(800);
myservo.write(90);
delay(800);
myservo.write(30);
delay(800);
myservo.write(120);
delay(800);
myservo.write(60);
delay(800);
myservo.write(120);
delay(800);
myservo.write(70);
delay(800);
myservo.write(0);
//------------------舵机----------
}
else if (distance == JL) {
Serial.println("j距离等于30厘米");
}
delay(20); //单次测离完成后加20mS的延时再进行下次测量。防止近距离测量时,测量到上次余波,导致测量不准确。
delay(1000); //500mS测量一次
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
void btn1_callback(const String & state) //点按键上触发
{
BLINKER_LOG("get button state: ", state);
if (state == "press") { //按下按键上
run_a();//执行方法run_a()的方法也就是上面的上 ,按住按键btn-a时就一直执行上的方法也就是一直前进, 下面都是一样的道理
}
else if (state == "pressup") {
stop_run(); //松开按键上 全部低电平也就是停止
}
}
void btn2_callback(const String & state) //点按键下触发
{
BLINKER_LOG("get button state: ", state);
if (state == "press") { //按下按键下
run_b(); // 执行方法run_b()的方法也就是上面的上 ,按住按键btn-b时就一直执行上的方法也就是一直后退,下面都是一样的道理,一下类似只是方向不一样
}
else if (state == "pressup") {
stop_run(); //松开按键下 停止
}
}
void btn3_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_c(); //左
}
else if (state == "pressup") {
stop_run();
}
}
void btn4_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_d();//右
}
else if (state == "pressup") {
stop_run();
}
}
void btn5_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_e();//左前
}
else if (state == "pressup") {
stop_run();
}
}
void btn6_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_f();//右前
}
else if (state == "pressup") {
stop_run();
}
}
void btn7_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_g();//左后
}
else if (state == "pressup") {
stop_run();
}
}
void btn8_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_h();//右后
}
else if (state == "pressup") {
stop_run();
}
}
void btn9_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_i();//原地右旋转
}
else if (state == "pressup") {
stop_run();
}
}
void btn10_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_j();//原地左旋转
}
else if (state == "pressup") {
stop_run();
}
}
void btn11_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_k();//右转
}
else if (state == "pressup") {
stop_run();
}
}
void btn12_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_l();//左转
}
else if (state == "pressup") {
stop_run();
}
}
void btn13_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_m();//右后转
}
else if (state == "pressup") {
stop_run();
}
}
void btn14_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_n();//左后转
}
else if (state == "pressup") {
stop_run();
}
}
void btn15_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_o();//后轴右转
}
else if (state == "pressup") {
stop_run();
}
}
void btn16_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_p();//后轴左转
}
else if (state == "pressup") {
stop_run();
}
}
void btn17_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_q();//前轴右转
}
else if (state == "pressup") {
stop_run();
}
}
void btn18_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_r();//前轴左转
}
else if (state == "pressup") {
stop_run();
}
}
void btn19_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_s();//测试 测试每个轮子 从ABCD依次正传500毫秒反正500毫秒 此方法也是测试轮子接线和IN的接线 方便调整
}
else if (state == "pressup") {
stop_run();
}
}
void btn20_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == "press") {
run_t();//右后
}
}
// 心跳包函数
void heartbeat() {
//Blinker readString: {"ran-a":177}
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
//定义引脚为输出
pinMode(A_1, OUTPUT);
pinMode(A_2, OUTPUT);
pinMode(B_1, OUTPUT);
pinMode(B_2, OUTPUT);
pinMode(C_1, OUTPUT);
pinMode(C_2, OUTPUT);
pinMode(D_1, OUTPUT);
pinMode(D_2, OUTPUT);
pinMode(echo, INPUT);
pinMode(trig, OUTPUT);
pinMode(fl, OUTPUT);//红外避障
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(MG1, OUTPUT);
pinMode(MG2, OUTPUT);
//初始化引脚
digitalWrite(A_1, LOW);
digitalWrite(A_2, LOW);
digitalWrite(B_1, LOW);
digitalWrite(B_2, LOW);
digitalWrite(C_1, LOW);
digitalWrite(C_2, LOW);
digitalWrite(D_1, LOW);
digitalWrite(D_2, LOW);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(MG1, HIGH);
digitalWrite(MG2, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
But1.attach(btn1_callback);
But2.attach(btn2_callback);
But3.attach(btn3_callback);
But4.attach(btn4_callback);
But5.attach(btn5_callback);
But6.attach(btn6_callback);
But7.attach(btn7_callback);
But8.attach(btn8_callback);
But9.attach(btn9_callback);
But10.attach(btn10_callback);
But11.attach(btn11_callback);
But12.attach(btn12_callback);
But13.attach(btn13_callback);
But14.attach(btn14_callback);
But15.attach(btn15_callback);
But16.attach(btn16_callback);
But17.attach(btn17_callback);
But18.attach(btn18_callback);
But19.attach(btn19_callback);
But20.attach(btn20_callback);
//注册一个心跳包
Blinker.attachHeartbeat(heartbeat);
//舵机1--------------------------------------
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo.setPeriodHertz(70); // standard 50 hz servo
myservo.attach(servoPin, 1000, 2000); // attaches the servo on pin 23 to the servo object
// using default min/max of 1000us and 2000us
// different servos may require different min/max settings
// for an accurate 0 to 180 sweep
//舵机1--------------------------------------
}
void loop()
{
Blinker.run();
leida();
}[/pre]
|
-
方向图
-
引脚按键对应图
|