找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

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

Arduino機(jī)械學(xué)習(xí)筆記1(從Easydriver開始)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
***********************
從淘寶上買了光驅(qū)式激光雕刻機(jī)DIY,目前還沒有能力裝上。相比安裝要求,配套教程確實(shí)不夠詳細(xì)。雖然不需要知道原理,只知道步驟也可以完成雕刻機(jī)安裝,但是步驟不清楚,另外自己對(duì)arduino和easydriver也不是很熟悉,所以難上加難。

為了不半途而廢,所以打算邊學(xué)基礎(chǔ)邊實(shí)踐,最終完成對(duì)激光雕刻機(jī)的理解和實(shí)踐。

***********************

選擇easydriver作為起點(diǎn),是因?yàn)樗竭B接 arduino 和 步進(jìn)電機(jī)的 關(guān)鍵核心。

機(jī)械坊已經(jīng)有兩個(gè)例子,
*******
http://www.torrancerestoration.com/bbs/dpj-47861-1.html
微型CNC制作基于開源項(xiàng)目GRBL

這個(gè)例子就是最終要完成的,只是覺得該例子對(duì)于基礎(chǔ)差的不夠詳細(xì),而且步驟有跳躍,一上來就按照該教程失敗率較高。

*******
http://www.torrancerestoration.com/bbs/dpj-47860-1.html
arduino當(dāng)Gcode解釋程序(CNC)

這個(gè)例子雖然有用,但對(duì)新手來說更不完整,新手無從下手。
********

直接找到easydriver的老家,
http://schmalzhaus.com/EasyDrive ... DriverExamples.html
果然提供新手的例子(并推薦進(jìn)階可以參考blogspot 上面的turorial,遺憾的是打不開,不知道是不是與長(zhǎng)城有關(guān))

第一個(gè)例子是 設(shè)置,怎樣連線
***************************************
Example 1: Basic Arduino setup


上面的示意圖可以使用 Arduino設(shè)計(jì)助手來畫,剛剛才搜到。

為什么這樣連接,需要看easydriver 示意圖


從上圖可以看出,步進(jìn)電機(jī)是4根線,選擇2相4線類型,淘寶上非常多。接線順序示意圖很清晰很簡(jiǎn)單。

這里有個(gè)事項(xiàng),easydriver的排針是沒有焊接上,需要玩家自己焊接,這時(shí)就有兩種選擇:
1、仿照上面的示意圖(使用面包板)


2、不使用面包板


需要仔細(xì)看,就是排針的方向,排針焊接在正面,就是使用面板板;焊接在反面,就不用面包板。

步進(jìn)電機(jī)需要單獨(dú)供電,6~30v。
******************************************

例子1的目的在于連線,代碼反而很簡(jiǎn)單,

void setup() {               

  pinMode(8, OUTPUT);     

  pinMode(9, OUTPUT);

  digitalWrite(8, LOW);

  digitalWrite(9, LOW);

}


void loop() {

  digitalWrite(9, HIGH);

  delay(1);         

  digitalWrite(9, LOW);

  delay(1);         

}



pin8 是方向控制,pin9是脈沖控制,內(nèi)容解釋可以看原文,這里不翻譯了。
除了這里
Well, with the STEP signal 1ms high and 1ms low, each complete pulse will take 2ms of time. Since there are 1000ms in 1 second, then 1000/2 = 500 microsteps/second.

脈沖信號(hào)是 1ms 高電平 1ms 低電平,所以一個(gè)完整的脈沖是 2ms。1秒 等于 1000ms,所以 1秒 / 2ms = 500 次脈沖 / 秒。

這樣第一例子就完成了。

例子1的測(cè)試效果并不是很好,可能與我使用的步進(jìn)電機(jī)有關(guān),我是淘寶上買的12元一只的,個(gè)頭比單片機(jī)配套的那種大很多。最大的問題是發(fā)熱。

例子2
  1.     int Distance = 0;  // Record the number of steps we've taken
  2.      
  3.     void setup() {               
  4.      
  5.       pinMode(8, OUTPUT);     
  6.      
  7.       pinMode(9, OUTPUT);
  8.      
  9.       digitalWrite(8, LOW);
  10.      
  11.       digitalWrite(9, LOW);
  12.      
  13.     }
  14.      
  15.      
  16.     void loop() {
  17.      
  18.       digitalWrite(9, HIGH);
  19.      
  20.       delayMicroseconds(100);         
  21.      
  22.       digitalWrite(9, LOW);
  23.      
  24.       delayMicroseconds(100);
  25.      
  26.       Distance = Distance + 1;   // record this step
  27.      
  28.       // Check to see if we are at the end of our move
  29.      
  30.       if (Distance == 3600)
  31.      
  32.       {
  33.      
  34.         // We are! Reverse direction (invert DIR signal)
  35.      
  36.         if (digitalRead(8) == LOW)
  37.      
  38.         {
  39.      
  40.           digitalWrite(8, HIGH);
  41.      
  42.         }
  43.      
  44.         else
  45.      
  46.         {
  47.      
  48.           digitalWrite(8, LOW);
  49.      
  50.         }
  51.      
  52.         // Reset our distance back to zero since we're
  53.      
  54.         // starting a new move
  55.      
  56.         Distance = 0;
  57.         // Now pause for half a second
  58.         delay(500);
  59.       }
  60.     }
