• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

MSP430G2553

Status
Not open for further replies.

engineergc

New Member
Dear All;

I am making red round object tracking project by using MSP430G2553 and Matlab. When red round object moves, a car will track red round object. Project is consist of from two parts as image processing and motion part. In motion part I am using L298N motor driver card, MSP430G2553. and a car that consist of from 2 wheels with 2 dc motors and a swivel castor. Image processing part in matlab is working as smooth.

But when a red ball moves, car can not track red object. When I compiled code in Code Composer Studio, it doesn't give any error. Therefore I can not find
problem in code. I added MSP430G2553 code and parts of image processing code that provide commnication between MSP430 and MATLAB. I think remander part of matlab code is working correctly.

CODE COMPOSER STUDIO PART
C:
#include <msp430.h>
//P1.3 ENA
//P1.4 IN1
//P1.5 IN2
//P1.6 ENB
//P2.0 IN3
//P2.1 IN4
int main(void) {
    WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer
    DCOCTL = 0; //
    BCSCTL1 = CALBC1_1MHZ;
    DCOCTL = CALDCO_1MHZ;
    P1SEL = BIT1; // P1.1 = RXD
    P1SEL2 = BIT1; // P1.1 = RXD
    UCA0CTL1 |=UCSWRST;
    UCA0CTL1 |= UCSSEL_2;
    UCA0BR0 = 104; // 1MHz 9600 baudrate
    UCA0BR1 = 0; // 1MHz 9600 baud rate
    UCA0MCTL = UCBRS0; // UART Baud adjustment
    UCA0CTL1 &= ~UCSWRST;
    IE2 |= UCA0RXIE; // USCI_A0 RX interrupt enable
    __bis_SR_register(LPM0_bits + GIE); // General interrrupts and LPM0 mode are active
    }
  
    #pragma vector=USCIAB0RX_VECTOR
    __interrupt void USCI0RX_ISR(void)
    {
    unsigned char received;
    received = UCA0RXBUF;
    if(received=='C') {  //C=CENTER
    P1OUT &= ~(BIT3 + BIT6);
    }

    else  if(received=='R') { //R=RIGHT
    P1OUT |=(BIT3 + BIT4);
    P1OUT &= ~BIT5;
    }

    else  if(received=='L') { //L=LEFT
    P1OUT |=BIT6;
    P2OUT |=BIT0;
    P2OUT &= ~BIT1;
    }

    else if(received=='F') { //F=FORWARD
    P1OUT |=(BIT3 + BIT6);
    P1OUT |=BIT4;
    P1OUT &= ~BIT5;
    P2OUT |=BIT0;
    P2OUT &= ~BIT1;
    }

    else if(received=='B') { //B=BACK
    P1OUT |=(BIT3 + BIT6);
    P1OUT |=BIT5;
    P1OUT &= ~BIT4;
    P2OUT |= BIT1;
    P2OUT &= ~BIT0;
    }
    else if(received=='N') { //N=NO MOTION
        P1OUT &= ~(BIT3 + BIT6);
    }
    }
MATLAB PART
Code:
priorPorts = instrfind % finds any existing Serial Ports in MATLAB
delete(priorPorts) % and deletes them
s = serial('COM3');
set(s,'BaudRate',4800);
fopen(s);

IMAGE PROCESSING CODES

case 1
fwrite(s,'C');
case 2
fwrite(s,'L');
case 3
fwrite(s,'R');
case 4
fwrite(s,'B');
case 5
fwrite(s,'F');
otherwise
fwrite(s,'N');
end
I hope you will be able to provide information.
 
Last edited by a moderator:

DerStrom8

Super Moderator
Most Helpful Member
Please format your code using proper spacing and the code tags. Otherwise it's nearly impossible to follow.
 

engineergc

New Member
Hi,

I can not add my matlab code as file. Therfore I added my matlab code below. My car can not track red round object. Could you help me please?

