Borrar filtros
Borrar filtros

use arduino uno and servo motor in matlab to track red object

5 visualizaciones (últimos 30 días)
Md
Md el 24 de Oct. de 2022
Respondida: Aman Banthia el 6 de Sept. de 2023
Do I need to do anything in the arduino ide to run this code in matlab?
a = arduino('COM7','Uno','Libraries','Servo');
s = servo(a,'D4','MinPulseDuration',7.00e-4,'MaxPulseDuration',2.3e-3);
cam = webcam(1);
color_selector = 1; % 1 for RED, 2 for GREEN, 3 for BLUE
while true
data = snapshot(cam);
diff_im = imsubtract(data(:,:,color_selector), rgb2gray(data));
diff_im = medfilt2(diff_im, [3 3]);
diff_im = im2bw(diff_im,0.15);
diff_im = bwareaopen(diff_im,100);
bw = bwlabel(diff_im, 8);
stats = regionprops(bw,'Centroid');
imshow(data)
hold on
line([320,320],[0,480], 'Color', 'black', 'LineWidth', 2)
for object = 1:length(stats)
bc = stats(object).Centroid;
x_coor = bc(1);
y_coor = bc(2);
plot(x_coor, y_coor, 'bx','LineWidth', 5, 'MarkerSize', 15);
%fprintf('X : %.2f Y : %.2f \n', bc(1), bc(2))
error = x_coor-320;
x_dist = error*0.06718;
deg = atan(x_dist/30);
deg_map = (deg+0.6)*(1-0)/(0.6+0.6) + 0;
fprintf('DEG %.2f \n', deg_map)
writePosition(s,deg_map)
end
end

Respuestas (1)

Aman Banthia
Aman Banthia el 6 de Sept. de 2023
Hi Md,
I understand that you want to know about MATLAB code with the Arduino board.
To run the code you provided in MATLAB, you don't need to do anything specific in the Arduino IDE. However, you need to make sure that you have the necessary hardware connections and libraries set up correctly.
Here are the steps to run the code:
  1. Connect your Arduino board to your computer using a USB cable.
  2. Open the Arduino IDE and make sure you have the correct board selected (in your case, 'Uno') and the correct port ('COM7') selected.
  3. Install the required libraries for the servo motor. In your case, it seems that you are using the 'Servo' library.
  4. Copy and paste the code into a MATLAB script or function file.
  5. Make sure you have the required MATLAB support packages installed. For this code, you need the 'Image Acquisition Toolbox Support Package for OS Generic Video Interface' to work with the webcam.
  6. Run the MATLAB script or function. The code will establish a connection with the Arduino board, initialize the servo motor, and start capturing images from the webcam.
Make sure you have the webcam connected and accessible by your computer.
Note: Ensure that your Arduino is properly configured, and the necessary libraries are installed to control the servo motor.
You can refer to the below documentation to know more about Arduino Programming with MATLAB:
You can also use the following application inside MATLAB to use Arduino more easily and efficiently:
Hope the above steps help you.
Best Regards,
Aman Banthia

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by