【发布时间】:2019-07-13 02:50:43
【问题描述】:
我正在尝试编写 Arduino 代码,以通过串行通信从我的 c++ 应用程序控制几个步进电机。我写的代码似乎没问题,但是当我从我的 c++ 应用程序或 Arduino IDE 串行监视器发送串行命令时,它给出了奇怪的输出类型。
代码
#include <AccelStepper.h>
// BaudRate
#define BAUD 9600
#define NO_OF_STEPPERS 3 // total # of stepper motors + 1 , as our motor count star from 1 not from 0 ,
struct steppers_struct {
int stepper_id = 0; // stepper no
bool status = false; // status bool
int steps_2_move = 0; // steps the motor needs to move.
};
steppers_struct steppers_rack[NO_OF_STEPPERS]; // init structure
// Step and Dir pins Array
int step_pin_m[NO_OF_STEPPERS];
int dir_pin_m[NO_OF_STEPPERS];
int enable_pin_m[NO_OF_STEPPERS];
// AcceStepper Stepper Array
AccelStepper stepper_m[NO_OF_STEPPERS];
// Driver A4988
#define motorInterfaceType 1
// Buffer
char buf[80];
// Readline funtion load buffer
int readline(int readch, char *buffer, int len) {
static int pos = 0;
int rpos;
if (readch > 0) {
switch (readch) {
case '\r': // Ignore CR
break;
case '\n': // Return on new-line
rpos = pos;
pos = 0; // Reset position index ready for next time
return rpos;
default:
if (pos < len-1) {
buffer[pos++] = readch;
buffer[pos] = 0;
}
}
}
return 0;
}
// Setup
void setup() {
Serial.begin(BAUD); // init serial
// stepper dir and step pins.
step_pin_m[1] = 2;
dir_pin_m[1] = 5;
enable_pin_m[1] = 8; // on CNC shield v3 its pin 8
step_pin_m[2] = 3;
dir_pin_m[2] = 6;
enable_pin_m[2] = 8; // on CNC shield v3 its pin 8
for(int i=1; i<=NO_OF_STEPPERS; i++)
{
stepper_m[i] = AccelStepper(motorInterfaceType, step_pin_m[i], dir_pin_m[i]);
stepper_m[i].setMaxSpeed(1000);
stepper_m[i].setSpeed(1000);
pinMode(enable_pin_m[i], OUTPUT); // Enable Motors
Serial.print("AccelStepper initalized..");Serial.print("\n");
}
// Some LED
// pinMode(53, OUTPUT); // Red LED it D53
// pinMode(52, OUTPUT); // Green LED it D52
}
void loop() {
// Serial Read Start
if (readline(Serial.read(), buf, 80) > 0) {
// command format 1:100;2:200;3:300
// Read each command pair
char* command = strtok(buf, ";");
while (command != 0)
{
// Split the command in two values
char* separator = strchr(command, ':');
if (separator != 0)
{
// Actually split the string in 2: replace ':' with 0
*separator = 0;
int stepperId = atoi(command);
++separator;
int stepperSteps = atoi(separator);
// Steppers id stars from 1
if(stepperId != 0){
steppers_rack[stepperId].stepper_id = stepperId;
steppers_rack[stepperId].steps_2_move = stepperSteps;
if(stepperSteps != 0){
steppers_rack[stepperId].status = true;
}else{
steppers_rack[stepperId].status = false;
}
// Serial.print(" stepper id: "); Serial.print(steppers_rack[stepperId].stepper_id); Serial.print(" stepper steps: "); Serial.print(steppers_rack[stepperId].steps_2_move); Serial.print("\n"); // output OK.
}
} // if end
// Find the next command in input string
command = strtok(0, ";");
} // while end
} // Serial Read End
// Problems start here when i try to use or change stepper structure data.
// Loop on Stepper_Struct
for(int i=1; i<=NO_OF_STEPPERS; i++)
{
if(steppers_rack[i].steps_2_move != 0 && steppers_rack[i].status == true){
Serial.print(" stepper id : "); Serial.print(steppers_rack[i].stepper_id); Serial.print(" Steps to move : "); Serial.print(steppers_rack[i].steps_2_move); Serial.print(" status : "); Serial.print(steppers_rack[i].status); Serial.print("\n"); // Output not OK
delay(500);
// Lets move stepper motor one step it time so that all motors looks moving it same time , sync
stepper_m[i].move(1); // this states to move one step ahead
stepper_m[i].runToPosition();
steppers_rack[i].steps_2_move -- ; // deducted one step from the total step motors is told to take
if(steppers_rack[i].steps_2_move == 0){
steppers_rack[i].status = false;
}
} // if end
} // for end
} // loop end
所以这里有几个已知问题遇到它
1) 首先当我从 Arduino 串行监视器发送命令 (1:3;2:3) 然后它不显示任何输出,我必须发送两次然后我在串行监视器中看到输出。
2) 另一个主要问题是我发送命令时得到的奇怪输出:1:3;2:3(意味着步进器 1 移动 3 步,步进器 2 也移动 3 步)
stepper id : 14897 Steps to move : 14848 status : 51
stepper id : 14897 Steps to move : 59 status : 51
stepper id : 14897 Steps to move : 12858 status : 51
stepper id : 14897 Steps to move : 12857 status : 51
stepper id : 14897 Steps to move : 12856 status : 51
stepper id : 1 Steps to move : 372 status : 1
stepper id : 49 Steps to move : 12855 status : 51
stepper id : 1 Steps to move : 371 status : 1
stepper id : 49 Steps to move : 12854 status : 51
所以不确定这些大数字 14897 、 51 、 49 等来自哪里?
所以知道哪里做错了,让代码工作,这样我就可以同步多个步进电机。
- 抱歉,我必须添加所有代码,以防有人尝试运行和调试它
【问题讨论】:
-
51 是字符 '3' 的 ASCII 码,49 是 '1'。你可以使用更多的分隔符,比如“,:”
-
strtok stackoverflow.com/questions/56939302/…的一些例子
-
@Juraj 已经将其拆分为:Stepper_Struct 上的 Loop 中的输出变得混乱......在它上面你可以看到我已经写入输出 OK
标签: arduino serial-port