我想使用 Blynk App 和 NodeMCU/ESP8266 的操纵杆来控制 2 个步进电机来运行机器人。但是当我在网上搜索步进电机实时控制的代码时,我没有得到很多代码,而且大部分都不是实时的。
这是我目前正在处理的代码:-
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define RightMotorSpeed D7
#define RightMotorDir D8
const int enPin = D2;
const int enPin2 = D3;
#define LeftMotorSpeed D6
#define LeftMotorDir D5
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
// Use your own WiFi settings
char auth[] = "LRTCZUnCI06P-pqh5rlPXRbuOUgQ_uGH";
char ssid[] = "Airtel_7599998800";
char pass[] = "air71454";
// neutral zone settings for x and y
// joystick must move outside these boundary numbers to activate the motors
// makes it a little easier to control the wifi car
int minRange = 312;
int maxRange = 712;
// analog speeds from 0 (lowest) - 1023 (highest)
// 3 speeds used -- 0 (noSpeed), 350 (minSpeed), 850 (maxSpeed).
// use whatever speeds you want...too fast made it a pain in the ass to control
int minSpeed = 450;
int maxSpeed = 1023;
int noSpeed = 0;
void moveControl(int x, int y)
{
// movement logic
// move forward
// y je vetsi jak maxrange a současně x je vetsi jak minRange a současne mensi jak max range
while(y >= maxRange && x >= minRange && x <= maxRange) //zataci R
{
digitalWrite(RightMotorDir,HIGH);
digitalWrite(LeftMotorDir,HIGH);
analogWrite(RightMotorSpeed,maxSpeed);
analogWrite(LeftMotorSpeed,maxSpeed);
delayMicroseconds(500);
digitalWrite(RightMotorSpeed,0);
digitalWrite(LeftMotorSpeed,0);
delayMicroseconds(500);
}
// move forward right
while(x >= maxRange && y >= maxRange) //zataci R
{
digitalWrite(RightMotorDir,HIGH);
digitalWrite(LeftMotorDir,HIGH);
analogWrite(RightMotorSpeed,minSpeed);
analogWrite(LeftMotorSpeed,maxSpeed);
}
// move forward left
while(x <= minRange && y >= maxRange)
{
digitalWrite(RightMotorDir,HIGH);
digitalWrite(LeftMotorDir,HIGH);
analogWrite(RightMotorSpeed,maxSpeed);
analogWrite(LeftMotorSpeed,minSpeed);
}
// neutral zone
while(y < maxRange && y > minRange && x < maxRange && x > minRange)
{
analogWrite(RightMotorSpeed,noSpeed);
analogWrite(LeftMotorSpeed,noSpeed);
}
// move back
while(y <= minRange && x >= minRange && x <= maxRange)
{
digitalWrite(RightMotorDir,LOW);
digitalWrite(LeftMotorDir,LOW);
analogWrite(RightMotorSpeed,maxSpeed);
analogWrite(LeftMotorSpeed,maxSpeed);
}
// move back and right
while(y <= minRange && x <= minRange)
{
digitalWrite(RightMotorDir,LOW);
digitalWrite(LeftMotorDir,LOW);
analogWrite(RightMotorSpeed,minSpeed);
analogWrite(LeftMotorSpeed,maxSpeed);
}
// move back and left
while(y <= minRange && x >= maxRange)
{
digitalWrite(RightMotorDir,LOW);
digitalWrite(LeftMotorDir,LOW);
analogWrite(RightMotorSpeed,maxSpeed);
analogWrite(LeftMotorSpeed,minSpeed);
}
}
void setup()
{
// initial settings for motors off and direction forward
pinMode(RightMotorSpeed, OUTPUT);
pinMode(LeftMotorSpeed, OUTPUT);
pinMode(RightMotorDir, OUTPUT);
pinMode(LeftMotorDir, OUTPUT);
digitalWrite(RightMotorSpeed, LOW);
digitalWrite(LeftMotorSpeed, LOW);
digitalWrite(RightMotorDir, HIGH);
digitalWrite(LeftMotorDir,HIGH);
Serial.begin(9600);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
pinMode(enPin2,OUTPUT);
digitalWrite(enPin2,LOW);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
}
BLYNK_WRITE(V1)
{
int x = param[0].asInt();
int y = param[1].asInt();
moveControl(x,y);
}
在这里,我将 2 个步进电机定义为左右,由于我使用的是 TB6600 电机驱动器,因此它们的脉冲和方向引脚也被定义。这是我无法将步进电机库用于代码的主要原因。
运行代码我看到两个电机都运行良好一次 3 到 5 秒,并且 Blynk 服务器再次断开连接并重新连接,导致电机停止并且没有创建实时通信。有人请帮我为这两个步进电机创建一个实时运行的代码。
我认为Blink.run()会导致服务器重新连接并停止电机。
我也搜索了这个原因,发现我应该使用 AccelStepper 库而不是步进电机库,但这也没有实现。请帮我解决一下这个。任何正确的参考也是可观的。提前致谢。