2014-11-25 99 views
0

我嘗試用程序控制步進電機(參見下面) 我可以用Accelstepper控制步進機(見下文),但不知道如何編程Arduino所以它能夠通過串口按照協議進行通信。通過程序Arduino步進控制

#include <AccelStepper.h> 

// Define a stepper and the pins it will use 
AccelStepper stepper(1, 3, 4); 

int pos = 8192; 

void setup() 
{ 
    stepper.setMaxSpeed(5000); 
    stepper.setAcceleration(1500); 
} 

void loop() 
{ 
    if (stepper.distanceToGo() == 0) 
    { 
    delay(500); 
    pos = -pos; 
    stepper.moveTo(pos); 
    } 
    stepper.run(); 
} 

發送到迴轉臺的所有命令都是簡單字符格式,包括電機編號。只有標記爲xxx的部分作爲字節數據傳遞給表。例如,如果你想讓表1旋轉4步而不是傳遞「I1M004」,你需要傳遞「I1M」+(char)0 +(char)0 +(char)4 通常,所有命令都會以下列形式得到回覆:^ XXXXXX

命令

V 請求旋轉臺的狀態。通常的回覆是^ R1R2R3R4,表示旋轉1準備就緒,旋轉2準備等。^ B1xxxR2R3R4表示旋轉1佔線,其中xxx爲3字節表示旋轉仍需執行多少步驟。

SmMxxx 將電機m的速度設置爲xxx,其中xxx是表示速度的3個字節的數據。示例代碼:port.Write(「S1M」+(char)0 +(char)6 +(char)255); //設置電機1的速度爲1791.我們的旋轉表的標準速度範圍爲:0x000001至0x0012FF(1至4863)。控制器將以^ mxx鏡像電機號碼和2個最後字節的速度設置作出響應。

ImMxxx 轉動電機m xxx步數。控制器將以^ Bmxxx

DmCWLO 確認設置電機編號m順時針旋轉(因此每個連續的電機旋轉命令m都會順時針旋轉)。

DmCWHi 將旋轉m設定爲逆時針旋轉。

EmHALT 旋轉m停止。 旋轉採樣命令序列

電機號碼作爲字符傳遞,但步數和速度作爲3字節的二進制傳遞以簡化。 發送:V回覆:^ R1R2R3R4 發送:S1M1791回覆:^ 191 發送:D1CWLO回覆:^ 發送:I1M100回覆:^ B1100

回答

0

我爲我的學位論文工作一個類似的項目,我控制的倒立擺從PC通過arduino uno。我假設你有一個PC程序向Arduino發送命令,問題是在Arduino板上接收和解釋它。 我寫了下面的代碼,主要幫助(一些複製粘貼修改)從here

它基本上打開COM端口,然後監聽來自PC的傳入命令。收到命令後,會將其分解(傳入命令以#00參數格式)。所有命令都以#開頭。以下2位數字定義命令本身,以下文本/數字是該命令的參數。

一旦命令及其參數已知,就可以執行與該命令相關的實際進程。在你的情況下,這應該是與傳入命令相關的電機控制。下面的代碼顯然需要更新以匹配您的電機控制功能,但傳入的命令處理工作正常。

String inputString = "";   // a string to hold incoming data 
boolean stringComplete = false; // whether the incloming string is complete 

float kp = 10;  //sample parameter 1 
float kd = 5;  //sample parameter 2 
float ki = 2;  //sample parameter 3 


void setup() 
{ 
    Serial.begin(9600);   //Start serial communication 
    inputString.reserve(200);  //Reserves 200 bytes for the string 
} 

void loop() 
{ 
    //This becomes true when the serial port receives a "\n" character (end of line) 
    if (stringComplete)    
    { 
    SerialProc();    //the function which runs when a full line is received 
    inputString = "";   //once processed, the string is cleared 
    stringComplete = false;  //set flag to false to indicate there is nothing in the buffer waiting 
    } 
} 

void serialEvent()    //This serial event runs between each loop cycles 
{ 
    while (Serial.available()) //if there is anything in the incoming buffer this while loop runs 
    { 
    // get the next new byte: 
    char inChar = (char)Serial.read(); 
    // add it to the inputString: 
    inputString += inChar; 
    // if the incoming character is a newline, set a flag 
    // so the main loop can do something about it: 
    if (inChar == '\n') 
    { 
     stringComplete = true; //This indicates the line is complete, and the main program can process it 
    } 
    } 
} 

void SerialProc() //the function which processes the incoming commands. It needs to be modified to your needs 
{ 
    //cmd is the first three characters of the incoming string/line 
    String cmd = inputString.substring(0,3); //first three characters in incoming string specifies the command 

    //param is the rest of the string to the end of the line (excluding the first three characters) 
    String param = inputString.substring(3, inputString.length()); //rest of incoming string is making up the parameter 

    //creating a buffer as an array of characters, same size as the length of the parameters string 
    char buf[param.length()]; 
    //moving the parameters from string to the char array 
    param.toCharArray(buf,param.length()); 

    //the above string to char array conversion is required for the string to float 
    //conversion below (atof) 

    //the below part is the command execution. Could have used a switch below, but the series of ifs 
    //just did the trick 

    if (cmd == "#00") 
     SendReply();      //Executing command 1 
    else if (cmd == "#01") 
     kp = atof(buf);     //executing command 2 (setting parameter kp) 
    else if (cmd == "#02") 
     kd = atof(buf);     //executing command 3 (setting parameter kd) 
    else if (cmd == "#03") 
     ki = atof(buf);     //executing command 4 (setting parameter ki) 
} 

void SendReply() 
{ 
    //This is called from the SerialProc function when the #00 command is received 
    //After the last parameter (TimeDelay) it sends the carrige return characters via the Serial.println 
    Serial.println("reply"); 
} 
相關問題