復(fù)制代碼


代碼清晰簡(jiǎn)潔,沒有算法和繞彎,就像看說明書一樣。
這段代碼的執(zhí)行效果比較好,正轉(zhuǎn)一下(3600個(gè)脈沖)然后反轉(zhuǎn)。
問題也是,半分鐘easydriver就有些燙了,感覺不能適應(yīng)功率稍微大一些的電機(jī)。下一個(gè)筆記試試TB6560。

*******************************
找到問題:easydriver 發(fā)燙

后面例子都不需要更換電路圖

第三個(gè)例子使用了一個(gè)庫,需要另外安裝(非常簡(jiǎn)單,下載AccelStepper庫,然后放到 arduino安裝目錄Arduinolibraries 下就可以了
AccelStepper-1.39.zip (73.9 KB, 下載次數(shù): 23)

只有安裝了上面的庫,才可以運(yùn)行例子3
  1.     #include
  2.      
  3.      
  4.     // Define a stepper and the pins it will use
  5.      
  6.     AccelStepper stepper(1, 9, 8);
  7.      
  8.      
  9.     int pos = 3600;
  10.      
  11.      
  12.     void setup()
  13.      
  14.     {  
  15.      
  16.       stepper.setMaxSpeed(3000);
  17.      
  18.       stepper.setAcceleration(1000);
  19.      
  20.     }
  21.      
  22.      
  23.     void loop()
  24.      
  25.     {
  26.      
  27.       if (stepper.distanceToGo() == 0)
  28.      
  29.       {
  30.      
  31.         delay(500);
  32.      
  33.         pos = -pos;
  34.      
  35.         stepper.moveTo(pos);
  36.      
  37.       }
  38.      
  39.       stepper.run();
  40.      
  41.     }
復(fù)制代碼


上面的代碼暫時(shí)不打算仔細(xì)研究,
如果要深入理解的話,可以到 http://www.airspayce.com/mikem/arduino/AccelStepper/index.html
上看例子。

****************************
在使用時(shí),發(fā)現(xiàn)如果電機(jī)不固定,根據(jù)牛頓作用力反作用力定理,轉(zhuǎn)到效果不佳。
另外查到使用電機(jī)步距角是 3.75度,轉(zhuǎn)一圈96步,所以轉(zhuǎn)的圈數(shù)比描述的多一倍(例子使用1.8 度)。

因?yàn)樽约汉附铀讲坏郊遥允褂脮r(shí)出現(xiàn)由于 easydriver 焊接不好而產(chǎn)生的故障,多準(zhǔn)備幾個(gè)互相校準(zhǔn)還是有用的。

第四個(gè)例子需要兩個(gè)電動(dòng)機(jī),如下示意圖,其實(shí)一個(gè)電機(jī)也可以,只不過把pin8 移到 pin6 ,pin9 移到 pin7 ;就可以作出,更簡(jiǎn)單。
例子4和例子3 都需要 AccelStepper 庫。



  1.     #include
  2.      
  3.      
  4.     // Define two steppers and the pins they will use
  5.      
  6.     AccelStepper stepper1(1, 9, 8);
  7.      
  8.     AccelStepper stepper2(1, 7, 6);
  9.      
  10.      
  11.     int pos1 = 3600;
  12.      
  13.     int pos2 = 5678;
  14.      
  15.      
  16.     void setup()
  17.      
  18.     {  
  19.      
  20.       stepper1.setMaxSpeed(3000);
  21.      
  22.       stepper1.setAcceleration(1000);
  23.      
  24.       stepper2.setMaxSpeed(2000);
  25.      
  26.       stepper2.setAcceleration(800);
  27.      
  28.     }
  29.      
  30.      
  31.     void loop()
  32.      
  33.     {
  34.      
  35.       if (stepper1.distanceToGo() == 0)
  36.      
  37.       {
  38.      
  39.             pos1 = -pos1;
  40.      
  41.         stepper1.moveTo(pos1);
  42.      
  43.       }
  44.      
  45.       if (stepper2.distanceToGo() == 0)
  46.      
  47.       {
  48.      
  49.         pos2 = -pos2;
  50.      
  51.         stepper2.moveTo(pos2);
  52.      
  53.       }
  54.      
  55.       stepper1.run();
  56.      
  57.       stepper2.run();
  58.      
  59.     }
