久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2776|回復(fù): 8
打印 上一主題 下一主題
收起左側(cè)

請問這個(gè)庫函數(shù)程序是怎么循環(huán)執(zhí)行的?

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
在學(xué)習(xí)使用“大連好人”的EasyStepper的步進(jìn)電機(jī)控制庫函數(shù)。

在看EasyStepper.cpp文件,看到圖片中這個(gè)內(nèi)容非常困惑。明明是if的命令,為什么程序能不斷的循環(huán)?


請大家指教。附件是完整的EasyStepper庫文件。










分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:158981 發(fā)表于 2018-3-30 09:39 | 只看該作者
本帖最后由 duanyz 于 2018-3-30 12:47 編輯


回復(fù)

使用道具 舉報(bào)

板凳
ID:155507 發(fā)表于 2018-3-30 11:08 | 只看該作者

這段是參照網(wǎng)上編碼器讀取程序?qū)懙淖x取步進(jìn)電機(jī)控制脈沖的程序

  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. }
復(fù)制代碼



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

復(fù)制代碼


再來看 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. }
復(fù)制代碼
回復(fù)

使用道具 舉報(bào)

地板
ID:158981 發(fā)表于 2018-3-30 12:44 | 只看該作者

回復(fù)

使用道具 舉報(bào)

5#
ID:158981 發(fā)表于 2018-3-30 19:30 | 只看該作者
angmall 發(fā)表于 2018-3-30 11:08
這段是參照網(wǎng)上編碼器讀取程序?qū)懙淖x取步進(jìn)電機(jī)控制脈沖的程序

請總工來指點(diǎn)一下,不勝感激!
回復(fù)

使用道具 舉報(bào)

6#
ID:299646 發(fā)表于 2018-3-30 20:48 | 只看該作者
要有break試試
回復(fù)

使用道具 舉報(bào)

7#
ID:299646 發(fā)表于 2018-3-30 20:51 | 只看該作者
可以了嗎
回復(fù)

使用道具 舉報(bào)

8#
ID:158981 發(fā)表于 2018-3-31 09:26 | 只看該作者

什么可以了?我就是想知道為什么if語句里面的內(nèi)容能循環(huán)執(zhí)行的?能指點(diǎn)下嗎?
回復(fù)

使用道具 舉報(bào)

9#
ID:158981 發(fā)表于 2018-3-31 19:34 | 只看該作者
感謝上面各位,今天重復(fù)看了這個(gè)程序,才發(fā)現(xiàn)循環(huán)調(diào)用是要在寫程序里實(shí)現(xiàn)的,跟庫文件的內(nèi)容沒啥關(guān)系。

初學(xué)者多笑話
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 国内精品免费久久久久软件老师 | 国产91在线播放精品91 | 国产精品99久久久久久久vr | 亚洲精品中文字幕av | 一区二区精品 | 国产亚洲精品综合一区 | 欧美激情精品久久久久久变态 | 中文字幕国产一区 | 国产精品一区二区在线 | 懂色tv| 国产精品高潮呻吟久久av黑人 | 久久99精品国产麻豆婷婷 | www,黄色,com| 国产精品久久久久久久久免费 | 免费看av大片 | 久久精品免费观看 | 亚洲欧美视频在线观看 | 最新国产精品精品视频 | 999精品视频 | 欧美一区免费 | 久热精品在线 | 久久久久久久久久久久久91 | 国产午夜精品视频 | 国产精品久久久久一区二区 | 精品亚洲一区二区 | 国产欧美视频一区二区三区 | 久久夜色精品国产 | 久久久精品| 亚洲视频www | 97精品国产97久久久久久免费 | 国产精品日韩一区二区 | 欧美性网站 | 欧美视频三区 | 久久日韩精品一区二区三区 | 久久国产视频播放 | av中文在线 | 99久久99久久精品国产片果冰 | 久久久国产一区 | 黑人一级片视频 | 国产一区二区三区在线观看免费 | 欧美日韩在线一区二区 |