找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2077|回复: 8
打印 上一主题 下一主题
收起左侧

请问这个库函数程序是怎么循环执行的?

[复制链接]
跳转到指定楼层
楼主
在学习使用“大连好人”的EasyStepper的步进电机控制库函数。

在看EasyStepper.cpp文件,看到图片中这个内容非常困惑。明明是if的命令,为什么程序能不断的循环?


请大家指教。附件是完整的EasyStepper库文件。










分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:158981 发表于 2018-3-30 09:39 | 只看该作者
本帖最后由 duanyz 于 2018-3-30 12:47 编辑


回复

使用道具 举报

板凳
ID:155507 发表于 2018-3-30 11:08 | 只看该作者

这段是参照网上编码器读取程序写的读取步进电机控制脉冲的程序

  1. #define countA  2   //脉冲
  2. #define countB  3   //方向
  3.  
  4. long int count = 0;
  5. //long int zuobiao = 0;
  6. void setup() {
  7.  
  8.  
  9.   pinMode(countA, INPUT);
  10.   digitalWrite(countA, HIGH);       // turn on pullup resistor
  11.   pinMode(countB, INPUT);
  12.   digitalWrite(countB, HIGH);       // turn on pullup resistor
  13.  
  14.   attachInterrupt(0, doCount, LOW );  // encoder pin on interrupt 0 - pin 2
  15.   Serial.begin (115200);
  16.   Serial.println("start");                // a personal quirk
  17.  
  18. }
  19.  
  20.  
  21. void loop(){
  22. // do some stuff here - the joy of interrupts is that they take care of themselves
  23. }
  24.  
  25. void doCount() {
  26.   /* If pinA and pinB are both high or both low, it is spinning
  27.    * forward. If they're different, it's going backward.
  28.    *
  29.    * For more information on speeding up this process, see
  30.    * [Reference/PortManipulation], specifically the PIND register.
  31.    */
  32.   if (digitalRead(countA) == digitalRead(countB)) {
  33.     count++;
  34.   } else {
  35.     count--;
  36.   }
  37.  // noInterrupts();
  38.  Serial.println (count, DEC);
  39.  // interrupts();
  40. }
复制代码



EasyStepper.h

  1. #ifndef EasyStepper_h
  2. #define EasyStepper_h

  3. #include <stdlib.h>
  4. #if ARDUINO >= 100
  5. #include <Arduino.h>
  6. #else
  7. #include <WProgram.h>
  8. #include <wiring.h>
  9. #endif

  10. /**
  11. * author [email]shangcm@gmail.com[/email]
  12. */
  13. class EasyStepper
  14. {
  15.         public:
  16.                
  17.                 /**
  18.                 * to create the EasyStepper
  19.                 * parameters:
  20.                 * stepPin the step pin
  21.                 * directionPin the direction pin
  22.                 * enablePin the enable pin
  23.                 * directionPinsInverted if the value is true then invert HIGH/LOW value for direction pin
  24.                 * enablePinsInverted if the value is true then invert HIGH/LOW value for enable pin
  25.                 */
  26.                 EasyStepper(byte stepPin, byte directionPin, byte enablePin, bool directionPinsInverted, bool enablePinsInverted);
  27.                
  28.                 /**
  29.                 * to startup the EasyStepper
  30.                 */
  31.                 void startup();
  32.                
  33.                 /**
  34.                 * to shutdown the EasyStepper, will release the enable pin to save power
  35.                 */
  36.                 void shutdown();
  37.                
  38.                 /**
  39.                 * to rotate steps from current position with speed, the acceleration = 0
  40.                 * speed the speed for rotation, the unit is steps per second
  41.                 * steps the steps to go
  42.                 */
  43.                 void rotate(float speed, int steps);
  44.                
  45.                 /**
  46.                 * to rotate steps from current position with speed
  47.                 * speed the speed for rotation, the unit is steps per second
  48.                 * steps the steps to go
  49.                 * acceleration the acceleration, if it = 0, means no acceleration
  50.                 */
  51.                 void rotate(float speed, int steps, int acceleration);
  52.                
  53.                 /**
  54.                 * to stop the stepper, the motor will stay at current position
  55.                 */
  56.                 void stop();
  57.    
  58.     /**
  59.     * run, you must call this as frequently as possible, but at least once per minimum step interval,
  60.     * preferably in your main loop.
  61.     */
  62.     void run();
  63.    
  64.     /**
  65.     * if return true means the action is done
  66.     */
  67.     boolean isDone();
  68.    
  69.     /**
  70.     * enable the debug mode?
  71.     */
  72.     void debugMode(boolean enabled);
  73.                
  74.         protected:
  75.                
  76.                 //
  77.                
  78.         private:
  79.                
  80.                 boolean debugModeFlag;
  81.                
  82.                 byte stepPin;
  83.                 byte directionPin;
  84.                 byte enablePin;
  85.                
  86.                 bool directionPinsInverted;
  87.                 bool enablePinsInverted;
  88.                
  89.                 int acceleration;
  90.                 int stage1; // the end step of the stage1
  91.                 int stage2; // the end step of the stage2
  92.                
  93.                 int stepsToGo;
  94.                 int stepsGone;
  95.                
  96.                 float stepTime;
  97.                 float startTime;
  98.                 float stage1StepTime;
  99.                 float stage3StepTime;
  100.                 unsigned long nextTimestamp;
  101.                
  102.                 boolean done;
  103.                
  104.                 void step();
  105.                
  106. };

  107. #endif

