Filter löschen
Filter löschen

Matlab - Arduino Robot Control

5 Ansichten (letzte 30 Tage)
John Antonakis
John Antonakis am 20 Mai 2022
Beantwortet: Aman Banthia am 15 Sep. 2023
So, i want to control a scara robot using Matlab and Arduino.
I have figured the kinematics and have already prepared the equations, in a way that i import the XYZ cordinates and matlab calculates the angle of my stepper motors .
My problem is i can't figure a proper way to enstablish a serial port communication between matlab and arduino and succesfully send data from matlab to arduino.
My MATLAB Code is :
a=input('Input X Cordinate :');
b=input('Input Y Cordinate :');
c=input('Input Z Position :');
x = a;
y = b;
double L1;
double L2;
L1 = 228; % L1 = 228mm
L2 = 136.5; % L2 = 136.5mm
theta2 = acos(((x)^2 + (y)^2 - (L1)^2 - (L2)^2) / (2 * L1 * L2));
if (x < 0) && (y < 0)
theta2 = (-1) * theta2;
end
theta1 = atan(x / y) - atan((L2 * sin(theta2)) / (L1 + L2 * cos(theta2)));
theta2 = (-1) * theta2 * 180 / pi;
theta1 = theta1 * 180 / pi;
% Angles adjustment depending in which quadrant the final tool coordinate x,y is
% 1st quadrant
if (x >= 0) && (y >= 0)
theta1 = 90 - theta1;
end
% 2nd quadrant
if (x < 0) && (y > 0)
theta1 = 90 - theta1;
end
%3rd quadrant
if (x < 0) && (y < 0)
theta1 = 270 - theta1;
phi = 270 - theta1 - theta2;
phi = (-1) * phi;
end
%4th quadrant
if (x > 0) && (y < 0)
theta1 = -90 - theta1;
end
if (x < 0) && (y == 0)
theta1 = 270 + theta1;
end
% Calculate "phi" angle so gripper is parallel to the X axis
phi = 90 + theta1 + theta2;
phi = (-1) * phi;
% Angle adjustment depending in which quadrant the final tool coordinate x,y is
if (x < 0) && (y < 0)
phi = 270 - theta1 - theta2;
end
if (abs(phi) > 165)
phi = 180 + phi;
end
theta1=round(theta1);
theta2=round(theta2);
phi=round(phi);
temp=[theta1, theta2, phi, c];
pos=regexprep(mat2str(temp), {'\[', '\]', '\s+'}, {'', '', ','});
disp(pos);
And my arduino Code is :
#include <AccelStepper.h>
#include <Servo.h>
#include <math.h>
#define limitSwitch1 11
#define limitSwitch2 10
#define limitSwitch3 9
#define limitSwitch4 A3
// Define the stepper motors and the pins the will use
AccelStepper stepper1(1, 2, 5); // (Type:driver, STEP, DIR)
AccelStepper stepper2(1, 3, 6);
AccelStepper stepper3(1, 4, 7);
AccelStepper stepper4(1, 12, 13);
Servo gripperServo; // create servo object to control a servo
double x = 10.0;
double y = 10.0;
double L1 = 228; // L1 = 228mm
double L2 = 136.5; // L2 = 136.5mm
double theta1, theta2, phi, z;
int stepper1Position, stepper2Position, stepper3Position, stepper4Position;
const float theta1AngleToSteps = 44.444444;
const float theta2AngleToSteps = 35.555555;
const float phiAngleToSteps = 10;
const float zDistanceToSteps = 100;
String content = "";
int data[10];
void setup() {
Serial.begin(115200);
pinMode(limitSwitch1, INPUT_PULLUP);
pinMode(limitSwitch2, INPUT_PULLUP);
pinMode(limitSwitch3, INPUT_PULLUP);
pinMode(limitSwitch4, INPUT_PULLUP);
// Stepper motors max speed
stepper1.setMaxSpeed(4000);
stepper1.setAcceleration(2000);
stepper2.setMaxSpeed(4000);
stepper2.setAcceleration(2000);
stepper3.setMaxSpeed(4000);
stepper3.setAcceleration(2000);
stepper4.setMaxSpeed(4000);
stepper4.setAcceleration(2000);
gripperServo.attach(A0, 600, 2500);
// initial servo value - open gripper
data[4] = 180;
gripperServo.write(data[4]);
delay(1000);
data[3] = 100;
homing();
}
void loop() {
if (Serial.available()) {
content = Serial.readString(); // Read the incomding data from Processing
Serial.readString();
// Extract the data from the string and put into separate integer variables (data[] array)
for (int i = 0; i < 5; i++) {
int index = content.indexOf(","); // locate the first ","
data[i] = atol(content.substring(0, index).c_str()); //Extract the number from start to the ","
content = content.substring(index + 1); //Remove the number from the string
}
}
stepper1Position = data[0] * theta1AngleToSteps;
stepper2Position = data[1] * theta2AngleToSteps;
stepper3Position = data[2] * phiAngleToSteps;
stepper4Position = data[3] * zDistanceToSteps;
stepper1.setSpeed(2000);
stepper2.setSpeed(2000);
stepper3.setSpeed(2000);
stepper4.setSpeed(2000);
stepper1.setAcceleration(2000);
stepper2.setAcceleration(2000);
stepper3.setAcceleration(2000);
stepper4.setAcceleration(2000);
stepper1.moveTo(stepper1Position);
stepper2.moveTo(stepper2Position);
stepper3.moveTo(stepper3Position);
stepper4.moveTo(stepper4Position);
while (stepper1.currentPosition() != stepper1Position || stepper2.currentPosition() != stepper2Position || stepper3.currentPosition() != stepper3Position || stepper4.currentPosition() != stepper4Position) {
stepper1.run();
stepper2.run();
stepper3.run();
stepper4.run();
}
delay(100);
gripperServo.write(100);
delay(300);
}
void serialFlush() {
while (Serial.available() > 0) { //while there are characters in the serial buffer, because Serial.available is >0
Serial.read(); // get one character
}
}
//Homing all stepper motors
void homing() {
// Homing Stepper4
while (digitalRead(limitSwitch4) != 1) {
stepper4.setSpeed(1500);
stepper4.runSpeed();
stepper4.setCurrentPosition(17000); // When limit switch pressed set position to 0 steps
}
delay(20);
stepper4.moveTo(10000);
while (stepper4.currentPosition() != 10000) {
stepper4.run();
}
// Homing Stepper3
while (digitalRead(limitSwitch3) != 1) {
stepper3.setSpeed(-1100);
stepper3.runSpeed();
stepper3.setCurrentPosition(-1662); // When limit switch pressed set position to 0 steps
}
delay(20);
stepper3.moveTo(0);
while (stepper3.currentPosition() != 0) {
stepper3.run();
}
// Homing Stepper2
while (digitalRead(limitSwitch2) != 1) {
stepper2.setSpeed(-1300);
stepper2.runSpeed();
stepper2.setCurrentPosition(-5420); // When limit switch pressed set position to -5440 steps
}
delay(20);
stepper2.moveTo(0);
while (stepper2.currentPosition() != 0) {
stepper2.run();
}
// Homing Stepper1
while (digitalRead(limitSwitch1) != 1) {
stepper1.setSpeed(-1200);
stepper1.runSpeed();
stepper1.setCurrentPosition(-3955); // When limit switch pressed set position to 0 steps
}
delay(20);
stepper1.moveTo(0);
while (stepper1.currentPosition() != 0) {
stepper1.run();
}
}
Any kind of help deeply appriciated.

