Использование MATLAB для записи на последовательный монитор Arduino для управления шаговым двигателем

#c #matlab #serial-port #arduino-ide

Вопрос:

В настоящее время я создаю позиционер, который будет перемещать зонд в направлении x и y с помощью трех шаговых двигателей и платы RAMBo 1.1 b. Я написал код через IDE Arduino, который позволяет мне делать это с помощью ввода параметров в последовательный монитор. Моя цель-использовать MATLAB для создания графического интерфейса и записи пользовательских данных из MATLAB на последовательный монитор. Однако я не понял, как писать на последовательный монитор.

Вот мой код, который я загрузил на доску (важный код находится в цикле):

 // These define all the pin outs for the Rambo board
#define ELECTRONICS "RAMBo11b"

#define stepPinX1                37//function D37  , port PD7
#define dirPinX1                 48//function D48  , port PL1
#define X_MIN_PIN                12//function PWM12, port PB6
#define X_MAX_PIN                24//function D24  , port PA2
#define enPinX1                  29//function D29  , port PA7
#define X_MS1_PIN                40//function D40  , port PG1
#define X_MS2_PIN                41//function D41  , port PG0

#define stepPinY                 36//function D36  , port PC1
#define dirPinY                  49//function D49  , port PL0
#define Y_MIN_PIN                11//function PWM11, port PB5
#define Y_MAX_PIN                -1
#define enPinY                   28//function D28  , port PA6
#define Y_MS1_PIN                69//function A15  , port PK7
#define Y_MS2_PIN                39//function D39  , port PG2

//These step pin X2 take place of the Z pinout. The Z pin actually goes from the board to the second x axis motor
#define stepPinX2                35//function D35  , port PC2
#define dirPinX2                 47//function D47  , port PL2
#define enPinX2                  27//function D27  , port PA5

#define stepPinE0                34//function D34  , port PC3
#define dirPinE0                 43//function D43  , port PL6
#define enPinE0                  26//function D26  , port PA4
#define E0_MS1_PIN               65//function A11  , port PK3
#define E0_MS2_PIN               66//function D12  , port PK4

#define MOTOR_CURRENT_PWM_XY_PIN 46//function D46  , port PL3
#define MOTOR_CURRENT_PWM_Z_PIN  45//function D45  , port PL4
#define MOTOR_CURRENT_PWM_E_PIN  44//function D44  , port PL5
#define LED_PIN                  13//function PWM13, port PB7

//These are as named. These focus on how fast, far, and long the motors move, respectively. 
float speedMove = 40; //mm/s
float speedHome = 70; //mm/s
float steps = .15; //mm/step (distance of one step)
int tMove = (steps / (speedMove*2))*1000000;// microsec
int tHome = (steps / (speedHome*2))*1000000;// microsec

//Below we will be defining the buffer size for the probe that will be navigating around the field attached to the end of the positioner. Also
//will be defining the 0,0 point in the x and y plane.  CENTER PROBE FOR THIS ITERATION OF CODE!
float buf = 10; //mm
float xloc = 0; //mm
float yloc = 0; //mm

//Setup function initializes all the outputs and inputs of the system
void setup() {
  pinMode(stepPinY, OUTPUT);      //function D36  , port PC1
  pinMode(dirPinY, OUTPUT);       //function D49  , port PL0
  pinMode(enPinY, OUTPUT);        //function D28  , port PA6
  
  pinMode(stepPinX1, OUTPUT);     //function D37  , port PD7
  pinMode(dirPinX1, OUTPUT);      //function D48  , port PL1
  pinMode(enPinX1, OUTPUT);       //function D29  , port PA7

  pinMode(stepPinX2, OUTPUT);     //function D35  , port PC2
  pinMode(dirPinX2, OUTPUT);      //function D47  , port PL2
  pinMode(enPinX2, OUTPUT);       //function D27  , port PA5

  pinMode(stepPinE0, OUTPUT);     //function D34  , port PC3
  pinMode(dirPinE0, OUTPUT);      //function D43  , port PL6
  pinMode(enPinE0, OUTPUT);       //function D26  , port PA4

  //Starts the serial monitor in order to give commands to the motors.
  Serial.begin(38400);
  
}

////Direction functions
//Sets direction of x-axis motors to negative (left)
void moveNegX(){
  digitalWrite(dirPinX2,HIGH);
  digitalWrite(dirPinX1, LOW);
}
//Sets direction of x-axis motors to positive (right)
void movePosX(){
  digitalWrite(dirPinX2,LOW);
  digitalWrite(dirPinX1, HIGH);
}
//Sets direction of y-axis motor to negative (left)
void moveNegY(){
  digitalWrite(dirPinY, LOW);
}
//Sets direction of y-axis motor to positive (right)
void movePosY(){
  digitalWrite(dirPinY, HIGH);
}

//This function will move in the x direction a specified amount (int X) in mm, in the specified direction (int dir)
float moveX(int X, int dir){
  if (dir == 2){
    movePosX();
  }else{
    moveNegX();
  }
  int numxSteps = X/steps;
  for (int i = 0; i < numxSteps; i  ) {
    digitalWrite(stepPinX2,HIGH);
    digitalWrite(stepPinX1, HIGH);
    delayMicroseconds(tMove); 
    digitalWrite(stepPinX2,LOW);
    digitalWrite(stepPinX1, LOW);
    delayMicroseconds(tMove); 
  }
}

//Same but for y
float moveY(int Y, int dir){
  if (dir == 2){
    movePosY();
  }else{
    moveNegY();
  }
  int numySteps = Y/steps;
  for (int i = 0; i < numySteps; i  ) {
    digitalWrite(stepPinY,HIGH);
    delayMicroseconds(tMove); 
    digitalWrite(stepPinY, LOW);
    delayMicroseconds(tMove); 
  }
}

void loop() {
////TAKE USER INPUT FOR X OR Y////
  while (!Serial.available()){
  }
  int ans = Serial.read();
  if (ans == 1 or ans == 2){//move along x- or y-axis
////TAKE USER INPUT FOR NEGATIVE OR POSITIVE////
    while (!Serial.available()){
    }
    int dir = Serial.parseInt();
////TAKE USER INPUT FOR DISTANCE////
    while (!Serial.available()){
    }
    int dist = Serial.parseInt();
    delay(500);
    if (ans == 1){//move along x-axis
      moveX(dist, dir);
    }else if (ans == 2){//move along y-axis
      moveY(dist, dir);
    }
    delay(1000);
  }
}
 

А вот мой код MATLAB:

 s = serialport("COM5",38400);
write(s,2,"uint8")
write(s,2,"uint8")
write(s,10,"uint8")
 

Я попытался использовать Serial.read() вместо Serial.parseint() Arduino IDE, но это не сработало. Я также пробовал разные типы данных для write() , а также writeline() в MATLAB, опять не повезло.

Ранее я использовал Serial.print() в Arduino IDE для печати на последовательный монитор и смог прочитать его с помощью MATLAB read() , поэтому я знаю , что соединения хорошие. Я ценю любой совет!

Комментарии:

1. Обновление: Я заметил, что на плате, которую мы используем, есть светодиод Rx. Я смог подтвердить, что плата получала данные, так как индикатор Rx включался каждый раз, когда я использовал write() MATLAB, что заставляет меня думать, что проблема связана с Serial.parseInt() Arduino IDE.