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:
- 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.
- Connect Arduino board to your computer via USB cable.
- Open MATLAB and run ‘arduino’ command.
- Follow the setup wizard to select the board and port.
- 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