Using MATLAB to write to Arduino serial monitor to control stepper motor

5 visualizaciones (últimos 30 días)
I am currently building a positioner that will move a probe in the x and y direction through the use of three stepper motors and a RAMBo 1.1b board. I have written code through the Arduino IDE that allows me to do so through inputting options in the serial monitor. My goal is to use MATLAB to build a GUI, and write user inputs from MATLAB to the serial monitor. However, I have not figured out how to write to the serial monitor.
Here is my code that I have uploaded to the board (important code is in the loop):
// 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.read();
////TAKE USER INPUT FOR DISTANCE////
while (!Serial.available()){
}
int dist = Serial.read();
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);
}
}
And here is my MATLAB code:
s = serialport("COM5",38400);
write(s,2,"uint8")
write(s,2,"uint8")
write(s,10,"uint8")
I tried using Serial.parseInt() instead of Serial.read() in Arduino IDE, but it did not work. I also tried different data types for write(), as well as writeline() in MATLAB, again no luck.
Earlier, I used Serial.print() in Arduino IDE to print to the serial monitor and was able to read it in MATLAB using read(), so I know the connections are good. I appreciate any advice!

Respuestas (1)

Chetan
Chetan el 9 de Mayo de 2024
I understand that you're building a positioner using a RAMBo 1.1b board and three stepper motors, controlled via MATLAB GUI. You're facing issues writing user inputs from MATLAB to the Arduino's serial monitor to control motor movement.
You can try these steps to reslve the issue:
  • Adjust Arduino Code: Replace `Serial.read()` with `Serial.parseInt()` to read integer inputs correctly.
  • Adjust MATLAB Code: Use `writeline(s, "number")` instead of `write(s, number, "uint8")` to send commands as strings, ensuring compatibility with `Serial.parseInt()`.
  • Debugging: Test with simple echo programs on Arduino to confirm correct data receipt from MATLAB.
  • Utilize MATLAB Support Package for Arduino for streamlined communication.
Refer to the following MathWorks Documentation for more details:
Hope it Helps

Categorías

Más información sobre Arduino Hardware en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by