Antworten (1)

Aman Banthia
Aman Banthia am 15 Sep. 2023
Hi John,
I understand that you are trying to use SCARA Robot with help of MATLAB and Arduino and want to know about a proper way to establish the connection between MATLAB and Arduino and successfully send data from MATLAB to Arduino.
To do so you would require to use the MATLAB Support Package for Arduino and then change the code for Arduino to a MATLAB code:
  1. Install MATLAB Support Package for Arduino: Open MATLAB and go to the "Add-Ons" tab. Click on "Get Hardware Support Packages" and search for "Arduino". Select the package and click on "Add" to install it.
  2. Connect Arduino board to your computer via USB cable.
  3. Open MATLAB and run ‘arduino’ command.
  4. Follow the setup wizard to select the board and port.
  5. Verify the connection using the ‘isconnected’ function.
I have tried to convert your Arduino code to a MATLAB compatible code, please find it below:
% MATLAB code using Arduino Support Package
% Create Arduino object
a = arduino();
% Define pin numbers
limitSwitch1 = 'D11';
limitSwitch2 = 'D10';
limitSwitch3 = 'D9';
limitSwitch4 = 'A3';
% Define the stepper motors and the pins they will use
stepper1 = servo(a, 'D2');
stepper2 = servo(a, 'D3');
stepper3 = servo(a, 'D4');
stepper4 = servo(a, 'D12');
% Create a servo object for the gripper
gripperServo = servo(a, 'A0');
% Define variables
x = 10.0;
y = 10.0;
L1 = 228; % L1 = 228mm
L2 = 136.5; % L2 = 136.5mm
theta1 = 0;
theta2 = 0;
phi = 0;
z = 0;
stepper1Position = 0;
stepper2Position = 0;
stepper3Position = 0;
stepper4Position = 0;
theta1AngleToSteps = 44.444444;
theta2AngleToSteps = 35.555555;
phiAngleToSteps = 10;
zDistanceToSteps = 100;
content = "";
data = zeros(1, 10);
% Set pin modes
configurePin(a, limitSwitch1, 'pullup');
configurePin(a, limitSwitch2, 'pullup');
configurePin(a, limitSwitch3, 'pullup');
configurePin(a, limitSwitch4, 'pullup');
% Set stepper motors max speed and acceleration
stepper1.MaxSpeed = 4000;
stepper1.Acceleration = 2000;
stepper2.MaxSpeed = 4000;
stepper2.Acceleration = 2000;
stepper3.MaxSpeed = 4000;
stepper3.Acceleration = 2000;
stepper4.MaxSpeed = 4000;
stepper4.Acceleration = 2000;
% Set initial servo value - open gripper
data(4) = 180;
writePosition(gripperServo, data(4));
% Homing function
homing();
% Main loop
while true
if a.AvailableBytes > 0
content = read(a, a.AvailableBytes, 'char');
% Extract data from the string and put into separate integer variables (data array)
data = sscanf(content, '%d, %d, %d, %d, %d');
end
% Convert data to motor positions
stepper1Position = data(1) * theta1AngleToSteps;
stepper2Position = data(2) * theta2AngleToSteps;
stepper3Position = data(3) * phiAngleToSteps;
stepper4Position = data(4) * zDistanceToSteps;
% Move stepper motors to desired positions
moveTo(stepper1, stepper1Position);
moveTo(stepper2, stepper2Position);
moveTo(stepper3, stepper3Position);
moveTo(stepper4, stepper4Position);
% Wait for the motors to reach the desired positions
while isMoving(stepper1) || isMoving(stepper2) || isMoving(stepper3) || isMoving(stepper4)
pause(0.01);
end
% Control the gripper servo
writePosition(gripperServo, data(5));
end
Please note that this is just a basic conversion example, and you may need to make further adjustments based on your specific requirements and hardware setup.
Please refer to the following MATLAB documentation to know more about the MATLAB Support Package for Arduino:
Please refer to the following MATLAB File Exchange link to get the MATLAB Support Package for Arduino:
Hope that the above solution helps.
Best Regards,
Aman Banthia

Kategorien

Mehr zu MATLAB Support Package for Arduino Hardware finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by