2014-11-25 63 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"); 
} 
相关问题