close
最近在更深入研究如何控制步進馬達時,
看了Arduino範例的stepper.c,了解其運作原理,
但卻也意外地發現其網路上有人提到的bug。
我們可以用NotePad打開stepper.c,找到:
看了Arduino範例的stepper.c,了解其運作原理,
但卻也意外地發現其網路上有人提到的bug。
我們可以用NotePad打開stepper.c,找到:

this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed; -->每一步所需要的時間
以及
if (millis() - this->last_step_time >= this->step_delay) {
// get the timeStamp of when you stepped:
this->last_step_time = millis();
其中step_delay是指每一步所需要的時間,為整數,是number_of_steps和whatSpeed運算的結果,控制了轉速的快慢,
以及
if (millis() - this->last_step_time >= this->step_delay) {
// get the timeStamp of when you stepped:
this->last_step_time = millis();
其中step_delay是指每一步所需要的時間,為整數,是number_of_steps和whatSpeed運算的結果,控制了轉速的快慢,
而millis()函式會回傳 Arduino 從開始執行程式一直到目前為止的千分之一秒數值(number of milliseconds),這個數值在大約 50 天後會溢位(overflow),屆時會從 0 開始計數。
其bug為:
所以當step_delay計算出來小於0時,祺值會變成0,而產生誤差。
解決方法是須要將millis()替換成micros(),code如下:
this->step_delay = 60L * 1000L / this->number_of_steps * 1000 / whatSpeed;
..
.
if (micros() - this->last_step_time >= this->step_delay)
this->last_step_time = micros();
其bug來源文章為:
http://forum.arduino.cc/index.php?topic=46964.0
文章標籤
全站熱搜