首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于ESP8266和Blynk应用的步进电机实时控制

基于ESP8266和Blynk应用的步进电机实时控制
EN

Stack Overflow用户
提问于 2020-05-26 10:12:44
回答 1查看 1.3K关注 0票数 0

我想控制两个步进电机运行机器人使用Blynk应用和NodeMCU/ App 8266操纵杆。但是当我在网上搜索步进电机的实时控制代码时,我没有得到太多的代码,而且大部分都不是实时的。

这是我目前正在处理的代码:-

代码语言:javascript
复制
#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); 
}

在这里,我已经定义了两个步进电机作为右和左,因为我使用的是TB6600电机驱动器,因此他们的脉冲和方向针也是定义的。这就是我无法使用Stepper电机库的主要原因。

运行代码时,我看到两个电机都正常运行了3到5秒,而Blynk服务器又断开并重新连接,导致电机停止运行,而不是创建实时通信。请帮助我创建这两个步进电机实时运行的代码。

我认为Blink.run()会导致服务器重新连接并停止马达。

我还搜索了这个原因,发现我应该使用AccelStepper库而不是,但这也是没有实现的。请帮我处理这个。任何正确的引用也是值得注意的。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2021-07-26 15:21:17

Blynk需要常量pings来保持连接的活力。while循环可以防止设备与Blynk服务器之间的任何形式的通信。首选的选择是使用计时器交互调用函数并在代码中前进。另一种方法是强制服务器ping。

例如,您可以在每个while循环中添加以下内容并调用softDelay(1)。

代码语言:javascript
复制
void softDelay(uint32_t t) {
  unsigned long currentTime = millis();`
  unsigned long newTime = currentTime + t;
  while (currentTime <= newTime)
  {
     Blynk.run();
     timer.run();
     currentTime = millis();
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62019726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档