具有结构无效输出的Arduino

时间:2019-07-12 12:29:25

标签: arduino serial-port

我正在尝试编写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等大数字从何而来?

所以任何想法都在做错什么并使代码起作用,以便我可以同步多个步进电机。

  • 对不起,我不得不添加所有代码,以防有人尝试运行和调试它

1 个答案:

答案 0 :(得分:1)

我看到了数组索引超出范围的问题。

您以这种方式声明了一个struct数组。

#define NO_OF_STEPPERS 2  // total # of stepper motors
steppers_struct  steppers_rack[NO_OF_STEPPERS]; // init structure

steppers_rack中的元素数为2。

现在,您正在下面的for循环中访问steppers_rack[2]。索引2超出范围。

for(int i=1; i<=NO_OF_STEPPERS; i++) {
  if (steppers_rack[i].steps_2_move != 0 && steppers_rack[i].status == true) {

i为2时,steppers_rack[i].steps_2_movesteppers_rack[i].status返回除0以外的值,并且if语句的值为true