MATLAB CODE
Code:
% bellekbilgisi=imaqmem;
% imaqmem(bellekbilgisi.AvailPhys);

video=videoinput('winvideo',1);%Create video object

set(video,'FramesPerTrigger',Inf);
set(video,'ReturnedColorspace','rgb');
video.FrameGrabInterval=10;
priorPorts = instrfind
delete(priorPorts)
s = serial('COM3');
set(s,'BaudRate',9600);
fopen(s);
start(video)

min=5500;
max=10500;

while(video.FramesAcquired<=200)
 
 nesne_bulundu=0;
 
data=getsnapshot(video);

[y x c]=size(data);

 x1=x/2; 
 x2=x1-40;                 
 x3=x1+40;
         

roundredcolor=imsubtract(data:),:,1),rgb2gray(data));
roundredcolor=medfilt2(roundredcolor,[3,3]);için median filtresi uyguluyoruz
roundredcolor=im2bw(roundredcolor,0.18);
roundredcolor=bwareaopen(roundredcolor,250);
se = strel('disk',4);
roundredcolor = imclose(roundredcolor,se);
roundredcolor = imfill(roundredcolor,'holes');

[B,L]=bwboundaries(roundredcolor,'noholes');

stats= regionprops(L,'BoundingBox','Centroid','Area');
circle_threshold = 0.80;

imshow(data)
hold on

for nesne = 1:length(stats)
    nesne_bulundu=1; % Bulunan her nesne için
 
        boundary = B{nesne} ;
        delta_sq = diff(boundary).^2;  % compute a simple estimate of the object's perimeter
        perimeter = sum(sqrt(sum(delta_sq,2)));
        alan=stats(nesne).Area; % Nesnenin alanı
        bc = stats(nesne).Centroid;
        metric = 4*pi*alan/perimeter^2;% compute the roundness metric
     
       resim=((((alan<=max)&(alan>=min))*2)+((alan>max)*4)+((alan<min)*5));
     
       % Find the circle
   if metric > circle_threshold
               bc = stats(nesne).Centroid;
               plot(bc(1),bc(2),'ko');
   
 
     if (bc(1)>x2)&(bc(1)<x3)
          disp('merkez')
        switch (resim)
            case 1
                  r=1;
                  disp('sabit')
            case 4
                r=4;
                disp('ileri')
            case 5
                r=5;
                disp('geri')
        end
        else if bc(1)<x2
         r=2;
               disp('sağ')
       else if bc(1)>x3
         r=3;
               disp('sol')
           else
            end
            end
     end
       
       
     y1=1:y;
        plot(x2,y1);
         plot(x3,y1);
         switch(r)
 case 1
 fwrite(s,'C');
 case 2
 fwrite(s,'L');
 case 3
 fwrite(s,'R');
 case 4
 fwrite(s,'B');
 case 5
 fwrite(s,'F');
 otherwise
 fwrite(s,'N');
         end
end
end
     if nesne_bulundu == 0
       disp('Red round object is not found');
     end

    if mod(video.FramesAcquired,100)==0
  flushdata(video);
   end
   
hold off
end

stop(video);
flushdata(video);

clear all
 

Attachments

Last edited by a moderator:

DerStrom8

Super Moderator
Most Helpful Member
I don't think you understand. I asked you to format your code using the code tags built into the editor. Use the "Insert" menu.

upload_2017-3-23_6-25-21.png

Right now your code is a mess because it does not have proper formatting.
 

DerStrom8

Super Moderator
Most Helpful Member
I don't see what's so hard about going to the "insert" menu and clicking "code", and pasting your code between the tags! I even posted pictures!
 

DerStrom8

Super Moderator
Most Helpful Member
Dear DerStrom8;
I think code proper format problem is solved. Now, my main problem is why my code is not working.
No, the format problem is not completely solved because you still don't know how to do it yourself. You needed Ian to do it for you.

Please try it yourself next time.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top