Go Down

Topic: Able to display the color selected on LED (Read 575 times) previous topic - next topic

aataylo3

Matlab Code for Arduino HELP

The code below is so I can track an object with my camera mounted on a servo when selected. I have the code below running in Matlab with a servo program running in the Arduino window.I would like to be able to modify the code to display the "DES COLOR" on an LED that is connected to an arduino board.

Can someone please help me with this?

close all; clc;

if exist('SerialCommPort','var'),fclose(SerialCommPort),end;

if exist('Camera','var'), release(Camera); clear vidDevice, end;

delete(instrfind);

SerialCommPort = serial('COM30');

SerialCommPort.BaudRate = 9600;

fopen(SerialCommPort);

fwrite(SerialCommPort, 90);

pause(2);

Camera = imaq.VideoDevice('winvideo', 1);

Frame = step(Camera);

[d1,d2,d3] =size(Frame);

Show = imshow(Frame); hold on;

plot([1 d2],[d1 d1]/2,'y');

plot([d2 d2]/2,[1 d1],'y');

Title = title('Double click to select color')

[xClick,yClick] = getpts;

PlotXY = plot(xClick,yClick,'oy'); pause(1);

DesColor(1:3) = Frame(round(yClick),round(xClick),:);

DeltaColor = 0.10;

xTgt = xClick; yTgt = yClick; beta = 0.4; alfa = 1 - beta;

N = 1000; dt = 0.01; F = 1; eInteg = 0;

errorArray = zeros(1,N);

tic

for k=1:N

Frame = step(Camera);

set(Show,'cdata',Frame);

set(Title,'string',num2str(k));

Mask = abs(Frame(:,:,1)-DesColor(1)) < DeltaColor & ...

abs(Frame(:,:,2)-DesColor(2)) < DeltaColor & ...

abs(Frame(:,:,3)-DesColor(3)) < DeltaColor;

[Row, Col] = find(Mask);

if ~isempty(Row),

xTgt = alfa*xTgt + beta*mean(Col);

yTgt = alfa*yTgt + beta*mean(Row);

end

%Kp = .2;

%xTgt

%AngDeg = round(Kp*(-xTgt+618) + 90)

Kp = Kpicked; Ki = Kp*(-Gc.z{1});

e = xTgt-512;

eInteg = eInteg + e*0.033;

eInteg = min([100 max([-100 eInteg])]);

AngDeg = round(-(Kp*e + Ki*eInteg) + 90);

AngDeg;

fwrite(SerialCommPort,AngDeg);

set(PlotXY,'xdata',xTgt,'ydata',yTgt);

errorArray(k) = e;

drawnow;

end

FramePerSec = N/toc;

set(Title,'string',['FramePerSec ',num2str(FramePerSec)]);

fclose(SerialCommPort);

release(Camera); clear vidDevice;

Time = [1:N]/FramePerSec;

figure(10); plot(Time,errorArray,'r')

title(['Kp = ',num2str(Kp),' K1 = ', num2str(Ki)])

xlabel('Time [sec]'); ylabel('Tracking Error [pixels]');

grid on;

ieee488

You want someone to translate Matlab code for you?
Seriously?
Display a color on a LED? Don't you mean display selected color on a LCD?

Paul__B

Right, first things first.

Go and read the instructions, then go back and modify your post (use the "More --> Modify" option to the bottom right of the post) to mark up the code as such so we can examine it conveniently and accurately.

If you do not do this, your code may well be meaningless.

Note: Also mark up any data in the same way.  This includes error output that you get from the IDE.

Until you reply and demonstrate that you understand these points that have been made so far, there is absolutely no point working through the other problems with the code, so I will leave it at that for the present.

ieee488

#3
Mar 29, 2016, 10:52 pm Last Edit: Mar 29, 2016, 10:55 pm by ieee488
Right, first things first.

Go and read the instructions, then go back and modify your post (use the "More --> Modify" option to the bottom right of the post) to mark up the code as such so we can examine it conveniently and accurately.

If you do not do this, your code may well be meaningless.

Note: Also mark up any data in the same way.  This includes error output that you get from the IDE.

Until you reply and demonstrate that you understand these points that have been made so far, there is absolutely no point working through the other problems with the code, so I will leave it at that for the present.

He's not posting Arduino code.

It's Matlab code apparently. And he wants us to figure out what it's doing and add the ability to display color in the LED that is connected to Arduino.



Paul__B

#4
Mar 29, 2016, 10:54 pm Last Edit: Mar 29, 2016, 10:55 pm by Paul__B
And there's the point.

Code is code, as you might say.

(I think you know this is cut-and-paste. :smiley-lol: )

ieee488

#5
Mar 29, 2016, 10:56 pm Last Edit: Mar 29, 2016, 10:58 pm by ieee488
And there's the point.

Code is code, as you might say.

You are more than welcome to help add Matlab code to do what he wants. Even if I knew Matlab I have no intention to wade through that jungle of lines of code even if it was properly formatted.


You do realize that nothing has been written for what he wants, so he can't provide you with error messages.


Paul__B

You are more than welcome to help add Matlab code to do what he wants.
Ah, now ...

I didn't say that!

ieee488

#7
Mar 29, 2016, 10:59 pm Last Edit: Mar 29, 2016, 10:59 pm by ieee488
Ah, now ...

I didn't say that!

So, what is the point of having him post it?

He hasn't written the code that he wants.


Paul__B

Hey, don't shoot the messenger!

I'm just giving him hints on how to pose a question in a manner that might even remotely excite some interest by someone else who might know what he is getting at, on the basis that badly presented questions tend to put people off to start with.

ieee488


Hey, don't shoot the messenger!

I'm just giving him hints on how to pose a question in a manner that might even remotely excite some interest by someone else who might know what he is getting at, on the basis that badly presented questions tend to put people off to start with.

Yeah.

He's the Matlab guy and he's asking us to modify Matlab code for him.  ::)   Talk about silly.


Go Up