復(fù)制代碼

懶得解釋。

*************************
這樣,基礎(chǔ)的幾個(gè)例子就看完了。



如果剛買了easydriver,焊接后測(cè)試是否能用,推薦

http://www.tinyos.net.cn/?article-38.html
EasyDriver 步進(jìn)電機(jī)驅(qū)動(dòng)板測(cè)試筆記



測(cè)試效果:

轉(zhuǎn)360度,
暫停1秒,
反轉(zhuǎn)360度,
暫停1秒,

轉(zhuǎn)1600個(gè)脈沖,速度5
暫停1秒
反轉(zhuǎn)1600個(gè)脈沖,速度25
暫停1秒

反復(fù)。
  1.     #define DIR_PIN 2
  2.     #define STEP_PIN 3
  3.      
  4.     void setup() {
  5.       pinMode(DIR_PIN, OUTPUT);
  6.       pinMode(STEP_PIN, OUTPUT);
  7.     }
  8.      
  9.     void loop(){
  10.      
  11.       //rotate a specific number of degrees
  12.       rotateDeg(360, 1);
  13.       delay(1000);
  14.      
  15.       rotateDeg(-360, .1);  //reverse
  16.       delay(1000);
  17.      
  18.       //rotate a specific number of microsteps (8 microsteps per step)
  19.       //a 200 step stepper would take 1600 micro steps for one full revolution
  20.       rotate(1600, .5);
  21.       delay(1000);
  22.      
  23.       rotate(-1600, .25); //reverse
  24.       delay(1000);
  25.     }
  26.      
  27.     void rotate(int steps, float speed){
  28.       //rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
  29.       //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  30.       int dir = (steps > 0)? HIGH:LOW;
  31.       steps = abs(steps);
  32.      
  33.       digitalWrite(DIR_PIN,dir);
  34.      
  35.       float usDelay = (1/speed) * 70;
  36.      
  37.       for(int i=0; i < steps; i++){
  38.         digitalWrite(STEP_PIN, HIGH);
  39.         delayMicroseconds(usDelay);
  40.      
  41.         digitalWrite(STEP_PIN, LOW);
  42.         delayMicroseconds(usDelay);
  43.       }
  44.     }
  45.      
  46.     void rotateDeg(float deg, float speed){
  47.       //rotate a specific number of degrees (negitive for reverse movement)
  48.       //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  49.       int dir = (deg > 0)? HIGH:LOW;
  50.       digitalWrite(DIR_PIN,dir);
  51.      
  52.       int steps = abs(deg)*(1/0.225);
  53.       float usDelay = (1/speed) * 70;
  54.      
  55.       for(int i=0; i < steps; i++){
  56.         digitalWrite(STEP_PIN, HIGH);
  57.         delayMicroseconds(usDelay);
  58.      
  59.         digitalWrite(STEP_PIN, LOW);
  60.         delayMicroseconds(usDelay);
  61.       }
  62.     }
復(fù)制代碼

easydriver 常見問題 (http://schmalzhaus.com/EasyDriver/
只把問題列出來,如果有類似的,則用上面的鏈接打開再看。

Q1) My motor says it can only take 2.1V at 2A. Will the EasyDriver (running from up to 30V) blow up my motor or damage it in any way?

Q2) So shouldn't I run the power to the EasyDriver at the voltage that my motor is rated for? (i.e. 2.1V as per the above example)

Q3) How much current does my power supply need to produce?

Q4) So why does my bench power supply show 12V at 400mA when I know my motor should be drawing 750mA/phase (1.5A total)? Huh smarty pants?

Q5) How do I adjust the current limit?

Q5.1) What kind of stepper motors can I use EasyDriver with?

Q6) Why does EasyDriver get so hot?

Q7) What hardware/software can I use to test my EasyDriver?

Q8) How do I connect my EasyDriver up?

Q9) My Easy Driver's labels don't match what I see in the picture. Why not? And how do I know which way to turn the current adjustment pot?

Q10) Man, this is a lot of work to just use the A3967 chip. Can't I just solder down a bunch of A3967s on my own board design and save a ton of money?

Q11) The datasheet for the driver chip shows that the motor connects to pins OUT1A, OUT1B, OUT2A and OUT2B, and the diagram in the datasheet has one coil connected across OUT1A and OUT1B, and the other across OUT2A and OUT2B.  But the Easy Driver only has A, A, B, B for motor connections, and it looks like one coil should be connected across the two A pins and the other across the two B pins. What's up with that?

