|
本帖最后由 myself1820 于 2021-4-8 20:31 编辑
情况是这样的,如果小车前一个动作是左转,然后执行直行动作,车子会有左偏的动作,如果前一个动作是右转,然后直行,车子就会右偏。不知道问题出在哪里了
下面是程序代码
zhi#define BLINKER_WIFI
#include <Blinker.h>
#include <Servo.h> //需要安装crypto库,new
Servo myServo; //定义一个舵机,接D2
char auth[] = "xxx";
char ssid[] = "xxx";
char pswd[] = "xxx";
// 新建组件对象
BlinkerButton Button1("btn-a"); //前进按钮
BlinkerButton Button2("btn-l"); //左转按钮
BlinkerButton Button3("btn-r"); //右转按钮
BlinkerButton Button4("btn-b"); //后退按钮
BlinkerButton Button5("btn-s"); //停止按钮
BlinkerSlider Slider1("ran-v"); //速度控制滑块
BlinkerSlider Slider2("ran-p"); //舵机控制滑块
BlinkerNumber Number1("WiFi");
BlinkerNumber Number2("num-Vol"); //定义电压数据键名
//#define Trig D0 //超声波模块引脚设置
//#define Ecoh D1
#define MOTOR_L_PWM_PIN 5 //GPIO 5(D1),左电机PWM控制
#define MOTOR_R_PWM_PIN 0 //GPIO 0(D3),右电机PWM控制
#define MOTOR_L_IN1_PIN 14 //GPIO14(D5) 左电机引脚设置
#define MOTOR_L_IN2_PIN 12 //GPIO12(D6)
#define MOTOR_R_IN1_PIN 13 //GPIO13(D7),右电机引脚设置
#define MOTOR_R_IN2_PIN 15 //GPIO15(D8)
#define TIME_TAP 500
int PWM; //电机速度控制值,0 - 1023
int pos; //舵机角度
int V; //ADC 采集的数据
float Vol; //电池电压
// 前进
void button1_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if(state=="tap"){
analogWrite(MOTOR_L_PWM_PIN, PWM); //左电机PWM
analogWrite(MOTOR_R_PWM_PIN, PWM); //右电机PWM
digitalWrite(MOTOR_L_IN1_PIN, HIGH); //左电机
digitalWrite(MOTOR_L_IN2_PIN, LOW);
digitalWrite(MOTOR_R_IN1_PIN, HIGH); //右电机
digitalWrite(MOTOR_R_IN2_PIN, LOW);
Button1.print("前进");
delay(TIME_TAP);
analogWrite(MOTOR_L_PWM_PIN, 0);
analogWrite(MOTOR_R_PWM_PIN, 0);
digitalWrite(MOTOR_L_IN1_PIN, LOW);
digitalWrite(MOTOR_L_IN2_PIN, LOW);
digitalWrite(MOTOR_R_IN1_PIN, LOW);
digitalWrite(MOTOR_R_IN2_PIN, LOW);
}
if(state=="press"){
analogWrite(MOTOR_L_PWM_PIN, PWM);
analogWrite(MOTOR_R_PWM_PIN, PWM);
digitalWrite(MOTOR_L_IN1_PIN, HIGH);
digitalWrite(MOTOR_L_IN2_PIN, LOW);
digitalWrite(MOTOR_R_IN1_PIN, HIGH);
digitalWrite(MOTOR_R_IN2_PIN, LOW);
}
if(state=="pressup"){
analogWrite(MOTOR_L_PWM_PIN, 0);
analogWrite(MOTOR_R_PWM_PIN, 0);
digitalWrite(MOTOR_L_IN1_PIN, LOW);
digitalWrite(MOTOR_L_IN2_PIN, LOW);
digitalWrite(MOTOR_R_IN1_PIN, LOW);
digitalWrite(MOTOR_R_IN2_PIN, LOW);
}
}
// 左转
void button2_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if(state=="tap"){
analogWrite(MOTOR_L_PWM_PIN, 600);
analogWrite(MOTOR_R_PWM_PIN, 600);
digitalWrite(MOTOR_L_IN1_PIN, LOW);
digitalWrite(MOTOR_L_IN2_PIN, HIGH);
digitalWrite(MOTOR_R_IN1_PIN, HIGH);
digitalWrite(MOTOR_R_IN2_PIN, LOW);
Button2.print("左转");
delay(400);
analogWrite(MOTOR_L_PWM_PIN, 0);
analogWrite(MOTOR_R_PWM_PIN, 0);
digitalWrite(MOTOR_L_IN1_PIN, LOW);
digitalWrite(MOTOR_L_IN2_PIN, LOW);
digitalWrite(MOTOR_R_IN1_PIN, LOW);
digitalWrite(MOTOR_R_IN2_PIN, LOW);
}
if(state=="press"){
analogWrite(MOTOR_L_PWM_PIN, 600);
analogWrite(MOTOR_R_PWM_PIN, 600);
digitalWrite(MOTOR_L_IN1_PIN, LOW);
digitalWrite(MOTOR_L_IN2_PIN, HIGH);
digitalWrite(MOTOR_R_IN1_PIN, HIGH);
digitalWrite(MOTOR_R_IN2_PIN, LOW);
}
if(state=="pressup"){
analogWrite(MOTOR_L_PWM_PIN, 0);
analogWrite(MOTOR_R_PWM_PIN, 0);
digitalWrite(MOTOR_L_IN1_PIN, LOW);
digitalWrite(MOTOR_L_IN2_PIN, LOW);
digitalWrite(MOTOR_R_IN1_PIN, LOW);
digitalWrite(MOTOR_R_IN2_PIN, LOW);
}
}
// 右转
void button3_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if(state=="tap"){
analogWrite(MOTOR_L_PWM_PIN, 600);
analogWrite(MOTOR_R_PWM_PIN, 600);
digitalWrite(MOTOR_L_IN1_PIN, HIGH);
digitalWrite(MOTOR_L_IN2_PIN, LOW);
digitalWrite(MOTOR_R_IN1_PIN, LOW);
digitalWrite(MOTOR_R_IN2_PIN, HIGH);
Button3.print("右转");
delay(400);
analogWrite(MOTOR_L_PWM_PIN, 0);
analogWrite(MOTOR_R_PWM_PIN, 0);
digitalWrite(MOTOR_L_IN1_PIN, LOW);
digitalWrite(MOTOR_L_IN2_PIN, LOW);
digitalWrite(MOTOR_R_IN1_PIN, LOW);
digitalWrite(MOTOR_R_IN2_PIN, LOW);
}
if(state=="press"){
analogWrite(MOTOR_L_PWM_PIN, 600);
analogWrite(MOTOR_R_PWM_PIN, 600);
digitalWrite(MOTOR_L_IN1_PIN, HIGH);
digitalWrite(MOTOR_L_IN2_PIN, LOW);
digitalWrite(MOTOR_R_IN1_PIN, LOW);
digitalWrite(MOTOR_R_IN2_PIN, HIGH);
}
if(state=="pressup"){
analogWrite(MOTOR_L_PWM_PIN, 0);
analogWrite(MOTOR_R_PWM_PIN, 0);
digitalWrite(MOTOR_L_IN1_PIN, LOW);
digitalWrite(MOTOR_L_IN2_PIN, LOW);
digitalWrite(MOTOR_R_IN1_PIN, LOW);
digitalWrite(MOTOR_R_IN2_PIN, LOW);
}
}
// 后退
void button4_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if(state=="tap"){
analogWrite(MOTOR_L_PWM_PIN, 600);
analogWrite(MOTOR_R_PWM_PIN, 600);
digitalWrite(MOTOR_L_IN1_PIN, LOW);
digitalWrite(MOTOR_L_IN2_PIN, HIGH);
digitalWrite(MOTOR_R_IN1_PIN, LOW);
digitalWrite(MOTOR_R_IN2_PIN, HIGH);
Button4.print("后退");
delay(TIME_TAP);
analogWrite(MOTOR_L_PWM_PIN, 0);
analogWrite(MOTOR_R_PWM_PIN, 0);
digitalWrite(MOTOR_L_IN1_PIN, LOW);
digitalWrite(MOTOR_L_IN2_PIN, LOW);
digitalWrite(MOTOR_R_IN1_PIN, LOW);
digitalWrite(MOTOR_R_IN2_PIN, LOW);
}
}
// 停止
void button5_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if(state=="tap"){
analogWrite(MOTOR_L_PWM_PIN, 0);
analogWrite(MOTOR_R_PWM_PIN, 0);
digitalWrite(MOTOR_L_IN1_PIN, LOW);
digitalWrite(MOTOR_L_IN2_PIN, LOW);
digitalWrite(MOTOR_R_IN1_PIN, LOW);
digitalWrite(MOTOR_R_IN2_PIN, LOW);
Button5.print("停止");
}
}
// 速度控制
void slider1_callback(int32_t value)
{
BLINKER_LOG("get slider value: ", value);
int num_v=value;
switch (num_v)
{
case 1:
PWM = 600; //低速
Button1.print("低速");
break;
case 2:
PWM = 700; //中速
Button1.print("中速");
break;
case 3:
PWM = 800; //高速
Button1.print("高速");
break;
}
}
// 舵机控制
void slider2_callback(int32_t value)
{
BLINKER_LOG("get slider value: ", value);
int num_p=value;
switch (num_p)
{
case 1:
myServo.write(60);
Button5.print("舵机60度");
break;
case 2:
myServo.write(70);
Button5.print("舵机70度");
break;
case 3:
myServo.write(80);
Button5.print("舵机80度");
break;
case 4:
myServo.write(90);
Button5.print("舵机90度");
break;
case 5:
myServo.write(100);
Button5.print("舵机100度");
break;
case 6:
myServo.write(110);
Button5.print("舵机110度");
break;
case 7:
myServo.write(120);
Button5.print("舵机120度");
break;
}
}
void heartbeat()
{
Number2.print(Vol); //给blinkerapp回传电压数据
Number1.print((uint8_t)WiFi.RSSI());
}
void setup() {
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
//pinMode(Trig,OUTPUT);
//pinMode(Ecoh,INPUT);
myServo.attach(D2); //舵机的引脚
pinMode(MOTOR_L_PWM_PIN, OUTPUT);
pinMode(MOTOR_R_PWM_PIN, OUTPUT);
pinMode(MOTOR_L_IN1_PIN, OUTPUT);
pinMode(MOTOR_L_IN2_PIN, OUTPUT);
pinMode(MOTOR_R_IN1_PIN, OUTPUT);
pinMode(MOTOR_R_IN2_PIN, OUTPUT);
PWM = 600; //电机速度初始值
pos = 60; //舵机初始角度
analogWrite(MOTOR_L_PWM_PIN, 0);
analogWrite(MOTOR_R_PWM_PIN, 0);
digitalWrite(MOTOR_L_IN1_PIN, LOW);
digitalWrite(MOTOR_L_IN2_PIN, LOW);
digitalWrite(MOTOR_R_IN1_PIN, LOW);
digitalWrite(MOTOR_R_IN2_PIN, LOW);
myServo.write(pos); //初始化电机角度90
delay(50);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachHeartbeat(heartbeat);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Button4.attach(button4_callback);
Button5.attach(button5_callback);
Slider1.attach(slider1_callback);
Slider2.attach(slider2_callback);
}
void loop() {
Blinker.run();
V=analogRead(A0);
Vol=0.0278*V ;// Vol是V经过处理的数值
}
|
|