极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13478|回复: 1

【已解决】上位机控制步进电机问题请教

[复制链接]
发表于 2016-11-23 17:09:43 | 显示全部楼层 |阅读模式
本帖最后由 情谊永恒1992 于 2016-11-23 17:17 编辑

我使用了大连好人写的库EasyStepper,以及借鉴了JasmineL的程序,现附上传送门:
http://www.geek-workshop.com/thread-3266-1-1.html
http://www.geek-workshop.com/thread-4319-1-1.html
==============
我想用PC上位机去操控一台57步进电机,利用串口通信,让上位机来控制电机运动步数,所以编写了如下程序,单次操作是没有问题的,但是单次运动完后,从串口监视器再输入步数,电机也不运动了,我测试过,感觉是程序一直在库文件的某个函数死循环没出来,因为我输入步数后也不会再返回该步数。。。
附上代码,希望大神帮忙看看问题出在哪里
  1. #include <EasyStepper.h>

  2. // define the pins
  3. #define STEP_PIN 3
  4. #define DIR_PIN 30
  5. #define EN_PIN 33

  6. // define the inverted
  7. #define DIR_PIN_INVERTED true
  8. #define EN_PIN_INVERTED false  //HIGH使能

  9. // the EasyStepper instance
  10. EasyStepper stepper1(STEP_PIN, DIR_PIN, EN_PIN, DIR_PIN_INVERTED, EN_PIN_INVERTED);

  11. // the stepps to rotate
  12. int stepps;
  13. int steps;

  14. void setup()
  15. {
  16.   Serial.begin(9600);
  17.   stepper1.debugMode(false);
  18.   stepper1.startup();
  19. }

  20. void loop()
  21. {
  22.    while (Serial.available())
  23.   {
  24.       stepps=Serial.parseInt();
  25.      Serial.println(stepps);
  26.      steps=stepps*4;//四分数
  27.      stepps=0;
  28.       stepper1.rotate(400.0,steps);
  29.     while(steps !=0 )
  30.     {
  31.       stepper1.run();
  32.    }
  33.   }  
  34. }
复制代码


==============================================
5分钟后在JasmineL的另一个帖子里找到了原因,还是自己太不熟练,希望新手借鉴不要犯跟我一样的错
原因是困在第二个while循环里没出来。。。
所以要把steps赋值0,出循环
附上正确代码
  1. #include <EasyStepper.h>

  2. // define the pins
  3. #define STEP_PIN 3
  4. #define DIR_PIN 30
  5. #define EN_PIN 33

  6. // define the inverted
  7. #define DIR_PIN_INVERTED true
  8. #define EN_PIN_INVERTED false

  9. // the EasyStepper instance
  10. EasyStepper stepper1(STEP_PIN, DIR_PIN, EN_PIN, DIR_PIN_INVERTED, EN_PIN_INVERTED);

  11. // the stepps to rotate
  12. int stepps;
  13. int steps;

  14. void setup()
  15. {
  16.   Serial.begin(9600);
  17.   stepper1.debugMode(false);
  18.   stepper1.startup();
  19. }

  20. void loop()
  21. {
  22.    while (Serial.available())
  23.   {
  24.       stepps=Serial.parseInt();
  25.      Serial.println(stepps);
  26.      steps=stepps*4;
  27.      stepps=0;
  28.       stepper1.rotate(400.0,steps);
  29.     while(steps !=0 )
  30.     {
  31.       stepper1.run();
  32.       if(stepper1.isDone())
  33.       {
  34.         steps=0;
  35.         }
  36.    }
  37.   }  
  38. }
复制代码
回复

使用道具 举报

发表于 2018-1-3 22:08:00 | 显示全部楼层
请教楼主,如果速度和脉冲数两个都从串口获得,要怎么写好呢?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-7 08:07 , Processed in 0.046109 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表