复制代码


再来看 EasyStepper.cpp (代码里面有注释)

  1. #include "EasyStepper.h"

  2. EasyStepper::EasyStepper(byte stepPin, byte directionPin, byte enablePin, bool directionPinsInverted, bool enablePinsInverted)
  3. {
  4.         // save the parameters
  5.         this->stepPin = stepPin;
  6.         this->directionPin = directionPin;
  7.         this->enablePin = enablePin;
  8.         this->directionPinsInverted = directionPinsInverted;
  9.         this->enablePinsInverted = enablePinsInverted;
  10. }

  11. void EasyStepper::startup()
  12. {
  13.         // set the pin mode
  14.         pinMode(this->stepPin, OUTPUT);
  15.         pinMode(this->directionPin, OUTPUT);
  16.         pinMode(this->enablePin, OUTPUT);
  17.         // enable the stepper
  18.         digitalWrite(enablePin, HIGH ^ this->enablePinsInverted);
  19.         // initialize the done to true
  20.         this->done = true;
  21. }

  22. void EasyStepper::shutdown()
  23. {
  24.         // disable the stepper
  25.         digitalWrite(enablePin, LOW ^ this->enablePinsInverted);
  26. }

  27. void EasyStepper::debugMode(boolean enabled)
  28. {
  29.         this->debugModeFlag = enabled;
  30. }

  31. void EasyStepper::rotate(float speed, int steps)
  32. {
  33.         this->rotate(speed, steps, 0);
  34. }

  35. void EasyStepper::rotate(float speed, int steps, int acceleration)
  36. {
  37.         // ignore the zero value
  38.         if (speed != 0 && steps != 0)
  39.         {
  40.                 if (steps > 0)
  41.                 {
  42.                         // CW
  43.                         digitalWrite(directionPin, HIGH ^ this->directionPinsInverted);
  44.                 }
  45.                 else if (steps < 0)
  46.                 {
  47.                         // CCW
  48.                         digitalWrite(directionPin, LOW ^ this->directionPinsInverted);
  49.                 }
  50.                 // done flag
  51.                 this->done = false;
  52.                 // the steps to go
  53.                 this->stepsToGo = abs(steps);
  54.                 // the acceleration
  55.                 this->acceleration = abs(acceleration);
  56.                 if (this->stepsToGo <= 4)
  57.                 {
  58.                         this->acceleration = 0;
  59.                 }
  60.                 // change the speed to stepTime, micro seconds per step
  61.                 this->stepTime = 1000.0 * 1000.0 / abs(speed);
  62.                 // start time
  63.                 this->startTime = this->stepTime * (this->acceleration + 1);
  64.                 // stage1
  65.                 this->stage1 = this->stepsToGo / 4;
  66.                 this->stage1StepTime = (float) this->acceleration * this->stepTime / this->stage1;
  67.                 // stage2
  68.                 this->stage2 = this->stepsToGo / 4 * 3;
  69.                 this->stage3StepTime = (float) this->acceleration * this->stepTime / (this->stepsToGo - this->stage2);
  70.                 // the steps gone
  71.                 this->stepsGone = 0;
  72.                 // current timestamp
  73.                 unsigned long time = micros();
  74.                 if (this->debugModeFlag)
  75.                 {
  76.                         Serial.print("rotate: direction=");
  77.                         Serial.print(steps > 0 ? "CW" : "CCW");
  78.                         Serial.print(", stepsToGo=");
  79.                         Serial.print(this->stepsToGo);
  80.                         Serial.print(", acceleration=");
  81.                         Serial.print(this->acceleration);
  82.                         Serial.print(", startTime=");
  83.                         Serial.print(this->startTime);
  84.                         Serial.print(", stage1=");
  85.                         Serial.print(this->stage1);
  86.                         Serial.print(", stage1StepTime=");
  87.                         Serial.print(this->stage1StepTime);
  88.                         Serial.print(", stage2=");
  89.                         Serial.print(this->stage2);
  90.                         Serial.print(", stage3StepTime=");
  91.                         Serial.print(this->stage3StepTime);
  92.                         Serial.print(", stepTime=");
  93.                         Serial.print(this->stepTime);
  94.                         Serial.print(", currentTimestamp=");
  95.                         Serial.println(time);
  96.                 }
  97.                 // call the step method to rotate the motor
  98.                 this->step();
  99.         }
  100. }

  101. void EasyStepper::stop()
  102. {
  103.         this->stepsToGo = 0;
  104.         this->done = true;
  105. }

  106. void EasyStepper::run()
  107. {
  108.         // the current timestamp
  109.         unsigned long time = micros();
  110.         if (!this->done && time >= this->nextTimestamp)
  111.         {
  112.                 this->step();
  113.         }
  114.         if (this->debugModeFlag)
  115.         {
  116.                 Serial.print("currentTimeStamp=");
  117.                 Serial.println(time);
  118.         }
  119. }

  120. void EasyStepper::step()
  121. {
  122.         // are there some steps to rotate?
  123.         if (this->stepsToGo > this->stepsGone)
  124.         {
  125.                 // HIGH value
  126.                 digitalWrite(stepPin, HIGH);
  127.                 // delay
  128.                 delayMicroseconds(2);
  129.                 // LOW value
  130.                 digitalWrite(stepPin, LOW);
  131.                 // increase the stepsGone
  132.                 this->stepsGone++;
  133.                 // default: stage2
  134.                 float nextStepTime = this->stepTime;
  135.                 if (this->acceleration != 0)
  136.                 {
  137.                         if (this->stepsGone < this->stage1)
  138.                         {
  139.                                 // stage1: down to stepTime from startTime
  140.                                 nextStepTime = this->startTime - this->stage1StepTime * this->stepsGone;
  141.                         }
  142.                         else if (this->stepsGone >= this->stage2)
  143.                         {
  144.                                 // stage3: up to startTime from stepTime
  145.                                 nextStepTime = this->stepTime + this->stage3StepTime * (this->stepsGone - this->stage2 + 1);
  146.                         }
  147.                 }
  148.                 // current timestamp
  149.                 unsigned long time = micros();
  150.                 this->nextTimestamp = time + nextStepTime;
  151.                 if (this->debugModeFlag)
  152.                 {
  153.                         Serial.print("step: stepsGone=");
  154.                         Serial.print(this->stepsGone);
  155.                         Serial.print(", currentTimestamp=");
  156.                         Serial.print(time);
  157.                         Serial.print(", nextTimestamp=");
  158.                         Serial.print(this->nextTimestamp);
  159.                         Serial.print(", nextStepTime=");
  160.                         Serial.println(nextStepTime);
  161.                 }
  162.         }
  163.         else
  164.         {
  165.                 this->done = true;
  166.         }
  167. }
  168.    
  169. boolean EasyStepper::isDone()
  170. {
  171.         return this->done;
  172. }
复制代码
回复

使用道具 举报

地板
ID:158981 发表于 2018-3-30 12:44 | 只看该作者

回复

使用道具 举报

5#
ID:158981 发表于 2018-3-30 19:30 | 只看该作者
angmall 发表于 2018-3-30 11:08
这段是参照网上编码器读取程序写的读取步进电机控制脉冲的程序

请总工来指点一下,不胜感激!
回复

使用道具 举报

6#
ID:299646 发表于 2018-3-30 20:48 | 只看该作者
要有break试试
回复

使用道具 举报

7#
ID:299646 发表于 2018-3-30 20:51 | 只看该作者
可以了吗
回复

使用道具 举报

8#
ID:158981 发表于 2018-3-31 09:26 | 只看该作者

什么可以了?我就是想知道为什么if语句里面的内容能循环执行的?能指点下吗?
回复

使用道具 举报

9#
ID:158981 发表于 2018-3-31 19:34 | 只看该作者
感谢上面各位,今天重复看了这个程序,才发现循环调用是要在写程序里实现的,跟库文件的内容没啥关系。

初学者多笑话
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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