Q12) So what's the deal with microsteps? How do I set what number of microsteps are used?

Q13) Help! I think my Easy Driver is not working like it should. How can I know if it's become damaged?

Q14) But Brian, can't you give us some real world numbers for power consumption and heat and stuff like that?

Q15) So I see that the EasyDriver has a pin labeled 5V. What is it for?
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏3 分享淘帖 頂 踩

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:98618 發(fā)表于 2016-4-9 23:03 | 只看該作者
很不錯(cuò)的帖子,我也是被那個(gè)激光的雕刻機(jī)拉下水的,機(jī)器沒做成,但是認(rèn)識(shí)了arduino。哈哈

其實(shí)那個(gè)機(jī)器不做也罷,知道原理就行了。那個(gè)機(jī)器的精度真的是在是低的不能再。。。或者說是沒有精度可言比較確切。

easydriver挺好,就是發(fā)熱大了點(diǎn)。其實(shí)我們可以把easydriver看做是一個(gè)硬件的頭文件,它有幾個(gè)函數(shù),我們只要調(diào)用函數(shù)就能比較精準(zhǔn)的驅(qū)動(dòng)步進(jìn)電機(jī)了。 其實(shí)那種微型的步進(jìn)電機(jī)完全可以用arduino直接驅(qū)動(dòng),但是需要占用4個(gè)口,并且需要更多的cpu資源。因?yàn)槲覀円煌5淖屇?個(gè)口high,low的來回調(diào)換。而且在程序中可能會(huì)有其他因素干擾,比如一些判斷語句,會(huì)占用額外的cpu資源,可能驅(qū)動(dòng)步進(jìn)電機(jī)的方波就會(huì)有一些變形。這樣以來電機(jī)的旋轉(zhuǎn)就不是那么精準(zhǔn)了。而easyd就不用考慮這些問題了,只要函數(shù)調(diào)用電機(jī)旋轉(zhuǎn)(速度,時(shí)間,方向),其他的就不用我們操心了。

如果要驅(qū)動(dòng)更大的電機(jī),easyd就不行了,我感覺那個(gè)發(fā)熱能把他自己燒死。 當(dāng)然那個(gè)big easydriver也不行,太貴了要150¥而且也沒見有幾家賣的。 大電機(jī)直接用步進(jìn)電機(jī)的驅(qū)動(dòng)器就好了,原理、使用方法和easyd是完全一樣的,只是那碩大的身軀和ardu的尺寸,有種猴騎駱駝的感覺。便宜的驅(qū)動(dòng)器才50¥就夠了。

前面這些東西都不是問題,都很容易掌握,最有技術(shù)含量的是,如何控制電機(jī)的加速和減速。讓電機(jī)高速旋轉(zhuǎn)只要一條指令就行了,但是電機(jī)所驅(qū)動(dòng)的設(shè)備未必能承受的住這個(gè)啟動(dòng)過程。所以要有個(gè)加速的過程,才能平穩(wěn)的運(yùn)行。對(duì)于cnc的控制這點(diǎn)非常重要,如果是xy2個(gè)軸的運(yùn)行就更復(fù)雜了,因?yàn)榧铀俣炔煌�,位置變化不同,運(yùn)行軌跡都有變化。這些計(jì)算還是挺復(fù)雜的。很多cnc都有個(gè)硬件的“運(yùn)動(dòng)控制卡”來處理這些計(jì)算。一些工業(yè)級(jí)的運(yùn)動(dòng)控制卡起步就要4,5k,上萬的也很正常。不過他們和軟件一樣都是計(jì)算,沒什么特殊的地方,無非是速度快,運(yùn)行穩(wěn)定而已。

步進(jìn)電機(jī)我也就只了解這些了,具體咋做我也迷迷糊糊的,隨便聊一聊和大家分享一下而已。有興趣的朋友可以仔細(xì)研究。
回復(fù)

使用道具 舉報(bào)

板凳
ID:156302 發(fā)表于 2017-9-27 23:21 | 只看該作者
好好研究下,謝謝分享
回復(fù)

使用道具 舉報(bào)

地板
ID:237069 發(fā)表于 2017-10-4 17:47 | 只看該作者
新人加入,等級(jí)夠了再來下載學(xué)習(xí)
回復(fù)

使用道具 舉報(bào)

5#
ID:282047 發(fā)表于 2018-2-3 22:14 | 只看該作者
為什么我接線正確,電機(jī)不轉(zhuǎn)啊,電機(jī)卡住了,而且是一開始可以轉(zhuǎn),然后過一會(huì)就不能轉(zhuǎn)了。
另外我用兩節(jié)一樣的9V電池供電,一個(gè)可以,一個(gè)不可以。
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表