我想使用 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 库而不是步进电机库,但这也没有实现。请在这件事上给予我帮助。任何正确的参考也是值得赞赏的。预先感谢。
Blynk 需要常量 ping 来保持连接处于活动状态。 while 循环会阻止您的设备和 Blynk 服务器之间进行任何形式的通信。 首选选项是使用计时器以交互方式调用您的函数并推进代码。另一种方法是强制服务器 ping。
例如,您可以添加以下内容并在每个 while 循环中调用 softDelay(1)。
void softDelay(uint32_t t) {
unsigned long currentTime = millis();`
unsigned long newTime = currentTime + t;
while (currentTime <= newTime)
{
Blynk.run();
timer.run();
currentTime = millis();
}
}